Reset hours on skip of day or month

This commit is contained in:
Andreas Schrell 2020-01-29 16:19:46 +01:00
parent 67346a81dd
commit 716da89727
2 changed files with 67 additions and 4 deletions

View File

@ -191,12 +191,12 @@ public class CronExpression {
@Override
ZonedDateTime setValue(ZonedDateTime dateTime, int value) {
return dateTime.withDayOfMonth(value).withMinute(0).withSecond(0).withNano(0);
return dateTime.withDayOfMonth(value).withHour(0).withMinute(0).withSecond(0).withNano(0);
}
@Override
ZonedDateTime overflow(ZonedDateTime dateTime) {
return dateTime.plusMonths(1).withDayOfMonth(0).withMinute(0).withSecond(0).withNano(0);
return dateTime.plusMonths(1).withDayOfMonth(0).withHour(0).withMinute(0).withSecond(0).withNano(0);
}
},
MONTH(1, 12,
@ -208,12 +208,12 @@ public class CronExpression {
@Override
ZonedDateTime setValue(ZonedDateTime dateTime, int value) {
return dateTime.withMonth(value).withDayOfMonth(1).withMinute(0).withSecond(0).withNano(0);
return dateTime.withMonth(value).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
}
@Override
ZonedDateTime overflow(ZonedDateTime dateTime) {
return dateTime.plusYears(1).withMonth(1).withDayOfMonth(1).withMinute(0).withSecond(0).withNano(0);
return dateTime.plusYears(1).withMonth(1).withHour(0).withDayOfMonth(1).withMinute(0).withSecond(0).withNano(0);
}
},
DAY_OF_WEEK(1, 7, Arrays.asList("MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN")) {

View File

@ -823,4 +823,67 @@ public class CronExpressionTest {
ZonedDateTime expected = ZonedDateTime.of(2016, 2, 29, 0, 0, 0, 0, zoneId);
assertTrue(CronExpression.createWithoutSeconds("* * 29 2 *").nextTimeAfter(after).equals(expected));
}
@Test
public void testTriggerProblemSameMonth() {
assertEquals(ZonedDateTime.parse("2020-01-02T00:50:00Z"),
new CronExpression("00 50 * 1-8 1 *")
.nextTimeAfter(ZonedDateTime.parse("2020-01-01T23:50:00Z")));
}
@Test
public void testTriggerProblemNextMonth() {
assertEquals(ZonedDateTime.parse("2020-02-01T00:50:00Z"),
new CronExpression("00 50 * 1-8 2 *")
.nextTimeAfter(ZonedDateTime.parse("2020-01-31T23:50:00Z")));
}
@Test
public void testTriggerProblemNextYear() {
assertEquals(ZonedDateTime.parse("2020-01-01T00:50:00Z"),
new CronExpression("00 50 * 1-8 1 *")
.nextTimeAfter(ZonedDateTime.parse("2019-12-31T23:50:00Z")));
}
@Test
public void testTriggerProblemNextMonthMonthAst() {
assertEquals(ZonedDateTime.parse("2020-02-01T00:50:00Z"),
new CronExpression("00 50 * 1-8 * *")
.nextTimeAfter(ZonedDateTime.parse("2020-01-31T23:50:00Z")));
}
@Test
public void testTriggerProblemNextYearMonthAst() {
assertEquals(ZonedDateTime.parse("2020-01-01T00:50:00Z"),
new CronExpression("00 50 * 1-8 * *")
.nextTimeAfter(ZonedDateTime.parse("2019-12-31T23:50:00Z")));
}
@Test
public void testTriggerProblemNextMonthDayAst() {
assertEquals(ZonedDateTime.parse("2020-02-01T00:50:00Z"),
new CronExpression("00 50 * * 2 *")
.nextTimeAfter(ZonedDateTime.parse("2020-01-31T23:50:00Z")));
}
@Test
public void testTriggerProblemNextYearDayAst() {
assertEquals(ZonedDateTime.parse("2020-01-01T00:50:00Z"),
new CronExpression("00 50 * * 1 *")
.nextTimeAfter(ZonedDateTime.parse("2019-12-31T22:50:00Z")));
}
@Test
public void testTriggerProblemNextMonthAllAst() {
assertEquals(ZonedDateTime.parse("2020-02-01T00:50:00Z"),
new CronExpression("00 50 * * * *")
.nextTimeAfter(ZonedDateTime.parse("2020-01-31T23:50:00Z")));
}
@Test
public void testTriggerProblemNextYearAllAst() {
assertEquals(ZonedDateTime.parse("2020-01-01T00:50:00Z"),
new CronExpression("00 50 * * * *")
.nextTimeAfter(ZonedDateTime.parse("2019-12-31T23:50:00Z")));
}
}