Skip to content

Commit

Permalink
feat: timezone api - tz.setDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
csvwolf committed Sep 4, 2020
1 parent de31592 commit c28e352
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
17 changes: 13 additions & 4 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,26 +70,33 @@ 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) {
const target = this.toDate().toLocaleString('en-US', { timeZone: timezone })
const target = this.toDate().toLocaleString('en-US', { timeZone: timezone || defaultTimezone })
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) {
const previousOffset = tzOffset(+d(), timezone)
const previousOffset = tzOffset(+d(), timezone || defaultTimezone)
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 [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone || defaultTimezone)
const ins = d(targetTs).utcOffset(targetOffset)
return ins
}

d.tz.guess = function () {
return Intl.DateTimeFormat().resolvedOptions().timeZone
}

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

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

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())
})

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 c28e352

Please sign in to comment.