不要怂,就是干,撸起袖子干!

Commit d21fffdc by Jan Aagaard Meier

Merge pull request #3728 from bigbento/named-timezones

add support for named timezones
2 parents 39a6a065 21883759
'use strict';
/* jshint -W110 */
var moment = require('moment')
var moment = require('moment-timezone')
, isArrayBufferView
, SqlString = exports;
......@@ -165,7 +165,11 @@ SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) {
};
SqlString.dateToString = function(date, timeZone, dialect) {
date = moment(date).utcOffset(timeZone);
if (moment.tz.zone(timeZone)) {
date = moment(date).tz(timeZone);
} else {
date = moment(date).utcOffset(timeZone);
}
if (dialect === 'mysql' || dialect === 'mariadb') {
return date.format('YYYY-MM-DD HH:mm:ss');
......@@ -200,4 +204,4 @@ SqlString.objectToValues = function(object, timeZone) {
}
return values.join(', ');
};
\ No newline at end of file
};
......@@ -38,6 +38,7 @@
"inflection": "^1.6.0",
"lodash": "^3.5.0",
"moment": "^2.9.0",
"moment-timezone": "^0.3.1",
"node-uuid": "~1.4.1",
"toposort-class": "~0.3.0",
"validator": "^3.34.0",
......
......@@ -15,6 +15,9 @@ if (dialect !== 'sqlite') {
this.sequelizeWithTimezone = Support.createSequelizeInstance({
timezone: '+07:00'
});
this.sequelizeWithNamedTimezone = Support.createSequelizeInstance({
timezone: 'America/New_York'
});
});
it('returns the same value for current timestamp', function() {
......@@ -52,6 +55,21 @@ if (dialect !== 'sqlite') {
expect(normalUser.createdAt.getTime() - this.timezonedUser.createdAt.getTime()).to.be.closeTo(60 * 60 * 7 * 1000, 50);
});
});
it('handles named timezones', function() {
var NormalUser = this.sequelize.define('user', {})
, TimezonedUser = this.sequelizeWithNamedTimezone.define('user', {});
return this.sequelize.sync({ force: true }).bind(this).then(function() {
return TimezonedUser.create({});
}).then(function(timezonedUser) {
this.timezonedUser = timezonedUser;
return NormalUser.find(timezonedUser.id);
}).then(function(normalUser) {
// Expect 5 hours difference, in milliseconds, +/- 1 hour for DST
expect(normalUser.createdAt.getTime() - this.timezonedUser.createdAt.getTime()).to.be.closeTo(60 * 60 * 4 * 1000 * -1, 60 * 60 * 1000);
});
});
}
});
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!