Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cron): allow LW characters combination in the day-of-month field #153

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/csm/day_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func (n *DayNode) max() int {
}

func (n *DayNode) nextDayN() (overflowed bool) {
switch n.n {
case NWeekday:
switch {
case n.n > 0 && n.n&NWeekday != 0:
n.nextWeekdayOfMonth()
default:
n.nextLastDayOfMonth()
Expand All @@ -155,7 +155,7 @@ func (n *DayNode) nextWeekdayOfMonth() {

monthLastDate := lastDayOfMonth(year, month)
date := n.c.values[0]
if date > monthLastDate {
if date > monthLastDate || n.n&NLastDayOfMonth != 0 {
date = monthLastDate
}

Expand Down
22 changes: 14 additions & 8 deletions quartz/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,22 @@ func parseDayOfMonthField(field string, min, max int, translate ...[]string) (*c
return newCronFieldN([]int{}, -n), nil
}

if strings.ContainsRune(field, weekdayRune) && cronWeekdayRegex.MatchString(field) {
day := strings.TrimSuffix(field, string(weekdayRune))
if day == "" {
return nil, newInvalidCronFieldError("weekday", field)
if strings.ContainsRune(field, weekdayRune) {
if field == fmt.Sprintf("%c%c", lastRune, weekdayRune) {
return newCronFieldN([]int{0}, cronLastDayOfMonthN|cronWeekdayN), nil
}
dayOfMonth, err := strconv.Atoi(day)
if err != nil || !inScope(dayOfMonth, min, max) {
return nil, newInvalidCronFieldError("weekday", field)

if cronWeekdayRegex.MatchString(field) {
day := strings.TrimSuffix(field, string(weekdayRune))
if day == "" {
return nil, newInvalidCronFieldError("weekday", field)
}
dayOfMonth, err := strconv.Atoi(day)
if err != nil || !inScope(dayOfMonth, min, max) {
return nil, newInvalidCronFieldError("weekday", field)
}
return newCronFieldN([]int{dayOfMonth}, cronWeekdayN), nil
}
return newCronFieldN([]int{dayOfMonth}, cronWeekdayN), nil
}

return parseField(field, min, max, translate...)
Expand Down
5 changes: 5 additions & 0 deletions quartz/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ func TestCronExpressionDayOfMonth(t *testing.T) {
expression: "0 15 10 31W * ?",
expected: "Mon Mar 31 10:15:00 2025",
},
{
expression: "0 15 10 LW * ?",
expected: "Mon Mar 31 10:15:00 2025",
},
}

prev := time.Date(2024, 1, 1, 12, 00, 00, 00, time.UTC).UnixNano()
Expand Down Expand Up @@ -419,6 +423,7 @@ func TestCronExpressionParseError(t *testing.T) {
"0 15 10 L- * ?",
"0 15 10 L-a * ?",
"0 15 10 L-32 * ?",
"0 15 10 WL * ?",
}

for _, tt := range tests {
Expand Down
Loading