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: fix DST bug in utc plugin #1053

Merged
merged 3 commits into from
Sep 23, 2020
Merged
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
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as C from './constant'
import U from './utils'
import en from './locale/en'
import U from './utils'

let L = 'en' // global locale
const Ls = {} // global loaded locale
Expand Down Expand Up @@ -43,6 +43,7 @@ const wrapper = (date, instance) =>
dayjs(date, {
locale: instance.$L,
utc: instance.$u,
x: instance.$x,
$offset: instance.$offset // todo: refactor; do not use this.$offset in you code
})

Expand Down Expand Up @@ -81,6 +82,7 @@ class Dayjs {

parse(cfg) {
this.$d = parseDate(cfg)
this.$x = cfg.x || {}
this.init()
}

Expand Down
8 changes: 5 additions & 3 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MILLISECONDS_A_MINUTE, MIN } from '../../constant'

export default (option, Dayjs, dayjs) => {
const localOffset = (new Date()).getTimezoneOffset()
const proto = Dayjs.prototype
dayjs.utc = function (date) {
const cfg = { date, utc: true, args: arguments } // eslint-disable-line prefer-rest-params
Expand Down Expand Up @@ -64,8 +63,11 @@ export default (option, Dayjs, dayjs) => {
return ins
}
if (input !== 0) {
ins = this.local().add(offset + localOffset, MIN)
const localTimezoneOffset = this.$u
? this.toDate().getTimezoneOffset() : -1 * this.utcOffset()
ins = this.local().add(offset + localTimezoneOffset, MIN)
ins.$offset = offset
ins.$x.$localOffset = localTimezoneOffset
} else {
ins = this.utc()
}
Expand All @@ -81,7 +83,7 @@ export default (option, Dayjs, dayjs) => {

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

Expand Down
10 changes: 9 additions & 1 deletion test/timezone.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import moment from 'moment'
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../src'
import utc from '../src/plugin/utc'

Expand Down Expand Up @@ -54,3 +54,11 @@ it('UTC add day in DST', () => {
.toBe(momentTest.clone().add(2, 'day').format())
})

it('UTC and utcOffset', () => {
const test1 = 1331449199000 // 2012/3/11 14:59:59
expect(moment(test1).utcOffset(-300).format())
.toBe(dayjs(test1).utcOffset(-300).format())
const test2 = '2000-01-01T06:31:00Z'
expect(moment.utc(test2).utcOffset(-60).format())
.toBe(dayjs.utc(test2).utcOffset(-60).format())
})