Skip to content

Commit

Permalink
fix: Fix set utcOffset in utc mode
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Oct 24, 2019
1 parent 26cfa63 commit d148115
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ const dayjs = (date, c, pl) => {
}

const wrapper = (date, instance) =>
dayjs(date, { locale: instance.$L, utc: instance.$u, $offset: instance.$offset })
dayjs(date, {
locale: instance.$L,
utc: instance.$u,
$offset: instance.$offset, // todo: refactor; do not use this.$offset this.$lo in you code
$lo: instance.$lo // save local utcoffset; todo: refactor
})

const Utils = U // for plugin use
Utils.l = parseLocale
Expand Down
6 changes: 5 additions & 1 deletion src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default (option, Dayjs, dayjs) => {
if (!this.$utils().u(cfg.$offset)) {
this.$offset = cfg.$offset
}
if (!this.$utils().u(cfg.$lo)) {
this.$lo = cfg.$lo
}
oldParse.call(this, cfg)
}

Expand Down Expand Up @@ -59,6 +62,7 @@ export default (option, Dayjs, dayjs) => {
const offset = Math.abs(input) <= 16 ? input * 60 : input
const newD = this.add(offset + (this.$u ? 0 : localOffset), MIN)
newD.$offset = offset
newD.$lo = this.$u ? 0 : localOffset
newD.$u = input === 0 // UTC mode
return newD
}
Expand All @@ -72,7 +76,7 @@ export default (option, Dayjs, dayjs) => {

proto.valueOf = function () {
const addedOffset = !this.$utils().u(this.$offset)
? this.$offset + localOffset : 0
? this.$offset + this.$lo : 0
return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE)
}

Expand Down
13 changes: 13 additions & 0 deletions test/plugin/utc-utcOffset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ test('change hours when changing the utc offset in UTC mode', () => {
expect(d.utcOffset(30).format('HH:mm')).toBe('07:01')
expect(d.utcOffset(-1380).format('HH:mm')).toBe('07:31')
})

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

expect(moment(d).utc().local()
.utcOffset(480)
.valueOf())
.toBe(dayjs(d).utc().local()
.utcOffset(480)
.valueOf())
})

0 comments on commit d148115

Please sign in to comment.