Browse Source

Fix CronExpression issue with ZonedDateTime & DST

This commit fixes an issue with CronExpression when used in combination
with ZonedDateTime and daylight saving time.

Closes gh-26744
pull/26752/head
Arjen Poutsma 4 years ago
parent
commit
ab18ab6025
  1. 9
      spring-context/src/main/java/org/springframework/scheduling/support/CronField.java
  2. 19
      spring-context/src/test/java/org/springframework/scheduling/support/CronExpressionTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -237,7 +237,12 @@ abstract class CronField { @@ -237,7 +237,12 @@ abstract class CronField {
public <T extends Temporal & Comparable<? super T>> T elapseUntil(T temporal, int goal) {
int current = get(temporal);
if (current < goal) {
return this.field.getBaseUnit().addTo(temporal, goal - current);
T result = this.field.getBaseUnit().addTo(temporal, goal - current);
current = get(result);
if (current > goal) { // can occur due to daylight saving, see gh-26744
result = this.field.getBaseUnit().addTo(result, goal - current);
}
return result;
}
else {
ValueRange range = temporal.range(this.field);

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -1261,6 +1261,23 @@ class CronExpressionTests { @@ -1261,6 +1261,23 @@ class CronExpressionTests {
assertThat(actual.getDayOfWeek()).isEqualTo(SUNDAY);
}
@Test
public void daylightSaving() {
CronExpression cronExpression = CronExpression.parse("0 0 9 * * *");
ZonedDateTime last = ZonedDateTime.parse("2021-03-27T09:00:00+01:00[Europe/Amsterdam]");
ZonedDateTime expected = ZonedDateTime.parse("2021-03-28T09:00:00+01:00[Europe/Amsterdam]");
ZonedDateTime actual = cronExpression.next(last);
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
last = ZonedDateTime.parse("2021-10-30T09:00:00+01:00[Europe/Amsterdam]");
expected = ZonedDateTime.parse("2021-10-31T09:00:00+02:00[Europe/Amsterdam]");
actual = cronExpression.next(last);
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
}
}

Loading…
Cancel
Save