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

Fix Timestamps fromDate for negative 'exact second' java.sql.Timestamps #10321

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -344,7 +344,7 @@ public static Timestamp fromDate(Date date) {
if (date instanceof java.sql.Timestamp) {
java.sql.Timestamp sqlTimestamp = (java.sql.Timestamp) date;
long time = sqlTimestamp.getTime();
long integralSeconds = (time < 0) ? time / 1000L - 1 : time / 1000L ; // truncate the fractional seconds
long integralSeconds = (time < 0 && time % 1000 != 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 @@ -483,6 +483,13 @@ public void testFromSqlTimestamp_beforeEpoch() {
assertThat(Timestamps.toString(timestamp)).isEqualTo("1969-12-31T23:59:58.889Z");
}

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

@Test
public void testTimeOperations() throws Exception {
Timestamp start = Timestamps.parse("0001-01-01T00:00:00Z");
Expand Down