Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Map deprecated timezone in shopify/dates
Browse files Browse the repository at this point in the history
  • Loading branch information
ginabak committed Jan 8, 2025
1 parent 8588780 commit 6111c4f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'@shopify/react-i18n': patch
'@shopify/dates': patch
---

Map deprecated timezone
5 changes: 0 additions & 5 deletions .changeset/shaggy-dots-eat.md

This file was deleted.

12 changes: 11 additions & 1 deletion packages/dates/src/utilities/formatDate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {mapDeprecatedTimezones} from '../map-deprecated-timezones';

const intl = new Map<string, Intl.DateTimeFormat>();
export function memoizedGetDateTimeFormat(
locales?: string | string[],
Expand Down Expand Up @@ -39,6 +41,7 @@ export function formatDate(
locales: string | string[],
options: FormatDateOptions = {},
) {
console.log('FormatDate', options);

Check failure on line 44 in packages/dates/src/utilities/formatDate.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 18)

Unexpected console statement

Check failure on line 44 in packages/dates/src/utilities/formatDate.ts

View workflow job for this annotation

GitHub Actions / Test (Node 20, React 18)

Unexpected console statement
const hourCycleRequired =
resolvedOptions != null &&
options.hour12 === false &&
Expand All @@ -58,7 +61,14 @@ export function formatDate(
}).format(adjustedDate);
}

return memoizedGetDateTimeFormat(locales, options).format(date);
console.log(options.timeZone);

Check failure on line 64 in packages/dates/src/utilities/formatDate.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 18)

Unexpected console statement

Check failure on line 64 in packages/dates/src/utilities/formatDate.ts

View workflow job for this annotation

GitHub Actions / Test (Node 20, React 18)

Unexpected console statement

return memoizedGetDateTimeFormat(locales, {
...options,
timeZone: options.timeZone
? mapDeprecatedTimezones(options.timeZone)
: undefined,
}).format(date);
}

export function dateTimeFormatCacheKey(
Expand Down
22 changes: 22 additions & 0 deletions packages/dates/src/utilities/tests/formatDate.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
20 changes: 4 additions & 16 deletions packages/react-i18n/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
TimeUnit,
isLessThanOneWeekAway,
isLessThanOneYearAway,
mapDeprecatedTimezones,
} from '@shopify/dates';
import {
formatName as importedFormatName,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -331,32 +330,25 @@ 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:
return this.formatDate(date, {...formatOptions, ...dateStyle[style]});
}
}

return formatDate(date, locale, {
...formatOptions,
timeZone: normalizedTimezone,
});
return formatDate(date, locale, {...formatOptions, timeZone});
}

ordinal(amount: number) {
Expand Down Expand Up @@ -833,8 +825,4 @@ export class I18n {
private defaultOnError(error: I18nError) {
throw error;
}

private normalizeTimezone(timeZone?: string) {
return timeZone ? mapDeprecatedTimezones(timeZone) : undefined;
}
}

0 comments on commit 6111c4f

Please sign in to comment.