From 5cfbedf86fa6c7251822e323e66d1ad4fda5cd8b Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 1 Mar 2019 14:55:07 +0530 Subject: [PATCH 1/5] Fix parseTimestamp docs --- .../src/main/java/com/google/cloud/Timestamp.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 02bb98131366..4f62d9af2575 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 @@ -166,8 +166,9 @@ public com.google.protobuf.Timestamp toProto() { } /** - * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without + * Creates a Timestamp instance from the given string. String is in the RFC 3339 format with * the timezone offset (always ends in "Z"). + * {@code 2007-12-03T10:15:30.000Z} */ public static Timestamp parseTimestamp(String timestamp) { Instant instant = Instant.parse(timestamp); From d7835783d137c0410372828a7450a6cb9df3f1f8 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 7 Mar 2019 11:41:50 +0530 Subject: [PATCH 2/5] Fix timestamp without timezone offset --- .../src/main/java/com/google/cloud/Timestamp.java | 10 +++++++--- .../src/test/java/com/google/cloud/TimestampTest.java | 8 ++++---- 2 files changed, 11 insertions(+), 7 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 4f62d9af2575..91cc4146b4e5 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 @@ -26,7 +26,9 @@ import org.threeten.bp.Instant; import org.threeten.bp.LocalDateTime; import org.threeten.bp.ZoneOffset; +import org.threeten.bp.ZonedDateTime; import org.threeten.bp.format.DateTimeFormatter; +import org.threeten.bp.temporal.TemporalAccessor; /** * Represents a timestamp with nanosecond precision. Timestamps cover the range [0001-01-01, @@ -166,12 +168,14 @@ public com.google.protobuf.Timestamp toProto() { } /** - * Creates a Timestamp instance from the given string. String is in the RFC 3339 format with + * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without * the timezone offset (always ends in "Z"). - * {@code 2007-12-03T10:15:30.000Z} */ public static Timestamp parseTimestamp(String timestamp) { - Instant instant = Instant.parse(timestamp); + TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(timestamp); + LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor); + ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneOffset.UTC); + Instant instant = Instant.from(zonedDateTime); return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano()); } 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 8a72abbee4e5..be7384d16f49 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 @@ -178,10 +178,10 @@ public void testToString() { @Test public void parseTimestamp() { - assertThat(Timestamp.parseTimestamp("0001-01-01T00:00:00Z")).isEqualTo(Timestamp.MIN_VALUE); - assertThat(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999Z")) + assertThat(Timestamp.parseTimestamp("0001-01-01T00:00:00")).isEqualTo(Timestamp.MIN_VALUE); + assertThat(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999")) .isEqualTo(Timestamp.MAX_VALUE); - assertThat(Timestamp.parseTimestamp(TEST_TIME_ISO)) + assertThat(Timestamp.parseTimestamp("2015-10-12T15:14:54")) .isEqualTo(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0)); } @@ -217,6 +217,6 @@ public void comparable() { @Test public void serialization() throws Exception { - reserializeAndAssert(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999Z")); + reserializeAndAssert(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999")); } } From a648df7d1f8b410d1723e531cdc85bb9163a3228 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 7 Mar 2019 17:32:04 +0530 Subject: [PATCH 3/5] Fix test cases related to timestamp.parseTimestamp --- .../src/main/java/com/google/cloud/Timestamp.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 91cc4146b4e5..64a08a8313ed 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 @@ -28,6 +28,7 @@ import org.threeten.bp.ZoneOffset; import org.threeten.bp.ZonedDateTime; import org.threeten.bp.format.DateTimeFormatter; +import org.threeten.bp.format.DateTimeFormatterBuilder; import org.threeten.bp.temporal.TemporalAccessor; /** @@ -49,6 +50,14 @@ public final class Timestamp implements Comparable, Serializable { private static final DateTimeFormatter format = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + private static final DateTimeFormatter timestampFormatter = + new DateTimeFormatterBuilder() + .appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + .optionalStart() + .appendOffsetId() + .optionalEnd() + .toFormatter(); + private final long seconds; private final int nanos; @@ -172,7 +181,7 @@ public com.google.protobuf.Timestamp toProto() { * the timezone offset (always ends in "Z"). */ public static Timestamp parseTimestamp(String timestamp) { - TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(timestamp); + TemporalAccessor temporalAccessor = timestampFormatter.parse(timestamp); LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneOffset.UTC); Instant instant = Instant.from(zonedDateTime); From 03f6cadba323afe0ca80ab9885407d9d7a3883ee Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 7 Mar 2019 19:59:50 +0530 Subject: [PATCH 4/5] added test case --- .../src/test/java/com/google/cloud/TimestampTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 be7384d16f49..db9ede84dce1 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 @@ -178,6 +178,15 @@ public void testToString() { @Test public void parseTimestamp() { + assertThat(Timestamp.parseTimestamp("0001-01-01T00:00:00Z")).isEqualTo(Timestamp.MIN_VALUE); + assertThat(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999Z")) + .isEqualTo(Timestamp.MAX_VALUE); + assertThat(Timestamp.parseTimestamp(TEST_TIME_ISO)) + .isEqualTo(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0)); + } + + @Test + public void parseTimestampWithoutTimeZoneOffset() { assertThat(Timestamp.parseTimestamp("0001-01-01T00:00:00")).isEqualTo(Timestamp.MIN_VALUE); assertThat(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999")) .isEqualTo(Timestamp.MAX_VALUE); @@ -217,6 +226,6 @@ public void comparable() { @Test public void serialization() throws Exception { - reserializeAndAssert(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999")); + reserializeAndAssert(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999Z")); } } From 03dc89c531f57b8ae8296020ee25df661e0f7430 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 7 Mar 2019 23:25:08 +0530 Subject: [PATCH 5/5] Fix timestampParser and added ZoneOffset in timestampParser --- .../src/main/java/com/google/cloud/Timestamp.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 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 64a08a8313ed..618ef9c7211e 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 @@ -26,7 +26,6 @@ import org.threeten.bp.Instant; import org.threeten.bp.LocalDateTime; import org.threeten.bp.ZoneOffset; -import org.threeten.bp.ZonedDateTime; import org.threeten.bp.format.DateTimeFormatter; import org.threeten.bp.format.DateTimeFormatterBuilder; import org.threeten.bp.temporal.TemporalAccessor; @@ -50,13 +49,14 @@ public final class Timestamp implements Comparable, Serializable { private static final DateTimeFormatter format = DateTimeFormatter.ISO_LOCAL_DATE_TIME; - private static final DateTimeFormatter timestampFormatter = + private static final DateTimeFormatter timestampParser = new DateTimeFormatterBuilder() .appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME) .optionalStart() .appendOffsetId() .optionalEnd() - .toFormatter(); + .toFormatter() + .withZone(ZoneOffset.UTC); private final long seconds; private final int nanos; @@ -181,10 +181,8 @@ public com.google.protobuf.Timestamp toProto() { * the timezone offset (always ends in "Z"). */ public static Timestamp parseTimestamp(String timestamp) { - TemporalAccessor temporalAccessor = timestampFormatter.parse(timestamp); - LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor); - ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneOffset.UTC); - Instant instant = Instant.from(zonedDateTime); + TemporalAccessor temporalAccessor = timestampParser.parse(timestamp); + Instant instant = Instant.from(temporalAccessor); return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano()); }