diff --git a/engine/action_timing.go b/engine/action_timing.go index 5136242b9..6c9aaea61 100644 --- a/engine/action_timing.go +++ b/engine/action_timing.go @@ -89,6 +89,7 @@ func (at *ActionTiming) GetNextStartTime() (t time.Time) { if i.MonthDays != nil && len(i.MonthDays) > 0 { i.MonthDays.Sort() now := time.Now() + month := now.Month() x := sort.SearchInts(i.MonthDays, now.Day()) d = i.MonthDays[0] if x < len(i.MonthDays) { @@ -104,14 +105,19 @@ func (at *ActionTiming) GetNextStartTime() (t time.Time) { } else { // today was not found in the list, x is the first greater day d = i.MonthDays[x] } + } else { + if len(i.Months) == 0 { + month += 1 + } } h, m, s := t.Clock() - t = time.Date(now.Year(), now.Month(), d, h, m, s, 0, time.Local) + t = time.Date(now.Year(), month, d, h, m, s, 0, time.Local) } MONTHS: if i.Months != nil && len(i.Months) > 0 { i.Months.Sort() now := time.Now() + year := now.Year() x := sort.Search(len(i.Months), func(x int) bool { return i.Months[x] >= now.Month() }) m = i.Months[0] if x < len(i.Months) { @@ -135,9 +141,13 @@ MONTHS: t = time.Date(t.Year(), t.Month(), i.MonthDays[0], t.Hour(), t.Minute(), t.Second(), 0, t.Location()) } } + } else { + if len(i.Years) == 0 { + year += 1 + } } h, min, s := t.Clock() - t = time.Date(now.Year(), m, t.Day(), h, min, s, 0, time.Local) + t = time.Date(year, m, t.Day(), h, min, s, 0, time.Local) } YEARS: if i.Years != nil && len(i.Years) > 0 { diff --git a/engine/actions_test.go b/engine/actions_test.go index 70f9b8bd0..ebabeac54 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -175,7 +175,6 @@ func TestActionTimingFirstOfTheMonth(t *testing.T) { at := &ActionTiming{Timing: &Interval{ Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December}, MonthDays: MonthDays{1}, - StartTime: "00:00:00", }} st := at.GetNextStartTime() expected := nextMonth @@ -297,6 +296,20 @@ func TestActionTimingFirstOfTheYear(t *testing.T) { } } +func TestActionTimingFirstMonthOfTheYear(t *testing.T) { + now := time.Now() + y, _, _ := now.Date() + nextYear := time.Date(y+1, 1, 1, 0, 0, 0, 0, time.Local) + at := &ActionTiming{Timing: &Interval{ + Months: Months{time.January}, + }} + st := at.GetNextStartTime() + expected := nextYear + if !st.Equal(expected) { + t.Errorf("Expected %v was %v", expected, st) + } +} + func TestActionTimingCheckForASAP(t *testing.T) { at := &ActionTiming{Timing: &Interval{StartTime: ASAP}} if !at.CheckForASAP() {