-
Notifications
You must be signed in to change notification settings - Fork 426
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
Add support for LocalDate, LocalTime and LocalDateTime to be passed as 'type' in ResultSet.getObject() #749
Add support for LocalDate, LocalTime and LocalDateTime to be passed as 'type' in ResultSet.getObject() #749
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #749 +/- ##
============================================
+ Coverage 48.32% 48.32% +<.01%
- Complexity 2774 2780 +6
============================================
Files 116 116
Lines 27871 27883 +12
Branches 4631 4635 +4
============================================
+ Hits 13468 13474 +6
- Misses 12215 12233 +18
+ Partials 2188 2176 -12
Continue to review full report at Codecov.
|
Thanks for coming up with PR! We should also add related API for I would also like to know your thoughts about providing |
@cheenamalhotra No such method exists in the JDBC ( When support for JSR-310 ( See this recent comment on the jdbc-spec-discuss mailing list: http://mail.openjdk.java.net/pipermail/jdbc-spec-discuss/2018-July/000361.html by Lance Andersen, the JDBC spec-lead. |
} else if (type == java.time.LocalDateTime.class) { | ||
returnValue = null; | ||
java.sql.Timestamp ts = getTimestamp(columnIndex, | ||
Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))); |
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.
I'm not sure if this is correct given JDBC specifies that the time should be rendered in the JVM default time zone. In other words, setting 'now' as a java.sql.Timestamp
and retrieving it as a LocalDateTime
in a datetime (no time zone info) should return the same value if you'd compare their toString()
output. This will depend on how the time is stored in SQL Server.
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.
@mrotteveel The Timestamp needs to be retrieved as UTC to prevent certain datetime2 values from being corrupted if they are in the "lost hour" when switching from Standard Time to Daylight Time (a.k.a. Summer Time). For example, in my time zone (America/Edmonton), the datetime2 value 2018-03-11T02:00:00
will magically change to 2018-03-11T03:00:00
if we try to retrieve it in the default time zone since there was no 02:00
that morning; the time changed from 01:59
to 03:00
.
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.
Clear. You may want to consider skipping timestamp retrieval and instead convert directly from the datetime value to LocalDateTime
; although that might also be considered as a future improvement.
java.sql.Timestamp ts = getTimestamp(columnIndex, | ||
Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))); | ||
if (ts != null) { | ||
java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter |
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.
Parsing to string and back is inefficient. Consider using Timestamp.toLocalDateTime()
(assuming my previous comment is addressed), or LocalDateTime.ofInstant(ts.getInstant(), <zone offset you want>)
.
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.
@mrotteveel Thanks. I went with the latter.
@gordthompson I left two comments regarding the conversion. I also wonder if you considered adding support for |
@mrotteveel I focused on LocalDateTime because that was where people could get stung by the "lost hour" issue (e.g., this SO answer). SQL Server |
@mrotteveel For (more) completeness I also added support for LocalDate and LocalTime, also achieved by first retrieving a Timestamp as UTC and then converting. I agree that it might be more efficient to bypass the Timestamp and retrieve the values directly from the TDS payload (as I did for a pyodbc Output Converter Function here) , but I strongly suspect that the Java code would then have to do significantly different things depending on the column type from which the value was retrieved. That's a can of worms that I don't have time to open right now, but perhaps it should be noted as a possible future enhancement. |
Issue #744 - support for rs.getObject(n, java.time.LocalDateTime.class)