Skip to content

Commit eb5fbc4

Browse files
authored
fix: fix startOf/endOf bug in timezone plugin (#1229)
1 parent 9a859a1 commit eb5fbc4

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/plugin/timezone/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ export default (o, c, d) => {
114114
return result && result.value
115115
}
116116

117+
const oldStartOf = proto.startOf
118+
proto.startOf = function (units, startOf) {
119+
if (!this.$x || !this.$x.$timezone) {
120+
return oldStartOf.call(this, units, startOf)
121+
}
122+
123+
const withoutTz = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'))
124+
const startOfWithoutTz = oldStartOf.call(withoutTz, units, startOf)
125+
return startOfWithoutTz.tz(this.$x.$timezone, true)
126+
}
127+
117128
d.tz = function (input, arg1, arg2) {
118129
const parseFormat = arg2 && arg1
119130
const timezone = arg2 || arg1 || defaultTimezone

test/plugin/timezone.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,17 @@ describe('CustomPraseFormat', () => {
290290
expect(dayjs.tz('10/15/2020 12:30', 'MM/DD/YYYY HH:mm', DEN).unix()).toBe(result)
291291
})
292292
})
293+
294+
describe('startOf and endOf', () => {
295+
it('corrects for timezone offset in startOf', () => {
296+
const originalDay = dayjs.tz('2010-01-01 00:00:00', NY)
297+
const startOfDay = originalDay.startOf('day')
298+
expect(startOfDay.valueOf()).toEqual(originalDay.valueOf())
299+
})
300+
301+
it('corrects for timezone offset in endOf', () => {
302+
const originalDay = dayjs.tz('2009-12-31 23:59:59.999', NY)
303+
const endOfDay = originalDay.endOf('day')
304+
expect(endOfDay.valueOf()).toEqual(originalDay.valueOf())
305+
})
306+
})

test/timezone.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import MockDate from 'mockdate'
22
import moment from 'moment'
33
import dayjs from '../src'
4+
import timezone from '../src/plugin/timezone'
45
import utc from '../src/plugin/utc'
56

67
dayjs.extend(utc)
8+
dayjs.extend(timezone)
79

810
beforeEach(() => {
911
MockDate.set(new Date())

0 commit comments

Comments
 (0)