Skip to content

Commit d148115

Browse files
committedOct 24, 2019
fix: Fix set utcOffset in utc mode
1 parent 26cfa63 commit d148115

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
 

‎src/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ const dayjs = (date, c, pl) => {
3939
}
4040

4141
const wrapper = (date, instance) =>
42-
dayjs(date, { locale: instance.$L, utc: instance.$u, $offset: instance.$offset })
42+
dayjs(date, {
43+
locale: instance.$L,
44+
utc: instance.$u,
45+
$offset: instance.$offset, // todo: refactor; do not use this.$offset this.$lo in you code
46+
$lo: instance.$lo // save local utcoffset; todo: refactor
47+
})
4348

4449
const Utils = U // for plugin use
4550
Utils.l = parseLocale

‎src/plugin/utc/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export default (option, Dayjs, dayjs) => {
2424
if (!this.$utils().u(cfg.$offset)) {
2525
this.$offset = cfg.$offset
2626
}
27+
if (!this.$utils().u(cfg.$lo)) {
28+
this.$lo = cfg.$lo
29+
}
2730
oldParse.call(this, cfg)
2831
}
2932

@@ -59,6 +62,7 @@ export default (option, Dayjs, dayjs) => {
5962
const offset = Math.abs(input) <= 16 ? input * 60 : input
6063
const newD = this.add(offset + (this.$u ? 0 : localOffset), MIN)
6164
newD.$offset = offset
65+
newD.$lo = this.$u ? 0 : localOffset
6266
newD.$u = input === 0 // UTC mode
6367
return newD
6468
}
@@ -72,7 +76,7 @@ export default (option, Dayjs, dayjs) => {
7276

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

‎test/plugin/utc-utcOffset.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,16 @@ test('change hours when changing the utc offset in UTC mode', () => {
7575
expect(d.utcOffset(30).format('HH:mm')).toBe('07:01')
7676
expect(d.utcOffset(-1380).format('HH:mm')).toBe('07:31')
7777
})
78+
79+
test('utc costrustor', () => {
80+
const d = new Date(2019, 8, 11, 0, 0, 0).getTime()
81+
expect(moment(d).utc().utcOffset(480).valueOf())
82+
.toBe(dayjs(d).utc().utcOffset(480).valueOf())
83+
84+
expect(moment(d).utc().local()
85+
.utcOffset(480)
86+
.valueOf())
87+
.toBe(dayjs(d).utc().local()
88+
.utcOffset(480)
89+
.valueOf())
90+
})

0 commit comments

Comments
 (0)
Please sign in to comment.