diff --git a/job.go b/job.go index 6345ad3..bc2773b 100644 --- a/job.go +++ b/job.go @@ -622,12 +622,7 @@ type dailyJob struct { } func (d dailyJob) next(lastRun time.Time) time.Time { - next := d.nextDay(lastRun) - if !next.IsZero() { - return next - } - startNextDay := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(d.interval), 0, 0, 0, lastRun.Nanosecond(), lastRun.Location()) - return d.nextDay(startNextDay) + return d.nextDay(lastRun) } func (d dailyJob) nextDay(lastRun time.Time) time.Time { @@ -643,6 +638,18 @@ func (d dailyJob) nextDay(lastRun time.Time) time.Time { return atDate } } + startNextDay := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(d.interval), 0, 0, 0, lastRun.Nanosecond(), lastRun.Location()) + for _, at := range d.atTimes { + // sub the at time hour/min/sec onto the lastRun's values + // to use in checks to see if we've got our next run time + atDate := time.Date(startNextDay.Year(), startNextDay.Month(), startNextDay.Day(), at.Hour(), at.Minute(), at.Second(), startNextDay.Nanosecond(), startNextDay.Location()) + + if !atDate.Before(startNextDay) { + // now that we're looking at the next day, it's ok to consider + // the same at time that was last run + return atDate + } + } return time.Time{} } diff --git a/job_test.go b/job_test.go index 16d8127..9aeb6be 100644 --- a/job_test.go +++ b/job_test.go @@ -50,6 +50,16 @@ func TestDailyJob_next(t *testing.T) { expectedNextRun time.Time expectedDurationToNextRun time.Duration }{ + { + "daily at midnight", + 1, + []time.Time{ + time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC), + }, + time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), + time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), + 24 * time.Hour, + }, { "daily multiple at times", 1,