Skip to content

Commit

Permalink
Support ranges with L
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilthoniel committed Dec 2, 2023
1 parent e46ecd9 commit 40df43c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
3 changes: 2 additions & 1 deletion cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ func TestSchedule_Next(t *testing.T) {
{"* * 2 * * *", time.Date(2000, time.March, 16, 2, 0, 0, 0, time.UTC)},
{"* * 10-13 * * *", time.Date(2000, time.March, 15, 12, 5, 2, 0, time.UTC)},
{"* * 2,13 * * *", time.Date(2000, time.March, 15, 13, 0, 0, 0, time.UTC)},
{"* * * 31 7 *", time.Date(2000, time.July, 31, 0, 0, 0, 0, time.UTC)},
{"* * * 31 4-7 *", time.Date(2000, time.May, 31, 0, 0, 0, 0, time.UTC)},
{"* * * 14 3,5 *", time.Date(2000, time.May, 14, 0, 0, 0, 0, time.UTC)},
{"* * * * 1-2 *", time.Date(2001, time.January, 1, 0, 0, 0, 0, time.UTC)},
{"* * * L * *", time.Date(2000, time.March, 31, 0, 0, 0, 0, time.UTC)},
{"* * * 1,L 4 *", time.Date(2000, time.April, 1, 0, 0, 0, 0, time.UTC)},
{"* * * 20-L 4 *", time.Date(2000, time.April, 20, 0, 0, 0, 0, time.UTC)},
{"* * * 1 12 SUN", time.Date(2002, time.December, 1, 0, 0, 0, 0, time.UTC)},
{"* * * 1 12 0", time.Date(2002, time.December, 1, 0, 0, 0, 0, time.UTC)},
{"* * * 1 12 SUN,FRI", time.Date(2000, time.December, 1, 0, 0, 0, 0, time.UTC)},
Expand Down
21 changes: 9 additions & 12 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,15 @@ func (Parser) parse(expr string, convFn ConvertFn) (fields []ExprField, err erro

func parseRange(expr string, convFn ConvertFn) (r Range, err error) {
parts := strings.Split(expr, "-")
from, err := convFn(parts[0])
r.from, err = convFn(parts[0])
if err != nil {
return
}
to, err := convFn(parts[1])
r.to, err = convFn(parts[1])
if err != nil {
return
}

r.from = from.Value(time.Time{})
r.to = to.Value(time.Time{})

return
}

Expand Down Expand Up @@ -259,23 +256,23 @@ func (u Unit) Value(_ time.Time) int {
}

type Range struct {
from int
to int
from ExprField
to ExprField
}

func (r Range) Compare(_ time.Time, other int) Ordering {
func (r Range) Compare(t time.Time, other int) Ordering {
switch {
case r.to < other:
case r.to.Compare(t, other) == OrderingLess:
return OrderingLess
case r.from > other:
case r.from.Compare(t, other) == OrderingGreater:
return OrderingGreater
default:
return OrderingEqual
}
}

func (r Range) Value(_ time.Time) int {
return r.from
func (r Range) Value(t time.Time) int {
return r.from.Value(t)
}

type LastDayOfMonth struct{}
Expand Down

0 comments on commit 40df43c

Please sign in to comment.