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: startOf get error when use mutable & timezone & UTC #1912

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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: 2 additions & 2 deletions src/plugin/badMutable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export default (o, c) => { // locale needed later

const oldStartOf = proto.startOf
proto.startOf = function (units, startOf) {
this.$d = oldStartOf.bind(this)(units, startOf).toDate()
this.$d = oldStartOf.bind(this)(units, startOf).$d
this.init()
return this
}

const oldAdd = proto.add
proto.add = function (number, units) {
this.$d = oldAdd.bind(this)(number, units).toDate()
this.$d = oldAdd.bind(this)(number, units).$d
this.init()
return this
}
Expand Down
14 changes: 14 additions & 0 deletions test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import dayjs from '../../src'
import timezone from '../../src/plugin/timezone'
import customParseFormat from '../../src/plugin/customParseFormat'
import utc from '../../src/plugin/utc'
import badMutable from '../../src/plugin/badMutable'

dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)
dayjs.extend(badMutable)

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -313,9 +315,21 @@ describe('startOf and endOf', () => {
expect(startOfDay.valueOf()).toEqual(originalDay.valueOf())
})

it('corrects for timezone offset in startOf with format', () => {
const dateStr = '2010-01-01 00:00:00'
const originalDay = dayjs.tz(dateStr, NY)
expect(originalDay.startOf('day').format('YYYY-MM-DD HH:mm:ss')).toEqual(dateStr)
})

it('corrects for timezone offset in endOf', () => {
const originalDay = dayjs.tz('2009-12-31 23:59:59.999', NY)
const endOfDay = originalDay.endOf('day')
expect(endOfDay.valueOf()).toEqual(originalDay.valueOf())
})

it('corrects for timezone offset in endOf with format', () => {
const dateStr = '2009-12-31 23:59:59.999'
const originalDay = dayjs.tz(dateStr, NY)
expect(originalDay.endOf('day').format('YYYY-MM-DD HH:mm:ss.SSS')).toEqual(dateStr)
})
})