Skip to content

Commit

Permalink
Change next day calculations to account for the change in date variat…
Browse files Browse the repository at this point in the history
…ions from a jump forward due to timezone from dst
  • Loading branch information
MikeZLin authored and Michael Lin committed Jul 6, 2020
1 parent 978959d commit 1b09a40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cronexpr_next.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (expr *Expression) nextDayOfMonth(t time.Time) time.Time {
return expr.nextMonth(t)
}

return time.Date(
newTime := time.Date(
t.Year(),
t.Month(),
expr.actualDaysOfMonthList[i],
Expand All @@ -117,6 +117,12 @@ func (expr *Expression) nextDayOfMonth(t time.Time) time.Time {
expr.secondList[0],
0,
t.Location())

// Fix for Daylight saving transition if first hour falls in the time jump
if newTime.Hour() < expr.hourList[0] {
newTime = newTime.Add(time.Duration(expr.hourList[0]-newTime.Hour()) * time.Hour).Truncate(time.Second)
}
return newTime
}

/******************************************************************************/
Expand Down
34 changes: 34 additions & 0 deletions cronexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,40 @@ func TestExpressionsWithTimeZones(t *testing.T) {
}
}

var specificLocationTests = []locationTestCase{
{"2019-03-31 02:30:00","2019-04-01 02:30:00", "Europe/London"},
{"2019-10-27 02:30:00","2019-10-28 02:30:00", "Europe/London"},
{"2019-03-31 02:30:00","2019-04-01 02:30:00", "Europe/Paris"},
{"2019-10-27 02:30:00","2019-10-28 02:30:00", "Europe/Paris"},
{"2019-03-09 02:30:00","2019-03-10 01:30:00", "America/New_York"},
{"2019-11-03 02:30:00","2019-11-04 02:30:00", "America/New_York"},
{"2019-03-09 02:30:00","2019-03-10 01:30:00", "America/Los_Angeles"},
{"2019-11-03 02:30:00","2019-11-04 02:30:00", "America/Los_Angeles"},
{"2019-03-09 02:30:00","2019-03-10 01:30:00", "US/Pacific"},
{"2019-11-03 02:30:00","2019-11-04 02:30:00", "US/Pacific"},
{"2019-04-05 02:30:00","2019-04-06 02:30:00", "Australia/Melbourne"},
{"2019-10-04 02:30:00","2019-10-05 02:30:00", "Australia/Melbourne"},
{"2019-10-04 02:30:00","2019-10-05 02:30:00", "America/Asuncion"},
}
func TestDailyExpressionsWithTimeZones(t *testing.T) {
for _, test := range specificLocationTests {
location, err := time.LoadLocation(test.where)
if err != nil {
t.Errorf("Invalid Test Location:%s", test.where)
}

from, _ := time.ParseInLocation("2006-01-02 15:04:05", test.when, location)
to, _ := time.ParseInLocation("2006-01-02 15:04:05", test.expected, location)
// Every 30 mins
expr, err := Parse("0 30 2 * * * *")
if err != nil {
t.Errorf(err.Error())
}
if expr.Next(from) != to{
t.Errorf("Expected Next time to be %s from %s in %s, when actually %s",to, from, test.where,expr.Next(from) )
}
}
}
/******************************************************************************/

func TestZero(t *testing.T) {
Expand Down

0 comments on commit 1b09a40

Please sign in to comment.