Skip to content

Commit

Permalink
fix(validator): months do not start from 0
Browse files Browse the repository at this point in the history
  • Loading branch information
codenem authored and adhocore committed Mar 12, 2024
1 parent 8e77b84 commit 1d36e13
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func boundsByPos(pos int) (bounds []int) {
case 5:
bounds = []int{0, 7}
case 6:
bounds = []int{1, 9999}
bounds = []int{0, 9999}
}
return
}
3 changes: 2 additions & 1 deletion gronx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ func testcases() []Case {
{"* 20,21,22 * * *", "2015-08-10 21:50:00", true, "2015-08-10 21:51:00"},
{"* 20,22 * * *", "2015-08-10 21:50:00", false, "2015-08-10 22:00:00"},
{"* 5,21-22 * * *", "2015-08-10 21:50:00", true, "2015-08-10 21:51:00"},
{"7-9 * */9 * *", "2015-08-10 22:02:00", false, "2015-08-18 00:07:00"},
{"7-9 * */9 * *", "2015-08-10 22:02:00", false, "2015-08-10 22:07:00"},
{"7-9 * */9 * *", "2015-08-11 22:02:00", false, "2015-08-19 00:07:00"},
{"1 * * * 7", "2015-08-10 21:47:00", false, "2015-08-16 00:01:00"},
{"47 21 * * *", "2015-08-10 21:47:00", true, "2015-08-11 21:47:00"},
{"00 * * * *", "2023-07-21 12:30:00", false, "2023-07-21 13:00:00"},
Expand Down
25 changes: 13 additions & 12 deletions next.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ over:
if reverse {
delta = -time.Second
}
next, _, err = bumpUntilDue(gron.C, segments[0], 0, next.Add(delta), reverse)
next = next.Add(delta)
continue
}
return
Expand Down Expand Up @@ -118,44 +118,45 @@ func bump(ref time.Time, pos int, reverse bool) time.Time {
if reverse {
factor = -1
}
loc := ref.Location()

switch pos {
case 0:
ref = ref.Add(time.Duration(factor) * time.Second)
case 1:
minTime := ref.Add(time.Duration(factor) * time.Minute)
if reverse {
ref = time.Date(minTime.Year(), minTime.Month(), minTime.Day(), minTime.Hour(), minTime.Minute(), 59, 0, minTime.Location())
ref = time.Date(minTime.Year(), minTime.Month(), minTime.Day(), minTime.Hour(), minTime.Minute(), 59, 0, loc)
} else {
ref = time.Date(minTime.Year(), minTime.Month(), minTime.Day(), minTime.Hour(), minTime.Minute(), 0, 0, minTime.Location())
ref = time.Date(minTime.Year(), minTime.Month(), minTime.Day(), minTime.Hour(), minTime.Minute(), 0, 0, loc)
}
case 2:
hTime := ref.Add(time.Duration(factor) * time.Hour)
if reverse {
ref = time.Date(hTime.Year(), hTime.Month(), hTime.Day(), hTime.Hour(), 59, 59, 0, hTime.Location())
ref = time.Date(hTime.Year(), hTime.Month(), hTime.Day(), hTime.Hour(), 59, 59, 0, loc)
} else {
ref = time.Date(hTime.Year(), hTime.Month(), hTime.Day(), hTime.Hour(), 0, 0, 0, hTime.Location())
ref = time.Date(hTime.Year(), hTime.Month(), hTime.Day(), hTime.Hour(), 0, 0, 0, loc)
}
case 3, 5:
dTime := ref.AddDate(0, 0, factor)
if reverse {
ref = time.Date(dTime.Year(), dTime.Month(), dTime.Day(), 23, 59, 59, 0, dTime.Location())
ref = time.Date(dTime.Year(), dTime.Month(), dTime.Day(), 23, 59, 59, 0, loc)
} else {
ref = time.Date(dTime.Year(), dTime.Month(), dTime.Day(), 0, 0, 0, 0, dTime.Location())
ref = time.Date(dTime.Year(), dTime.Month(), dTime.Day(), 0, 0, 0, 0, loc)
}
case 4:
mTime := ref.AddDate(0, factor, 0)
ref = time.Date(ref.Year(), ref.Month(), 1, 0, 0, 0, 0, loc)
if reverse {
ref = time.Date(mTime.Year(), ref.Month(), -1, 23, 59, 59, 0, mTime.Location())
ref = ref.Add(time.Duration(factor) * time.Second)
} else {
ref = time.Date(mTime.Year(), mTime.Month(), 1, 0, 0, 0, 0, mTime.Location())
ref = ref.AddDate(0, factor, 0)
}
case 6:
yTime := ref.AddDate(factor, 0, 0)
if reverse {
ref = time.Date(yTime.Year(), 12, 31, 23, 59, 59, 0, yTime.Location())
ref = time.Date(yTime.Year(), 12, 31, 23, 59, 59, 0, loc)
} else {
ref = time.Date(yTime.Year(), 1, 1, 0, 0, 0, 0, yTime.Location())
ref = time.Date(yTime.Year(), 1, 1, 0, 0, 0, 0, loc)
}
}
return ref
Expand Down
5 changes: 4 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func inStep(val int, s string, bounds []int) (bool, error) {
return false, errors.New("step can't be 0")
}

if strings.Index(s, "*/") == 0 || strings.Index(s, "0/") == 0 {
if strings.Index(s, "*/") == 0 {
return (val-bounds[0])%step == 0, nil
}
if strings.Index(s, "0/") == 0 {
return val%step == 0, nil
}

Expand Down

0 comments on commit 1d36e13

Please sign in to comment.