Skip to content

Commit

Permalink
Support SUN as minimum of range in CronExpression
Browse files Browse the repository at this point in the history
This commit makes sure that SUN can be used at the beginning of a range,
 like SUN-FRI.

Closes spring-projectsgh-26598
  • Loading branch information
poutsma authored and lxbzmy committed Mar 26, 2022
1 parent ff81253 commit 79cf747
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ private static ValueRange parseRange(String value, Type type) {
int max = Integer.parseInt(value.substring(hyphenPos + 1));
min = type.checkValidValue(min);
max = type.checkValidValue(max);
if (type == Type.DAY_OF_WEEK && min == 7) {
// If used as a minimum in a range, Sunday means 0 (not 7)
min = 0;
}
return ValueRange.of(min, max);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void parse() {
assertThat(BitsCronField.parseMonth("1")).has(set(1)).has(clearRange(2, 12));

assertThat(BitsCronField.parseDaysOfWeek("0")).has(set(7, 7)).has(clearRange(0, 6));

assertThat(BitsCronField.parseDaysOfWeek("7-5")).has(clear(0)).has(setRange(1, 5)).has(clear(6)).has(set(7));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,5 +1242,25 @@ void quartzLastFridayOfTheMonthEveryHour() {
assertThat(actual).isEqualTo(expected);
}

@Test
public void sundayToFriday() {
CronExpression expression = CronExpression.parse("0 0 0 ? * SUN-FRI");

LocalDateTime last = LocalDateTime.of(2021, 2, 25, 15, 0);
LocalDateTime expected = LocalDateTime.of(2021, 2, 26, 0, 0);
LocalDateTime actual = expression.next(last);
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
assertThat(actual.getDayOfWeek()).isEqualTo(FRIDAY);

last = actual;
expected = LocalDateTime.of(2021, 2, 28, 0, 0);
actual = expression.next(last);
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
assertThat(actual.getDayOfWeek()).isEqualTo(SUNDAY);
}



}

0 comments on commit 79cf747

Please sign in to comment.