Skip to content

Commit 71a3f93

Browse files
committed
fix: fix Timezone plugin parsing js date, Day.js object, timestamp bug && update type file
fix #992, fix #989
1 parent f2e5f32 commit 71a3f93

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

src/plugin/timezone/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ export default (o, c, d) => {
5959
}
6060
d.tz = function (input, timezone) {
6161
const previousOffset = tzOffset(+d(), timezone)
62-
const localTs = d.utc(input).valueOf()
62+
let localTs
63+
if (typeof input !== 'string') {
64+
// timestamp number || js Date || Day.js
65+
localTs = d(input) + (previousOffset * 60 * 1000)
66+
}
67+
localTs = localTs || d.utc(input).valueOf()
6368
const [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone)
6469
const ins = d(targetTs).utcOffset(targetOffset)
6570
return ins

test/plugin/timezone.test.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ afterEach(() => {
1616
})
1717

1818
const NY = 'America/New_York'
19+
const VAN = 'America/Vancouver'
20+
const TOKYO = 'Asia/Tokyo'
1921

2022
describe('Guess', () => {
2123
it('return string', () => {
@@ -36,6 +38,19 @@ describe('Parse', () => {
3638
expect(newYork.valueOf()).toBe(MnewYork.valueOf())
3739
})
3840

41+
it('parse timestamp, js Date, Day.js object', () => {
42+
const d = new Date('2020-08-07T12:00-07:00')
43+
const result = '2020-08-07T12:00:00-07:00'
44+
const TjsDate = dayjs.tz(d, VAN)
45+
const Tdayjs = dayjs.tz(dayjs(d), VAN)
46+
const Timestamp = dayjs.tz(d.getTime(), VAN)
47+
const Tmoment = moment.tz(d, VAN)
48+
expect(TjsDate.format()).toBe(result)
49+
expect(Tdayjs.format()).toBe(result)
50+
expect(Timestamp.format()).toBe(result)
51+
expect(Tmoment.format()).toBe(result)
52+
})
53+
3954
it('parse and convert between timezones', () => {
4055
const newYork = dayjs.tz('2014-06-01 12:00', NY)
4156
expect(newYork.tz('America/Los_Angeles').format()).toBe('2014-06-01T09:00:00-07:00')
@@ -71,17 +86,17 @@ describe('Convert', () => {
7186
expect(dec.tz('America/Los_Angeles').format('ha')).toBe('4am')
7287
expect(jun.tz(NY).format('ha')).toBe('8am')
7388
expect(dec.tz(NY).format('ha')).toBe('7am')
74-
expect(jun.tz('Asia/Tokyo').format('ha')).toBe('9pm')
75-
expect(dec.tz('Asia/Tokyo').format('ha')).toBe('9pm')
89+
expect(jun.tz(TOKYO).format('ha')).toBe('9pm')
90+
expect(dec.tz(TOKYO).format('ha')).toBe('9pm')
7691
expect(jun.tz('Australia/Sydney').format('ha')).toBe('10pm')
7792
expect(dec.tz('Australia/Sydney').format('ha')).toBe('11pm')
7893
})
7994
})
8095

8196
it('format Z', () => {
8297
[dayjs, moment].forEach((_) => {
83-
const losAngeles = _('2020-08-06T03:48:10.258Z').tz('Asia/Tokyo')
84-
expect(losAngeles.format('Z')).toBe('+09:00')
98+
const t = _('2020-08-06T03:48:10.258Z').tz(TOKYO)
99+
expect(t.format('Z')).toBe('+09:00')
85100
})
86101
})
87102
})

types/plugin/timezone.d.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { PluginFunc } from 'dayjs'
1+
import { PluginFunc, ConfigType } from 'dayjs'
22

33
declare const plugin: PluginFunc
44
export = plugin
55

6-
interface DayjsTimezone {
7-
(): Dayjs
8-
guess(): string
9-
}
10-
116
declare module 'dayjs' {
127
interface Dayjs {
13-
tz(): Dayjs
8+
tz(timezone: string): Dayjs
9+
}
10+
11+
interface DayjsTimezone {
12+
(date: ConfigType, timezone: string): Dayjs
13+
guess(): string
1414
}
1515

1616
const tz: DayjsTimezone

0 commit comments

Comments
 (0)