Skip to content

Commit

Permalink
fix: Retain UTC mode when constructing a new instance from another in…
Browse files Browse the repository at this point in the history
…stance in UTC mode
  • Loading branch information
prantlf committed Sep 27, 2018
1 parent 16581d7 commit beb80fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const parseLocale = (preset, object, isLocal) => {

const dayjs = (date, c) => {
if (isDayjs(date)) {
return date.clone()
if (c) {
date = date.toDate()
} else {
return date.clone()
}
}
const cfg = c || {}
cfg.date = date
Expand Down
22 changes: 22 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ it('Does not apply the UTC mode by default', () => {
expect(instance.minute()).toEqual(34)
})

it('Creates an UTC instance from another instance', () => {
const source = dayjs('2018-09-06')
const instance = dayjs(source, { utc: true })
expect(instance.$u).toBeTruthy()
expect(instance.hour()).toEqual(source.toDate().getUTCHours())
expect(instance.minute()).toEqual(source.toDate().getUTCMinutes())
})

it('Creating a new instance from another instance retains the UTC mode', () => {
const source = dayjs('2018-09-06', { utc: true })
const instance = dayjs(source)
expect(instance.$u).toBeTruthy()
expect(instance.hour()).toEqual(source.hour())
expect(instance.minute()).toEqual(source.minute())
})

it('Clone not affect each other', () => {
const base = dayjs(20170101)
const year = base.year()
Expand All @@ -93,3 +109,9 @@ it('Clone with same value', () => {
const another = newBase.clone()
expect(newBase.toString()).toBe(another.toString())
})

it('Clone retains the UTC mode', () => {
const instance = dayjs('2018-09-06', { utc: true })
const another = instance.clone()
expect(another.$u).toBeTruthy()
})

0 comments on commit beb80fc

Please sign in to comment.