Skip to content

Commit da8b86c

Browse files
committed
Don't choke on null dates with timezone set
1 parent 93eb6d1 commit da8b86c

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

lib/Drivers/DML/postgres.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ Driver.prototype.valueToProperty = function (value, property) {
264264
}
265265
break;
266266
case "date":
267-
if (this.config.timezone && this.config.timezone != 'local') {
267+
if (_.isDate(value) && this.config.timezone && this.config.timezone != 'local') {
268268
var tz = convertTimezone(this.config.timezone);
269269

270270
// shift local to UTC
271271
value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000));
272+
272273
if (tz !== false) {
273274
// shift UTC to timezone
274275
value.setTime(value.getTime() - (tz * 60000));
@@ -320,7 +321,7 @@ Driver.prototype.propertyToValue = function (value, property) {
320321
}
321322
break;
322323
case "date":
323-
if (this.config.timezone && this.config.timezone != 'local') {
324+
if (_.isDate(value) && this.config.timezone && this.config.timezone != 'local') {
324325
var tz = convertTimezone(this.config.timezone);
325326

326327
// shift local to UTC

test/integration/drivers/postgres_spec.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("Postgres driver", function() {
1010
describe("#valueToProperty", function () {
1111
var driver = null;
1212

13-
before(function () {
13+
beforeEach(function () {
1414
driver = new Driver({}, {}, {});
1515
});
1616

@@ -72,6 +72,35 @@ describe("Postgres driver", function() {
7272
should.strictEqual(valueToProperty('1.200 '), 1);
7373
});
7474
});
75+
76+
describe("dates with non local timezone", function () {
77+
beforeEach(function () {
78+
driver = new Driver({ timezone: 'Z' }, {}, {});
79+
});
80+
81+
function valueToProperty (value) {
82+
return driver.valueToProperty(value, { type: 'date' });
83+
}
84+
85+
it("should accept null", function () {
86+
should.strictEqual(valueToProperty(null), null);
87+
});
88+
89+
it("should work", function () {
90+
should.strictEqual(_.isDate(valueToProperty(new Date())), true);
91+
});
92+
93+
describe("calculations", function () {
94+
it("should offset time, relative to timezone", function () {
95+
d = new Date();
96+
97+
expected = d.getTime() - d.getTimezoneOffset() * 60000;
98+
converted = valueToProperty(d).getTime();
99+
100+
should.equal(converted, expected);
101+
});
102+
});
103+
});
75104
});
76105
});
77106

@@ -116,9 +145,24 @@ describe("Postgres driver", function() {
116145
should.equal(inputStr, out.toString());
117146
});
118147

119-
it("should offset time by specified timezone amount for + timezones");
148+
it("should work with null dates", function () {
149+
should.strictEqual(evaluate(null, { config: { timezone: 'Z' }}), null);
150+
});
151+
152+
it("should work with date objects", function () {
153+
should.strictEqual(_.isDate(evaluate(new Date(), { config: { timezone: 'Z' }})), true);
154+
});
155+
156+
describe("calculations", function () {
157+
it("should offset time, relative to timezone", function () {
158+
d = new Date();
159+
160+
expected = d.getTime() + d.getTimezoneOffset() * 60000;
161+
converted = evaluate(d, { config: { timezone: 'Z' }}).getTime();
120162

121-
it("should offset time by specified timezone amount for + timezones");
163+
should.equal(converted, expected);
164+
});
165+
});
122166
});
123167

124168
describe("type point", function () {

0 commit comments

Comments
 (0)