Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamp.of(java.util.Date) fails for pre-epoch dates not truncated to seconds #3429

Closed
matjazs opened this issue Jul 4, 2018 · 0 comments
Assignees
Labels
api: core priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@matjazs
Copy link

matjazs commented Jul 4, 2018

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);
   }
}
@JustinBeckwith JustinBeckwith added triage me I really want to be triaged. 🚨 This issue needs some love. labels Jul 5, 2018
@yihanzhen yihanzhen added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. api: core priority: p2 Moderately-important priority. Fix may not be included in next release. and removed 🚨 This issue needs some love. triage me I really want to be triaged. labels Jul 9, 2018
@yihanzhen yihanzhen self-assigned this Jul 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: core priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants