Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix Timezone plugin parsing js date, Day.js object, timestamp bug #994

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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