Skip to content

Commit a0d328c

Browse files
author
Harshal Pradhan
committed
Fix problems with times in timezone offsets which are not multiples of an hour.
Truncate does something we do not expect in the case of non integer hour timezone offset. Its basically rounding to hour from t = 0. Which in this case is Unix t=0 ... which means it rounds to the half hour.
1 parent 32d9c27 commit a0d328c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

spec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ WRAP:
108108
for 1<<uint(t.Hour())&s.Hour == 0 {
109109
if !added {
110110
added = true
111-
t = t.Truncate(time.Hour)
111+
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
112112
}
113113
t = t.Add(1 * time.Hour)
114114

spec_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,48 @@ func getTime(value string) time.Time {
202202

203203
return t
204204
}
205+
206+
func TestNextWithTz(t *testing.T) {
207+
runs := []struct {
208+
time, spec string
209+
expected string
210+
}{
211+
// Failing tests
212+
{"2016-01-03T13:09:03+0530", "0 14 14 * * *", "2016-01-03T14:14:00+0530"},
213+
{"2016-01-03T04:09:03+0530", "0 14 14 * * ?", "2016-01-03T14:14:00+0530"},
214+
215+
// Passing tests
216+
{"2016-01-03T14:09:03+0530", "0 14 14 * * *", "2016-01-03T14:14:00+0530"},
217+
{"2016-01-03T14:00:00+0530", "0 14 14 * * ?", "2016-01-03T14:14:00+0530"},
218+
}
219+
for _, c := range runs {
220+
sched, err := Parse(c.spec)
221+
if err != nil {
222+
t.Error(err)
223+
continue
224+
}
225+
actual := sched.Next(getTimeTZ(c.time))
226+
expected := getTimeTZ(c.expected)
227+
if !actual.Equal(expected) {
228+
t.Errorf("%s, \"%s\": (expected) %v != %v (actual)", c.time, c.spec, expected, actual)
229+
}
230+
}
231+
}
232+
233+
func getTimeTZ(value string) time.Time {
234+
if value == "" {
235+
return time.Time{}
236+
}
237+
t, err := time.Parse("Mon Jan 2 15:04 2006", value)
238+
if err != nil {
239+
t, err = time.Parse("Mon Jan 2 15:04:05 2006", value)
240+
if err != nil {
241+
t, err = time.Parse("2006-01-02T15:04:05-0700", value)
242+
if err != nil {
243+
panic(err)
244+
}
245+
}
246+
}
247+
248+
return t
249+
}

0 commit comments

Comments
 (0)