Skip to content

Normalize source string when parsing #609

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

Merged
merged 2 commits into from
Jun 27, 2021
Merged
Changes from all 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
7 changes: 4 additions & 3 deletions src/main/java/org/sqlite/date/FastDateParser.java
Original file line number Diff line number Diff line change
@@ -288,15 +288,16 @@ public Object parseObject(final String source) throws ParseException {
* @see org.apache.commons.lang3.time.DateParser#parse(java.lang.String)
*/
public Date parse(final String source) throws ParseException {
final Date date= parse(source, new ParsePosition(0));
String normalizedSource = source.length() == 19 ? (source + ".000") : source;
final Date date= parse(normalizedSource, new ParsePosition(0));
if(date==null) {
// Add a note re supported date range
if (locale.equals(JAPANESE_IMPERIAL)) {
throw new ParseException(
"(The " +locale + " locale does not support dates before 1868 AD)\n" +
"Unparseable date: \""+source+"\" does not match "+parsePattern.pattern(), 0);
"Unparseable date: \""+normalizedSource+"\" does not match "+parsePattern.pattern(), 0);
}
throw new ParseException("Unparseable date: \""+source+"\" does not match "+parsePattern.pattern(), 0);
throw new ParseException("Unparseable date: \""+normalizedSource+"\" does not match "+parsePattern.pattern(), 0);
}
return date;
}
12 changes: 12 additions & 0 deletions src/test/java/org/sqlite/StatementTest.java
Original file line number Diff line number Diff line change
@@ -421,6 +421,18 @@ public void dateTimeTest() throws SQLException {
Date d = rs.getDate(1);
assertEquals(day.getTime(), d.getTime());
}

@Test
public void defaultDateTimeTest() throws SQLException {
stat.executeUpdate("create table daywithdefaultdatetime (id integer, datetime datatime default current_timestamp)");
PreparedStatement prep = conn.prepareStatement("insert into daywithdefaultdatetime (id) values (1)");
prep.setInt(1, 1);
prep.executeUpdate();
ResultSet rs = stat.executeQuery("select * from day");
assertTrue(rs.next());
Date d = rs.getDate(2);
assertTrue(d != null);
}

@Test
public void maxRows() throws SQLException {