Skip to content

Commit

Permalink
fix: Fix Timezone plugin parsing js date, Day.js object, timestamp bu…
Browse files Browse the repository at this point in the history
…g && update type file (#994)

fix #992, fix #989
  • Loading branch information
iamkun authored Aug 8, 2020
1 parent f2e5f32 commit 22f3d49
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ afterEach(() => {
})

const NY = 'America/New_York'
const VAN = 'America/Vancouver'
const TOKYO = 'Asia/Tokyo'

describe('Guess', () => {
it('return string', () => {
Expand All @@ -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')
Expand Down Expand Up @@ -71,17 +86,17 @@ 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')
})
})

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')
})
})
})
Expand Down
14 changes: 7 additions & 7 deletions types/plugin/timezone.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 22f3d49

Please sign in to comment.