From c7532a5347962bc502fe68ba26621da1721a101e Mon Sep 17 00:00:00 2001 From: Lisa Kim Date: Wed, 27 Mar 2024 11:48:21 -0700 Subject: [PATCH 1/2] Web: intl add a date time short formatter --- web/packages/shared/services/loc/loc.test.ts | 6 +++++- web/packages/shared/services/loc/loc.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/web/packages/shared/services/loc/loc.test.ts b/web/packages/shared/services/loc/loc.test.ts index 46b8d5608d52e..b66e546ae5abb 100644 --- a/web/packages/shared/services/loc/loc.test.ts +++ b/web/packages/shared/services/loc/loc.test.ts @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { displayDate, displayDateTime } from './loc'; +import { displayDate, displayDateTime, dateTimeShortFormat } from './loc'; const testDate = new Date('2022-01-28T16:00:44.309Z'); @@ -31,3 +31,7 @@ test('displayDateTime', () => { expect(output).toBe('2022-01-28 16:00:44'); }); + +test('dateTimeShortFormat', () => { + expect(dateTimeShortFormat(testDate)).toEqual('4:00 PM'); +}); diff --git a/web/packages/shared/services/loc/loc.ts b/web/packages/shared/services/loc/loc.ts index e4af2b6663f13..7c4cb2dc4520f 100644 --- a/web/packages/shared/services/loc/loc.ts +++ b/web/packages/shared/services/loc/loc.ts @@ -83,3 +83,19 @@ export function displayDateTime(date: Date) { export function dateToUtc(date: Date) { return new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000); } + +export function dateTimeShortFormat(date: Date) { + try { + if (isTest) { + return new Intl.DateTimeFormat('en-US', { timeStyle: 'short' }).format( + date + ); + } + return new Intl.DateTimeFormat(undefined, { timeStyle: 'short' }).format( + date + ); + } catch (err) { + logger.error('dateTimeShortFormat()', err); + return 'undefined'; + } +} From d80c9420545e111ba78ffbb3308063ea0b7fd0ec Mon Sep 17 00:00:00 2001 From: Lisa Kim Date: Thu, 28 Mar 2024 07:47:48 -0700 Subject: [PATCH 2/2] Address CRs --- web/packages/shared/services/loc/loc.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/web/packages/shared/services/loc/loc.ts b/web/packages/shared/services/loc/loc.ts index 7c4cb2dc4520f..dc33c09b5cf2a 100644 --- a/web/packages/shared/services/loc/loc.ts +++ b/web/packages/shared/services/loc/loc.ts @@ -21,6 +21,7 @@ import { format } from 'date-fns'; import Logger from 'shared/libs/logger'; import cfg from 'shared/config'; +const DEFAULT_LOCALE = 'en-US'; const isTest = process.env.NODE_ENV === 'test'; const logger = Logger.create('services/loc'); @@ -84,18 +85,14 @@ export function dateToUtc(date: Date) { return new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000); } +/** + * Accepts a date and returns the formatted time part of the date. + * The format depends on the browser and system settings locale, + * eg: if locale was `en-US` the returned value will be say `4:00 PM`. + * + * During tests, the locale will always default to `en-US`. + */ export function dateTimeShortFormat(date: Date) { - try { - if (isTest) { - return new Intl.DateTimeFormat('en-US', { timeStyle: 'short' }).format( - date - ); - } - return new Intl.DateTimeFormat(undefined, { timeStyle: 'short' }).format( - date - ); - } catch (err) { - logger.error('dateTimeShortFormat()', err); - return 'undefined'; - } + const locale = isTest ? DEFAULT_LOCALE : undefined; + return new Intl.DateTimeFormat(locale, { timeStyle: 'short' }).format(date); }