-
Notifications
You must be signed in to change notification settings - Fork 832
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: adjust date, hour, minute, second when rounding timestamp #1212
Conversation
PostgreSQL supports microsecond resolution only, so PgJDBC rounds nanoseconds to micros. When that happens the number of seconds, minutes, etc might change as well fixes pgjdbc#1211
Codecov Report
@@ Coverage Diff @@
## master #1212 +/- ##
============================================
- Coverage 69.26% 54.7% -14.56%
- Complexity 3807 3812 +5
============================================
Files 171 186 +15
Lines 15769 19993 +4224
Branches 2578 2698 +120
============================================
+ Hits 10922 10937 +15
- Misses 3613 7821 +4208
- Partials 1234 1235 +1 |
LGTM |
nanos = 0; | ||
timeMillis++; | ||
} else if (nanos % 1000 >= 500) { | ||
nanos = nanos + 1000 - nanos % 1000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it appears appendTime now simply truncates at micro precision. that would seem to mean that nanos += 1000;
would be sufficient here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, just some minor suggestions
return "24:00:00"; | ||
} | ||
|
||
int nano = localTime.getNano(); | ||
if (nano % 1000 >= 500) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This expression (nano % 1000 >= 500
) is used in more than one place, why don't extract it to a function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any ideas for the function name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isNanoCarry
?
isNanoOverflow
may be misleading.
insertThenReadWithType(time, Types.TIME, "time_without_time_zone_column", Time.class); | ||
assertEquals(Time.valueOf("24:00:00"), actual); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider writing a test for year change, like for '2018-12-31 23:59:59.999999500' to prevent potential bugs after refactorings in the future.
* @param nanos nanosecond part of the time | ||
* @return true when microsecond part of the time should be increased when rounding to microseconds | ||
*/ | ||
private static boolean nanosExceed499(int nanos) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Implement proper rounding using a TemporalAdjuster that sets to nanos to 0 instead of relying on them being truncated later. See pgjdbc#1212
PostgreSQL supports microsecond resolution only, so PgJDBC rounds
nanoseconds to micros. When that happens the number of seconds, minutes, etc
might change as well
fixes #1211