From 276e3133a17b7eec4fb8550959eccfe93e4c262d Mon Sep 17 00:00:00 2001 From: SkyAo Date: Thu, 3 Sep 2020 23:36:03 +0800 Subject: [PATCH] feat: timezone api - tz.setDefault --- src/plugin/timezone/index.js | 15 ++++++++--- test/plugin/timezone.test.js | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index 8ecf3f87b..6746356bd 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -10,6 +10,8 @@ const typeToPos = { const ms = 'ms' export default (o, c, d) => { + let defaultTimezone + const localUtcOffset = d().utcOffset() const tzOffset = (timestamp, timezone) => { const date = new Date(timestamp) @@ -33,7 +35,7 @@ export default (o, c, d) => { filled[pos] = parseInt(value, 10) } } - // Workaround for the same performance in different node version + // Workaround for the same behavior in different node version // https://github.com/nodejs/node/issues/33027 const hour = filled[3] const fixedHour = hour === 24 ? 0 : hour @@ -68,13 +70,16 @@ export default (o, c, d) => { // The offset has changed, but the we don't adjust the time return [localTS - (Math.min(o2, o3) * 60 * 1000), Math.max(o2, o3)] } + const proto = c.prototype - proto.tz = function (timezone) { + + proto.tz = function (timezone = defaultTimezone) { const target = this.toDate().toLocaleString('en-US', { timeZone: timezone }) const diff = Math.round((this.toDate() - new Date(target)) / 1000 / 60) return d(target).utcOffset(localUtcOffset - diff, true).$set(ms, this.$ms) } - d.tz = function (input, timezone) { + + d.tz = function (input, timezone = defaultTimezone) { const previousOffset = tzOffset(+d(), timezone) let localTs if (typeof input !== 'string') { @@ -90,4 +95,8 @@ export default (o, c, d) => { d.tz.guess = function () { return Intl.DateTimeFormat().resolvedOptions().timeZone } + + d.tz.setDefault = function (timezone) { + defaultTimezone = timezone + } } diff --git a/test/plugin/timezone.test.js b/test/plugin/timezone.test.js index ca807aa4f..343bb6265 100644 --- a/test/plugin/timezone.test.js +++ b/test/plugin/timezone.test.js @@ -203,3 +203,52 @@ describe('DST, a time that never existed Fall Back', () => { }) }) }) + +describe('set Default', () => { + it('default timezone', () => { + const dateStr = '2014-06-01 12:00' + dayjs.tz.setDefault(NY) + const newYork = dayjs.tz(dateStr) + expect(newYork.format()).toBe('2014-06-01T12:00:00-04:00') + expect(newYork.utcOffset()).toBe(-240) + expect(newYork.valueOf()).toBe(1401638400000) + + expect(dayjs(dateStr).tz().format()).toBe(dayjs(dateStr).tz(NY).format()) + }) + + it('empty timezone means local timezone', () => { + const LOCAL_TZ = dayjs.tz.guess() + const dateStr = '2014-06-01 12:00' + dayjs.tz.setDefault() + expect(dayjs(dateStr).tz().valueOf()).toBe(dayjs(dateStr).tz(LOCAL_TZ).valueOf()) + expect(dayjs.tz(dateStr).valueOf()).toBe(dayjs.tz(dateStr, LOCAL_TZ).valueOf()) + }) + + it('change default timezone', () => { + dayjs.tz.setDefault(NY) + const newYork = dayjs.tz('2014-06-01 12:00') + expect(newYork.utcOffset()).toBe(-240) + + dayjs.tz.setDefault(TOKYO) + const tokyo = dayjs.tz('2014-06-01 12:00') + expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00') + expect(tokyo.format('Z')).toBe('+09:00') + expect(tokyo.valueOf()).toBe(1401591600000) + }) + + it('override default timezone in proto.tz', () => { + dayjs.tz.setDefault(NY) + const tokyo = dayjs.tz('2014-06-01 12:00', TOKYO) + expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00') + expect(tokyo.format('Z')).toBe('+09:00') + expect(tokyo.valueOf()).toBe(1401591600000) + }) + + it('override default timezone in d.tz', () => { + dayjs.tz.setDefault(NY) + const tokyo = dayjs.tz('2014-06-01 12:00', TOKYO) + expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00') + expect(tokyo.format('Z')).toBe('+09:00') + expect(tokyo.valueOf()).toBe(1401591600000) + }) +})