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

Add support for LocalDate, LocalTime and LocalDateTime to be passed as 'type' in ResultSet.getObject() #749

Merged
merged 6 commits into from
Aug 18, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -2373,6 +2373,14 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
returnValue = getTime(columnIndex);
} else if (type == java.sql.Timestamp.class) {
returnValue = getTimestamp(columnIndex);
} else if (type == java.time.LocalDateTime.class) {
returnValue = null;
java.sql.Timestamp ts = getTimestamp(columnIndex,
Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")));
Copy link

@mrotteveel mrotteveel Jul 17, 2018

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.

Copy link
Contributor Author

@gordthompson gordthompson Jul 17, 2018

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.

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.

if (ts != null) {
returnValue = java.time.LocalDateTime.ofInstant(ts.toInstant(),
java.time.ZoneId.of("UTC"));
}
} else if (type == microsoft.sql.DateTimeOffset.class) {
returnValue = getDateTimeOffset(columnIndex);
} else if (type == UUID.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.util.TimeZone;
import java.util.UUID;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -240,6 +242,37 @@ public void testJdbc41ResultSetMethods() throws SQLException {
}
}

/**
* Tests getObject(n, java.time.LocalDateTime.class).
*
* @throws SQLException
*/
@Test
public void testGetObjectAsLocalDateTime() throws SQLException {
try (Connection con = DriverManager.getConnection(connectionString); Statement stmt = con.createStatement()) {
TimeZone prevTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("America/Edmonton"));

// a local date/time that does not actually exist because of Daylight Saving Time
final String testValue = "2018-03-11T02:00:00.1234567";

stmt.executeUpdate(
"CREATE TABLE " + tableName + " (id INT PRIMARY KEY, dt2 DATETIME2)");
stmt.executeUpdate(
"INSERT INTO " + tableName + " (id, dt2) VALUES (1, '" + testValue + "')");

try (ResultSet rs = stmt.executeQuery("SELECT dt2 FROM " + tableName + " WHERE id=1")) {
rs.next();
LocalDateTime actual = rs.getObject(1, LocalDateTime.class);
LocalDateTime expected = LocalDateTime.parse(testValue);
assertEquals(expected, actual);
} finally {
Utils.dropTableIfExists(tableName, stmt);
TimeZone.setDefault(prevTimeZone);
}
}
}

/**
* Tests ResultSet#isWrapperFor and ResultSet#unwrap.
*
Expand Down