Skip to content

Commit

Permalink
fix: fix UTC plugin .valueOf not taking DST into account (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
aloisklink committed May 3, 2022
1 parent 24218bd commit 27d1c50
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
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

0 comments on commit 27d1c50

Please sign in to comment.