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
This commit is contained in:
vikey
2022-02-12 18:51:24 +08:00
committed by Arjen Poutsma
parent 685a195ba1
commit 7276752e7c
2 changed files with 15 additions and 2 deletions

View File

@@ -260,7 +260,7 @@ abstract class CronField {
* Roll forward the give temporal until it reaches the next higher
* order field. Calling this method is equivalent to calling
* {@link #elapseUntil(Temporal, int)} with goal set to the
* minimum value of this field's range.
* minimum value of this field's range, except for daylight saving.
* @param temporal the temporal to roll forward
* @param <T> the type of temporal
* @return the rolled forward temporal
@@ -269,7 +269,12 @@ abstract class CronField {
int current = get(temporal);
ValueRange range = temporal.range(this.field);
long amount = range.getMaximum() - current + 1;
return this.field.getBaseUnit().addTo(temporal, amount);
T result = this.field.getBaseUnit().addTo(temporal, amount);
//adjust daylight saving
if (get(result) != range.getMinimum()) {
result = this.field.adjustInto(result,result.range(this.field).getMinimum());
}
return result;
}
/**

View File

@@ -1336,6 +1336,14 @@ class CronExpressionTests {
actual = cronExpression.next(last);
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
cronExpression = CronExpression.parse("0 5 0 * * *");
last = ZonedDateTime.parse("2021-03-28T01:00:00+01:00[Europe/Amsterdam]");
expected = ZonedDateTime.parse("2021-03-29T00:05+02:00[Europe/Amsterdam]");
actual = cronExpression.next(last);
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
}
@Test