Skip to content

Commit d32b6d4

Browse files
committed
ISO dates for SQLite
Simplified the way that dates are handled for SQLite.
1 parent 2324d34 commit d32b6d4

13 files changed

+42
-58
lines changed

lib/Drivers/DML/sqlite.js

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,20 @@ Driver.prototype.valueToProperty = function (value, property) {
260260
}
261261
break;
262262
case "date":
263-
if (typeof value == 'string') {
264-
if (value.indexOf('Z', value.length - 1) === -1) {
265-
value = new Date(value + 'Z');
266-
} else {
267-
value = new Date(value);
268-
}
269-
270-
if (this.config.timezone && this.config.timezone != 'local') {
271-
var tz = convertTimezone(this.config.timezone);
263+
if (typeof value === 'string') {
264+
var hasTimezone = value.indexOf('Z') !== -1;
265+
value = new Date(value);
272266

267+
if (!hasTimezone) {
273268
// shift local to UTC
274269
value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000));
275-
if (tz !== false) {
276-
// shift UTC to timezone
277-
value.setTime(value.getTime() - (tz * 60000));
270+
271+
if (this.config.timezone && this.config.timezone != 'local') {
272+
var tz = convertTimezone(this.config.timezone);
273+
if (tz !== false) {
274+
// shift UTC to timezone
275+
value.setTime(value.getTime() - (tz * 60000));
276+
}
278277
}
279278
}
280279
}
@@ -301,45 +300,8 @@ Driver.prototype.propertyToValue = function (value, property) {
301300
}
302301
break;
303302
case "date":
304-
if (this.config.query && this.config.query.strdates) {
305-
if (value instanceof Date) {
306-
var year = value.getUTCFullYear();
307-
var month = value.getUTCMonth() + 1;
308-
if (month < 10) {
309-
month = '0' + month;
310-
}
311-
var date = value.getUTCDate();
312-
if (date < 10) {
313-
date = '0' + date;
314-
}
315-
var strdate = year + '-' + month + '-' + date;
316-
if (property.time === false) {
317-
value = strdate;
318-
break;
319-
}
320-
321-
var hours = value.getUTCHours();
322-
if (hours < 10) {
323-
hours = '0' + hours;
324-
}
325-
var minutes = value.getUTCMinutes();
326-
if (minutes < 10) {
327-
minutes = '0' + minutes;
328-
}
329-
var seconds = value.getUTCSeconds();
330-
if (seconds < 10) {
331-
seconds = '0' + seconds;
332-
}
333-
var millis = value.getUTCMilliseconds();
334-
if (millis < 10) {
335-
millis = '0' + millis;
336-
}
337-
if (millis < 100) {
338-
millis = '0' + millis;
339-
}
340-
strdate += ' ' + hours + ':' + minutes + ':' + seconds + '.' + millis + '000';
341-
value = strdate;
342-
}
303+
if (value instanceof Date) {
304+
value = value.toISOString();
343305
}
344306
break;
345307
default:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"analyse" : false,
3838
"dependencies": {
3939
"enforce" : "0.1.2",
40-
"sql-query" : "git+https://github.com/dresende/node-sql-query.git#v0.1.23",
40+
"sql-query" : "git+https://github.com/apoco/node-sql-query.git#sqlite-utc-dates",
4141
"sql-ddl-sync" : "git+https://github.com/dresende/node-sql-ddl-sync.git#v0.3.10",
4242
"hat" : "0.0.3",
4343
"lodash" : "2.4.1"

test/integration/association-extend.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe("Model.extendsTo()", function() {
77
var Person = null;
88
var PersonAddress = null;
99

10+
this.timeout(5000);
11+
1012
var setup = function () {
1113
return function (done) {
1214
Person = db.define("person", {

test/integration/association-hasmany-hooks.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe("hasMany hooks", function() {
77
var Person = null;
88
var Pet = null;
99

10+
this.timeout(5000);
11+
1012
var setup = function (props, opts) {
1113
return function (done) {
1214
db.settings.set('instance.cache', false);

test/integration/association-hasmany.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ var common = require('../common');
66
var protocol = common.protocol();
77

88
describe("hasMany", function () {
9-
this.timeout(4000);
109
var db = null;
1110
var Person = null;
1211
var Pet = null;
1312

13+
this.timeout(5000);
14+
1415
before(function(done) {
1516
helper.connect(function (connection) {
1617
db = connection;

test/integration/association-hasone-reverse.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe("hasOne", function () {
1010
var Person = null;
1111
var Pet = null;
1212

13+
this.timeout(5000);
14+
1315
var setup = function () {
1416
return function (done) {
1517
Person = db.define('person', {

test/integration/association-hasone.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ describe("hasOne", function() {
1313
var treeId = null;
1414
var stalkId = null;
1515

16+
this.timeout(5000);
17+
1618
var setup = function (opts) {
1719
opts = opts || {};
1820
return function (done) {

test/integration/db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe("db.driver", function () {
284284
db.driver.execQuery(query, args, function (err, data) {
285285
should.not.exist(err);
286286

287-
should(JSON.stringify(data) == JSON.stringify([{ "what": "user login" }]));
287+
JSON.stringify(data).should.equal(JSON.stringify([{ "what": "user login" }]));
288288
done();
289289
});
290290
});

test/integration/model-find-chain.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ describe("Model.find() chaining", function() {
99
var Person = null;
1010
var Dog = null;
1111

12+
this.timeout(5000);
13+
1214
var setup = function (extraOpts) {
1315
if (!extraOpts) extraOpts = {};
1416

test/integration/model-one.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ describe("Model.one()", function() {
66
var db = null;
77
var Person = null;
88

9+
this.timeout(5000);
10+
911
var setup = function () {
1012
return function (done) {
1113
Person = db.define("person", {

0 commit comments

Comments
 (0)