Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.

Commit 964dd14

Browse files
authored
fix: docs of com.google.cloud.Timestamp.parseTimestamp (#258)
* docs: update doc of parseTimestamp * docs: update doc of parseTimestamp * docs: update doc and test of parseTimestamp
1 parent 54944c8 commit 964dd14

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

google-cloud-core/src/main/java/com/google/cloud/Timestamp.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.threeten.bp.ZoneOffset;
2929
import org.threeten.bp.format.DateTimeFormatter;
3030
import org.threeten.bp.format.DateTimeFormatterBuilder;
31+
import org.threeten.bp.format.DateTimeParseException;
3132
import org.threeten.bp.temporal.TemporalAccessor;
3233

3334
/**
@@ -189,8 +190,13 @@ public com.google.protobuf.Timestamp toProto() {
189190
}
190191

191192
/**
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
194200
*/
195201
public static Timestamp parseTimestamp(String timestamp) {
196202
TemporalAccessor temporalAccessor = timestampParser.parse(timestamp);

google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ public void parseTimestampWithoutTimeZoneOffset() {
257257
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0));
258258
}
259259

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+
260272
@Test
261273
public void fromProto() {
262274
com.google.protobuf.Timestamp proto =

0 commit comments

Comments
 (0)