Switch to Java Date/Time API in ScheduleConfig
Change-Id: I2db9b803c3e9e607ad65dda2199d3cbf9af46fe4
This commit is contained in:
@@ -16,16 +16,16 @@ package com.google.gerrit.server.config;
|
|||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
import java.time.DayOfWeek;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import org.eclipse.jgit.lib.Config;
|
import org.eclipse.jgit.lib.Config;
|
||||||
import org.joda.time.DateTime;
|
|
||||||
import org.joda.time.LocalDateTime;
|
|
||||||
import org.joda.time.LocalTime;
|
|
||||||
import org.joda.time.MutableDateTime;
|
|
||||||
import org.joda.time.format.DateTimeFormat;
|
|
||||||
import org.joda.time.format.DateTimeFormatter;
|
|
||||||
import org.joda.time.format.ISODateTimeFormat;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -49,16 +49,16 @@ public class ScheduleConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleConfig(Config rc, String section, String subsection) {
|
public ScheduleConfig(Config rc, String section, String subsection) {
|
||||||
this(rc, section, subsection, DateTime.now());
|
this(rc, section, subsection, ZonedDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleConfig(
|
public ScheduleConfig(
|
||||||
Config rc, String section, String subsection, String keyInterval, String keyStartTime) {
|
Config rc, String section, String subsection, String keyInterval, String keyStartTime) {
|
||||||
this(rc, section, subsection, keyInterval, keyStartTime, DateTime.now());
|
this(rc, section, subsection, keyInterval, keyStartTime, ZonedDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
ScheduleConfig(Config rc, String section, String subsection, DateTime now) {
|
ScheduleConfig(Config rc, String section, String subsection, ZonedDateTime now) {
|
||||||
this(rc, section, subsection, KEY_INTERVAL, KEY_STARTTIME, now);
|
this(rc, section, subsection, KEY_INTERVAL, KEY_STARTTIME, now);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ public class ScheduleConfig {
|
|||||||
String subsection,
|
String subsection,
|
||||||
String keyInterval,
|
String keyInterval,
|
||||||
String keyStartTime,
|
String keyStartTime,
|
||||||
DateTime now) {
|
ZonedDateTime now) {
|
||||||
this.rc = rc;
|
this.rc = rc;
|
||||||
this.section = section;
|
this.section = section;
|
||||||
this.subsection = subsection;
|
this.subsection = subsection;
|
||||||
@@ -122,31 +122,24 @@ public class ScheduleConfig {
|
|||||||
String section,
|
String section,
|
||||||
String subsection,
|
String subsection,
|
||||||
String keyStartTime,
|
String keyStartTime,
|
||||||
DateTime now,
|
ZonedDateTime now,
|
||||||
long interval) {
|
long interval) {
|
||||||
long delay = MISSING_CONFIG;
|
long delay = MISSING_CONFIG;
|
||||||
String start = rc.getString(section, subsection, keyStartTime);
|
String start = rc.getString(section, subsection, keyStartTime);
|
||||||
try {
|
try {
|
||||||
if (start != null) {
|
if (start != null) {
|
||||||
DateTimeFormatter formatter;
|
DateTimeFormatter formatter =
|
||||||
MutableDateTime startTime = now.toMutableDateTime();
|
DateTimeFormatter.ofPattern("[E ]HH:mm").withLocale(Locale.US);
|
||||||
|
LocalTime firstStartTime = LocalTime.parse(start, formatter);
|
||||||
|
ZonedDateTime startTime = now.with(firstStartTime);
|
||||||
try {
|
try {
|
||||||
formatter = ISODateTimeFormat.hourMinute();
|
DayOfWeek dayOfWeek = formatter.parse(start, DayOfWeek::from);
|
||||||
LocalTime firstStartTime = formatter.parseLocalTime(start);
|
startTime = startTime.with(dayOfWeek);
|
||||||
startTime.hourOfDay().set(firstStartTime.getHourOfDay());
|
} catch (DateTimeParseException ignored) {
|
||||||
startTime.minuteOfHour().set(firstStartTime.getMinuteOfHour());
|
// Day of week is an optional parameter.
|
||||||
} catch (IllegalArgumentException e1) {
|
|
||||||
formatter = DateTimeFormat.forPattern("E HH:mm").withLocale(Locale.US);
|
|
||||||
LocalDateTime firstStartDateTime = formatter.parseLocalDateTime(start);
|
|
||||||
startTime.dayOfWeek().set(firstStartDateTime.getDayOfWeek());
|
|
||||||
startTime.hourOfDay().set(firstStartDateTime.getHourOfDay());
|
|
||||||
startTime.minuteOfHour().set(firstStartDateTime.getMinuteOfHour());
|
|
||||||
}
|
}
|
||||||
startTime.secondOfMinute().set(0);
|
startTime = startTime.truncatedTo(ChronoUnit.MINUTES);
|
||||||
startTime.millisOfSecond().set(0);
|
delay = Duration.between(now, startTime).toMillis() % interval;
|
||||||
long s = startTime.getMillis();
|
|
||||||
long n = now.getMillis();
|
|
||||||
delay = (s - n) % interval;
|
|
||||||
if (delay <= 0) {
|
if (delay <= 0) {
|
||||||
delay += interval;
|
delay += interval;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,15 +20,19 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|||||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import org.eclipse.jgit.lib.Config;
|
import org.eclipse.jgit.lib.Config;
|
||||||
import org.joda.time.DateTime;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ScheduleConfigTest {
|
public class ScheduleConfigTest {
|
||||||
|
|
||||||
// Friday June 13, 2014 10:00 UTC
|
// Friday June 13, 2014 10:00 UTC
|
||||||
private static final DateTime NOW = DateTime.parse("2014-06-13T10:00:00-00:00");
|
private static final ZonedDateTime NOW =
|
||||||
|
LocalDateTime.of(2014, Month.JUNE, 13, 10, 0, 0).atOffset(ZoneOffset.UTC).toZonedDateTime();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void initialDelay() throws Exception {
|
public void initialDelay() throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user