Skip to content

Commit

Permalink
fix: improve utc plugin when 0 offset and keepLocalTime (#1778)
Browse files Browse the repository at this point in the history
  • Loading branch information
chs97 committed Oct 15, 2024
1 parent e720cfe commit e553ece
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,16 @@ export default (option, Dayjs, dayjs) => {
const offset = Math.abs(input) <= 16 ? input * 60 : input
let ins = this
if (keepLocalTime) {
const isUTCBefore = !!ins.$u
ins.$offset = offset
ins.$u = input === 0
if (isUTCBefore) {
return ins
}
if (ins.$u && offset === 0) {
// notUTC to UTC and keep localTime , should offset
return ins.add(oldUtcOffset.call(ins), MIN)
}
return ins
}
if (input !== 0) {
Expand Down
15 changes: 15 additions & 0 deletions test/plugin/utc-utcOffset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ afterEach(() => {
MockDate.reset()
})

it('utcOffset return clone object', () => {
const original = dayjs()
const clone = original.utcOffset(0, true)
expect(original !== clone).toBeTruthy()
})

it('add minute after 0 utcOffset', () => {
const du = dayjs().add(1, 'minute').utcOffset(0, true)
const mu = moment().add(1, 'minute').utcOffset(0, true)
const duu = dayjs().utcOffset(0, true).add(1, 'minute')
const muu = moment().utcOffset(0, true).add(1, 'minute')
expect(duu.format()).toEqual(mu.format())
expect(du.format()).toEqual(muu.format())
})

it('Set utcOffset -> Get utcOffset', () => {
expect(dayjs().utcOffset(540).utcOffset()).toBe(moment().utcOffset(540).utcOffset())
expect(dayjs().utcOffset(540).format()).toBe(moment().utcOffset(540).format())
Expand Down

0 comments on commit e553ece

Please sign in to comment.