From 6111c4f12af3398cd166e68993868113fb961e32 Mon Sep 17 00:00:00 2001 From: Gina Bak Date: Wed, 8 Jan 2025 13:11:57 -0800 Subject: [PATCH] Map deprecated timezone in shopify/dates --- ...ries-tickle.md => light-students-learn.md} | 2 +- .changeset/shaggy-dots-eat.md | 5 ----- packages/dates/src/utilities/formatDate.ts | 12 +++++++++- .../src/utilities/tests/formatDate.test.ts | 22 +++++++++++++++++++ packages/react-i18n/src/i18n.ts | 20 ++++------------- 5 files changed, 38 insertions(+), 23 deletions(-) rename .changeset/{rude-countries-tickle.md => light-students-learn.md} (53%) delete mode 100644 .changeset/shaggy-dots-eat.md create mode 100644 packages/dates/src/utilities/tests/formatDate.test.ts diff --git a/.changeset/rude-countries-tickle.md b/.changeset/light-students-learn.md similarity index 53% rename from .changeset/rude-countries-tickle.md rename to .changeset/light-students-learn.md index 36b001635d..714d9c81b9 100644 --- a/.changeset/rude-countries-tickle.md +++ b/.changeset/light-students-learn.md @@ -1,5 +1,5 @@ --- -'@shopify/react-i18n': patch +'@shopify/dates': patch --- Map deprecated timezone diff --git a/.changeset/shaggy-dots-eat.md b/.changeset/shaggy-dots-eat.md deleted file mode 100644 index d3c3ce390f..0000000000 --- a/.changeset/shaggy-dots-eat.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@shopify/dates': patch ---- - -Update deprecated timezone for Europe/Kyiv diff --git a/packages/dates/src/utilities/formatDate.ts b/packages/dates/src/utilities/formatDate.ts index 2c3a8e8a6b..6c437eaf65 100644 --- a/packages/dates/src/utilities/formatDate.ts +++ b/packages/dates/src/utilities/formatDate.ts @@ -1,3 +1,5 @@ +import {mapDeprecatedTimezones} from '../map-deprecated-timezones'; + const intl = new Map(); export function memoizedGetDateTimeFormat( locales?: string | string[], @@ -39,6 +41,7 @@ export function formatDate( locales: string | string[], options: FormatDateOptions = {}, ) { + console.log('FormatDate', options); const hourCycleRequired = resolvedOptions != null && options.hour12 === false && @@ -58,7 +61,14 @@ export function formatDate( }).format(adjustedDate); } - return memoizedGetDateTimeFormat(locales, options).format(date); + console.log(options.timeZone); + + return memoizedGetDateTimeFormat(locales, { + ...options, + timeZone: options.timeZone + ? mapDeprecatedTimezones(options.timeZone) + : undefined, + }).format(date); } export function dateTimeFormatCacheKey( diff --git a/packages/dates/src/utilities/tests/formatDate.test.ts b/packages/dates/src/utilities/tests/formatDate.test.ts new file mode 100644 index 0000000000..cdbc578449 --- /dev/null +++ b/packages/dates/src/utilities/tests/formatDate.test.ts @@ -0,0 +1,22 @@ +import {formatDate} from '../formatDate'; + +jest.mock('../../map-deprecated-timezones', () => ({ + mapDeprecatedTimezones: jest.fn(), +})); + +const mapDeprecatedTimezones: jest.Mock = jest.requireMock( + '../../map-deprecated-timezones', +).mapDeprecatedTimezones; + +describe('formatDate', () => { + it('maps the deprecated timezone', () => { + const date = new Date(); + const locale = 'en-UA'; + const options: Intl.DateTimeFormatOptions = { + timeZone: 'Europe/Kyiv', + }; + + formatDate(date, locale, options); + expect(mapDeprecatedTimezones).toHaveBeenCalledWith('Europe/Kyiv'); + }); +}); diff --git a/packages/react-i18n/src/i18n.ts b/packages/react-i18n/src/i18n.ts index 9fb4e9c16f..49c9dfab3f 100644 --- a/packages/react-i18n/src/i18n.ts +++ b/packages/react-i18n/src/i18n.ts @@ -11,7 +11,6 @@ import { TimeUnit, isLessThanOneWeekAway, isLessThanOneYearAway, - mapDeprecatedTimezones, } from '@shopify/dates'; import { formatName as importedFormatName, @@ -133,7 +132,7 @@ export class I18n { this.locale = locale; this.defaultCountry = country; this.defaultCurrency = currency; - this.defaultTimezone = this.normalizeTimezone(timezone); + this.defaultTimezone = timezone; this.pseudolocalize = pseudolocalize; this.defaultInterpolate = interpolate; this.onError = onError || this.defaultOnError; @@ -331,21 +330,17 @@ export class I18n { ): string { const {locale, defaultTimezone} = this; const {timeZone = defaultTimezone} = options; - const normalizedTimezone = this.normalizeTimezone(timeZone); const {style = undefined, ...formatOptions} = options || {}; if (style) { switch (style) { case DateStyle.Humanize: - return this.humanizeDate(date, { - ...formatOptions, - timeZone: normalizedTimezone, - }); + return this.humanizeDate(date, {...formatOptions, timeZone}); case DateStyle.DateTime: return this.formatDateTime(date, { ...formatOptions, - timeZone: normalizedTimezone, + timeZone, ...dateStyle[style], }); default: @@ -353,10 +348,7 @@ export class I18n { } } - return formatDate(date, locale, { - ...formatOptions, - timeZone: normalizedTimezone, - }); + return formatDate(date, locale, {...formatOptions, timeZone}); } ordinal(amount: number) { @@ -833,8 +825,4 @@ export class I18n { private defaultOnError(error: I18nError) { throw error; } - - private normalizeTimezone(timeZone?: string) { - return timeZone ? mapDeprecatedTimezones(timeZone) : undefined; - } }