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

Fixing LocalTime rounding (losing precision) #1570

Merged
merged 7 commits into from Jan 29, 2020

Conversation

franetw
Copy link
Contributor

@franetw franetw commented Sep 11, 2019

When converting the Java LocalTime to PostgreSQL time value we have to consider the following:

  • Java LocalTime has nano and time has micro precision
  • PostgreSQL time max value is 24:00:00 while LocalTime is 23:59:59.999999999

Conversion logic:

  • truncate and round up when nanos >= 500
  • rounding up all >= 23:59:59.999999500 to 24:00:00

The current code intends to do this but it has a bug where it rounds too much. This PR fixes it.

I've added test for toLocalTime() which demostrates that these two methods are asymmetrical, you can lose/change information if you store and retrieve the time.
Example:

timestampUtils.toLocalTime(timestampUtils.toString(LocalTime.parse("23:59:59.999999500")))) -> LocalTime.parse("23:59:59.999999999")

I'm open to suggestions on how can this be consolidated and if there's anything hidden to consider with this kind of change.

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. Does your submission pass tests?
  2. Does mvn checkstyle:check pass ?
  3. Have you added your new test classes to an existing test suite?

Changes to Existing Features:

  • Does this break existing behaviour? If so please explain.
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully run tests with your changes locally?

@AppVeyorBot
Copy link

@AppVeyorBot AppVeyorBot commented Sep 11, 2019

@AppVeyorBot
Copy link

@AppVeyorBot AppVeyorBot commented Sep 11, 2019

@AppVeyorBot
Copy link

@AppVeyorBot AppVeyorBot commented Sep 12, 2019

@codecov-io
Copy link

@codecov-io codecov-io commented Sep 12, 2019

Codecov Report

Merging #1570 into master will decrease coverage by <.01%.
The diff coverage is 100%.

@@             Coverage Diff              @@
##             master    #1570      +/-   ##
============================================
- Coverage      68.9%   68.89%   -0.01%     
+ Complexity     3972     3969       -3     
============================================
  Files           179      179              
  Lines         16565    16557       -8     
  Branches       2695     2694       -1     
============================================
- Hits          11414    11407       -7     
+ Misses         3899     3897       -2     
- Partials       1252     1253       +1

@franetw
Copy link
Contributor Author

@franetw franetw commented Sep 12, 2019

I assume the failing build is due to these tests running on JRE7 when LocalTime did not exist.
I'd appreciate some help on how to include/exclude these tests to get the builds passing.

@AppVeyorBot
Copy link

@AppVeyorBot AppVeyorBot commented Sep 12, 2019

@davecramer
Copy link
Member

@davecramer davecramer commented Sep 16, 2019

search for
//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"

it is used in before TimestampUtilsTest.toLocalTimeBin()

@AppVeyorBot
Copy link

@AppVeyorBot AppVeyorBot commented Sep 17, 2019

@AppVeyorBot
Copy link

@AppVeyorBot AppVeyorBot commented Sep 17, 2019

@davecramer davecramer merged commit a7480d2 into pgjdbc:master Jan 29, 2020
1 of 3 checks passed
paplorinc added a commit to paplorinc/pgjdbc that referenced this issue Feb 10, 2020
* origin/master: (427 commits)
  refactor: make PSQLState enum consts for integrity constraint violations (pgjdbc#1699)
  [maven-release-plugin] prepare for next development iteration
  [maven-release-plugin] prepare release REL42.2.10
  pass gpg key through arguments
  add passphrase to release mvn task
  chore: update signing key
  Metadata queries improvment (pgjdbc#1694)
  WIP release notes for 42.2.10 (pgjdbc#1688)
  chore(deps): bump checkstyle from 8.28 to 8.29 in /pgjdbc (pgjdbc#1691)
  Cleanup PGProperty, sort values, and add some missing to docs (pgjdbc#1686)
  fix: Fixes issue pgjdbc#1592 where one thread is reading the copy and another thread closes the connection (pgjdbc#1594)
  Fixing LocalTime rounding (losing precision) (pgjdbc#1570)
  sync error message value with tested value (pgjdbc#1664)
  add DatabaseMetaDataCacheTest to test suite to run it (pgjdbc#1685)
  Fix Network Performance of PgDatabaseMetaData.getTypeInfo() method (pgjdbc#1668)
  fix: Issue pgjdbc#1680 updating a boolean field requires special handling to set it to t or f instead of true or false (pgjdbc#1682)
  fix testSetNetworkTimeoutEnforcement test failure (pgjdbc#1681)
  minor: fix checkstyle violation of unused import (pgjdbc#1683)
  fix: pgjdbc#1677 NumberFormatException when fetching PGInterval with small value (pgjdbc#1678)
  fix: actually use milliseconds instead of microseconds for timeouts (pgjdbc#1653)
  ...
davecramer pushed a commit to davecramer/pgjdbc that referenced this issue Jul 5, 2021
* Fixing LocalTime rounding

* added tests for toLocalTime as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants