Skip to content

Commit

Permalink
Fix CronExpression issue with ZonedDateTime & DST
Browse files Browse the repository at this point in the history
This commit fixes an issue with CronExpression when used in combination
with ZonedDateTime and daylight saving time.

Closes spring-projectsgh-26744
  • Loading branch information
poutsma authored and lxbzmy committed Mar 26, 2022
1 parent 67eefb8 commit 1414a15
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
@@ -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.
Expand Down Expand Up @@ -237,7 +237,12 @@ public int checkValidValue(int value) {
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);
Expand Down
@@ -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.
Expand Down Expand Up @@ -1261,6 +1261,23 @@ public void sundayToFriday() {
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);
}



}

0 comments on commit 1414a15

Please sign in to comment.