Skip to content

Commit

Permalink
fix: Timezone plugin set default timezone (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
csvwolf authored Sep 4, 2020
1 parent de31592 commit 0c2050a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const typeToPos = {
const ms = 'ms'

export default (o, c, d) => {
let defaultTimezone

const localUtcOffset = d().utcOffset()
const tzOffset = (timestamp, timezone) => {
const date = new Date(timestamp)
Expand All @@ -33,7 +35,7 @@ export default (o, c, d) => {
filled[pos] = parseInt(value, 10)
}
}
// Workaround for the same performance in different node version
// Workaround for the same behavior in different node version
// https://github.com/nodejs/node/issues/33027
const hour = filled[3]
const fixedHour = hour === 24 ? 0 : hour
Expand Down Expand Up @@ -68,13 +70,16 @@ export default (o, c, d) => {
// The offset has changed, but the we don't adjust the time
return [localTS - (Math.min(o2, o3) * 60 * 1000), Math.max(o2, o3)]
}

const proto = c.prototype
proto.tz = function (timezone) {

proto.tz = function (timezone = defaultTimezone) {
const target = this.toDate().toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((this.toDate() - new Date(target)) / 1000 / 60)
return d(target).utcOffset(localUtcOffset - diff, true).$set(ms, this.$ms)
}
d.tz = function (input, timezone) {

d.tz = function (input, timezone = defaultTimezone) {
const previousOffset = tzOffset(+d(), timezone)
let localTs
if (typeof input !== 'string') {
Expand All @@ -90,4 +95,8 @@ export default (o, c, d) => {
d.tz.guess = function () {
return Intl.DateTimeFormat().resolvedOptions().timeZone
}

d.tz.setDefault = function (timezone) {
defaultTimezone = timezone
}
}
49 changes: 49 additions & 0 deletions test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,52 @@ describe('DST, a time that never existed Fall Back', () => {
})
})
})

describe('set Default', () => {
it('default timezone', () => {
const dateStr = '2014-06-01 12:00'
dayjs.tz.setDefault(NY)
const newYork = dayjs.tz(dateStr)
expect(newYork.format()).toBe('2014-06-01T12:00:00-04:00')
expect(newYork.utcOffset()).toBe(-240)
expect(newYork.valueOf()).toBe(1401638400000)

expect(dayjs(dateStr).tz().format()).toBe(dayjs(dateStr).tz(NY).format())
})

it('empty timezone means local timezone', () => {
const LOCAL_TZ = dayjs.tz.guess()
const dateStr = '2014-06-01 12:00'
dayjs.tz.setDefault()
expect(dayjs(dateStr).tz().valueOf()).toBe(dayjs(dateStr).tz(LOCAL_TZ).valueOf())
expect(dayjs.tz(dateStr).valueOf()).toBe(dayjs.tz(dateStr, LOCAL_TZ).valueOf())
})

it('change default timezone', () => {
dayjs.tz.setDefault(NY)
const newYork = dayjs.tz('2014-06-01 12:00')
expect(newYork.utcOffset()).toBe(-240)

dayjs.tz.setDefault(TOKYO)
const tokyo = dayjs.tz('2014-06-01 12:00')
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
expect(tokyo.format('Z')).toBe('+09:00')
expect(tokyo.valueOf()).toBe(1401591600000)
})

it('override default timezone in proto.tz', () => {
dayjs.tz.setDefault(NY)
const tokyo = dayjs.tz('2014-06-01 12:00', TOKYO)
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
expect(tokyo.format('Z')).toBe('+09:00')
expect(tokyo.valueOf()).toBe(1401591600000)
})

it('override default timezone in d.tz', () => {
dayjs.tz.setDefault(NY)
const tokyo = dayjs.tz('2014-06-01 12:00', TOKYO)
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
expect(tokyo.format('Z')).toBe('+09:00')
expect(tokyo.valueOf()).toBe(1401591600000)
})
})

0 comments on commit 0c2050a

Please sign in to comment.