|
7 | 7 | import com.cronutils.model.definition.CronDefinitionBuilder; |
8 | 8 | import com.cronutils.model.time.ExecutionTime; |
9 | 9 | import org.junit.Assert; |
| 10 | +import org.junit.Ignore; |
10 | 11 | import org.junit.Test; |
11 | 12 |
|
12 | 13 | import java.time.*; |
13 | 14 | import java.util.Optional; |
14 | 15 |
|
| 16 | +import static com.cronutils.model.field.expression.FieldExpression.always; |
15 | 17 | import static com.cronutils.model.field.expression.FieldExpression.questionMark; |
16 | 18 | import static com.cronutils.model.field.expression.FieldExpressionFactory.every; |
17 | 19 | import static com.cronutils.model.field.expression.FieldExpressionFactory.on; |
@@ -59,6 +61,107 @@ public static CronBuilder getEveryMonthFromNow(ZonedDateTime now, int every) { |
59 | 61 | .withMonth(every(on(now.getMonthValue()), every)); |
60 | 62 | } |
61 | 63 |
|
| 64 | + |
| 65 | + @Test |
| 66 | + public void testDaylightSavingOverlapMinuteNextRun() { |
| 67 | + |
| 68 | + |
| 69 | + LocalDateTime daylightSaving2020 = LocalDateTime.of(2020, 10, 25, 1, 10); |
| 70 | + Clock clock = Clock.fixed(daylightSaving2020.toInstant(ZoneOffset.ofHours(2)),ZoneId.of("Europe/Rome")); |
| 71 | + ZonedDateTime daylightSaving2020InLocalTimezone = ZonedDateTime.now(clock); |
| 72 | + System.out.println("\nnow: " + daylightSaving2020InLocalTimezone); |
| 73 | + Cron cron = getEvery30Minute(daylightSaving2020InLocalTimezone).instance(); |
| 74 | + |
| 75 | + ZonedDateTime nextRun = nextRun(cron, daylightSaving2020InLocalTimezone); // first run |
| 76 | + Assert.assertEquals(40, nextRun.getMinute()); |
| 77 | + Assert.assertEquals(1, nextRun.getHour()); |
| 78 | + nextRun = nextRun(cron, nextRun); // second |
| 79 | + Assert.assertEquals(10, nextRun.getMinute()); |
| 80 | + Assert.assertEquals(2, nextRun.getHour()); |
| 81 | + Assert.assertEquals(ZoneOffset.ofHours(2), nextRun.getOffset()); |
| 82 | + nextRun = nextRun(cron, nextRun); // second |
| 83 | + Assert.assertEquals(40, nextRun.getMinute()); |
| 84 | + Assert.assertEquals(2, nextRun.getHour()); |
| 85 | + Assert.assertEquals(ZoneOffset.ofHours(2), nextRun.getOffset()); |
| 86 | + nextRun = nextRun(cron, nextRun); // second |
| 87 | + Assert.assertEquals(10, nextRun.getMinute()); |
| 88 | + Assert.assertEquals(2, nextRun.getHour()); |
| 89 | + Assert.assertEquals(ZoneOffset.ofHours(1), nextRun.getOffset()); |
| 90 | + nextRun = nextRun(cron, nextRun); // second |
| 91 | + Assert.assertEquals(40, nextRun.getMinute()); |
| 92 | + Assert.assertEquals(2, nextRun.getHour()); |
| 93 | + Assert.assertEquals(ZoneOffset.ofHours(1), nextRun.getOffset()); |
| 94 | + nextRun = nextRun(cron, nextRun); // second |
| 95 | + Assert.assertEquals(10, nextRun.getMinute()); |
| 96 | + Assert.assertEquals(3, nextRun.getHour()); |
| 97 | + nextRun = nextRun(cron, nextRun); // second |
| 98 | + Assert.assertEquals(40, nextRun.getMinute()); |
| 99 | + Assert.assertEquals(3, nextRun.getHour()); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @Ignore |
| 105 | + public void testDaylightSavingOverlapHourNextRun() { |
| 106 | + |
| 107 | + LocalDateTime startDay = LocalDateTime.of(2020, 10, 24, 1, 0); // Day before Daylight saving |
| 108 | + Clock clock = Clock.fixed(startDay.toInstant(ZoneOffset.ofHours(2)),ZoneId.of("Europe/Rome")); |
| 109 | + ZonedDateTime daylightSaving2020InLocalTimezone = ZonedDateTime.now(clock); |
| 110 | + System.out.println("\nnow: " + daylightSaving2020InLocalTimezone); |
| 111 | + Cron cron = getEveryHour(daylightSaving2020InLocalTimezone).instance(); |
| 112 | + |
| 113 | + ZonedDateTime nextRun = nextRun(cron, daylightSaving2020InLocalTimezone); |
| 114 | + Assert.assertEquals(0, nextRun.getMinute()); |
| 115 | + Assert.assertEquals(2, nextRun.getHour()); |
| 116 | + Assert.assertEquals(ZoneOffset.ofHours(2), nextRun.getOffset()); |
| 117 | + nextRun = nextRun(cron, nextRun); // second |
| 118 | + Assert.assertEquals(0, nextRun.getMinute()); |
| 119 | + Assert.assertEquals(3, nextRun.getHour()); |
| 120 | + Assert.assertEquals(ZoneOffset.ofHours(2), nextRun.getOffset()); |
| 121 | + |
| 122 | + startDay = LocalDateTime.of(2020, 10, 25, 1, 0); // Daylight saving |
| 123 | + clock = Clock.fixed(startDay.toInstant(ZoneOffset.ofHours(2)),ZoneId.of("Europe/Rome")); |
| 124 | + daylightSaving2020InLocalTimezone = ZonedDateTime.now(clock); |
| 125 | + System.out.println("\nnow: " + daylightSaving2020InLocalTimezone); |
| 126 | + cron = getEveryHour(daylightSaving2020InLocalTimezone).instance(); |
| 127 | + |
| 128 | + nextRun = nextRun(cron, daylightSaving2020InLocalTimezone); // first run |
| 129 | + Assert.assertEquals(0, nextRun.getMinute()); |
| 130 | + Assert.assertEquals(2, nextRun.getHour()); |
| 131 | + Assert.assertEquals(ZoneOffset.ofHours(2), nextRun.getOffset()); |
| 132 | + nextRun = nextRun(cron, nextRun); // second |
| 133 | + Assert.assertEquals(0, nextRun.getMinute()); |
| 134 | + Assert.assertEquals(2, nextRun.getHour()); |
| 135 | + Assert.assertEquals(ZoneOffset.ofHours(1), nextRun.getOffset()); |
| 136 | + nextRun = nextRun(cron, nextRun); // second |
| 137 | + Assert.assertEquals(0, nextRun.getMinute()); |
| 138 | + Assert.assertEquals(3, nextRun.getHour()); |
| 139 | + Assert.assertEquals(ZoneOffset.ofHours(1), nextRun.getOffset()); |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + public static CronBuilder getEveryHour(ZonedDateTime now) { |
| 145 | + return CronBuilder.cron(definition) |
| 146 | + .withMinute(on(now.getMinute())) |
| 147 | + .withHour(every(on(now.getHour()),1)) |
| 148 | + .withDoW(questionMark()) |
| 149 | + .withDoM(on(now.getDayOfMonth())) |
| 150 | + .withDoY(questionMark()) |
| 151 | + .withMonth(on(now.getMonthValue())); |
| 152 | + } |
| 153 | + |
| 154 | + public static CronBuilder getEvery30Minute(ZonedDateTime now) { |
| 155 | + return CronBuilder.cron(definition) |
| 156 | + .withMinute(every(on(now.getMinute()),30)) |
| 157 | + .withHour(always()) |
| 158 | + .withDoW(questionMark()) |
| 159 | + .withDoM(on(now.getDayOfMonth())) |
| 160 | + .withDoY(questionMark()) |
| 161 | + .withMonth(on(now.getMonthValue())); |
| 162 | + } |
| 163 | + |
| 164 | + |
62 | 165 | private static ZonedDateTime nextRun(Cron cron, ZonedDateTime when) { |
63 | 166 | final Optional<ZonedDateTime> next = ExecutionTime.forCron(cron).nextExecution(when); |
64 | 167 | if (!next.isPresent()) { |
|
0 commit comments