From d0e6738a66e1b65d3706aad2f9168ebb43d4f887 Mon Sep 17 00:00:00 2001 From: Yungoo Kong Date: Thu, 18 Jul 2024 20:20:41 +0900 Subject: [PATCH] fix: Fix zero offset issue when use tz with locale (#2532) * test: add test case for tz with locale * fix: set utc as false if $offset is 0 --- src/index.js | 2 +- test/timezone.test.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 061ade17..f28d6498 100644 --- a/src/index.js +++ b/src/index.js @@ -50,7 +50,7 @@ const dayjs = function (date, c) { const wrapper = (date, instance) => dayjs(date, { locale: instance.$L, - utc: instance.$u, + utc: instance.$offset !== 0 && instance.$u, x: instance.$x, $offset: instance.$offset // todo: refactor; do not use this.$offset in you code }) diff --git a/test/timezone.test.js b/test/timezone.test.js index 42cab893..9d61fe06 100644 --- a/test/timezone.test.js +++ b/test/timezone.test.js @@ -3,6 +3,7 @@ import moment from 'moment' import dayjs from '../src' import timezone from '../src/plugin/timezone' import utc from '../src/plugin/utc' +import '../src/locale/en' dayjs.extend(utc) dayjs.extend(timezone) @@ -80,3 +81,12 @@ it('UTC diff in DST', () => { expect(day1.diff(day2, 'd')) .toBe(-3) }) + +it('TZ with Locale', () => { + const test1 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Seoul').locale('en') + expect(test1.hour()).toBe(9) + const test2 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Hong_Kong').locale('en') + expect(test2.hour()).toBe(8) + const test3 = dayjs('2000-01-01T09:00:00+09:00').tz('Etc/UTC').locale('en') + expect(test3.hour()).toBe(0) +})