Skip to content

Commit

Permalink
fix: fix utc plugin diff bug in DST (#1171)
Browse files Browse the repository at this point in the history
fix #1097, fix #1021
  • Loading branch information
iamkun committed Oct 30, 2020
1 parent 9e8f8d9 commit f8da3fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export default (option, Dayjs, dayjs) => {
}
const oldDiff = proto.diff
proto.diff = function (input, units, float) {
if (this.$u === input.$u) {
return oldDiff.call(this, input, units, float)
}
const localThis = this.local()
const localInput = dayjs(input).local()
return oldDiff.call(localThis, localInput, units, float)
Expand Down
8 changes: 8 additions & 0 deletions test/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ it('UTC and utcOffset', () => {
expect(moment.utc(test2).utcOffset(-60).format())
.toBe(dayjs.utc(test2).utcOffset(-60).format())
})

it('UTC diff in DST', () => {
// DST till 2020-10-25
const day1 = dayjs.utc('20201023') // in DST
const day2 = dayjs.utc('20201026')
expect(day1.diff(day2, 'd'))
.toBe(-3)
})

2 comments on commit f8da3fe

@saharalon
Copy link

@saharalon saharalon commented on f8da3fe Nov 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamkun
This fix throws an error when passing undefined to diff() first param.
i.e: dayjs().diff(undefined, 'seconds')
which has worked previously

@iamkun
Copy link
Owner Author

@iamkun iamkun commented on f8da3fe Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saharalon I'll fix it soon. #1187

Please sign in to comment.