Skip to content

Commit

Permalink
fix: incorrect startOf/endOf behavior with +00:00 timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
sevtdy committed Dec 19, 2022
1 parent e4468a4 commit cbc9c0f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
11 changes: 0 additions & 11 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,6 @@ export default (o, c, d) => {
return result && result.value
}

const oldStartOf = proto.startOf
proto.startOf = function (units, startOf) {
if (!this.$x || !this.$x.$timezone) {
return oldStartOf.call(this, units, startOf)
}

const withoutTz = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'))
const startOfWithoutTz = oldStartOf.call(withoutTz, units, startOf)
return startOfWithoutTz.tz(this.$x.$timezone, true)
}

d.tz = function (input, arg1, arg2) {
const parseFormat = arg2 && arg1
const timezone = arg2 || arg1 || defaultTimezone
Expand Down
16 changes: 16 additions & 0 deletions test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,27 @@ describe('startOf and endOf', () => {
const originalDay = dayjs.tz('2010-01-01 00:00:00', NY)
const startOfDay = originalDay.startOf('day')
expect(startOfDay.valueOf()).toEqual(originalDay.valueOf())
expect(startOfDay.format()).toEqual(originalDay.format())
})

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())
expect(endOfDay.format()).toEqual(originalDay.format())
})

it('corrects for +00:00 timezone offset in startOf', () => {
const originalDay = dayjs.tz('2021-05-10 00:00:00', 'UTC')
const startOfDay = originalDay.startOf('day')
expect(startOfDay.valueOf()).toEqual(originalDay.valueOf())
expect(startOfDay.format()).toEqual(originalDay.format())
})

it('corrects for +00:00 timezone offset in endOf', () => {
const originalDay = dayjs.tz('2021-05-10 23:59:59.999', 'UTC')
const endOfDay = originalDay.endOf('day')
expect(endOfDay.valueOf()).toEqual(originalDay.valueOf())
expect(endOfDay.format()).toEqual(originalDay.format())
})
})

0 comments on commit cbc9c0f

Please sign in to comment.