Skip to content

Commit

Permalink
fix: Fix UTC plugin wrong hour bug while adding month or year (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypresto authored Jul 20, 2020
1 parent b2642e2 commit 28ae070
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class Dayjs {
const date = this.clone().set(C.DATE, 1)
date.$d[name](arg)
date.init()
this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).toDate()
this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).$d
} else if (name) this.$d[name](arg)

this.init()
Expand Down
26 changes: 25 additions & 1 deletion test/plugin/utc-utcOffset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ it('clone', () => {

it('immutable', () => {
const d = dayjs()
const du = d.utcOffset(9)
const du = d.utcOffset(d.utcOffset() + 1)
expect(d.utcOffset()).not.toBe(du.utcOffset())
expect(d.format()).not.toBe(du.format())
})
Expand Down Expand Up @@ -76,6 +76,30 @@ test('change hours when changing the utc offset in UTC mode', () => {
expect(d.utcOffset(-1380).format('HH:mm')).toBe('07:31')
})

test('correctly set and add hours in offset mode', () => {
const d10 = dayjs('2000-01-30T06:31:00+10:00').utcOffset(10)
const dm8 = dayjs('2000-01-30T06:31:00-08:00').utcOffset(-8)

expect(d10.hour(5).hour()).toBe(5)
expect(d10.hour(5).add(1, 'hour').hour()).toBe(6)
expect(d10.hour(5).add(-10, 'hour').hour()).toBe(19)

expect(dm8.hour(5).hour()).toBe(5)
expect(dm8.hour(5).add(1, 'hour').hour()).toBe(6)
expect(dm8.hour(5).add(-10, 'hour').hour()).toBe(19)
})

test('keep hours when adding month in offset mode', () => {
const d10 = dayjs('2000-01-30T06:31:00+10:00').utcOffset(10)
const dm8 = dayjs('2000-01-30T06:31:00-08:00').utcOffset(-8)

expect(d10.add(1, 'month').hour()).toBe(6)
expect(dm8.add(1, 'month').hour()).toBe(6)

expect(d10.add(-2, 'month').hour()).toBe(6)
expect(dm8.add(-2, 'month').hour()).toBe(6)
})

test('utc costrustor', () => {
const d = new Date(2019, 8, 11, 0, 0, 0).getTime()
expect(moment(d).utc().utcOffset(480).valueOf())
Expand Down

0 comments on commit 28ae070

Please sign in to comment.