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: preserve locality when using timezone plugin methods #2420

Merged
merged 1 commit into from
Sep 19, 2023
Merged
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/timezone/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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')
})
})