From 4f4501256fa1bc72128aae1d841bbd782df86aed Mon Sep 17 00:00:00 2001 From: Tim Sanderson <47798264+timsand@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:46:43 -0500 Subject: [PATCH] fix: fix timezone plugin to get correct locale setting (#2420) Signed-off-by: Tim Sanderson Co-authored-by: Tim Sanderson --- src/plugin/timezone/index.js | 4 ++-- test/plugin/timezone.test.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index c8f8a4675..832fb4a73 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -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() @@ -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) } diff --git a/test/plugin/timezone.test.js b/test/plugin/timezone.test.js index 2b28cc309..c4529c6c8 100644 --- a/test/plugin/timezone.test.js +++ b/test/plugin/timezone.test.js @@ -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') + }) })