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: utcOffset when local in DST but date is not #1448

Merged
merged 3 commits into from
May 3, 2022
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
2 changes: 1 addition & 1 deletion src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default (option, Dayjs, dayjs) => {

proto.valueOf = function () {
const addedOffset = !this.$utils().u(this.$offset)
? this.$offset + (this.$x.$localOffset || (new Date()).getTimezoneOffset()) : 0
? this.$offset + (this.$x.$localOffset || this.$d.getTimezoneOffset()) : 0
return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE)
}

Expand Down
13 changes: 13 additions & 0 deletions test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const NY = 'America/New_York'
const VAN = 'America/Vancouver'
const DEN = 'America/Denver'
const TOKYO = 'Asia/Tokyo'
const PARIS = 'Europe/Paris'

describe('Guess', () => {
it('return string', () => {
Expand Down Expand Up @@ -212,6 +213,18 @@ describe('DST, a time that never existed Fall Back', () => {
})
})

it('DST valueOf', () => {
const day1 = '2021-11-17T09:45:00.000Z'
const d1 = dayjs.utc(day1).tz(PARIS)
const m1 = moment.tz(day1, PARIS)
expect(d1.valueOf()).toBe(m1.valueOf())

const day2 = '2021-05-17T09:45:00.000Z'
const d2 = dayjs.utc(day2).tz(PARIS)
const m2 = moment.tz(day2, PARIS)
expect(d2.valueOf()).toBe(m2.valueOf())
})

describe('set Default', () => {
it('default timezone', () => {
const dateStr = '2014-06-01 12:00'
Expand Down
18 changes: 13 additions & 5 deletions test/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,20 @@ it('UTC add day in DST', () => {
})

it('UTC and utcOffset', () => {
const test1 = 1331449199000 // 2012/3/11 14:59:59
expect(moment(test1).utcOffset(-300).format())
.toBe(dayjs(test1).utcOffset(-300).format())
const test1 = 1331449199000 // 2012/3/11 06:59:59 GMT+0000
expect(dayjs(test1).utcOffset(-300).format())
.toBe(moment(test1).utcOffset(-300).format())
const test2 = '2000-01-01T06:31:00Z'
expect(moment.utc(test2).utcOffset(-60).format())
.toBe(dayjs.utc(test2).utcOffset(-60).format())
expect(dayjs.utc(test2).utcOffset(-60).format())
.toBe(moment.utc(test2).utcOffset(-60).format())

// across DST, copied from utc.test.js#get utc offset with a number value
const time = '2021-02-28 19:40:10'
const hoursOffset = -8
const daysJS = dayjs(time).utc().utcOffset(hoursOffset * 60, true)
const momentJS = moment(time).utc(true).utcOffset(hoursOffset, true)

expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
})

it('UTC diff in DST', () => {
Expand Down