Skip to content

Commit 0c2050a

Browse files
authored
fix: Timezone plugin set default timezone (#1033)
1 parent de31592 commit 0c2050a

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/plugin/timezone/index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const typeToPos = {
1010
const ms = 'ms'
1111

1212
export default (o, c, d) => {
13+
let defaultTimezone
14+
1315
const localUtcOffset = d().utcOffset()
1416
const tzOffset = (timestamp, timezone) => {
1517
const date = new Date(timestamp)
@@ -33,7 +35,7 @@ export default (o, c, d) => {
3335
filled[pos] = parseInt(value, 10)
3436
}
3537
}
36-
// Workaround for the same performance in different node version
38+
// Workaround for the same behavior in different node version
3739
// https://github.com/nodejs/node/issues/33027
3840
const hour = filled[3]
3941
const fixedHour = hour === 24 ? 0 : hour
@@ -68,13 +70,16 @@ export default (o, c, d) => {
6870
// The offset has changed, but the we don't adjust the time
6971
return [localTS - (Math.min(o2, o3) * 60 * 1000), Math.max(o2, o3)]
7072
}
73+
7174
const proto = c.prototype
72-
proto.tz = function (timezone) {
75+
76+
proto.tz = function (timezone = defaultTimezone) {
7377
const target = this.toDate().toLocaleString('en-US', { timeZone: timezone })
7478
const diff = Math.round((this.toDate() - new Date(target)) / 1000 / 60)
7579
return d(target).utcOffset(localUtcOffset - diff, true).$set(ms, this.$ms)
7680
}
77-
d.tz = function (input, timezone) {
81+
82+
d.tz = function (input, timezone = defaultTimezone) {
7883
const previousOffset = tzOffset(+d(), timezone)
7984
let localTs
8085
if (typeof input !== 'string') {
@@ -90,4 +95,8 @@ export default (o, c, d) => {
9095
d.tz.guess = function () {
9196
return Intl.DateTimeFormat().resolvedOptions().timeZone
9297
}
98+
99+
d.tz.setDefault = function (timezone) {
100+
defaultTimezone = timezone
101+
}
93102
}

test/plugin/timezone.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,52 @@ describe('DST, a time that never existed Fall Back', () => {
203203
})
204204
})
205205
})
206+
207+
describe('set Default', () => {
208+
it('default timezone', () => {
209+
const dateStr = '2014-06-01 12:00'
210+
dayjs.tz.setDefault(NY)
211+
const newYork = dayjs.tz(dateStr)
212+
expect(newYork.format()).toBe('2014-06-01T12:00:00-04:00')
213+
expect(newYork.utcOffset()).toBe(-240)
214+
expect(newYork.valueOf()).toBe(1401638400000)
215+
216+
expect(dayjs(dateStr).tz().format()).toBe(dayjs(dateStr).tz(NY).format())
217+
})
218+
219+
it('empty timezone means local timezone', () => {
220+
const LOCAL_TZ = dayjs.tz.guess()
221+
const dateStr = '2014-06-01 12:00'
222+
dayjs.tz.setDefault()
223+
expect(dayjs(dateStr).tz().valueOf()).toBe(dayjs(dateStr).tz(LOCAL_TZ).valueOf())
224+
expect(dayjs.tz(dateStr).valueOf()).toBe(dayjs.tz(dateStr, LOCAL_TZ).valueOf())
225+
})
226+
227+
it('change default timezone', () => {
228+
dayjs.tz.setDefault(NY)
229+
const newYork = dayjs.tz('2014-06-01 12:00')
230+
expect(newYork.utcOffset()).toBe(-240)
231+
232+
dayjs.tz.setDefault(TOKYO)
233+
const tokyo = dayjs.tz('2014-06-01 12:00')
234+
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
235+
expect(tokyo.format('Z')).toBe('+09:00')
236+
expect(tokyo.valueOf()).toBe(1401591600000)
237+
})
238+
239+
it('override default timezone in proto.tz', () => {
240+
dayjs.tz.setDefault(NY)
241+
const tokyo = dayjs.tz('2014-06-01 12:00', TOKYO)
242+
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
243+
expect(tokyo.format('Z')).toBe('+09:00')
244+
expect(tokyo.valueOf()).toBe(1401591600000)
245+
})
246+
247+
it('override default timezone in d.tz', () => {
248+
dayjs.tz.setDefault(NY)
249+
const tokyo = dayjs.tz('2014-06-01 12:00', TOKYO)
250+
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
251+
expect(tokyo.format('Z')).toBe('+09:00')
252+
expect(tokyo.valueOf()).toBe(1401591600000)
253+
})
254+
})

0 commit comments

Comments
 (0)