Skip to content

Commit

Permalink
Fix timestampParser and added ZoneOffset in timestampParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Praful Makani committed Mar 7, 2019
1 parent 03f6cad commit 03dc89c
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.threeten.bp.Instant;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.ZoneOffset;
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeFormatterBuilder;
import org.threeten.bp.temporal.TemporalAccessor;
Expand All @@ -50,13 +49,14 @@ public final class Timestamp implements Comparable<Timestamp>, Serializable {

private static final DateTimeFormatter format = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

private static final DateTimeFormatter timestampFormatter =
private static final DateTimeFormatter timestampParser =
new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.appendOffsetId()
.optionalEnd()
.toFormatter();
.toFormatter()
.withZone(ZoneOffset.UTC);

private final long seconds;
private final int nanos;
Expand Down Expand Up @@ -181,10 +181,8 @@ public com.google.protobuf.Timestamp toProto() {
* the timezone offset (always ends in "Z").
*/
public static Timestamp parseTimestamp(String timestamp) {
TemporalAccessor temporalAccessor = timestampFormatter.parse(timestamp);
LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneOffset.UTC);
Instant instant = Instant.from(zonedDateTime);
TemporalAccessor temporalAccessor = timestampParser.parse(timestamp);
Instant instant = Instant.from(temporalAccessor);
return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano());
}

Expand Down

0 comments on commit 03dc89c

Please sign in to comment.