8073394: Clock.systemUTC() should return a constant

Clock.systemUTC() now returns SystemClock.UTC

Reviewed-by: scolebourne, rriggs, plevart, lancea
This commit is contained in:
Daniel Fuchs 2015-02-24 21:51:45 +01:00
parent 8af70656ce
commit 2390a77789
2 changed files with 35 additions and 1 deletions

View File

@ -155,7 +155,7 @@ public abstract class Clock {
* @return a clock that uses the best available system clock in the UTC zone, not null
*/
public static Clock systemUTC() {
return new SystemClock(ZoneOffset.UTC);
return SystemClock.UTC;
}
/**
@ -198,6 +198,9 @@ public abstract class Clock {
*/
public static Clock system(ZoneId zone) {
Objects.requireNonNull(zone, "zone");
if (zone == ZoneOffset.UTC) {
return SystemClock.UTC;
}
return new SystemClock(zone);
}
@ -451,6 +454,8 @@ public abstract class Clock {
private static final long serialVersionUID = 6740630888130243051L;
private static final long OFFSET_SEED =
System.currentTimeMillis()/1000 - 1024; // initial offest
static final SystemClock UTC = new SystemClock(ZoneOffset.UTC);
private final ZoneId zone;
// We don't actually need a volatile here.
// We don't care if offset is set or read concurrently by multiple

View File

@ -66,8 +66,10 @@ import java.lang.reflect.Field;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
/**
* Test system clock.
@ -76,6 +78,7 @@ import org.testng.annotations.Test;
public class TestClock_System {
private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
private static final Clock systemUTC = Clock.systemUTC();
public void test_withZone_same() {
Clock test = Clock.system(PARIS);
@ -89,6 +92,32 @@ public class TestClock_System {
assertEquals(test.toString(), "SystemClock[Europe/Paris]");
}
//-----------------------------------------------------------------------
@DataProvider(name="sampleSystemUTC")
Object[][] provider_sampleSystemUTC() {
return new Object[][] {
{"Clock.systemUTC()#1", Clock.systemUTC()},
{"Clock.systemUTC()#2", Clock.systemUTC()},
{"Clock.system(ZoneOffset.UTC)#1", Clock.system(ZoneOffset.UTC)},
{"Clock.system(ZoneOffset.UTC)#2", Clock.system(ZoneOffset.UTC)}
};
}
// Test for 8073394
@Test(dataProvider="sampleSystemUTC")
public void test_systemUTC(String s, Clock clock) {
if (clock != systemUTC) {
throw new RuntimeException("Unexpected clock instance for " + s + ": "
+ "\n\texpected: " + toString(systemUTC)
+ "\n\tactual: " + toString(clock));
}
}
private static String toString(Clock c) {
return c == null ? null :
c + " " + c.getClass().getName() + "@" + System.identityHashCode(c);
}
//-----------------------------------------------------------------------
private static String formatTime(String prefix, Instant time) {