bug fixes

This commit is contained in:
Ziver Koc 2017-03-01 17:51:37 +01:00
parent 9b1737acf0
commit bb038dce1f
2 changed files with 32 additions and 17 deletions

View file

@ -158,6 +158,9 @@ public class CronTimer implements Iterator<Long>, Iterable<Long>{
*/
public Long next(long fromTimestamp) {
Calendar cal = getCalendar(fromTimestamp);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
cal.add(Calendar.MINUTE, 1); // skipp current time
while (true) {
int index;
@ -169,6 +172,10 @@ public class CronTimer implements Iterator<Long>, Iterable<Long>{
return -1L; // We have reach the limit no more years left
else
cal.set(Calendar.YEAR, years[Math.abs(index + 1)]);
cal.set(Calendar.MONTH, months[0] - 1);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonths[0]);
cal.set(Calendar.HOUR_OF_DAY, hours[0]);
cal.set(Calendar.MINUTE, minutes[0]);
continue;
}
@ -180,6 +187,9 @@ public class CronTimer implements Iterator<Long>, Iterable<Long>{
cal.add(Calendar.YEAR, 1);
} else
cal.set(Calendar.MONTH, months[Math.abs(index + 1)] - 1);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonths[0]);
cal.set(Calendar.HOUR_OF_DAY, hours[0]);
cal.set(Calendar.MINUTE, minutes[0]);
continue;
}
@ -193,6 +203,8 @@ public class CronTimer implements Iterator<Long>, Iterable<Long>{
cal.add(Calendar.MONTH, 1);
} else
cal.set(Calendar.DAY_OF_MONTH, dayOfMonths[Math.abs(index + 1)]);
cal.set(Calendar.HOUR_OF_DAY, hours[0]);
cal.set(Calendar.MINUTE, minutes[0]);
continue;
}
@ -205,6 +217,8 @@ public class CronTimer implements Iterator<Long>, Iterable<Long>{
cal.add(Calendar.WEEK_OF_YEAR, 1);
} else
cal.set(Calendar.DAY_OF_WEEK, getDayOfWeekEnum(dayOfWeeks[Math.abs(index + 1)]));
cal.set(Calendar.HOUR_OF_DAY, hours[0]);
cal.set(Calendar.MINUTE, minutes[0]);
continue;
}
@ -216,6 +230,7 @@ public class CronTimer implements Iterator<Long>, Iterable<Long>{
cal.add(Calendar.DAY_OF_MONTH, 1);
} else
cal.set(Calendar.HOUR_OF_DAY, hours[Math.abs(index + 1)]);
cal.set(Calendar.MINUTE, minutes[0]);
continue;
}

View file

@ -58,58 +58,58 @@ public class CronTimerTest {
public void specificTime() {
CronTimer cron = new CronTimer("59 23 31 12 3 2003");
assertEquals(-1, (long) cron.next());
assertEquals(1072915140000L, (long) cron.next(1072915140000L));
assertEquals(-1, (long) cron.next(1072915140000L));
assertEquals(1072915140000L, (long) cron.next(978307140000L));
}
@Test
public void minuteWildcard(){
CronTimer cron = new CronTimer("00 * * * * *");
assertEquals(1041379200000L, (long)cron.next(1041379200000L));
CronTimer cron = new CronTimer("0 * * * * *");
assertEquals(1041382800000L, (long)cron.next(1041379200000L));
assertEquals(1041382800000L, (long)cron.next(1041379260000L));
}
@Test
public void hourWildcard(){
CronTimer cron = new CronTimer("* 0 * * * *");
assertEquals(1041379200000L, (long)cron.next(1041379200000L));
assertEquals(1041379260000L, (long)cron.next(1041379260000L)); // minute change
assertEquals(1041382790000L, (long)cron.next(1041382790000L)); // minute border
assertEquals(1041379260000L, (long)cron.next(1041379200000L));
assertEquals(1041379320000L, (long)cron.next(1041379260000L)); // minute change
assertEquals(1041465600000L, (long)cron.next(1041382790000L)); // minute border
assertEquals(1041465600000L, (long)cron.next(1041382800000L)); // hour change
}
@Test
public void dayWildcard(){
CronTimer cron = new CronTimer("* * 1 * * *");
assertEquals(1041379200000L, (long)cron.next(1041379200000L));
assertEquals(1041379260000L, (long)cron.next(1041379260000L)); // minute change
assertEquals(1041379260000L, (long)cron.next(1041379200000L));
assertEquals(1041379320000L, (long)cron.next(1041379260000L)); // minute change
assertEquals(1044057600000L, (long)cron.next(1041465600000L)); // day change
}
@Test
public void monthWildcard(){
CronTimer cron = new CronTimer("* * * 1 * *");
assertEquals(1041379200000L, (long)cron.next(1041379200000L));
assertEquals(1041382800000L, (long)cron.next(1041382800000L)); // hour change
assertEquals(1041469200000L, (long)cron.next(1041469200000L)); // day change
assertEquals(1041379260000L, (long)cron.next(1041379200000L));
assertEquals(1041382860000L, (long)cron.next(1041382800000L)); // hour change
assertEquals(1041469260000L, (long)cron.next(1041469200000L)); // day change
assertEquals(1072915200000L, (long)cron.next(1044057600000L)); // month change
}
@Test
public void weekDayWildcard(){
CronTimer cron = new CronTimer("* * * * 3 *");
assertEquals(1041379200000L, (long)cron.next(1041379200000L));
assertEquals(1041379260000L, (long)cron.next(1041379200000L));
assertEquals(1041984000000L, (long)cron.next(1041465600000L)); // day change
}
@Test
public void yearWildcard(){
CronTimer cron = new CronTimer("* * * * * 2003");
assertEquals(1041379200000L, (long)cron.next(1041379200000L));
assertEquals(1041379260000L, (long)cron.next(1041379260000L)); // min change
assertEquals(1041382860000L, (long)cron.next(1041382860000L)); // hour change
assertEquals(1041469260000L, (long)cron.next(1041469260000L)); // day change
assertEquals(1044147660000L, (long)cron.next(1044147660000L)); // month change
assertEquals(1041379260000L, (long)cron.next(1041379200000L));
assertEquals(1041379320000L, (long)cron.next(1041379260000L)); // min change
assertEquals(1041382980000L, (long)cron.next(1041382920000L)); // hour change
assertEquals(1041469440000L, (long)cron.next(1041469380000L)); // day change
assertEquals(1044147900000L, (long)cron.next(1044147840000L)); // month change
assertEquals(-1, (long)cron.next(1075683660000L)); // year change
}