From 71bed155edf32bff24379930ac684fc783538d8f Mon Sep 17 00:00:00 2001 From: hansdaniels <63750717+hansdaniels@users.noreply.github.com> Date: Thu, 28 Jan 2021 06:46:42 +0100 Subject: [PATCH] fix: timezone plugin DST error (#1352) --- src/plugin/timezone/index.js | 10 +++++----- test/plugin/timezone.test.js | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index 97f24060b..c8f8a4675 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -36,8 +36,6 @@ const getDateTimeFormat = (timezone, options = {}) => { export default (o, c, d) => { let defaultTimezone - const localUtcOffset = d().utcOffset() - const makeFormatParts = (timestamp, timezone, options = {}) => { const date = new Date(timestamp) const dtf = getDateTimeFormat(timezone, options) @@ -96,9 +94,11 @@ export default (o, c, d) => { proto.tz = function (timezone = defaultTimezone, keepLocalTime) { const oldOffset = this.utcOffset() - const target = this.toDate().toLocaleString('en-US', { timeZone: timezone }) - const diff = Math.round((this.toDate() - new Date(target)) / 1000 / 60) - let ins = d(target).$set(MS, this.$ms).utcOffset(localUtcOffset - diff, true) + const date = this.toDate() + const target = date.toLocaleString('en-US', { timeZone: timezone }) + const diff = Math.round((date - new Date(target)) / 1000 / 60) + let ins = d(target).$set(MS, this.$ms) + .utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true) if (keepLocalTime) { const newOffset = ins.utcOffset() ins = ins.add(oldOffset - newOffset, MIN) diff --git a/test/plugin/timezone.test.js b/test/plugin/timezone.test.js index 4feb7bf3d..dfcebf47e 100644 --- a/test/plugin/timezone.test.js +++ b/test/plugin/timezone.test.js @@ -101,7 +101,9 @@ describe('Convert', () => { const jun = _('2014-06-01T12:00:00Z') const dec = _('2014-12-01T12:00:00Z') expect(jun.tz('America/Los_Angeles').format('ha')).toBe('5am') + expect(jun.tz('America/Los_Angeles').utcOffset()).toBe(-7 * 60) expect(dec.tz('America/Los_Angeles').format('ha')).toBe('4am') + expect(dec.tz('America/Los_Angeles').utcOffset()).toBe(-8 * 60) expect(jun.tz(NY).format('ha')).toBe('8am') expect(dec.tz(NY).format('ha')).toBe('7am') expect(jun.tz(TOKYO).format('ha')).toBe('9pm')