timezone.test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('./support');
  5. const dialectName = Support.getTestDialect();
  6. const dialect = Support.sequelize.dialect;
  7. const queryGenerator = Support.sequelize.queryGenerator;
  8. describe(Support.getTestDialectTeaser('Timezone'), () => {
  9. if (!dialect.supports.globalTimeZoneConfig) {
  10. return;
  11. }
  12. before(function () {
  13. this.sequelizeWithTimezone = Support.createSequelizeInstance({
  14. timezone: '+07:00',
  15. });
  16. this.sequelizeWithNamedTimezone = Support.createSequelizeInstance({
  17. timezone: 'America/New_York',
  18. });
  19. });
  20. after(function () {
  21. this.sequelizeWithTimezone.close();
  22. this.sequelizeWithNamedTimezone.close();
  23. });
  24. it('returns the same value for current timestamp', async function () {
  25. const startQueryTime = Date.now();
  26. const now = dialectName === 'mssql' ? 'GETDATE()' : 'now()';
  27. const query = `SELECT ${now} as ${queryGenerator.quoteIdentifier('now')}`;
  28. const [now1, now2, now3] = await Promise.all([
  29. this.sequelize.query(query, { type: this.sequelize.QueryTypes.SELECT }),
  30. this.sequelizeWithTimezone.query(query, { type: this.sequelize.QueryTypes.SELECT }),
  31. this.sequelizeWithNamedTimezone.query(query, { type: this.sequelize.QueryTypes.SELECT }),
  32. ]);
  33. const elapsedQueryTime = Date.now() - startQueryTime + 1001;
  34. expect(new Date(now1[0].now).getTime()).to.be.closeTo(
  35. new Date(now2[0].now).getTime(),
  36. elapsedQueryTime,
  37. );
  38. expect(new Date(now1[0].now).getTime()).to.be.closeTo(
  39. new Date(now3[0].now).getTime(),
  40. elapsedQueryTime,
  41. );
  42. });
  43. if (['mysql', 'mariadb'].includes(dialectName)) {
  44. it('handles existing timestamps', async function () {
  45. const NormalUser = this.sequelize.define('user', {});
  46. const TimezonedUser = this.sequelizeWithTimezone.define('user', {});
  47. await this.sequelize.sync({ force: true });
  48. const normalUser = await NormalUser.create({});
  49. this.normalUser = normalUser;
  50. const timezonedUser = await TimezonedUser.findByPk(normalUser.id);
  51. // Expect 7 hours difference, in milliseconds.
  52. // This difference is expected since two instances, configured for each their timezone is trying to read the same timestamp
  53. // this test does not apply to PG, since it stores the timezone along with the timestamp.
  54. expect(this.normalUser.createdAt.getTime() - timezonedUser.createdAt.getTime()).to.be.closeTo(
  55. 60 * 60 * 7 * 1000,
  56. 1000,
  57. );
  58. });
  59. it('handles named timezones', async function () {
  60. const NormalUser = this.sequelize.define('user', {});
  61. const TimezonedUser = this.sequelizeWithNamedTimezone.define('user', {});
  62. await this.sequelize.sync({ force: true });
  63. const timezonedUser0 = await TimezonedUser.create({});
  64. const [normalUser, timezonedUser] = await Promise.all([
  65. NormalUser.findByPk(timezonedUser0.id),
  66. TimezonedUser.findByPk(timezonedUser0.id),
  67. ]);
  68. // Expect 5 hours difference, in milliseconds, +/- 1 hour for DST
  69. expect(normalUser.createdAt.getTime() - timezonedUser.createdAt.getTime()).to.be.closeTo(
  70. 60 * 60 * 4 * 1000 * -1,
  71. 60 * 60 * 1000,
  72. );
  73. });
  74. }
  75. });