Switch to Java Date/Time API for TimeUtil
Instead of adding a mechanism to exchange the provider of the current time in millis, we should rather use Instant.now(Clock).toEpochMilli() in combination with Clock#fixed for tests. However, this would require adjustments which are beyond the scope of this change. Change-Id: I91e2de5d6cbda5235f54fc7ca8851534d8c85aa0
This commit is contained in:
@@ -15,14 +15,21 @@
|
||||
package com.google.gerrit.common;
|
||||
|
||||
import com.google.common.annotations.GwtIncompatible;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import java.sql.Timestamp;
|
||||
import org.joda.time.DateTimeUtils;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
/** Static utility methods for dealing with dates and times. */
|
||||
@GwtIncompatible("Unemulated org.joda.time.DateTimeUtils")
|
||||
@GwtIncompatible("Unemulated Java 8 functionalities")
|
||||
public class TimeUtil {
|
||||
private static final LongSupplier SYSTEM_CURRENT_MILLIS_SUPPLIER = System::currentTimeMillis;
|
||||
|
||||
private static volatile LongSupplier currentMillisSupplier = SYSTEM_CURRENT_MILLIS_SUPPLIER;
|
||||
|
||||
public static long nowMs() {
|
||||
return DateTimeUtils.currentTimeMillis();
|
||||
// We should rather use Instant.now(Clock).toEpochMilli() instead but this would require some
|
||||
// changes in our testing code as we wouldn't have clock steps anymore.
|
||||
return currentMillisSupplier.getAsLong();
|
||||
}
|
||||
|
||||
public static Timestamp nowTs() {
|
||||
@@ -33,5 +40,15 @@ public class TimeUtil {
|
||||
return new Timestamp((t.getTime() / 1000) * 1000);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static void setCurrentMillisSupplier(LongSupplier customCurrentMillisSupplier) {
|
||||
currentMillisSupplier = customCurrentMillisSupplier;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static void resetCurrentMillisSupplier() {
|
||||
currentMillisSupplier = SYSTEM_CURRENT_MILLIS_SUPPLIER;
|
||||
}
|
||||
|
||||
private TimeUtil() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user