Skip to content

Commit bc61f74

Browse files
Praful Makanisduskis
authored andcommitted
---
yaml --- r: 27383 b: refs/heads/mrschmidt-collectiongroup c: a671baf h: refs/heads/master i: 27381: d0ac566 27379: c890d3a 27375: b8fc117
1 parent fe60556 commit bc61f74

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ refs/heads/autosynth-securitycenter: f2f2feb8f3949c986d37e55cd23cdb0ce774f287
173173
refs/heads/autosynth-talent: 4ca901879f86aab61091cea52e8a9b653639df24
174174
refs/heads/cscc-samples: 620d105e6b574cfeeee04e413a157b7bd34ebc8b
175175
refs/heads/igorbernstein2-patch-1: f62464ee14df1e44a3b173cdc3976563d1b3078b
176-
refs/heads/mrschmidt-collectiongroup: 04206ac472d8d68dd30c9dd7bbabcc89453b2d72
176+
refs/heads/mrschmidt-collectiongroup: a671baf76464f1e50e15f193e4c4e8a30309c8ee
177177
refs/heads/release-google-cloud-java-v0.83.0: 4b55ec1b81b3886ede61ae868391a3cdf7eed90e
178178
refs/heads/release-google-cloud-java-v0.83.1-SNAPSHOT: 8d6db7ee534d12b1df38d8cf314871df76f87577
179179
refs/heads/v4support: aa837fd70877f5a0628d8036e88952db035b792c

branches/mrschmidt-collectiongroup/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.threeten.bp.LocalDateTime;
2828
import org.threeten.bp.ZoneOffset;
2929
import org.threeten.bp.format.DateTimeFormatter;
30+
import org.threeten.bp.format.DateTimeFormatterBuilder;
31+
import org.threeten.bp.temporal.TemporalAccessor;
3032

3133
/**
3234
* Represents a timestamp with nanosecond precision. Timestamps cover the range [0001-01-01,
@@ -47,6 +49,15 @@ public final class Timestamp implements Comparable<Timestamp>, Serializable {
4749

4850
private static final DateTimeFormatter format = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
4951

52+
private static final DateTimeFormatter timestampParser =
53+
new DateTimeFormatterBuilder()
54+
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
55+
.optionalStart()
56+
.appendOffsetId()
57+
.optionalEnd()
58+
.toFormatter()
59+
.withZone(ZoneOffset.UTC);
60+
5061
private final long seconds;
5162
private final int nanos;
5263

@@ -170,7 +181,8 @@ public com.google.protobuf.Timestamp toProto() {
170181
* the timezone offset (always ends in "Z").
171182
*/
172183
public static Timestamp parseTimestamp(String timestamp) {
173-
Instant instant = Instant.parse(timestamp);
184+
TemporalAccessor temporalAccessor = timestampParser.parse(timestamp);
185+
Instant instant = Instant.from(temporalAccessor);
174186
return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano());
175187
}
176188

branches/mrschmidt-collectiongroup/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ public void parseTimestamp() {
185185
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0));
186186
}
187187

188+
@Test
189+
public void parseTimestampWithoutTimeZoneOffset() {
190+
assertThat(Timestamp.parseTimestamp("0001-01-01T00:00:00")).isEqualTo(Timestamp.MIN_VALUE);
191+
assertThat(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999"))
192+
.isEqualTo(Timestamp.MAX_VALUE);
193+
assertThat(Timestamp.parseTimestamp("2015-10-12T15:14:54"))
194+
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0));
195+
}
196+
188197
@Test
189198
public void fromProto() {
190199
com.google.protobuf.Timestamp proto =

0 commit comments

Comments
 (0)