From 201bc088e4865c3819df178ebcd0f9bb23e30a9d Mon Sep 17 00:00:00 2001 From: Telmo Brugnara Date: Fri, 3 May 2019 10:54:57 -0300 Subject: [PATCH 1/2] Fix for Timestamp with Time Zone datatype - #6 --- .../jdbc/types/TimestampType.java | 14 ++++++++++++++ .../jdbc/types/TimestampTypeTests.java | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java b/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java index 57b9086..3aa4de5 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java @@ -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 @@ -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); diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampTypeTests.java b/src/test/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampTypeTests.java index 65884ac..9fcd64d 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampTypeTests.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampTypeTests.java @@ -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( From 6fe6ff060130655bf203fc5f7cfc154677381f8b Mon Sep 17 00:00:00 2001 From: Telmo Brugnara Date: Thu, 16 May 2019 16:34:05 -0300 Subject: [PATCH 2/2] Fix for Timestamp with Time Zone datatype (formatting) - #6 --- .../jdbc/types/TimestampType.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java b/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java index 3aa4de5..4f97788 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java @@ -76,13 +76,13 @@ public java.sql.Timestamp asTimestamp(String value, Calendar calendar) throws SQ } // Timestamp.valueOf() does not like timezone information if (value.length() > 23) { - if(value.length() == 24 && value.charAt(23) == 'Z') { + if (value.length() == 24 && value.charAt(23) == 'Z') { value = value.substring(0, 23); } - else if(value.charAt(23) == '+' || value.charAt(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))); + if (calendar == null) { + calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT" + value.substring(23))); } value = value.substring(0, 23); }