mirror of https://github.com/go-co-op/gocron.git
daily job next logic failed to consider 1 midnight attime (#635)
This commit is contained in:
parent
1177ef07d1
commit
b0fdf551b9
19
job.go
19
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{}
|
||||
}
|
||||
|
||||
|
|
|
|||
10
job_test.go
10
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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue