com.google.cloud.Timestamp.of(Date) fails with IllegalArgumentException if the given date is before epoch and not truncated to seconds, in the concrete example, one millisecond before epoch was used.
google-cloud-core version: 1.35.0
Stacktrace fragment:
java.lang.IllegalArgumentException: timestamp out of range: 0, -1000000
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:356)
at com.google.cloud.Timestamp.ofTimeMicroseconds(Timestamp.java:84)
at com.google.cloud.Timestamp.of(Timestamp.java:95)
Unit test (the variant with seconds and nanos succeeds, the variant with Date fails for the same instant in time):
package com.outfit7.gcloud;
import java.time.Instant;
import java.util.Date;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import com.google.cloud.Timestamp;
class TimestampTest {
private static final long EPOCH_MILLIS = 0L;
private static final long EPOCH_MILLIS_MINUS_ONE = EPOCH_MILLIS - 1;
@Test
void ofTimeSecondsAndNanos_preEpoch() {
// given (one second backwards, 999 millis forward, effectively one millisecond before epoch)
long seconds = -1;
int nanos = 999 * 1000000;
// when
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(seconds, nanos);
// then
Assertions.assertThat(timestamp.toDate().getTime()).isEqualTo(EPOCH_MILLIS_MINUS_ONE);
}
@Test
void ofJavaUtilDate_preEpoch() {
// given
Date epochMinusMillisecond = Date.from(Instant.ofEpochMilli(EPOCH_MILLIS_MINUS_ONE));
// when
Timestamp timestamp = Timestamp.of(epochMinusMillisecond);
// then
Assertions.assertThat(timestamp.toDate().getTime()).isEqualTo(EPOCH_MILLIS_MINUS_ONE);
}
}
com.google.cloud.Timestamp.of(Date) fails with IllegalArgumentException if the given date is before epoch and not truncated to seconds, in the concrete example, one millisecond before epoch was used.
google-cloud-core version: 1.35.0
Stacktrace fragment:
Unit test (the variant with seconds and nanos succeeds, the variant with Date fails for the same instant in time):