Skip to content

Commit bc4d8d9

Browse files
barryhammenbrianc
authored andcommitted
Fix sqlite date functions for epoch (brianc#319)
1 parent eec86e4 commit bc4d8d9

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

lib/dialect/sqlite.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ Sqlite.prototype.visitFunctionCall = function (functionCall) {
8181
format = "'%H'";
8282
break;
8383
}
84-
var txt = 'strftime(' + format + ', ' + (nodes[0] + '') + ')';
84+
var col = (nodes[0] + '');
85+
if (_this.config.dateTimeMillis) {
86+
// Convert to a datetime before running the strftime function
87+
// Sqlite unix epoch is in seconds, but javascript is milliseconds.
88+
col = 'datetime(' + col + '/1000, "unixepoch")';
89+
}
90+
var txt = 'strftime(' + format + ', ' + col + ')';
8591
return txt;
8692
}
8793

test/dialects/date-tests.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ Harness.test({
3636
string: 'SELECT EXTRACT(MONTH FROM "customer"."metadata") FROM "customer"'
3737
},
3838
sqlite: {
39-
text : 'SELECT strftime(\'%m\', "customer"."metadata") FROM "customer"',
40-
string: 'SELECT strftime(\'%m\', "customer"."metadata") FROM "customer"'
39+
text: 'SELECT strftime(\'%m\', datetime("customer"."metadata"/1000, "unixepoch")) FROM "customer"',
40+
string: 'SELECT strftime(\'%m\', datetime("customer"."metadata"/1000, "unixepoch")) FROM "customer"',
41+
config: {
42+
dateTimeMillis: true
43+
}
4144
},
4245
mysql: {
4346
text : 'SELECT MONTH(`customer`.`metadata`) FROM `customer`',
@@ -86,8 +89,11 @@ Harness.test({
8689
string: 'SELECT EXTRACT(HOUR FROM "customer"."metadata") FROM "customer"'
8790
},
8891
sqlite: {
89-
text : 'SELECT strftime(\'%H\', "customer"."metadata") FROM "customer"',
90-
string: 'SELECT strftime(\'%H\', "customer"."metadata") FROM "customer"'
92+
text: 'SELECT strftime(\'%H\', datetime("customer"."metadata"/1000, "unixepoch")) FROM "customer"',
93+
string: 'SELECT strftime(\'%H\', datetime("customer"."metadata"/1000, "unixepoch")) FROM "customer"',
94+
config: {
95+
dateTimeMillis: true
96+
}
9197
},
9298
mysql: {
9399
text : 'SELECT HOUR(`customer`.`metadata`) FROM `customer`',

0 commit comments

Comments
 (0)