From b4aa4ad310cf0ff7e7d1cd3b8f03d5028a226b8e Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Sat, 1 Jan 2022 17:13:50 -0500 Subject: [PATCH] ts shouldnt yield zero ever, yield now if so --- pttodo/schedule_test.go | 19 ++++++++++++++----- pttodo/ts.go | 6 ++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pttodo/schedule_test.go b/pttodo/schedule_test.go index 8e96f4b..8b25989 100644 --- a/pttodo/schedule_test.go +++ b/pttodo/schedule_test.go @@ -70,11 +70,6 @@ func TestSchedulerFactory(t *testing.T) { want: scheduleDue(1), next: time.Unix(1, 0), }, - "zero ts": { - input: `0`, - want: scheduleDue(0), - next: time.Unix(0, 0), - }, "never": { input: ``, 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) + } + }) } diff --git a/pttodo/ts.go b/pttodo/ts.go index 6d671b3..d0cac52 100644 --- a/pttodo/ts.go +++ b/pttodo/ts.go @@ -11,6 +11,9 @@ import ( type TS int64 func (ts TS) time() time.Time { + if ts == 0 { + ts = TS(time.Now().Unix()) + } return time.Unix(int64(ts), 0) } @@ -26,8 +29,7 @@ func (ts TS) MarshalYAML() (interface{}, error) { if ts == 0 { ts = TS(time.Now().Unix()) } - t := time.Unix(int64(ts), 0) - return t.Format(time.UnixDate), nil + return ts.time().Format(time.UnixDate), nil } func (ts *TS) UnmarshalJSON(b []byte) error {