Skip to content

Commit 82abb19

Browse files
Praful Makanisduskis
authored andcommitted
---
yaml --- r: 23965 b: refs/heads/autosynth-kms c: a671baf h: refs/heads/master i: 23963: 95da8e6
1 parent 64ca507 commit 82abb19

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
@@ -132,7 +132,7 @@ refs/heads/autosynth-dialogflow: cb1ceeb3ec1a132d3096fc88d44b930a52130e18
132132
refs/heads/autosynth-errorreporting: 3f176c20b55dfaaa8fc32f28d82b31784b93e636
133133
refs/heads/autosynth-firestore: d48d82caace227856b6cd85ac30ee474528733ea
134134
refs/heads/autosynth-iot: 4025d1804241e74d54950a324dc4f667aeaad4b3
135-
refs/heads/autosynth-kms: 04206ac472d8d68dd30c9dd7bbabcc89453b2d72
135+
refs/heads/autosynth-kms: a671baf76464f1e50e15f193e4c4e8a30309c8ee
136136
refs/heads/autosynth-language: c3d990dd34d81e7e935041e7147fb9dd27f8a557
137137
refs/heads/autosynth-os-login: 092fdbed6d5317948f92b708e9f50dedd89fc666
138138
refs/heads/autosynth-redis: 9e1fe503973c7b4a9ba26afb1fcddc2a57ba795a

branches/autosynth-kms/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/autosynth-kms/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)