Skip to content

Commit

Permalink
fix: Fix Timezone plugin to preserve milliseconds while changing time…
Browse files Browse the repository at this point in the history
…zone (#1003)

* fix: fix Timezone plugin to preserve milliseconds while changing timezone

fix #1002
  • Loading branch information
iamkun authored Aug 14, 2020
1 parent 6e5ffce commit 5f446ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const typeToPos = {
second: 5
}

const ms = 'ms'

export default (o, c, d) => {
const localUtcOffset = d().utcOffset()
const tzOffset = (timestamp, timezone) => {
Expand Down Expand Up @@ -55,7 +57,7 @@ export default (o, c, d) => {
proto.tz = function (timezone) {
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)
return d(target).utcOffset(localUtcOffset - diff, true).$set(ms, this.$ms)
}
d.tz = function (input, timezone) {
const previousOffset = tzOffset(+d(), timezone)
Expand Down
8 changes: 8 additions & 0 deletions test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ describe('Parse', () => {
expect(newYork.tz('America/Los_Angeles').format()).toBe('2014-06-01T09:00:00-07:00')
expect(newYork.tz('Europe/London').format()).toBe('2014-06-01T17:00:00+01:00')
})

it('preserve milliseconds', () => {
const d = dayjs(1596735327399)
const oldMs = d.millisecond()
const dTz = d.tz('America/New_York')
const newMs = dTz.millisecond()
expect(oldMs).toEqual(newMs)
})
})

describe('Convert', () => {
Expand Down

0 comments on commit 5f446ed

Please sign in to comment.