Skip to content

Commit

Permalink
Various bug fixes in CronExpression
Browse files Browse the repository at this point in the history
This commit makes various bug fixes in CronExpression and related files.

Closes gh-27136
  • Loading branch information
poutsma committed Jul 7, 2021
1 parent 94f56a2 commit 76b1c0f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Expand Up @@ -66,8 +66,9 @@ private CronExpression(
CronField daysOfWeek,
String expression) {

// reverse order, to make big changes first
// to make sure we end up at 0 nanos, we add an extra field
this.fields = new CronField[]{CronField.zeroNanos(), seconds, minutes, hours, daysOfMonth, months, daysOfWeek};
this.fields = new CronField[]{daysOfWeek, months, daysOfMonth, hours, minutes, seconds, CronField.zeroNanos()};
this.expression = expression;
}

Expand Down
Expand Up @@ -230,9 +230,7 @@ public int checkValidValue(int value) {
* Elapse the given temporal for the difference between the current
* value of this field and the goal value. Typically, the returned
* temporal will have the given goal as the current value for this type,
* but this is not the case for {@link #DAY_OF_MONTH}. For instance,
* if {@code goal} is 31, and {@code temporal} is April 16th,
* this method returns May 1st, because April 31st does not exist.
* but this is not the case for {@link #DAY_OF_MONTH}.
* @param temporal the temporal to elapse
* @param goal the goal value
* @param <T> the type of temporal
Expand All @@ -247,8 +245,9 @@ public <T extends Temporal & Comparable<? super T>> T elapseUntil(T temporal, in
return cast(temporal.with(this.field, goal));
}
else {
// goal is invalid, eg. 29th Feb, lets try to get as close as possible
return this.field.getBaseUnit().addTo(temporal, goal - current);
// goal is invalid, eg. 29th Feb, so roll forward
long amount = range.getMaximum() - current + 1;
return this.field.getBaseUnit().addTo(temporal, amount);
}
}
else {
Expand Down
Expand Up @@ -334,6 +334,9 @@ public <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal) {
// We ended up before the start, roll forward and try again
temporal = this.rollForwardType.rollForward(temporal);
result = adjust(temporal);
if (result != null) {
result = type().reset(result);
}
}
}
return result;
Expand Down
Expand Up @@ -1319,6 +1319,21 @@ public void daylightSaving() {
assertThat(actual).isEqualTo(expected);
}

@Test
public void various() {
CronExpression cronExpression = CronExpression.parse("3-57 13-28 17,18 1,15 3-12 6#1");
LocalDateTime last = LocalDateTime.of(2022, 9, 15, 17, 44, 11);
LocalDateTime expected = LocalDateTime.of(2022, 10, 1, 17, 13, 3);
LocalDateTime actual = cronExpression.next(last);
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);

cronExpression = CronExpression.parse("*/28 56 22 */6 * *");
last = LocalDateTime.of(2022, 2, 27, 8, 0, 42);
expected = LocalDateTime.of(2022, 3, 1, 22, 56, 0);
actual = cronExpression.next(last);
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
}

}

0 comments on commit 76b1c0f

Please sign in to comment.