Skip to content

Commit

Permalink
fix: Fix UTC plugin add day DST bug (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed May 7, 2019
1 parent 434f774 commit 86cd839
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"types": "index.d.ts",
"module": "dayjs.min.js",
"scripts": {
"test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && jest",
"test-tz": "jest test/timezone.test --coverage=false",
"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",
"test-tz": "date && jest test/timezone.test --coverage=false",
"lint": "./node_modules/.bin/eslint src/* test/* build/*",
"prettier": "prettier --write \"docs/**/*.md\"",
"babel": "cross-env BABEL_ENV=build babel src --out-dir esm --copy-files && node build/esm",
Expand Down
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ class Dayjs {
number = Number(number) // eslint-disable-line no-param-reassign
const unit = Utils.p(units)
const instanceFactorySet = (n) => {
const date = new Date(this.$d)
date.setDate(date.getDate() + Math.round(n * number))
return Utils.w(date, this)
const d = dayjs(this)
return Utils.w(d.date(d.date() + Math.round(n * number)), this)
}
if (unit === C.M) {
return this.set(C.M, this.$M + number)
Expand Down
6 changes: 6 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ it('Format Minute m mm', () => {
})

it('Format Second s ss SSS', () => {
// Todo: debug CI error
console.log(Date.now()) // eslint-disable-line no-console
console.log((new Date()).toString()) // eslint-disable-line no-console
console.log((new Date()).toLocaleString()) // eslint-disable-line no-console
console.log((new Date()).getTimezoneOffset()) // eslint-disable-line no-console
// debug
expect(dayjs().format('s')).toBe(moment().format('s'))
expect(dayjs().format('ss')).toBe(moment().format('ss'))
expect(dayjs().format('SSS')).toBe(moment().format('SSS'))
Expand Down
18 changes: 18 additions & 0 deletions test/timezone.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import utc from '../src/plugin/utc'

dayjs.extend(utc)

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -36,3 +39,18 @@ it('Diff (DST)', () => {
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
})
})

it('UTC add day in DST', () => {
const testDate = '2019-03-10'
const dayTest = dayjs(testDate)
.utc()
.startOf('day')
const momentTest = moment(testDate)
.utc()
.startOf('day')
expect(dayTest.add(1, 'day').format())
.toBe(momentTest.clone().add(1, 'day').format())
expect(dayTest.add(2, 'day').format())
.toBe(momentTest.clone().add(2, 'day').format())
})

0 comments on commit 86cd839

Please sign in to comment.