This repository was archived by the owner on Dec 3, 2023. It is now read-only.
Commit 964dd14 1 parent 54944c8 commit 964dd14 Copy full SHA for 964dd14
File tree 2 files changed +20
-2
lines changed
main/java/com/google/cloud
test/java/com/google/cloud
2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change 28
28
import org .threeten .bp .ZoneOffset ;
29
29
import org .threeten .bp .format .DateTimeFormatter ;
30
30
import org .threeten .bp .format .DateTimeFormatterBuilder ;
31
+ import org .threeten .bp .format .DateTimeParseException ;
31
32
import org .threeten .bp .temporal .TemporalAccessor ;
32
33
33
34
/**
@@ -189,8 +190,13 @@ public com.google.protobuf.Timestamp toProto() {
189
190
}
190
191
191
192
/**
192
- * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without
193
- * the timezone offset (always ends in "Z").
193
+ * Creates a Timestamp instance from the given string. Input string should be in the RFC 3339
194
+ * format, like '2020-12-01T10:15:30.000Z' or with the timezone offset, such as
195
+ * '2020-12-01T10:15:30+01:00'.
196
+ *
197
+ * @param timestamp string in the RFC 3339 format
198
+ * @return created Timestamp
199
+ * @throws DateTimeParseException if unable to parse
194
200
*/
195
201
public static Timestamp parseTimestamp (String timestamp ) {
196
202
TemporalAccessor temporalAccessor = timestampParser .parse (timestamp );
Original file line number Diff line number Diff line change @@ -257,6 +257,18 @@ public void parseTimestampWithoutTimeZoneOffset() {
257
257
.isEqualTo (Timestamp .ofTimeSecondsAndNanos (TEST_TIME_SECONDS , 0 ));
258
258
}
259
259
260
+ @ Test
261
+ public void parseTimestampWithTimeZoneOffset () {
262
+ assertThat (Timestamp .parseTimestamp ("0001-01-01T00:00:00-00:00" ))
263
+ .isEqualTo (Timestamp .MIN_VALUE );
264
+ assertThat (Timestamp .parseTimestamp ("9999-12-31T23:59:59.999999999-00:00" ))
265
+ .isEqualTo (Timestamp .MAX_VALUE );
266
+ assertThat (Timestamp .parseTimestamp ("2020-12-06T19:21:12.123+05:30" ))
267
+ .isEqualTo (Timestamp .ofTimeSecondsAndNanos (1607262672 , 123000000 ));
268
+ assertThat (Timestamp .parseTimestamp ("2020-07-10T14:03:00-07:00" ))
269
+ .isEqualTo (Timestamp .ofTimeSecondsAndNanos (1594414980 , 0 ));
270
+ }
271
+
260
272
@ Test
261
273
public void fromProto () {
262
274
com .google .protobuf .Timestamp proto =
You can’t perform that action at this time.
0 commit comments