Skip to content

Commit f793b95

Browse files
committed
Merge pull request sequelize#2067 from jthele/dst
dst fix
2 parents 09a3eb4 + 0d1557c commit f793b95

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

lib/sql-string.js

+6-27
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,14 @@ SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) {
153153
}
154154

155155
SqlString.dateToString = function(date, timeZone, dialect) {
156-
var dt = new Date(date)
156+
date = moment(date).zone(timeZone);
157157

158-
// TODO: Ideally all dialects would work a bit more like this
159-
if (dialect === "postgres") {
160-
return moment(dt).zone('+00:00').format("YYYY-MM-DD HH:mm:ss.SSS Z")
161-
}
162-
163-
if (timeZone !== 'local') {
164-
var tz = convertTimezone(timeZone)
165-
166-
dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000))
167-
if (tz !== false) {
168-
dt.setTime(dt.getTime() + (tz * 60000))
169-
}
158+
if (dialect === 'mysql' || dialect === 'mariadb') {
159+
return date.format('YYYY-MM-DD HH:mm:ss');
160+
} else {
161+
// ZZ here means current timezone, _not_ UTC
162+
return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
170163
}
171-
172-
return moment(dt).format("YYYY-MM-DD HH:mm:ss")
173164
}
174165

175166
SqlString.bufferToString = function(buffer, dialect) {
@@ -208,15 +199,3 @@ SqlString.objectToValues = function(object, timeZone) {
208199
function zeroPad(number) {
209200
return (number < 10) ? '0' + number : number
210201
}
211-
212-
function convertTimezone(tz) {
213-
if (tz == "Z") {
214-
return 0
215-
}
216-
217-
var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/)
218-
if (m) {
219-
return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60
220-
}
221-
return false
222-
}

test/sqlite/query-generator.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ if (dialect === 'sqlite') {
182182
]
183183
}
184184
}],
185-
expectation: "SELECT * FROM `myTable` ORDER BY f1(`myTable`.`id`) DESC, f2(12, 'lalala', '2011-03-27 10:01:55') ASC;",
185+
expectation: "SELECT * FROM `myTable` ORDER BY f1(`myTable`.`id`) DESC, f2(12, 'lalala', '2011-03-27 10:01:55.000 +00:00') ASC;",
186186
context: QueryGenerator,
187187
needsSequelize: true
188188
}, {
@@ -315,7 +315,7 @@ if (dialect === 'sqlite') {
315315
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL);"
316316
}, {
317317
arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}],
318-
expectation: "INSERT INTO `myTable` (`name`,`birthday`) VALUES ('foo','2011-03-27 10:01:55');"
318+
expectation: "INSERT INTO `myTable` (`name`,`birthday`) VALUES ('foo','2011-03-27 10:01:55.000 +00:00');"
319319
}, {
320320
arguments: ['myTable', { name: "foo", value: true }],
321321
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',1);"
@@ -357,7 +357,7 @@ if (dialect === 'sqlite') {
357357
expectation: "INSERT INTO `myTable` (`name`) VALUES ('''bar'''),('foo');"
358358
}, {
359359
arguments: ['myTable', [{name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, {name: 'bar', birthday: moment("2012-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}]],
360-
expectation: "INSERT INTO `myTable` (`name`,`birthday`) VALUES ('foo','2011-03-27 10:01:55'),('bar','2012-03-27 10:01:55');"
360+
expectation: "INSERT INTO `myTable` (`name`,`birthday`) VALUES ('foo','2011-03-27 10:01:55.000 +00:00'),('bar','2012-03-27 10:01:55.000 +00:00');"
361361
}, {
362362
arguments: ['myTable', [{name: "bar", value: null}, {name: 'foo', value: 1}]],
363363
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL),('foo',1);"
@@ -394,10 +394,10 @@ if (dialect === 'sqlite') {
394394
updateQuery: [
395395
{
396396
arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, {id: 2}],
397-
expectation: "UPDATE `myTable` SET `name`='foo',`birthday`='2011-03-27 10:01:55' WHERE `id`=2"
397+
expectation: "UPDATE `myTable` SET `name`='foo',`birthday`='2011-03-27 10:01:55.000 +00:00' WHERE `id`=2"
398398
}, {
399399
arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, 2],
400-
expectation: "UPDATE `myTable` SET `name`='foo',`birthday`='2011-03-27 10:01:55' WHERE `id`=2"
400+
expectation: "UPDATE `myTable` SET `name`='foo',`birthday`='2011-03-27 10:01:55.000 +00:00' WHERE `id`=2"
401401
}, {
402402
arguments: ['myTable', { name: 'foo' }, { id: 2 }],
403403
expectation: "UPDATE `myTable` SET `name`='foo' WHERE `id`=2"

0 commit comments

Comments
 (0)