Skip to content

Commit eb68e6a

Browse files
committed
Support SUN as minimum of range in CronExpression
This commit makes sure that SUN can be used at the beginning of a range, like SUN-FRI. Closes gh-26598
1 parent a9240e0 commit eb68e6a

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

spring-context/src/main/java/org/springframework/scheduling/support/BitsCronField.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ private static ValueRange parseRange(String value, Type type) {
165165
int max = Integer.parseInt(value.substring(hyphenPos + 1));
166166
min = type.checkValidValue(min);
167167
max = type.checkValidValue(max);
168+
if (type == Type.DAY_OF_WEEK && min == 7) {
169+
// If used as a minimum in a range, Sunday means 0 (not 7)
170+
min = 0;
171+
}
168172
return ValueRange.of(min, max);
169173
}
170174
}

spring-context/src/test/java/org/springframework/scheduling/support/BitsCronFieldTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ void parse() {
4848
assertThat(BitsCronField.parseMonth("1")).has(set(1)).has(clearRange(2, 12));
4949

5050
assertThat(BitsCronField.parseDaysOfWeek("0")).has(set(7, 7)).has(clearRange(0, 6));
51+
52+
assertThat(BitsCronField.parseDaysOfWeek("7-5")).has(clear(0)).has(setRange(1, 5)).has(clear(6)).has(set(7));
5153
}
5254

5355
@Test

spring-context/src/test/java/org/springframework/scheduling/support/CronExpressionTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,5 +1242,25 @@ void quartzLastFridayOfTheMonthEveryHour() {
12421242
assertThat(actual).isEqualTo(expected);
12431243
}
12441244

1245+
@Test
1246+
public void sundayToFriday() {
1247+
CronExpression expression = CronExpression.parse("0 0 0 ? * SUN-FRI");
1248+
1249+
LocalDateTime last = LocalDateTime.of(2021, 2, 25, 15, 0);
1250+
LocalDateTime expected = LocalDateTime.of(2021, 2, 26, 0, 0);
1251+
LocalDateTime actual = expression.next(last);
1252+
assertThat(actual).isNotNull();
1253+
assertThat(actual).isEqualTo(expected);
1254+
assertThat(actual.getDayOfWeek()).isEqualTo(FRIDAY);
1255+
1256+
last = actual;
1257+
expected = LocalDateTime.of(2021, 2, 28, 0, 0);
1258+
actual = expression.next(last);
1259+
assertThat(actual).isNotNull();
1260+
assertThat(actual).isEqualTo(expected);
1261+
assertThat(actual.getDayOfWeek()).isEqualTo(SUNDAY);
1262+
}
1263+
1264+
12451265

12461266
}

0 commit comments

Comments
 (0)