Skip to content

Commit 0fc5b16

Browse files
Praful Makanisduskis
authored andcommitted
---
yaml --- r: 23083 b: refs/heads/autosynth-containeranalysis c: a671baf h: refs/heads/master i: 23081: b8a031c 23079: 4ee94c4
1 parent de29019 commit 0fc5b16

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
@@ -126,7 +126,7 @@ refs/heads/autosynth-bigquerydatatransfer: d88aa5aae5fd9d3c6d75bbab1a05162c6d4d9
126126
refs/heads/autosynth-bigquerystorage: d2c53da3b012e38c662e4df0738042435f19365f
127127
refs/heads/autosynth-bigtable: 9e5429f45cf9face9fed585d0233534993e36b58
128128
refs/heads/autosynth-bigtable-admin: 6379a2bc712f2736c83de0e009b4d26da4fa82ca
129-
refs/heads/autosynth-containeranalysis: 04206ac472d8d68dd30c9dd7bbabcc89453b2d72
129+
refs/heads/autosynth-containeranalysis: a671baf76464f1e50e15f193e4c4e8a30309c8ee
130130
refs/heads/autosynth-datastore: 9acd400b484d6691a080c9152a331d88d24fefc1
131131
refs/heads/autosynth-dialogflow: 7dbc2c1ea714328ccfa4f33645045f017ff080e7
132132
refs/heads/autosynth-errorreporting: 1101a04e8be074802c35332d5fcf8297f61cae32

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