Skip to content

Commit 7276752

Browse files
vikeychenpoutsma
authored andcommitted
Fix CronExpression issue with DST
This commit fixes an issue with CronExpression fails to calculate next execution on the day of daylight saving time. Closes gh-28038
1 parent 685a195 commit 7276752

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public <T extends Temporal & Comparable<? super T>> T elapseUntil(T temporal, in
260260
* Roll forward the give temporal until it reaches the next higher
261261
* order field. Calling this method is equivalent to calling
262262
* {@link #elapseUntil(Temporal, int)} with goal set to the
263-
* minimum value of this field's range.
263+
* minimum value of this field's range, except for daylight saving.
264264
* @param temporal the temporal to roll forward
265265
* @param <T> the type of temporal
266266
* @return the rolled forward temporal
@@ -269,7 +269,12 @@ public <T extends Temporal & Comparable<? super T>> T rollForward(T temporal) {
269269
int current = get(temporal);
270270
ValueRange range = temporal.range(this.field);
271271
long amount = range.getMaximum() - current + 1;
272-
return this.field.getBaseUnit().addTo(temporal, amount);
272+
T result = this.field.getBaseUnit().addTo(temporal, amount);
273+
//adjust daylight saving
274+
if (get(result) != range.getMinimum()) {
275+
result = this.field.adjustInto(result,result.range(this.field).getMinimum());
276+
}
277+
return result;
273278
}
274279

275280
/**

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

+8
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,14 @@ public void daylightSaving() {
13361336
actual = cronExpression.next(last);
13371337
assertThat(actual).isNotNull();
13381338
assertThat(actual).isEqualTo(expected);
1339+
1340+
cronExpression = CronExpression.parse("0 5 0 * * *");
1341+
1342+
last = ZonedDateTime.parse("2021-03-28T01:00:00+01:00[Europe/Amsterdam]");
1343+
expected = ZonedDateTime.parse("2021-03-29T00:05+02:00[Europe/Amsterdam]");
1344+
actual = cronExpression.next(last);
1345+
assertThat(actual).isNotNull();
1346+
assertThat(actual).isEqualTo(expected);
13391347
}
13401348

13411349
@Test

0 commit comments

Comments
 (0)