Skip to content

Commit 4f45012

Browse files
timsandTim Sanderson
andauthored
fix: fix timezone plugin to get correct locale setting (#2420)
Signed-off-by: Tim Sanderson <[email protected]> Co-authored-by: Tim Sanderson <[email protected]>
1 parent 0a91056 commit 4f45012

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/plugin/timezone/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export default (o, c, d) => {
9797
const date = this.toDate()
9898
const target = date.toLocaleString('en-US', { timeZone: timezone })
9999
const diff = Math.round((date - new Date(target)) / 1000 / 60)
100-
let ins = d(target).$set(MS, this.$ms)
100+
let ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
101101
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
102102
if (keepLocalTime) {
103103
const newOffset = ins.utcOffset()
@@ -120,7 +120,7 @@ export default (o, c, d) => {
120120
return oldStartOf.call(this, units, startOf)
121121
}
122122

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

test/plugin/timezone.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,15 @@ describe('startOf and endOf', () => {
318318
const endOfDay = originalDay.endOf('day')
319319
expect(endOfDay.valueOf()).toEqual(originalDay.valueOf())
320320
})
321+
322+
it('preserves locality when tz is called', () => {
323+
const tzWithoutLocality = dayjs.tz('2023-02-17 00:00:00', NY)
324+
const tzWithLocality = dayjs.tz('2023-02-17 00:00:00', NY).locale({
325+
name: 'locale_test',
326+
weekStart: 3
327+
})
328+
329+
expect(tzWithoutLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-12')
330+
expect(tzWithLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-15')
331+
})
321332
})

0 commit comments

Comments
 (0)