ts shouldnt yield zero ever, yield now if so

Bel LaPointe 2022-01-01 17:13:50 -05:00
parent 0dddf26265
commit b4aa4ad310
2 changed files with 18 additions and 7 deletions

View File

@ -70,11 +70,6 @@ func TestSchedulerFactory(t *testing.T) {
want: scheduleDue(1), want: scheduleDue(1),
next: time.Unix(1, 0), next: time.Unix(1, 0),
}, },
"zero ts": {
input: `0`,
want: scheduleDue(0),
next: time.Unix(0, 0),
},
"never": { "never": {
input: ``, input: ``,
want: scheduleNever{}, want: scheduleNever{},
@ -108,4 +103,18 @@ func TestSchedulerFactory(t *testing.T) {
} }
}) })
} }
t.Run("zero ts", func(t *testing.T) {
got := schedulerFactory("0")
if fmt.Sprintf("%T", scheduleDue(0)) != fmt.Sprintf("%T", got) {
t.Fatalf("want type %T, got %T", scheduleDue(0), got)
}
if fmt.Sprint(scheduleDue(0)) != fmt.Sprint(got) {
t.Fatalf("want %+v, got %+v", scheduleDue(0), got)
}
next, _ := got.next(start)
if now := time.Now(); next.Sub(now) > time.Second {
t.Fatalf("want next %+v, got %+v", now, next)
}
})
} }

View File

@ -11,6 +11,9 @@ import (
type TS int64 type TS int64
func (ts TS) time() time.Time { func (ts TS) time() time.Time {
if ts == 0 {
ts = TS(time.Now().Unix())
}
return time.Unix(int64(ts), 0) return time.Unix(int64(ts), 0)
} }
@ -26,8 +29,7 @@ func (ts TS) MarshalYAML() (interface{}, error) {
if ts == 0 { if ts == 0 {
ts = TS(time.Now().Unix()) ts = TS(time.Now().Unix())
} }
t := time.Unix(int64(ts), 0) return ts.time().Format(time.UnixDate), nil
return t.Format(time.UnixDate), nil
} }
func (ts *TS) UnmarshalJSON(b []byte) error { func (ts *TS) UnmarshalJSON(b []byte) error {