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

fix: fix conversion for pre-epoch timestamps #160

Merged
merged 3 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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;
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
int nanos = timestamp.getNanos();
if (secs < 0) {
--secs;
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
}
return Timestamp.ofTimeSecondsAndNanos(secs, nanos);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ public void ofDate() {
assertThat(timestamp.getNanos()).isEqualTo(expectedNanos);
}

@Test
public void ofSqlTimestamp() {
String expectedTimestamp = "1970-01-01T00:00:12.345000000Z";
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
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);
}

@Test
public void ofDatePreEpoch() {
Timestamp timestamp = Timestamp.of(TEST_DATE_PRE_EPOCH);
Expand Down