diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index 4e494d31c..b9703b2de 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -59,7 +59,12 @@ export default (o, c, d) => { } d.tz = function (input, timezone) { const previousOffset = tzOffset(+d(), timezone) - const localTs = d.utc(input).valueOf() + let localTs + if (typeof input !== 'string') { + // timestamp number || js Date || Day.js + localTs = d(input) + (previousOffset * 60 * 1000) + } + localTs = localTs || d.utc(input).valueOf() const [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone) const ins = d(targetTs).utcOffset(targetOffset) return ins diff --git a/test/plugin/timezone.test.js b/test/plugin/timezone.test.js index a8c02ed32..21bb08f9d 100644 --- a/test/plugin/timezone.test.js +++ b/test/plugin/timezone.test.js @@ -16,6 +16,8 @@ afterEach(() => { }) const NY = 'America/New_York' +const VAN = 'America/Vancouver' +const TOKYO = 'Asia/Tokyo' describe('Guess', () => { it('return string', () => { @@ -36,6 +38,19 @@ describe('Parse', () => { expect(newYork.valueOf()).toBe(MnewYork.valueOf()) }) + it('parse timestamp, js Date, Day.js object', () => { + const d = new Date('2020-08-07T12:00-07:00') + const result = '2020-08-07T12:00:00-07:00' + const TjsDate = dayjs.tz(d, VAN) + const Tdayjs = dayjs.tz(dayjs(d), VAN) + const Timestamp = dayjs.tz(d.getTime(), VAN) + const Tmoment = moment.tz(d, VAN) + expect(TjsDate.format()).toBe(result) + expect(Tdayjs.format()).toBe(result) + expect(Timestamp.format()).toBe(result) + expect(Tmoment.format()).toBe(result) + }) + it('parse and convert between timezones', () => { const newYork = dayjs.tz('2014-06-01 12:00', NY) expect(newYork.tz('America/Los_Angeles').format()).toBe('2014-06-01T09:00:00-07:00') @@ -71,8 +86,8 @@ describe('Convert', () => { expect(dec.tz('America/Los_Angeles').format('ha')).toBe('4am') expect(jun.tz(NY).format('ha')).toBe('8am') expect(dec.tz(NY).format('ha')).toBe('7am') - expect(jun.tz('Asia/Tokyo').format('ha')).toBe('9pm') - expect(dec.tz('Asia/Tokyo').format('ha')).toBe('9pm') + expect(jun.tz(TOKYO).format('ha')).toBe('9pm') + expect(dec.tz(TOKYO).format('ha')).toBe('9pm') expect(jun.tz('Australia/Sydney').format('ha')).toBe('10pm') expect(dec.tz('Australia/Sydney').format('ha')).toBe('11pm') }) @@ -80,8 +95,8 @@ describe('Convert', () => { it('format Z', () => { [dayjs, moment].forEach((_) => { - const losAngeles = _('2020-08-06T03:48:10.258Z').tz('Asia/Tokyo') - expect(losAngeles.format('Z')).toBe('+09:00') + const t = _('2020-08-06T03:48:10.258Z').tz(TOKYO) + expect(t.format('Z')).toBe('+09:00') }) }) }) diff --git a/types/plugin/timezone.d.ts b/types/plugin/timezone.d.ts index b2893be76..dce3b8b06 100644 --- a/types/plugin/timezone.d.ts +++ b/types/plugin/timezone.d.ts @@ -1,16 +1,16 @@ -import { PluginFunc } from 'dayjs' +import { PluginFunc, ConfigType } from 'dayjs' declare const plugin: PluginFunc export = plugin -interface DayjsTimezone { - (): Dayjs - guess(): string -} - declare module 'dayjs' { interface Dayjs { - tz(): Dayjs + tz(timezone: string): Dayjs + } + + interface DayjsTimezone { + (date: ConfigType, timezone: string): Dayjs + guess(): string } const tz: DayjsTimezone