Skip to content

Commit ad37c52

Browse files
Normalize source string when parsing 2 (#620)
* Normalize source string when parsing The regex used to parse dates does not account for the fact that new dates created by SQLite's CURRENT_TIMESTAMP do not include millisecond information. The regex used by the date parser tries to find a string matching "yyyy-MM-dd HH:mm:ss.SSS" but the dates created by SQLite do not include the milliseconds (.SSS) so the date strings are not matching and this is causing the parser to return null. The solution is to detect this exact case (string length is exactly 19) and then append .000 to the source string. This causes the regex to match on the string. allowing the date string to be parsed, and the Date object to be returned. The source object is not mutated, and the extra characters do not change the DATETIME represented by the date string, so I think this is a safe fix. * Add unit test for default datetime * Bug fix to newly added StatementTest unit test * Update StatementTest.java
1 parent 7d72e04 commit ad37c52

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/main/java/org/sqlite/date/FastDateParser.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,16 @@ public Object parseObject(final String source) throws ParseException {
288288
* @see org.apache.commons.lang3.time.DateParser#parse(java.lang.String)
289289
*/
290290
public Date parse(final String source) throws ParseException {
291-
final Date date= parse(source, new ParsePosition(0));
291+
String normalizedSource = source.length() == 19 ? (source + ".000") : source;
292+
final Date date= parse(normalizedSource, new ParsePosition(0));
292293
if(date==null) {
293294
// Add a note re supported date range
294295
if (locale.equals(JAPANESE_IMPERIAL)) {
295296
throw new ParseException(
296297
"(The " +locale + " locale does not support dates before 1868 AD)\n" +
297-
"Unparseable date: \""+source+"\" does not match "+parsePattern.pattern(), 0);
298+
"Unparseable date: \""+normalizedSource+"\" does not match "+parsePattern.pattern(), 0);
298299
}
299-
throw new ParseException("Unparseable date: \""+source+"\" does not match "+parsePattern.pattern(), 0);
300+
throw new ParseException("Unparseable date: \""+normalizedSource+"\" does not match "+parsePattern.pattern(), 0);
300301
}
301302
return date;
302303
}

src/test/java/org/sqlite/StatementTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@ public void dateTimeTest() throws SQLException {
421421
Date d = rs.getDate(1);
422422
assertEquals(day.getTime(), d.getTime());
423423
}
424+
425+
@Test
426+
public void defaultDateTimeTest() throws SQLException {
427+
stat.executeUpdate("create table daywithdefaultdatetime (id integer, datetime datatime default current_timestamp)");
428+
PreparedStatement prep = conn.prepareStatement("insert into daywithdefaultdatetime (id) values (?)");
429+
prep.setInt(1, 1);
430+
prep.executeUpdate();
431+
ResultSet rs = stat.executeQuery("select * from daywithdefaultdatetime");
432+
assertTrue(rs.next());
433+
Date d = rs.getDate(2);
434+
assertTrue(d != null);
435+
}
424436

425437
@Test
426438
public void maxRows() throws SQLException {

0 commit comments

Comments
 (0)