Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ default ByteString map(BytesValue f) {
}

default Instant mapToInstant(Timestamp t) {
if (t == null) {
return null;
}

Timestamp sanitized = Timestamps.sanitize(t);

if (sanitized != null) {
Expand All @@ -151,11 +155,11 @@ default Instant mapToInstant(Timestamp t) {
}

default Timestamp mapToTimestamp(Instant i) {
if (i == null || i.getEpochSecond() == 0) {
if (i == null) {
return null;
} else {
Timestamp t = Timestamp.newBuilder().setSeconds(i.getEpochSecond()).setNanos(i.getNano()).build();
return Timestamps.sanitize(t);
}

Timestamp t = Timestamp.newBuilder().setSeconds(i.getEpochSecond()).setNanos(i.getNano()).build();
return Timestamps.sanitize(t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public class Timestamps {
* Sanitize Timestamps outside legal range where possible.
*/
public static Timestamp sanitize(Timestamp t) {
if (t.getSeconds() == 0 && t.getNanos() == 0) {
return null; // Assuming null for epoch, cannot differentiate
}
if (t.getNanos() < 0 || t.getNanos() >= NANOS_PER_SECOND) {
throw new IllegalArgumentException(String.format("Timestamp is not valid. See proto definition for valid values. Seconds (%s) must be in range [-62,135,596,800, +253,402,300,799]. Nanos (%s) must be in range [0, +999,999,999].", t.getSeconds(), t.getNanos()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,19 @@ public void testMapLocalDateToTimestampWintertime() {
}

@Test
public void mapToInstant_whenSecondsAndNanosIs0_thenMapToNull() {
assertNull(MAPPER.mapToInstant(Timestamp.newBuilder().build()));
public void testMapInstantToTimestampEpoch() {
Instant epoch = Instant.EPOCH;

Timestamp timestamp = MAPPER.mapToTimestamp(epoch);

Instant back = MAPPER.mapToInstant(timestamp);

assertEquals(epoch, back);
}

@Test
public void mapToInstant_whenSecondsAndNanosIsNull_thenMapToNull() {
assertNull(MAPPER.mapToInstant(null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,21 @@ public void testMapLocalDateToTimestampWintertime() {
assertEquals(l, back);
}

@Test
public void testMapInstantToTimestampEpoch() {
Instant epoch = Instant.EPOCH;

Timestamp timestamp = MAPPER.mapToTimestamp(epoch);

Instant back = MAPPER.mapToInstant(timestamp);

assertEquals(epoch, back);
}


@Test
public void mapToInstant_whenSecondsAndNanosIs0_thenMapToNull() {
assertNull(MAPPER.mapToInstant(Timestamp.newBuilder().build()));
public void mapToInstant_whenSecondsAndNanosIsNull_thenMapToNull() {
assertNull(MAPPER.mapToInstant(null));
}

@Test
Expand Down