Skip to content

Commit

Permalink
fix: fix timezone plugin to get correct locale setting (#2420)
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Sanderson <tim.sanderson@grow.com>
Co-authored-by: Tim Sanderson <tim.sanderson@grow.com>
  • Loading branch information
timsand and Tim Sanderson committed Sep 19, 2023
1 parent 0a91056 commit 4f45012
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/plugin/timezone/index.js
Expand Up @@ -97,7 +97,7 @@ export default (o, c, d) => {
const date = this.toDate()
const target = date.toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((date - new Date(target)) / 1000 / 60)
let ins = d(target).$set(MS, this.$ms)
let ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
if (keepLocalTime) {
const newOffset = ins.utcOffset()
Expand All @@ -120,7 +120,7 @@ export default (o, c, d) => {
return oldStartOf.call(this, units, startOf)
}

const withoutTz = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'))
const withoutTz = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'), { locale: this.$L })
const startOfWithoutTz = oldStartOf.call(withoutTz, units, startOf)
return startOfWithoutTz.tz(this.$x.$timezone, true)
}
Expand Down
11 changes: 11 additions & 0 deletions test/plugin/timezone.test.js
Expand Up @@ -318,4 +318,15 @@ describe('startOf and endOf', () => {
const endOfDay = originalDay.endOf('day')
expect(endOfDay.valueOf()).toEqual(originalDay.valueOf())
})

it('preserves locality when tz is called', () => {
const tzWithoutLocality = dayjs.tz('2023-02-17 00:00:00', NY)
const tzWithLocality = dayjs.tz('2023-02-17 00:00:00', NY).locale({
name: 'locale_test',
weekStart: 3
})

expect(tzWithoutLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-12')
expect(tzWithLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-15')
})
})

0 comments on commit 4f45012

Please sign in to comment.