From f874c5de1fbe64760fe167ed8c20b26a84632b3a Mon Sep 17 00:00:00 2001 From: Ayo1984 Date: Wed, 29 Oct 2025 13:53:45 +0000 Subject: [PATCH] resepct utc when passed to tz --- src/plugin/timezone/index.js | 8 ++++++++ test/plugin/timezone.test.js | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index bcacf9ab6..a14224355 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -140,6 +140,14 @@ export default (o, c, d) => { // timestamp number || js Date || Day.js return d(input).tz(timezone) } + + const hasExplicitOffset = /[Zz]|[+-]\d\d:?(\d\d)?$/.test(input) + if (hasExplicitOffset) { + const utcDate = d.utc(input, parseFormat) + utcDate.$x.$timezone = timezone + return utcDate + } + const localTs = d.utc(input, parseFormat).valueOf() const [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone) const ins = d(targetTs).utcOffset(targetOffset) diff --git a/test/plugin/timezone.test.js b/test/plugin/timezone.test.js index d83a03f8a..654a1465a 100644 --- a/test/plugin/timezone.test.js +++ b/test/plugin/timezone.test.js @@ -1,8 +1,8 @@ import MockDate from 'mockdate' import moment from 'moment-timezone' import dayjs from '../../src' -import timezone from '../../src/plugin/timezone' import customParseFormat from '../../src/plugin/customParseFormat' +import timezone from '../../src/plugin/timezone' import utc from '../../src/plugin/utc' dayjs.extend(utc) @@ -22,6 +22,7 @@ const VAN = 'America/Vancouver' const DEN = 'America/Denver' const TOKYO = 'Asia/Tokyo' const PARIS = 'Europe/Paris' +const MELBOURNE = 'Australia/Melbourne' describe('Guess', () => { it('return string', () => { @@ -352,3 +353,22 @@ describe('UTC timezone', () => { expect(dayjs2.format()).toBe(moment2.format()) }) }) + +describe('timezone: parsing UTC input when default timezone is set', () => { + beforeEach(() => { + dayjs.tz.setDefault(MELBOURNE) + moment.tz.setDefault(MELBOURNE) + }) + afterEach(() => { + dayjs.tz.setDefault() + moment.tz.setDefault() + }) + + it('should honor Z (UTC) when parsing with tz()', () => { + const input = '2025-10-24T05:00:00Z' + const d = dayjs.tz(input, MELBOURNE) + const m = moment.tz(input, MELBOURNE) + + expect(d.toISOString()).toBe(m.toISOString()) + }) +})