Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix Timezone plugin to preserve milliseconds while changing timezone #1003

Merged
merged 2 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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