Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions google-cloud-core/src/main/java/com/google/cloud/Timestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.threeten.bp.ZoneOffset;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeFormatterBuilder;
import org.threeten.bp.format.DateTimeParseException;
import org.threeten.bp.temporal.TemporalAccessor;

/**
Expand Down Expand Up @@ -189,8 +190,13 @@ public com.google.protobuf.Timestamp toProto() {
}

/**
* Creates a Timestamp instance from the given string. String is in the RFC 3339 format without
* the timezone offset (always ends in "Z").
* Creates a Timestamp instance from the given string. Input string should be in the RFC 3339
* format, like '2020-12-01T10:15:30.000Z' or with the timezone offset, such as
* '2020-12-01T10:15:30+01:00'.
*
* @param timestamp string in the RFC 3339 format
* @return created Timestamp
* @throws DateTimeParseException if unable to parse
*/
public static Timestamp parseTimestamp(String timestamp) {
TemporalAccessor temporalAccessor = timestampParser.parse(timestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ public void parseTimestampWithoutTimeZoneOffset() {
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0));
}

@Test
public void parseTimestampWithTimeZoneOffset() {
assertThat(Timestamp.parseTimestamp("0001-01-01T00:00:00-00:00"))
.isEqualTo(Timestamp.MIN_VALUE);
assertThat(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999-00:00"))
.isEqualTo(Timestamp.MAX_VALUE);
assertThat(Timestamp.parseTimestamp("2020-12-06T19:21:12.123+05:30"))
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(1607262672, 123000000));
assertThat(Timestamp.parseTimestamp("2020-07-10T14:03:00-07:00"))
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(1594414980, 0));
}

@Test
public void fromProto() {
com.google.protobuf.Timestamp proto =
Expand Down