Skip to content

Commit

Permalink
fix: Fix UTC plugin bug while comparing an utc instance to a local one (
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Aug 8, 2020
1 parent 22f3d49 commit 747c0fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ export default (option, Dayjs, dayjs) => {
}
return oldToDate.call(this)
}
const oldDiff = proto.diff
proto.diff = function (input, units, float) {
const localThis = this.local()
const localInput = dayjs(input).local()
return oldDiff.call(localThis, localInput, units, float)
}
}
23 changes: 23 additions & 0 deletions test/plugin/utc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,26 @@ describe('UTC Offset', () => {
expect(dayjs().utc().utcOffset()).toBe(moment().utc().utcOffset())
})
})

describe('Diff', () => {
const d1 = '2021-06-07'
const d2 = '2021-06-06'
it('utc.diff(utc)', () => {
[dayjs, moment].forEach((_) => {
expect(_.utc(d1).diff(_.utc(d2), 'days')).toBe(1)
expect(_.utc(d1).diff(_.utc(d2), 'm')).toBe(1440)
})
})
it('local.diff(utc)', () => {
expect(dayjs(d1).diff(dayjs.utc(d2), 'days'))
.toBe(moment(d1).diff(moment.utc(d2), 'days'))
expect(dayjs(d1).diff(dayjs.utc(d2), 'm'))
.toBe(moment(d1).diff(moment.utc(d2), 'm'))
})
it('utc.diff(local)', () => {
expect(dayjs.utc(d1).diff(d2, 'days'))
.toBe(moment.utc(d1).diff(d2, 'days'))
expect(dayjs.utc(d1).diff(d2, 'm'))
.toBe(moment.utc(d1).diff(d2, 'm'))
})
})

0 comments on commit 747c0fb

Please sign in to comment.