diff --git a/src/main/java/org/mariadb/jdbc/internal/common/AbstractValueObject.java b/src/main/java/org/mariadb/jdbc/internal/common/AbstractValueObject.java index 433a4b8..29dd0e6 100644 --- a/src/main/java/org/mariadb/jdbc/internal/common/AbstractValueObject.java +++ b/src/main/java/org/mariadb/jdbc/internal/common/AbstractValueObject.java @@ -52,6 +52,7 @@ import org.mariadb.jdbc.MySQLBlob; import org.mariadb.jdbc.MySQLClob; import org.mariadb.jdbc.internal.mysql.MySQLType; + import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -245,7 +246,7 @@ } else { sdf = new SimpleDateFormat("yyyy-MM-dd"); } - sdf.setLenient(false); + sdf.setLenient(true); final java.util.Date utilTime = sdf.parse(rawValue); Timestamp ts = new Timestamp(utilTime.getTime()); if(rawValue.indexOf('.') != -1) { diff --git a/src/test/java/org/mariadb/jdbc/TimestampTest.java b/src/test/java/org/mariadb/jdbc/TimestampTest.java new file mode 100644 index 0000000..0aeeb6f --- /dev/null +++ b/src/test/java/org/mariadb/jdbc/TimestampTest.java @@ -0,0 +1,88 @@ +package org.mariadb.jdbc; + +import static org.junit.Assert.*; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Locale; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TimestampTest extends BaseTest { + + private Statement stmt; + private Locale previousFormatLocale; + private TimeZone previousTimeZone; + private TimeZone swedishTimeZone; + + @Before + public void setUp() throws SQLException { + //Save the previous FORMAT locate so we can restore it later + previousFormatLocale = Locale.getDefault(Locale.Category.FORMAT); + //Save the previous timezone so we can restore it later + previousTimeZone = TimeZone.getDefault(); + + swedishTimeZone = TimeZone.getTimeZone("Europe/Stockholm"); + TimeZone.setDefault(swedishTimeZone); //Everybody running this test case should be in Sweden + + stmt = connection.createStatement(); + + stmt.execute("drop table if exists timestamptest"); + stmt.execute("create table timestamptest (id int not null primary key auto_increment, tm timestamp)"); + } + + @After + public void tearDown() { + //Reset the FORMAT locate so other test cases are not disturbed. + Locale.setDefault(Locale.Category.FORMAT, previousFormatLocale); + //Reset the timezone so so other test cases are not disturbed. + TimeZone.setDefault(previousTimeZone); + } + + + @Test + public void testGetTimestamp() throws SQLException { + Calendar _20140101_1320_59 = Calendar.getInstance(TimeZone.getTimeZone("utc")); + _20140101_1320_59.clear(); + _20140101_1320_59.set(2014, 0, 1, 13, 20, 59); + + PreparedStatement ps = connection.prepareStatement("insert into timestamptest values(1, ?)"); + ps.setTimestamp(1, new Timestamp(_20140101_1320_59.getTimeInMillis())); + ps.execute(); + + ResultSet rs = stmt.executeQuery("select * from timestamptest"); + while(rs.next()) { + Timestamp timestamp = rs.getTimestamp(2); + assertEquals(_20140101_1320_59.getTimeInMillis(), timestamp.getTime()); + } + } + + @Test + public void testGetTimestampWhenDaylightSavingRemovesHour() throws SQLException { + //The time 2014-03-30 01:15:00 is valid in UTC + Calendar quaterPastTwo30thOfMarth2014 = Calendar.getInstance(TimeZone.getTimeZone("utc")); + quaterPastTwo30thOfMarth2014.clear(); + quaterPastTwo30thOfMarth2014.set(2014, 2, 30, 1, 15, 0); + + PreparedStatement ps = connection.prepareStatement("insert into timestamptest values(1, ?)"); + //Explicitly set a time that does not exists in Swedish timezone + //Note: this query will only work if the server is set to UTC + ps.setString(1, "2014-03-30 02:15:00"); +// ps.setTimestamp(1, new Timestamp(quaterPastTwo30thOfMarth2014.getTimeInMillis())); + ps.execute(); + + ResultSet rs = stmt.executeQuery("select * from timestamptest"); + while(rs.next()) { + Timestamp timestamp = rs.getTimestamp(2); + assertEquals(quaterPastTwo30thOfMarth2014.getTimeInMillis(), timestamp.getTime()); + } + } +} +