Browse Source

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.
Kazuhisa Togo 9 years ago
parent
commit
64eb71ae78
2 changed files with 9 additions and 8 deletions
  1. 1 0
      cron.go
  2. 8 8
      cron_test.go

+ 1 - 0
cron.go

@@ -183,6 +183,7 @@ func (c *Cron) run() {
 		timer := time.NewTimer(effective.Sub(now))
 		timer := time.NewTimer(effective.Sub(now))
 		select {
 		select {
 		case now = <-timer.C:
 		case now = <-timer.C:
+			now = now.In(c.location)
 			// Run every entry whose next time was this effective time.
 			// Run every entry whose next time was this effective time.
 			for _, e := range c.entries {
 			for _, e := range c.entries {
 				if e.Next != effective {
 				if e.Next != effective {

+ 8 - 8
cron_test.go

@@ -219,11 +219,11 @@ func TestRunningMultipleSchedules(t *testing.T) {
 // Test that the cron is run in the local time zone (as opposed to UTC).
 // Test that the cron is run in the local time zone (as opposed to UTC).
 func TestLocalTimezone(t *testing.T) {
 func TestLocalTimezone(t *testing.T) {
 	wg := &sync.WaitGroup{}
 	wg := &sync.WaitGroup{}
-	wg.Add(1)
+	wg.Add(2)
 
 
 	now := time.Now().Local()
 	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 := New()
 	cron.AddFunc(spec, func() { wg.Done() })
 	cron.AddFunc(spec, func() { wg.Done() })
@@ -231,7 +231,7 @@ func TestLocalTimezone(t *testing.T) {
 	defer cron.Stop()
 	defer cron.Stop()
 
 
 	select {
 	select {
-	case <-time.After(ONE_SECOND):
+	case <-time.After(ONE_SECOND * 2):
 		t.FailNow()
 		t.FailNow()
 	case <-wait(wg):
 	case <-wait(wg):
 	}
 	}
@@ -240,7 +240,7 @@ func TestLocalTimezone(t *testing.T) {
 // Test that the cron is run in the given time zone (as opposed to local).
 // Test that the cron is run in the given time zone (as opposed to local).
 func TestNonLocalTimezone(t *testing.T) {
 func TestNonLocalTimezone(t *testing.T) {
 	wg := &sync.WaitGroup{}
 	wg := &sync.WaitGroup{}
-	wg.Add(1)
+	wg.Add(2)
 
 
 	loc, err := time.LoadLocation("Atlantic/Cape_Verde")
 	loc, err := time.LoadLocation("Atlantic/Cape_Verde")
 	if err != nil {
 	if err != nil {
@@ -249,8 +249,8 @@ func TestNonLocalTimezone(t *testing.T) {
 	}
 	}
 
 
 	now := time.Now().In(loc)
 	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 := NewWithLocation(loc)
 	cron.AddFunc(spec, func() { wg.Done() })
 	cron.AddFunc(spec, func() { wg.Done() })
@@ -258,7 +258,7 @@ func TestNonLocalTimezone(t *testing.T) {
 	defer cron.Stop()
 	defer cron.Stop()
 
 
 	select {
 	select {
-	case <-time.After(ONE_SECOND):
+	case <-time.After(ONE_SECOND * 2):
 		t.FailNow()
 		t.FailNow()
 	case <-wait(wg):
 	case <-wait(wg):
 	}
 	}