Skip to content

Commit

Permalink
jmrozanec#402 Fix every x years behavior by checking offset in range …
Browse files Browse the repository at this point in the history
…and correcting previous value calculation in EveryFieldValueGenerator
  • Loading branch information
IndeedSi committed Oct 13, 2020
1 parent 38cabb4 commit 5c7d5ac
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
Expand Up @@ -86,8 +86,14 @@ private int getNext(int reference, Every every) {
@Override
public int generatePreviousValue(final int reference) throws NoSuchValueException {
final Every every = (Every) cronField.getExpression();
if (reference < from) {
throw new NoSuchValueException();
}
if (reference > to) {
return to;
}
final int period = every.getPeriod().getValue();
final int remainder = reference % period;
final int remainder = (reference - from) % period;
if (remainder == 0) {
return reference - period;
} else {
Expand All @@ -100,7 +106,7 @@ protected List<Integer> generateCandidatesNotIncludingIntervalExtremes(final int
final List<Integer> values = new ArrayList<>();
try {
final int offset = offset();
if (start != offset) {
if (start < offset && offset < end) {
values.add(offset);
}
int reference = generateNextValue(start);
Expand Down
46 changes: 0 additions & 46 deletions src/test/java/com/cronutils/Issue402Test.java

This file was deleted.

Expand Up @@ -44,6 +44,7 @@
import static com.cronutils.model.CronType.QUARTZ;
import static java.time.ZoneOffset.UTC;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -842,6 +843,49 @@ public void testLastExecutionIssue312() {
}
}

/**
* Issue #402
* https://github.com/jmrozanec/cron-utils/issues/402
* Fix last and next execution time when using every X years
*/
@Test
public void testLastExecutionIssue402() {
// Every 2 years at March 1st midnight
ExecutionTime execution = ExecutionTime.forCron(parser.parse("0 0 0 1 3 ? 2001-2020/2"));

ZonedDateTime currentDateTime = ZonedDateTime.of(LocalDate.of(2015, 1, 15), LocalTime.MIDNIGHT, ZoneOffset.UTC);
// Check next execution time is correct
Optional<ZonedDateTime> nextExecution = execution.nextExecution(currentDateTime);
assertTrue(nextExecution.isPresent());
assertEquals(ZonedDateTime.of(LocalDate.of(2015, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), nextExecution.get());
// Check previous execution time is correct
Optional<ZonedDateTime> lastExecution = execution.lastExecution(currentDateTime);
assertTrue(lastExecution.isPresent());
assertEquals(ZonedDateTime.of(LocalDate.of(2013, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), lastExecution.get());

lastExecution = execution.lastExecution(nextExecution.get());
assertTrue(lastExecution.isPresent());
assertEquals(ZonedDateTime.of(LocalDate.of(2013, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), lastExecution.get());

// Assume the current time is before the start time of every expression, check last and next execution.ß
ZonedDateTime timeBeforeStart = ZonedDateTime.of(LocalDate.of(1996, 1, 15), LocalTime.MIDNIGHT, ZoneOffset.UTC);
Optional<ZonedDateTime> nextBeforeStart = execution.nextExecution(timeBeforeStart);
assertTrue(nextBeforeStart.isPresent());
assertEquals(ZonedDateTime.of(LocalDate.of(2001, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), nextBeforeStart.get());

Optional<ZonedDateTime> lastBeforeStart = execution.lastExecution(nextBeforeStart.get());
assertFalse(lastBeforeStart.isPresent());

// Assume the current time is before the start time of every expression, check last and next execution.ß
ZonedDateTime timeAfterEnd = ZonedDateTime.of(LocalDate.of(2025, 1, 15), LocalTime.MIDNIGHT, ZoneOffset.UTC);
Optional<ZonedDateTime> nextAfterEnd = execution.nextExecution(timeAfterEnd);
assertFalse(nextAfterEnd.isPresent());

Optional<ZonedDateTime> lastAfterEnd = execution.lastExecution(timeAfterEnd);
assertTrue(lastAfterEnd.isPresent());
assertEquals(ZonedDateTime.of(LocalDate.of(2019, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), lastAfterEnd.get());
}

/**
* Issue #424
* https://github.com/jmrozanec/cron-utils/issues/424
Expand Down

0 comments on commit 5c7d5ac

Please sign in to comment.