From 955da366af7863ebf60fda521af4d2adaa292bcf Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 4 Dec 2017 13:48:37 +0800 Subject: [PATCH 1/3] FIX timezone bug in sqlite:issue #820 --- lib/Drivers/DML/sqlite.js | 5 +++-- package-lock.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Drivers/DML/sqlite.js b/lib/Drivers/DML/sqlite.js index f36220de..d49abee6 100644 --- a/lib/Drivers/DML/sqlite.js +++ b/lib/Drivers/DML/sqlite.js @@ -273,12 +273,13 @@ Driver.prototype.valueToProperty = function (value, property) { if (this.config.timezone && this.config.timezone != 'local') { var tz = convertTimezone(this.config.timezone); - // shift local to UTC - value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000)); if (tz !== false) { // shift UTC to timezone value.setTime(value.getTime() - (tz * 60000)); } + }else { + // shift local to UTC + value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000)); } } break; diff --git a/package-lock.json b/package-lock.json index b1760b04..27ccc198 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "orm", - "version": "3.2.4", + "version": "4.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { From 93926224ffe0e768a49de984f0013cbd6119dddf Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Tue, 5 Dec 2017 09:35:07 +0800 Subject: [PATCH 2/3] Update sqlite.js FIX:cal --- lib/Drivers/DML/sqlite.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Drivers/DML/sqlite.js b/lib/Drivers/DML/sqlite.js index d49abee6..43905d17 100644 --- a/lib/Drivers/DML/sqlite.js +++ b/lib/Drivers/DML/sqlite.js @@ -279,7 +279,7 @@ Driver.prototype.valueToProperty = function (value, property) { } }else { // shift local to UTC - value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000)); + value.setTime(value.getTime() + (value.getTimezoneOffset() * 60000)); } } break; From a3a8f8d3baf40e1f0f7d20fbbb407691ea910757 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Thu, 7 Dec 2017 10:40:32 +0800 Subject: [PATCH 3/3] add test suit with sqlite date type --- test/integration/drivers/sqlite_spec.js | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/integration/drivers/sqlite_spec.js b/test/integration/drivers/sqlite_spec.js index d0e956f7..8e1d302c 100644 --- a/test/integration/drivers/sqlite_spec.js +++ b/test/integration/drivers/sqlite_spec.js @@ -72,6 +72,40 @@ describe("Sqlite driver", function() { should.strictEqual(valueToProperty('1.200 '), 1); }); }); + + describe("date", function () { + var timezone = /GMT([+/-]\d{4})/.exec(new Date().toString())[1]; + + function valueToProperty (value) { + return driver.valueToProperty(value, { type: 'date' }); + } + + it("should return origin object when given non-string", function () { + var now = new Date(); + should.strictEqual(valueToProperty(now), now); + var array = []; + should.strictEqual(valueToProperty(array), array); + var obj = {}; + should.strictEqual(valueToProperty(obj), obj); + }) + + it("should pass on normal time", function () { + var normal = '2017-12-07 00:00:00'; + should.strictEqual(valueToProperty(normal).toString(), new Date(normal).toString()); + }) + + it("should pass on utc time by orm saved with local config", function () { + var utc = '2017-12-07T00:00:00'; + should.strictEqual(valueToProperty(utc+'Z').toString(), new Date(utc+timezone).toString()); + }) + + it("should pass on utc time by orm saved with timezone config", function () { + var utc = '2017-12-07T00:00:00'; + driver.config.timezone = timezone; + should.strictEqual(valueToProperty(utc+'Z').toString(), new Date(utc+timezone).toString()); + driver.config.timezone = ''; + }) + }); }); });