ScheduleConfig: compute delay without using loops
Also make the computation of the delay for the case "start < now" consistent with the case "now < start" i.e. make sure that the computed delay is never zero. Change-Id: I807637fdaf209f8a3b7e7a04d3504809ee591a07
This commit is contained in:
@@ -91,30 +91,28 @@ public class ScheduleConfig {
|
|||||||
String start = rc.getString(section, subsection, KEY_STARTTIME);
|
String start = rc.getString(section, subsection, KEY_STARTTIME);
|
||||||
try {
|
try {
|
||||||
if (start != null) {
|
if (start != null) {
|
||||||
DateTimeFormatter formatter = ISODateTimeFormat.hourMinute();
|
DateTimeFormatter formatter;
|
||||||
MutableDateTime startTime = now.toMutableDateTime();
|
MutableDateTime startTime = now.toMutableDateTime();
|
||||||
LocalTime firstStartTime = null;
|
|
||||||
LocalDateTime firstStartDateTime = null;
|
|
||||||
try {
|
try {
|
||||||
firstStartTime = formatter.parseLocalTime(start);
|
formatter = ISODateTimeFormat.hourMinute();
|
||||||
|
LocalTime firstStartTime = formatter.parseLocalTime(start);
|
||||||
startTime.hourOfDay().set(firstStartTime.getHourOfDay());
|
startTime.hourOfDay().set(firstStartTime.getHourOfDay());
|
||||||
startTime.minuteOfHour().set(firstStartTime.getMinuteOfHour());
|
startTime.minuteOfHour().set(firstStartTime.getMinuteOfHour());
|
||||||
} catch (IllegalArgumentException e1) {
|
} catch (IllegalArgumentException e1) {
|
||||||
formatter = DateTimeFormat.forPattern("E HH:mm");
|
formatter = DateTimeFormat.forPattern("E HH:mm");
|
||||||
firstStartDateTime = formatter.parseLocalDateTime(start);
|
LocalDateTime firstStartDateTime = formatter.parseLocalDateTime(start);
|
||||||
startTime.dayOfWeek().set(firstStartDateTime.getDayOfWeek());
|
startTime.dayOfWeek().set(firstStartDateTime.getDayOfWeek());
|
||||||
startTime.hourOfDay().set(firstStartDateTime.getHourOfDay());
|
startTime.hourOfDay().set(firstStartDateTime.getHourOfDay());
|
||||||
startTime.minuteOfHour().set(firstStartDateTime.getMinuteOfHour());
|
startTime.minuteOfHour().set(firstStartDateTime.getMinuteOfHour());
|
||||||
}
|
}
|
||||||
startTime.secondOfMinute().set(0);
|
startTime.secondOfMinute().set(0);
|
||||||
startTime.millisOfSecond().set(0);
|
startTime.millisOfSecond().set(0);
|
||||||
while (startTime.isBefore(now)) {
|
long s = startTime.getMillis();
|
||||||
startTime.add(interval);
|
long n = now.getMillis();
|
||||||
|
delay = (s - n) % interval;
|
||||||
|
if (delay <= 0) {
|
||||||
|
delay += interval;
|
||||||
}
|
}
|
||||||
while (startTime.getMillis() - now.getMillis() > interval) {
|
|
||||||
startTime.add(-interval);
|
|
||||||
}
|
|
||||||
delay = startTime.getMillis() - now.getMillis();
|
|
||||||
} else {
|
} else {
|
||||||
log.info(MessageFormat.format(
|
log.info(MessageFormat.format(
|
||||||
"{0} schedule parameter \"{0}.{1}\" is not configured", section,
|
"{0} schedule parameter \"{0}.{1}\" is not configured", section,
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public class ScheduleConfigTest {
|
|||||||
|
|
||||||
assertEquals(ms(1, HOURS), initialDelay("Mon 11:00", "1d"));
|
assertEquals(ms(1, HOURS), initialDelay("Mon 11:00", "1d"));
|
||||||
assertEquals(ms(23, HOURS), initialDelay("Mon 09:00", "1d"));
|
assertEquals(ms(23, HOURS), initialDelay("Mon 09:00", "1d"));
|
||||||
assertEquals(0, initialDelay("Mon 10:00", "1d"));
|
assertEquals(ms(1, DAYS), initialDelay("Mon 10:00", "1d"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long initialDelay(String startTime, String interval)
|
private static long initialDelay(String startTime, String interval)
|
||||||
|
|||||||
Reference in New Issue
Block a user