diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index 97f24060..c8f8a467 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 4feb7bf3..dfcebf47 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')