Interpret HH:MM as an implicit at-spec

This commit is contained in:
Tim Van Baak 2025-05-03 20:24:50 -07:00
parent 38e5cf0c0d
commit d0dbc19799
2 changed files with 26 additions and 6 deletions

View File

@ -8,7 +8,7 @@ import (
"time" "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 var nextUpdates []time.Time
switch { switch {
case strings.HasPrefix(spec, "every "): 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 "): case strings.HasPrefix(spec, "on "):
nextUpdates, err = parseOnSpec(lastUpdated, spec[len("on "):]) nextUpdates, err = parseOnSpec(lastUpdated, spec[len("on "):])
default: 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 { if err != nil {
return time.Time{}, err return time.Time{}, err
@ -186,7 +190,7 @@ func fetchReadySources(db DB) {
} }
now := time.Now().UTC() now := time.Now().UTC()
for _, schedule := range schedules { for _, schedule := range schedules {
nextUpdate, err := GetNextUpdate(schedule.LastUpdated, schedule.Spec) nextUpdate, err := GetNextSpecTime(schedule.LastUpdated, schedule.Spec)
if err != nil { if err != nil {
log.Printf("error: could not determine next update for %s: %v", schedule.Source, err) log.Printf("error: could not determine next update for %s: %v", schedule.Source, err)
continue continue

View File

@ -6,7 +6,7 @@ import (
"time" "time"
) )
func TestGetNextUpdate(t *testing.T) { func TestGetNextSpecTime(t *testing.T) {
date := func(year int, month time.Month, day, hour, minute int) time.Time { 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) 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 { if comment := strings.Index(spec, "#"); comment > -1 {
spec = spec[:comment] spec = spec[:comment]
} }
nextUpdate, err := GetNextUpdate(lastUpdated, spec) nextUpdate, err := GetNextSpecTime(lastUpdated, spec)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@ -31,7 +31,7 @@ func TestGetNextUpdate(t *testing.T) {
if comment := strings.Index(spec, "#"); comment > -1 { if comment := strings.Index(spec, "#"); comment > -1 {
spec = spec[:comment] spec = spec[:comment]
} }
_, err := GetNextUpdate(lastUpdated, spec) _, err := GetNextSpecTime(lastUpdated, spec)
if err == nil { if err == nil {
t.Error("expected error") t.Error("expected error")
} }
@ -65,6 +65,22 @@ func TestGetNextUpdate(t *testing.T) {
"every 3", "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( expect(
date(2020, 10, 22, 12, 0), date(2020, 10, 22, 12, 0),
"at 14:00#1", "at 14:00#1",