Skip to content

Commit

Permalink
Merge pull request #822 from moonrailgun/master
Browse files Browse the repository at this point in the history
FIX timezone bug in sqlite:issue #820
  • Loading branch information
dxg authored Dec 7, 2017
2 parents 4615d96 + a3a8f8d commit dbf3564
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/Drivers/DML/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions test/integration/drivers/sqlite_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
})
});
});
});

Expand Down

0 comments on commit dbf3564

Please sign in to comment.