Skip to content

Commit 45e5c08

Browse files
committed
fix #76 DateTime format YYYYMMDDTHHMMSS
1 parent 3385c34 commit 45e5c08

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

source/ddbc/drivers/sqliteddbc.d

+39-6
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,13 @@ version(USE_SQLITE) {
111111
case 12:
112112
if (sqliteString[2] == ':' && sqliteString[5] == ':') {
113113
// HH:MM:SS.SSS
114-
auto time = TimeOfDay.fromISOExtString(sqliteString[0..8]);
115-
int hours = cast(int) to!uint(sqliteString[0 .. 2]);
114+
auto time = TimeOfDay.fromISOExtString(sqliteString[0..8]); // chop the '.SSS' off
116115
return DateTime(Date(), time);
117116
}
118117
break;
118+
case 15:
119+
// YYYYMMDDTHHMMSS
120+
return DateTime.fromISOString(sqliteString);
119121
case 16:
120122
// YYYY-MM-DD HH:MM
121123
// YYYY-MM-DDTHH:MM
@@ -128,10 +130,10 @@ version(USE_SQLITE) {
128130
return DateTime(date, time);
129131
case 19:
130132
case 23:
131-
// YYYY-MM-DD HH:MM:SS
132-
// YYYY-MM-DD HH:MM:SS.SSS
133-
// YYYY-MM-DDTHH:MM:SS
134-
// YYYY-MM-DDTHH:MM:SS.SSS
133+
// YYYY-MM-DD HH:MM:SS
134+
// YYYY-MM-DD HH:MM:SS.SSS
135+
// YYYY-MM-DDTHH:MM:SS
136+
// YYYY-MM-DDTHH:MM:SS.SSS
135137

136138
auto date = Date.fromISOExtString(sqliteString[0..10]);
137139
auto time = TimeOfDay.fromISOExtString(sqliteString[11..19]);
@@ -146,6 +148,37 @@ version(USE_SQLITE) {
146148
throw new DateTimeException(format("Unknown SQLite date string: %s", sqliteString));
147149
}
148150

151+
unittest {
152+
DateTime hm = fromResultSet("15:18"); // HH:MM
153+
DateTime hms = fromResultSet("15:18:51"); // HH:MM:SS
154+
155+
DateTime hmss = fromResultSet("15:18:51.500"); // HH:MM:SS.SSS
156+
assert(hmss.toISOExtString() == "0001-01-01T15:18:51"); // it'll lose the precision and default to 0001-01-01
157+
158+
159+
DateTime ymd = fromResultSet("2019-09-15"); // YYYY-MM-DD
160+
DateTime ymdhm = fromResultSet("2019-09-15 15:18"); // YYYY-MM-DD HH:MM
161+
DateTime ymdthm = fromResultSet("2019-09-15T15:18"); // YYYY-MM-DDTHH:MM
162+
163+
DateTime nonstandard = fromResultSet("20190915T151851"); // YYYYMMDDTHHMMSS
164+
165+
DateTime ymdhms = fromResultSet("2019-09-15 15:18:51"); // YYYY-MM-DD HH:MM:SS
166+
167+
DateTime ymdhmss = fromResultSet("2019-09-15 15:18:51.500"); // YYYY-MM-DD HH:MM:SS.SSS
168+
assert(ymdhmss.toISOExtString() == "2019-09-15T15:18:51"); // it'll lose the precision
169+
170+
DateTime ymdthms = fromResultSet("2019-09-15T15:18:51"); // YYYY-MM-DDTHH:MM:SS
171+
172+
DateTime ymdthmss = fromResultSet("2019-09-15T15:18:51.500"); // YYYY-MM-DDTHH:MM:SS.SSS
173+
assert(ymdthmss.toISOExtString() == "2019-09-15T15:18:51"); // it'll lose the precision
174+
175+
// todo. SQLite DATETIME values can also have timezone : [+-]HH:MM or Z (for UTC)
176+
// as well as greater preciion than a std.datetime.date : DateTime can handle.
177+
// we need to add support for std.datetime.systime : SysTime so that we can do:
178+
// SysTime.fromISOExtString("2018-01-01T10:30:00Z");
179+
// see: https://github.com/buggins/ddbc/issues/62
180+
}
181+
149182
class SQLITEConnection : ddbc.core.Connection {
150183
private:
151184
string filename;

0 commit comments

Comments
 (0)