Skip to content

Commit

Permalink
Fix Timestamps.fromDate to correctly handle java.sql.Timestamps befor…
Browse files Browse the repository at this point in the history
…e unix epoch.
  • Loading branch information
q-nathangrand committed Jun 11, 2022
1 parent 922f502 commit bfa663a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ public static Timestamp fromMillis(long milliseconds) {
public static Timestamp fromDate(Date date) {
if (date instanceof java.sql.Timestamp) {
java.sql.Timestamp sqlTimestamp = (java.sql.Timestamp) date;
long integralSeconds = sqlTimestamp.getTime() / 1000L; // truncate the fractional seconds
long time = sqlTimestamp.getTime();
long integralSeconds = (time < 0) ? time / 1000L - 1 : time / 1000L ; // truncate the fractional seconds
return Timestamp.newBuilder()
.setSeconds(integralSeconds)
.setNanos(sqlTimestamp.getNanos())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,13 @@ public void testFromSqlTimestamp() {
Timestamp timestamp = Timestamps.fromDate(date);
assertThat(Timestamps.toString(timestamp)).isEqualTo("1970-01-01T00:00:01.111Z");
}

@Test
public void testFromSqlTimestamp_beforeEpoch() {
Date date = new java.sql.Timestamp(-1111);
Timestamp timestamp = Timestamps.fromDate(date);
assertThat(Timestamps.toString(timestamp)).isEqualTo("1969-12-31T23:59:58.889Z");
}

@Test
public void testTimeOperations() throws Exception {
Expand Down

0 comments on commit bfa663a

Please sign in to comment.