From d0dbc19799fb90f0be82e17205612f2f625a115d Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sat, 3 May 2025 20:24:50 -0700 Subject: [PATCH] Interpret HH:MM as an implicit at-spec --- core/cron.go | 10 +++++++--- core/cron_test.go | 22 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/core/cron.go b/core/cron.go index aab698b..2e3f289 100644 --- a/core/cron.go +++ b/core/cron.go @@ -8,7 +8,7 @@ import ( "time" ) -func GetNextUpdate(lastUpdated time.Time, spec string) (nextUpdate time.Time, err error) { +func GetNextSpecTime(lastUpdated time.Time, spec string) (nextUpdate time.Time, err error) { var nextUpdates []time.Time switch { case strings.HasPrefix(spec, "every "): @@ -18,7 +18,11 @@ func GetNextUpdate(lastUpdated time.Time, spec string) (nextUpdate time.Time, er case strings.HasPrefix(spec, "on "): nextUpdates, err = parseOnSpec(lastUpdated, spec[len("on "):]) default: - return time.Time{}, fmt.Errorf("unknown spec format: %v", spec) + // "HH:MM" is implicitly an at-spec "at HH:MM", so see if it's that + nextUpdates, err = parseAtSpec(lastUpdated, spec) + if err != nil { + return time.Time{}, fmt.Errorf("unknown spec format: %v", spec) + } } if err != nil { return time.Time{}, err @@ -186,7 +190,7 @@ func fetchReadySources(db DB) { } now := time.Now().UTC() for _, schedule := range schedules { - nextUpdate, err := GetNextUpdate(schedule.LastUpdated, schedule.Spec) + nextUpdate, err := GetNextSpecTime(schedule.LastUpdated, schedule.Spec) if err != nil { log.Printf("error: could not determine next update for %s: %v", schedule.Source, err) continue diff --git a/core/cron_test.go b/core/cron_test.go index c95d511..9cb1e9c 100644 --- a/core/cron_test.go +++ b/core/cron_test.go @@ -6,7 +6,7 @@ import ( "time" ) -func TestGetNextUpdate(t *testing.T) { +func TestGetNextSpecTime(t *testing.T) { date := func(year int, month time.Month, day, hour, minute int) time.Time { return time.Date(year, month, day, hour, minute, 0, 0, time.UTC) } @@ -16,7 +16,7 @@ func TestGetNextUpdate(t *testing.T) { if comment := strings.Index(spec, "#"); comment > -1 { spec = spec[:comment] } - nextUpdate, err := GetNextUpdate(lastUpdated, spec) + nextUpdate, err := GetNextSpecTime(lastUpdated, spec) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -31,7 +31,7 @@ func TestGetNextUpdate(t *testing.T) { if comment := strings.Index(spec, "#"); comment > -1 { spec = spec[:comment] } - _, err := GetNextUpdate(lastUpdated, spec) + _, err := GetNextSpecTime(lastUpdated, spec) if err == nil { t.Error("expected error") } @@ -65,6 +65,22 @@ func TestGetNextUpdate(t *testing.T) { "every 3", ) + expect( + date(2020, 10, 22, 12, 0), + "14:00#1", + date(2020, 10, 22, 14, 0), + ) + expect( + date(2020, 10, 22, 14, 0), + "14:00#2", + date(2020, 10, 23, 14, 0), + ) + expect( + date(2020, 10, 22, 15, 0), + "14:00#3", + date(2020, 10, 23, 14, 0), + ) + expect( date(2020, 10, 22, 12, 0), "at 14:00#1",