Skip to content
Open
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 @@ -200,7 +200,7 @@ protected <T> T convertToType(final Class<T> targetType, final Object value) thr
// didn't include the milliseconds. The following code
// ensures it works consistently across JDK versions
final java.sql.Timestamp timestamp = (java.sql.Timestamp) value;
long timeInMillis = timestamp.getTime() / 1000 * 1000;
long timeInMillis = Math.floorDiv(timestamp.getTime(), 1000) * 1000;
timeInMillis += timestamp.getNanos() / 1000000;
return toDate(targetType, timeInMillis);
Comment on lines +203 to 205

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @rootvector2
Please review Copiolot's comment. The comment suggests we are also missing a unit tests for the path Copilot found. Please verify and provide an additional test.
Thank you!

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

package org.apache.commons.beanutils2.converters;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.junit.jupiter.api.Test;

/**
* Test Case for the DateConverter class.
*/
Expand Down Expand Up @@ -66,4 +71,16 @@ protected DateConverter makeConverter(final Date defaultValue) {
protected Date toType(final Calendar value) {
return value.getTime();
}

/**
* A pre-epoch {@link Timestamp} carries a non-negative sub-second part in {@code getNanos()}, so decomposing
* {@code getTime()} into whole seconds must floor: integer division truncates toward zero for negative values and
* gains a whole second.
*/
@Test
void testConvertPreEpochSqlTimestamp() {
// 1969-12-31T23:59:59.500Z: getTime() == -500, getNanos() == 500_000_000
final Timestamp timestamp = new Timestamp(-500L);
assertEquals(-500L, makeConverter().convert(getExpectedType(), timestamp).getTime());
}
}
Loading