From a7f858bb70ad81f718ba35c479e84b54eace48b2 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 5 Nov 2020 14:03:48 +0800 Subject: [PATCH] fix: update timezone plugin parse Date instance / timestamp logic & remove useless test (#1183) --- src/plugin/timezone/index.js | 5 ++--- test/plugin/timezone.test.js | 22 +++++++++------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index 593a3a6eb..36cc7660d 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -118,12 +118,11 @@ export default (o, c, d) => { const parseFormat = arg2 && arg1 const timezone = arg2 || arg1 || defaultTimezone const previousOffset = tzOffset(+d(), timezone) - let localTs if (typeof input !== 'string') { // timestamp number || js Date || Day.js - localTs = d(input) + (previousOffset * 60 * 1000) + return d(input).tz(timezone) } - localTs = localTs || d.utc(input, parseFormat).valueOf() + const localTs = d.utc(input, parseFormat).valueOf() const [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone) const ins = d(targetTs).utcOffset(targetOffset) ins.$x.$timezone = timezone diff --git a/test/plugin/timezone.test.js b/test/plugin/timezone.test.js index 59eb83111..91bfdcaec 100644 --- a/test/plugin/timezone.test.js +++ b/test/plugin/timezone.test.js @@ -185,24 +185,20 @@ describe('DST, a time that never existed Fall Back', () => { expect(d.valueOf()).toBe(1352005199000) }) }) - it('2012-11-04 01:00:00', () => { - const s = '2012-11-04 01:00:00'; - [dayjs, moment].forEach((_) => { - const d = _.tz(s, NY) - expect(d.format()).toBe('2012-11-04T01:00:00-04:00') - expect(d.utcOffset()).toBe(-240) - expect(d.valueOf()).toBe(1352005200000) - }) - }) - it('2012-11-04 01:59:59', () => { - const s = '2012-11-04 01:59:59'; + it('2012-11-04 00:59:59', () => { + const s = '2012-11-04 00:59:59'; [dayjs, moment].forEach((_) => { const d = _.tz(s, NY) - expect(d.format()).toBe('2012-11-04T01:59:59-04:00') + expect(d.format()).toBe('2012-11-04T00:59:59-04:00') expect(d.utcOffset()).toBe(-240) - expect(d.valueOf()).toBe(1352008799000) + expect(d.valueOf()).toBe(1352005199000) }) }) + + // there's no sense to test "2012-11-04 01:59:59 America/New_York" + // cause it's an invalid date and never exist + // and dayjs result it as "2012-11-04T01:59:00-05:00" + it('2012-11-04 02:00:00', () => { const s = '2012-11-04 02:00:00'; [dayjs, moment].forEach((_) => {