ISO8601 timestamp sql

This commit is contained in:
Rsl1122 2019-08-13 13:35:34 +03:00
parent e51e6f4194
commit 01fb2debb6
2 changed files with 40 additions and 31 deletions

View File

@ -44,80 +44,69 @@ public interface Sql {
return "varchar(" + length + ')'; return "varchar(" + length + ')';
} }
String dateFromEpochSecond(String sql); String epochSecondToDate(String sql);
String epochSecondFromDate(String sql); String dateToEpochSecond(String sql);
String dayOfYear(String sql); String dateToDayStamp(String sql);
String year(String sql);
// https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
class MySQL implements Sql { class MySQL implements Sql {
@Override @Override
public String dateFromEpochSecond(String sql) { public String epochSecondToDate(String sql) {
return "FROM_UNIXTIME(" + sql + ')'; return "FROM_UNIXTIME(" + sql + ')';
} }
@Override @Override
public String epochSecondFromDate(String sql) { public String dateToEpochSecond(String sql) {
return "UNIX_TIMESTAMP(" + sql + ')'; return "UNIX_TIMESTAMP(" + sql + ')';
} }
@Override @Override
public String dayOfYear(String sql) { public String dateToDayStamp(String sql) {
return "DAYOFYEAR(" + sql + ')'; return "DATE(" + sql + ')';
} }
@Override
public String year(String sql) {
return "YEAR(" + sql + ')';
}
} }
// https://h2database.com/html/functions.html
class H2 extends MySQL { class H2 extends MySQL {
@Override @Override
public String dateFromEpochSecond(String sql) { public String epochSecondToDate(String sql) {
return "DATEADD('SECOND', " + sql + ", DATE '1970-01-01')"; return "DATEADD('SECOND', " + sql + ", DATE '1970-01-01')";
} }
@Override @Override
public String epochSecondFromDate(String sql) { public String dateToEpochSecond(String sql) {
return "DATEDIFF('SECOND', DATE '1970-01-01', " + sql + ')'; return "DATEDIFF('SECOND', DATE '1970-01-01', " + sql + ')';
} }
@Override @Override
public String dayOfYear(String sql) { public String dateToDayStamp(String sql) {
return "DAY_OF_YEAR(" + sql + ')'; return "DATE(" + sql + ')';
} }
@Override
public String year(String sql) {
return "YEAR(" + sql + ')';
}
} }
// https://sqlite.org/lang_datefunc.html
class SQLite implements Sql { class SQLite implements Sql {
@Override @Override
public String dateFromEpochSecond(String sql) { public String epochSecondToDate(String sql) {
return "datetime(" + sql + ", 'unixepoch')"; return "datetime(" + sql + ", 'unixepoch')";
} }
@Override @Override
public String epochSecondFromDate(String sql) { public String dateToEpochSecond(String sql) {
return "strftime('%s'," + sql + ")"; return "strftime('%s'," + sql + ")";
} }
@Override @Override
public String dayOfYear(String sql) { public String dateToDayStamp(String sql) {
return "strftime('%j'," + sql + ')'; return "strftime('%Y-%m-%d'," + sql + ')';
} }
@Override
public String year(String sql) {
return "strftime('%Y'" + sql + ')';
}
} }
} }

View File

@ -1096,13 +1096,13 @@ public interface DatabaseTest {
} }
@Test @Test
default void sqlDateParsingSanityCheck() { default void sqlDateConversionSanityCheck() {
Database db = db(); Database db = db();
long expected = System.currentTimeMillis() / 1000; long expected = System.currentTimeMillis() / 1000;
Sql sql = db.getType().getSql(); Sql sql = db.getType().getSql();
String testSQL = SELECT + sql.epochSecondFromDate(sql.dateFromEpochSecond(Long.toString(expected))) + " as ms"; String testSQL = SELECT + sql.dateToEpochSecond(sql.epochSecondToDate(Long.toString(expected))) + " as ms";
long result = db.query(new QueryAllStatement<Long>(testSQL) { long result = db.query(new QueryAllStatement<Long>(testSQL) {
@Override @Override
@ -1113,6 +1113,26 @@ public interface DatabaseTest {
assertEquals(expected, result); assertEquals(expected, result);
} }
@Test
default void sqlDateParsingSanityCheck() {
Database db = db();
long time = System.currentTimeMillis();
Sql sql = db.getType().getSql();
String testSQL = SELECT + sql.dateToDayStamp(sql.epochSecondToDate(Long.toString(time / 1000))) + " as date";
System.out.println(testSQL);
String expected = system().getHtmlUtilities().getFormatters().iso8601NoClockLong().apply(time);
String result = db.query(new QueryAllStatement<String>(testSQL) {
@Override
public String processResults(ResultSet set) throws SQLException {
return set.next() ? set.getString("date") : null;
}
});
assertEquals(expected, result);
}
@Test @Test
default void extensionPlayerValuesAreStored() { default void extensionPlayerValuesAreStored() {
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system().getExtensionService(); ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system().getExtensionService();