Skip to content

Commit 64eb71a

Browse files
committed
Fix: Cron.run() was resetting the timezone
time.Timer() does not return the timezone within the time since its unaware of the desired timezone as it only accepts time.Duration when it's initialized, thus the receiver always needs to take care of the timezone.
1 parent 990e14e commit 64eb71a

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

cron.go

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ func (c *Cron) run() {
183183
timer := time.NewTimer(effective.Sub(now))
184184
select {
185185
case now = <-timer.C:
186+
now = now.In(c.location)
186187
// Run every entry whose next time was this effective time.
187188
for _, e := range c.entries {
188189
if e.Next != effective {

cron_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,19 @@ func TestRunningMultipleSchedules(t *testing.T) {
219219
// Test that the cron is run in the local time zone (as opposed to UTC).
220220
func TestLocalTimezone(t *testing.T) {
221221
wg := &sync.WaitGroup{}
222-
wg.Add(1)
222+
wg.Add(2)
223223

224224
now := time.Now().Local()
225-
spec := fmt.Sprintf("%d %d %d %d %d ?",
226-
now.Second()+1, now.Minute(), now.Hour(), now.Day(), now.Month())
225+
spec := fmt.Sprintf("%d,%d %d %d %d %d ?",
226+
now.Second()+1, now.Second()+2, now.Minute(), now.Hour(), now.Day(), now.Month())
227227

228228
cron := New()
229229
cron.AddFunc(spec, func() { wg.Done() })
230230
cron.Start()
231231
defer cron.Stop()
232232

233233
select {
234-
case <-time.After(ONE_SECOND):
234+
case <-time.After(ONE_SECOND * 2):
235235
t.FailNow()
236236
case <-wait(wg):
237237
}
@@ -240,7 +240,7 @@ func TestLocalTimezone(t *testing.T) {
240240
// Test that the cron is run in the given time zone (as opposed to local).
241241
func TestNonLocalTimezone(t *testing.T) {
242242
wg := &sync.WaitGroup{}
243-
wg.Add(1)
243+
wg.Add(2)
244244

245245
loc, err := time.LoadLocation("Atlantic/Cape_Verde")
246246
if err != nil {
@@ -249,16 +249,16 @@ func TestNonLocalTimezone(t *testing.T) {
249249
}
250250

251251
now := time.Now().In(loc)
252-
spec := fmt.Sprintf("%d %d %d %d %d ?",
253-
now.Second()+1, now.Minute(), now.Hour(), now.Day(), now.Month())
252+
spec := fmt.Sprintf("%d,%d %d %d %d %d ?",
253+
now.Second()+1, now.Second()+2, now.Minute(), now.Hour(), now.Day(), now.Month())
254254

255255
cron := NewWithLocation(loc)
256256
cron.AddFunc(spec, func() { wg.Done() })
257257
cron.Start()
258258
defer cron.Stop()
259259

260260
select {
261-
case <-time.After(ONE_SECOND):
261+
case <-time.After(ONE_SECOND * 2):
262262
t.FailNow()
263263
case <-wait(wg):
264264
}

0 commit comments

Comments
 (0)