Skip to content

Commit 2bd6519

Browse files
author
Elena Sacchi
committed
Additional failing test for issue #446
1 parent b88e7ee commit 2bd6519

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

src/test/java/com/cronutils/Issue446Test.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import com.cronutils.model.definition.CronDefinitionBuilder;
88
import com.cronutils.model.time.ExecutionTime;
99
import org.junit.Assert;
10+
import org.junit.Ignore;
1011
import org.junit.Test;
1112

1213
import java.time.*;
1314
import java.util.Optional;
1415

16+
import static com.cronutils.model.field.expression.FieldExpression.always;
1517
import static com.cronutils.model.field.expression.FieldExpression.questionMark;
1618
import static com.cronutils.model.field.expression.FieldExpressionFactory.every;
1719
import static com.cronutils.model.field.expression.FieldExpressionFactory.on;
@@ -59,6 +61,107 @@ public static CronBuilder getEveryMonthFromNow(ZonedDateTime now, int every) {
5961
.withMonth(every(on(now.getMonthValue()), every));
6062
}
6163

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+
62165
private static ZonedDateTime nextRun(Cron cron, ZonedDateTime when) {
63166
final Optional<ZonedDateTime> next = ExecutionTime.forCron(cron).nextExecution(when);
64167
if (!next.isPresent()) {

0 commit comments

Comments
 (0)