From e48bebdc6755b488bf07f5ba3f21c2c06cd49690 Mon Sep 17 00:00:00 2001 From: Jumaru Date: Fri, 3 Dec 2021 18:38:14 -0500 Subject: [PATCH] Timezone issue --- src/plugin/timezone/index.js | 13 ++++++++++++- src/plugin/utc/index.js | 3 +++ test/timezone.test.js | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index c8f8a4675..6ef0d5d82 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -92,6 +92,17 @@ export default (o, c, d) => { const proto = c.prototype + const oldAdd = proto.add + proto.add = function (number, units) { + if (!this.$x || !this.$x.$timezone) { + return oldAdd.call(this, number, units) + } + + const withoutTz = d(this.valueOf()) + const addWithoutTz = oldAdd.call(withoutTz, number, units) + return addWithoutTz.tz(this.$x.$timezone) + } + proto.tz = function (timezone = defaultTimezone, keepLocalTime) { const oldOffset = this.utcOffset() const date = this.toDate() @@ -99,11 +110,11 @@ export default (o, c, d) => { 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) + ins.$x.$timezone = timezone if (keepLocalTime) { const newOffset = ins.utcOffset() ins = ins.add(oldOffset - newOffset, MIN) } - ins.$x.$timezone = timezone return ins } diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 10ff1ec9a..afe8971d4 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -89,8 +89,11 @@ export default (option, Dayjs, dayjs) => { const offset = Math.abs(input) <= 16 ? input * 60 : input let ins = this if (keepLocalTime) { + const localTimezoneOffset = this.$u + ? this.toDate().getTimezoneOffset() : -1 * this.utcOffset() ins.$offset = offset ins.$u = input === 0 + ins.$x.$localOffset = localTimezoneOffset return ins } if (input !== 0) { diff --git a/test/timezone.test.js b/test/timezone.test.js index 58e83d225..984ebd0d1 100644 --- a/test/timezone.test.js +++ b/test/timezone.test.js @@ -15,6 +15,15 @@ afterEach(() => { MockDate.reset() }) +it('Parse timezones', () => { + expect(dayjs('2021-07-07').tz('Europe/London').startOf('week').format('HH:mm')).toBe('00:00') + expect(dayjs('2021-12-12').tz('Europe/London').startOf('week').format('HH:mm')).toBe('00:00') + + expect(dayjs('2013-11-18 11:00').tz('Europe/London').add(-300, 'minute').valueOf()).toBe(dayjs('2013-11-18 11:00').add(-300, 'minute').valueOf()) + expect(dayjs('2013-11-18 11:00').tz('Europe/London').format('HH:mm')).toBe('16:00') + expect(dayjs('2013-11-18 11:00').tz('Europe/London').add(-300, 'minute').format('HH:mm')).toBe('11:00') +}) + it('Add Time days (DST)', () => { // change timezone before running test // New Zealand (-720)