Skip to content

Commit

Permalink
Fix CronExpression issue with DST
Browse files Browse the repository at this point in the history
This commit fixes an issue with CronExpression fails to calculate next
execution on the day of daylight saving time.

Closes gh-28038
  • Loading branch information
vikeychen authored and poutsma committed Feb 16, 2022
1 parent 685a195 commit 7276752
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public <T extends Temporal & Comparable<? super T>> T elapseUntil(T temporal, in
* 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
Expand All @@ -269,7 +269,12 @@ public <T extends Temporal & Comparable<? super T>> T rollForward(T temporal) {
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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,14 @@ public void daylightSaving() {
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
Expand Down

0 comments on commit 7276752

Please sign in to comment.