Skip to content

Commit 0336bd2

Browse files
committed
Don't skip crons when time jumps fwd due to DST
1 parent bc59245 commit 0336bd2

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

spec.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,25 @@ WRAP:
135135
}
136136
}
137137

138+
HOUR:
138139
for 1<<uint(t.Hour())&s.Hour == 0 {
139140
if !added {
140141
added = true
141142
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, loc)
142143
}
144+
prev := t
143145
t = t.Add(1 * time.Hour)
144-
146+
// Special handling to avoid skipped crons when time jumps forward due
147+
// to DST. E.g. if time jumps from 1:59 to 3:00, a 2:00 cron should get
148+
// triggered at 3:00. To do this we calculate how many hours were
149+
// skipped after calling t.Add(1 * time.Hour), then we check each
150+
// skipped hour to see if it was set in the bitmap.
151+
step := t.Hour() - prev.Hour()
152+
for i := 1; i < step; i++ {
153+
if 1<<uint(t.Hour()-i)&s.Hour > 0 {
154+
break HOUR
155+
}
156+
}
145157
if t.Hour() == 0 {
146158
goto WRAP
147159
}

spec_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestNext(t *testing.T) {
111111
{"Mon Jul 9 23:35 2012", "0 0 0 29 Feb ?", "Mon Feb 29 00:00 2016"},
112112

113113
// Daylight savings time 2am EST (-5) -> 3am EDT (-4)
114-
{"2012-03-11T00:00:00-0500", "TZ=America/New_York 0 30 2 11 Mar ?", "2013-03-11T02:30:00-0400"},
114+
{"2012-03-11T00:00:00-0500", "TZ=America/New_York 0 30 2 11 Mar ?", "2012-03-11T03:30:00-0400"},
115115

116116
// hourly job
117117
{"2012-03-11T00:00:00-0500", "TZ=America/New_York 0 0 * * * ?", "2012-03-11T01:00:00-0500"},
@@ -129,8 +129,8 @@ func TestNext(t *testing.T) {
129129
{"2012-03-11T00:00:00-0500", "TZ=America/New_York 0 0 1 * * ?", "2012-03-11T01:00:00-0500"},
130130
{"2012-03-11T01:00:00-0500", "TZ=America/New_York 0 0 1 * * ?", "2012-03-12T01:00:00-0400"},
131131

132-
// 2am nightly job (skipped)
133-
{"2012-03-11T00:00:00-0500", "TZ=America/New_York 0 0 2 * * ?", "2012-03-12T02:00:00-0400"},
132+
// Daylight savings time 2am EST (-5) -> 3am EDT (-4)
133+
{"2012-03-11T00:00:00-0500", "TZ=America/New_York 0 0 2 * * ?", "2012-03-11T03:00:00-0400"},
134134

135135
// Daylight savings time 2am EDT (-4) => 1am EST (-5)
136136
{"2012-11-04T00:00:00-0400", "TZ=America/New_York 0 30 2 04 Nov ?", "2012-11-04T02:30:00-0500"},

0 commit comments

Comments
 (0)