Skip to content

Commit

Permalink
Issue #71: Feature: validate if date matches given cron.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrozanec committed Mar 5, 2016
1 parent c39dae9 commit 41d7231
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/com/cronutils/model/time/ExecutionTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ public Duration timeFromLastExecution(DateTime date){
return new Interval(lastExecution(date), date).toDuration();
}

/**
* Provide feedback if a given date matches the cron expression.
* @param date - jodatime DateTime instance. If null, a NullPointerException will be raised.
* @return true if date matches cron expression requirements, false otherwise.
*/
public boolean isMatch(DateTime date){
return nextExecution(lastExecution(date)).equals(date);
}

private List<Integer> generateDayCandidatesQuestionMarkNotSupported(int year, int month, WeekDay mondayDoWValue) {
DateTime date = new DateTime(year, month, 1, 1, 1);
Set<Integer> candidates = Sets.newHashSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,30 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class ExecutionTimeUnixIntegrationTest {

@Test
public void testIsMatchForUnix01(){
CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
String crontab = "* * * * *";//m,h,dom,M,dow
Cron cron = parser.parse(crontab);
ExecutionTime executionTime = ExecutionTime.forCron(cron);
DateTime scanTime = DateTime.parse("2016-02-29T11:00:00.000-06:00");
assertTrue(executionTime.isMatch(scanTime));
}

@Test
public void testIsMatchForUnix02(){
CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
String crontab = "0 * * * 1-5";//m,h,dom,M,dow
Cron cron = parser.parse(crontab);
ExecutionTime executionTime = ExecutionTime.forCron(cron);
DateTime scanTime = DateTime.parse("2016-03-04T11:00:00.000-06:00");
assertTrue(executionTime.isMatch(scanTime));
}

/**
* Issue #37: for pattern "every 10 minutes", nextExecution returns a date from past.
*/
Expand Down

0 comments on commit 41d7231

Please sign in to comment.