From 4cdaf68bc8da8297b9089e2237df093e745c553c Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Fri, 25 May 2018 15:26:51 -0700 Subject: [PATCH 1/2] Adding Timestamp.toDate(). This will be used by consumers of the Firestore API. --- .../main/java/com/google/cloud/Timestamp.java | 12 ++++++++++ .../java/com/google/cloud/TimestampTest.java | 23 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java index 456877bd2740..e2f492107a27 100644 --- a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java +++ b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java @@ -95,6 +95,18 @@ public static Timestamp of(Date date) { return ofTimeMicroseconds(TimeUnit.MILLISECONDS.toMicros(date.getTime())); } + /** + * Returns a new {@code java.util.Date} corresponding to this {@code timestamp}. Any + * sub-millisecond precision will be stripped. + * + * @return An approximate {@code java.util.Date} representation of this {@code timestamp}. + */ + public Date toDate() { + long secondsInMilliseconds = TimeUnit.SECONDS.toMillis(this.seconds); + long nanosInMilliseconds = TimeUnit.NANOSECONDS.toMillis(this.nanos); + return new Date(secondsInMilliseconds + nanosInMilliseconds); + } + /** Creates an instance with current time. */ public static Timestamp now() { java.sql.Timestamp date = new java.sql.Timestamp(System.currentTimeMillis()); diff --git a/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java b/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java index 2aa07999c823..51d2ecb20f18 100644 --- a/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java +++ b/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java @@ -21,8 +21,10 @@ import com.google.common.testing.EqualsTester; import java.util.Calendar; +import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; +import java.util.concurrent.TimeUnit; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -35,6 +37,9 @@ public class TimestampTest { private static final String TEST_TIME_ISO = "2015-10-12T15:14:54Z"; private static final long TEST_TIME_SECONDS = 1444662894L; private static final long TEST_TIME_MICROSECONDS = 10000100L; + private static final long TEST_TIME_MILLISECONDS = + TimeUnit.SECONDS.toMillis(1444662894L) + TimeUnit.MICROSECONDS.toMillis(1234); + private static final Date TEST_DATE = new Date(TEST_TIME_MILLISECONDS); @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -64,6 +69,24 @@ public void ofMicroseconds() { assertThat(timestamp.getNanos()).isEqualTo(TEST_TIME_MICROSECONDS % 1000000L * 1000); } + @Test + public void ofDate() { + Timestamp timestamp = Timestamp.of(TEST_DATE); + Long expectedSeconds = TimeUnit.MILLISECONDS.toSeconds(TEST_TIME_MILLISECONDS); + Long expectedNanos = + TimeUnit.MILLISECONDS.toNanos(TEST_TIME_MILLISECONDS) + - TimeUnit.SECONDS.toNanos(expectedSeconds); + assertThat(timestamp.getSeconds()).isEqualTo(expectedSeconds); + assertThat(timestamp.getNanos()).isEqualTo(expectedNanos); + } + + @Test + public void toDate() { + Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 1234 * 1000); + Date date = timestamp.toDate(); + assertThat(TEST_TIME_MILLISECONDS).isEqualTo(date.getTime()); + } + @Test public void toFromSqlTimestamp() { long seconds = TEST_TIME_SECONDS; From 19c1eca95467e588c21378cae2b136d14973d532 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Fri, 25 May 2018 15:39:33 -0700 Subject: [PATCH 2/2] Moving `toDate()`. --- .../main/java/com/google/cloud/Timestamp.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java index e2f492107a27..0c2eca786367 100644 --- a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java +++ b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java @@ -95,18 +95,6 @@ public static Timestamp of(Date date) { return ofTimeMicroseconds(TimeUnit.MILLISECONDS.toMicros(date.getTime())); } - /** - * Returns a new {@code java.util.Date} corresponding to this {@code timestamp}. Any - * sub-millisecond precision will be stripped. - * - * @return An approximate {@code java.util.Date} representation of this {@code timestamp}. - */ - public Date toDate() { - long secondsInMilliseconds = TimeUnit.SECONDS.toMillis(this.seconds); - long nanosInMilliseconds = TimeUnit.NANOSECONDS.toMillis(this.nanos); - return new Date(secondsInMilliseconds + nanosInMilliseconds); - } - /** Creates an instance with current time. */ public static Timestamp now() { java.sql.Timestamp date = new java.sql.Timestamp(System.currentTimeMillis()); @@ -142,6 +130,18 @@ public java.sql.Timestamp toSqlTimestamp() { return ts; } + /** + * Returns a new {@code java.util.Date} corresponding to this {@code timestamp}. Any + * sub-millisecond precision will be stripped. + * + * @return An approximate {@code java.util.Date} representation of this {@code timestamp}. + */ + public Date toDate() { + long secondsInMilliseconds = TimeUnit.SECONDS.toMillis(this.seconds); + long nanosInMilliseconds = TimeUnit.NANOSECONDS.toMillis(this.nanos); + return new Date(secondsInMilliseconds + nanosInMilliseconds); + } + @Override public int compareTo(Timestamp other) { int r = Long.compare(seconds, other.seconds);