Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve utc plugin when 0 offset and keepLocalTime (#1778) #2617

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading