From 31f7efd560d72c0965e3fa4463cf3ceec95cc3cc Mon Sep 17 00:00:00 2001 From: Nadeesh TV Date: Tue, 17 Nov 2015 11:06:46 -0500 Subject: [PATCH] 8071919: Add java.time.Clock.tickMillis(ZoneId zone) method Reviewed-by: dfuchs, rriggs, scolebourne --- .../share/classes/java/time/Clock.java | 32 ++++++++++++++++--- .../share/classes/java/time/LocalTime.java | 6 +++- .../time/tck/java/time/TCKClock_Tick.java | 13 +++++++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/jdk/src/java.base/share/classes/java/time/Clock.java b/jdk/src/java.base/share/classes/java/time/Clock.java index 6402806070d..0798b563eaa 100644 --- a/jdk/src/java.base/share/classes/java/time/Clock.java +++ b/jdk/src/java.base/share/classes/java/time/Clock.java @@ -65,7 +65,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import static java.time.LocalTime.NANOS_PER_MINUTE; import static java.time.LocalTime.NANOS_PER_SECOND; - +import static java.time.LocalTime.NANOS_PER_MILLI; import java.io.Serializable; import java.util.Objects; import java.util.TimeZone; @@ -182,7 +182,7 @@ public abstract class Clock { } /** - * Obtains a clock that returns the current instant using best available + * Obtains a clock that returns the current instant using the best available * system clock. *

* This clock is based on the best available system clock. @@ -204,10 +204,34 @@ public abstract class Clock { return new SystemClock(zone); } + //------------------------------------------------------------------------- + /** + * Obtains a clock that returns the current instant ticking in whole milliseconds + * using the best available system clock. + *

+ * This clock will always have the nano-of-second field truncated to milliseconds. + * This ensures that the visible time ticks in whole milliseconds. + * The underlying clock is the best available system clock, equivalent to + * using {@link #system(ZoneId)}. + *

+ * Implementations may use a caching strategy for performance reasons. + * As such, it is possible that the start of the millisecond observed via this + * clock will be later than that observed directly via the underlying clock. + *

+ * The returned implementation is immutable, thread-safe and {@code Serializable}. + * It is equivalent to {@code tick(system(zone), Duration.ofMillis(1))}. + * + * @param zone the time-zone to use to convert the instant to date-time, not null + * @return a clock that ticks in whole milliseconds using the specified zone, not null + */ + public static Clock tickMillis(ZoneId zone) { + return new TickClock(system(zone), NANOS_PER_MILLI); + } + //------------------------------------------------------------------------- /** * Obtains a clock that returns the current instant ticking in whole seconds - * using best available system clock. + * using the best available system clock. *

* This clock will always have the nano-of-second field set to zero. * This ensures that the visible time ticks in whole seconds. @@ -230,7 +254,7 @@ public abstract class Clock { /** * Obtains a clock that returns the current instant ticking in whole minutes - * using best available system clock. + * using the best available system clock. *

* This clock will always have the nano-of-second and second-of-minute fields set to zero. * This ensures that the visible time ticks in whole minutes. diff --git a/jdk/src/java.base/share/classes/java/time/LocalTime.java b/jdk/src/java.base/share/classes/java/time/LocalTime.java index fde0da77c98..85cd7a1bf81 100644 --- a/jdk/src/java.base/share/classes/java/time/LocalTime.java +++ b/jdk/src/java.base/share/classes/java/time/LocalTime.java @@ -189,10 +189,14 @@ public final class LocalTime * Microseconds per day. */ static final long MICROS_PER_DAY = SECONDS_PER_DAY * 1000_000L; + /** + * Nanos per millisecond. + */ + static final long NANOS_PER_MILLI = 1000_000L; /** * Nanos per second. */ - static final long NANOS_PER_SECOND = 1000_000_000L; + static final long NANOS_PER_SECOND = 1000_000_000L; /** * Nanos per minute. */ diff --git a/jdk/test/java/time/tck/java/time/TCKClock_Tick.java b/jdk/test/java/time/tck/java/time/TCKClock_Tick.java index c2212373cd8..08b0a524828 100644 --- a/jdk/test/java/time/tck/java/time/TCKClock_Tick.java +++ b/jdk/test/java/time/tck/java/time/TCKClock_Tick.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -159,6 +159,17 @@ public class TCKClock_Tick extends AbstractTCKTest { Clock.tick(Clock.systemUTC(), null); } + //----------------------------------------------------------------------- + public void test_tickMillis_ZoneId() throws Exception { + Clock test = Clock.tickMillis(PARIS); + assertEquals(test.getZone(), PARIS); + assertEquals(test.instant().getNano() % 1000_000, 0); + } + + @Test(expectedExceptions = NullPointerException.class) + public void test_tickMillis_ZoneId_nullZoneId() { + Clock.tickMillis(null); + } //----------------------------------------------------------------------- public void test_tickSeconds_ZoneId() throws Exception { Clock test = Clock.tickSeconds(PARIS);