Floor pre-epoch Timestamp seconds in DateTimeConverter.convertToType - #433
Open
rootvector2 wants to merge 1 commit into
Open
Floor pre-epoch Timestamp seconds in DateTimeConverter.convertToType#433rootvector2 wants to merge 1 commit into
rootvector2 wants to merge 1 commit into
Conversation
getTime() / 1000 truncates toward zero, so a pre-epoch java.sql.Timestamp with a sub-second part gained a whole second; use Math.floorDiv so the whole-second term agrees with the non-negative getNanos() term.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes DateTimeConverter.convertToType for pre-epoch java.sql.Timestamp values with a fractional component by using floor semantics when deriving the whole-second millisecond portion, and adds a regression test to cover the negative timestamp case.
Changes:
- Use
Math.floorDivwhen deriving whole-second milliseconds forjava.sql.Timestampconversions to avoid rounding toward zero for negative timestamps. - Add a unit test that verifies converting
new Timestamp(-500L)preserves-500epoch millis.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/main/java/org/apache/commons/beanutils2/converters/DateTimeConverter.java | Adjusts timestamp whole-second computation to floor for negative values. |
| src/test/java/org/apache/commons/beanutils2/converters/DateConverterTest.java | Adds a regression test for pre-epoch Timestamp conversion correctness. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+203
to
205
| long timeInMillis = Math.floorDiv(timestamp.getTime(), 1000) * 1000; | ||
| timeInMillis += timestamp.getNanos() / 1000000; | ||
| return toDate(targetType, timeInMillis); |
Member
There was a problem hiding this comment.
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DateTimeConverter.convertToTypedecomposes ajava.sql.Timestampinto whole seconds withtimestamp.getTime() / 1000 * 1000and adds the sub-second part back viatimestamp.getNanos() / 1000000, but integer division truncates toward zero whilegetNanos()is always in[0, 999999999](the JDK constructor borrows a second to normalize a negative fraction), so a pre-epoch timestamp with a non-zero fraction rounds the whole-second term up toward zero and then adds a non-negative nanos term, gaining a full second:new java.sql.Timestamp(-500L)(1969-12-31T23:59:59.500Z) converts to epoch millis500instead of-500.Math.floorDivmakes the whole-second term floor so it agrees with the nanos term; every date/time converter inherits this one path and post-epoch values are unchanged. Found auditing the date/time converters; the existing tests only feed positiveSystem.currentTimeMillis(), so they never hit it.mvn; that'smvnon the command line by itself.