Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Fix for Timestamp with Time Zone datatype - #6 #10

Merged
merged 2 commits into from May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,6 +21,7 @@
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone;

/**
* Supports returning a java.sql.Timestamp from a String in the
Expand Down Expand Up @@ -73,6 +74,19 @@ public java.sql.Timestamp asTimestamp(String value, Calendar calendar) throws SQ
if (value.length() > 11 && value.charAt(10) == 'T') {
value = value.replace('T', ' ');
}
// Timestamp.valueOf() does not like timezone information
if (value.length() > 23) {
if (value.length() == 24 && value.charAt(23) == 'Z') {
value = value.substring(0, 23);
}
else if (value.charAt(23) == '+' || value.charAt(23) == '-') {
// 'calendar' parameter takes precedence
if (calendar == null) {
calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT" + value.substring(23)));
}
value = value.substring(0, 23);
}
}

if (calendar == null) {
return Timestamp.valueOf(value);
Expand Down
Expand Up @@ -48,7 +48,11 @@ public class TimestampTypeTests {
@CsvSource(value = {
"2009-06-16T07:28:52.333, 1245137332333",
"2015-01-01 00:34:46, 1420072486000",
"2015-01-01 00:34:46.778, 1420072486778"
"2015-01-01 00:34:46.778, 1420072486778",
"2015-01-01 00:34:46.778+00:00, 1420072486778",
"2015-01-01 00:34:46.778Z, 1420072486778",
"2015-01-01T00:34:46.778+01:00, 1420068886778",
"2015-01-01 00:34:46.778-02, 1420079686778",
})
void testTimestampFromStringDefaultTZ(String stringValue, long longValue) {
Timestamp timestamp = Assertions.assertDoesNotThrow(
Expand Down