Skip to content

Commit 86cd839

Browse files
authored
fix: Fix UTC plugin add day DST bug (#590)
1 parent 434f774 commit 86cd839

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"types": "index.d.ts",
77
"module": "dayjs.min.js",
88
"scripts": {
9-
"test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && jest",
10-
"test-tz": "jest test/timezone.test --coverage=false",
9+
"test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && TZ=America/Whitehorse npm run test-tz && npm run test-tz && jest",
10+
"test-tz": "date && jest test/timezone.test --coverage=false",
1111
"lint": "./node_modules/.bin/eslint src/* test/* build/*",
1212
"prettier": "prettier --write \"docs/**/*.md\"",
1313
"babel": "cross-env BABEL_ENV=build babel src --out-dir esm --copy-files && node build/esm",

src/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,8 @@ class Dayjs {
242242
number = Number(number) // eslint-disable-line no-param-reassign
243243
const unit = Utils.p(units)
244244
const instanceFactorySet = (n) => {
245-
const date = new Date(this.$d)
246-
date.setDate(date.getDate() + Math.round(n * number))
247-
return Utils.w(date, this)
245+
const d = dayjs(this)
246+
return Utils.w(d.date(d.date() + Math.round(n * number)), this)
248247
}
249248
if (unit === C.M) {
250249
return this.set(C.M, this.$M + number)

test/display.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ it('Format Minute m mm', () => {
9898
})
9999

100100
it('Format Second s ss SSS', () => {
101+
// Todo: debug CI error
102+
console.log(Date.now()) // eslint-disable-line no-console
103+
console.log((new Date()).toString()) // eslint-disable-line no-console
104+
console.log((new Date()).toLocaleString()) // eslint-disable-line no-console
105+
console.log((new Date()).getTimezoneOffset()) // eslint-disable-line no-console
106+
// debug
101107
expect(dayjs().format('s')).toBe(moment().format('s'))
102108
expect(dayjs().format('ss')).toBe(moment().format('ss'))
103109
expect(dayjs().format('SSS')).toBe(moment().format('SSS'))

test/timezone.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import moment from 'moment'
22
import MockDate from 'mockdate'
33
import dayjs from '../src'
4+
import utc from '../src/plugin/utc'
5+
6+
dayjs.extend(utc)
47

58
beforeEach(() => {
69
MockDate.set(new Date())
@@ -36,3 +39,18 @@ it('Diff (DST)', () => {
3639
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
3740
})
3841
})
42+
43+
it('UTC add day in DST', () => {
44+
const testDate = '2019-03-10'
45+
const dayTest = dayjs(testDate)
46+
.utc()
47+
.startOf('day')
48+
const momentTest = moment(testDate)
49+
.utc()
50+
.startOf('day')
51+
expect(dayTest.add(1, 'day').format())
52+
.toBe(momentTest.clone().add(1, 'day').format())
53+
expect(dayTest.add(2, 'day').format())
54+
.toBe(momentTest.clone().add(2, 'day').format())
55+
})
56+

0 commit comments

Comments
 (0)