Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
fix: fix conversion for timestamps pre-epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 committed Feb 19, 2020
1 parent 2ce2f8d commit 0030226
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Expand Up @@ -119,7 +119,13 @@ public static Timestamp now() {
* @throws IllegalArgumentException if the timestamp is outside the representable range
*/
public static Timestamp of(java.sql.Timestamp timestamp) {
return ofTimeSecondsAndNanos(timestamp.getTime() / 1000, timestamp.getNanos());
// TODO: replace with Math.floorDiv when we drop Java 7 support
long secs = timestamp.getTime() / 1000;
int nanos = timestamp.getNanos();
if (secs < 0) {
--secs;
}
return Timestamp.ofTimeSecondsAndNanos(secs, nanos);
}

/**
Expand Down
Expand Up @@ -82,9 +82,24 @@ public void ofDate() {

@Test
public void ofSqlTimestamp() {
String timestampString = "1950-01-01 01:23:45.123456789";
String expectedTimestamp = "1950-01-01T09:23:45.123456789Z";
java.sql.Timestamp input = java.sql.Timestamp.valueOf(timestampString);
String expectedTimestamp = "1970-01-01T00:00:12.345000000Z";
java.sql.Timestamp input = new java.sql.Timestamp(12345);
Timestamp timestamp = Timestamp.of(input);
assertThat(timestamp.toString()).isEqualTo(expectedTimestamp);
}

@Test
public void ofSqlTimestampPreEpoch() {
String expectedTimestamp = "1969-12-31T23:59:47.655000000Z";
java.sql.Timestamp input = new java.sql.Timestamp(-12345);
Timestamp timestamp = Timestamp.of(input);
assertThat(timestamp.toString()).isEqualTo(expectedTimestamp);
}

@Test
public void ofSqlTimestampOnEpoch() {
String expectedTimestamp = "1970-01-01T00:00:00Z";
java.sql.Timestamp input = new java.sql.Timestamp(0);
Timestamp timestamp = Timestamp.of(input);
assertThat(timestamp.toString()).isEqualTo(expectedTimestamp);
}
Expand Down

0 comments on commit 0030226

Please sign in to comment.