Skip to content

Commit

Permalink
Merge pull request robfig#76 from ktogo/patch-1
Browse files Browse the repository at this point in the history
Fix: Cron.run() was resetting the timezone
  • Loading branch information
robfig authored Sep 27, 2016
2 parents 990e14e + 64eb71a commit 9585fd5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (c *Cron) run() {
timer := time.NewTimer(effective.Sub(now))
select {
case now = <-timer.C:
now = now.In(c.location)
// Run every entry whose next time was this effective time.
for _, e := range c.entries {
if e.Next != effective {
Expand Down
16 changes: 8 additions & 8 deletions cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,19 @@ func TestRunningMultipleSchedules(t *testing.T) {
// Test that the cron is run in the local time zone (as opposed to UTC).
func TestLocalTimezone(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
wg.Add(2)

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

cron := New()
cron.AddFunc(spec, func() { wg.Done() })
cron.Start()
defer cron.Stop()

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

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

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

cron := NewWithLocation(loc)
cron.AddFunc(spec, func() { wg.Done() })
cron.Start()
defer cron.Stop()

select {
case <-time.After(ONE_SECOND):
case <-time.After(ONE_SECOND * 2):
t.FailNow()
case <-wait(wg):
}
Expand Down

0 comments on commit 9585fd5

Please sign in to comment.