Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web: intl add a date time short formatter #39915

Merged
merged 4 commits into from
Mar 28, 2024
Merged

Conversation

kimlisa
Copy link
Contributor

@kimlisa kimlisa commented Mar 27, 2024

Adds a date time short formatter helper, that when in test mode, use en-US as a default locale

Copy link

The PR changelog entry failed validation: Changelog entry not found in the PR body. Please add a "no-changelog" label to the PR, or changelog lines starting with changelog: followed by the changelog entries for the PR.

@kimlisa kimlisa added the no-changelog Indicates that a PR does not require a changelog entry label Mar 27, 2024
@kimlisa kimlisa requested review from gzdunek and bl-nero March 27, 2024 18:50
);
} catch (err) {
logger.error('dateTimeShortFormat()', err);
return 'undefined';
Copy link
Member

@ravicious ravicious Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. What errors do we expect to be thrown here? I see that other functions in this file do use try … catch, but they also use format from date-fns and not Intl.DateTimeFormat.
  2. In case of an error, wouldn't it be better to use some kind of method on Date to display something approximate instead of saying "undefined"? If Intl.DateTimeFormat().format cannot fail, then the error handling is not even necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. From what I see, the function throws only when locales or options contain invalid values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#exceptions.
    If so, should we catch the error at all? It can come only from our invalid configuration, not from the user input.

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to put an example in a JSDoc of how the formatted date is going to look like, say for en-US at least. And maybe mention that it's going to use browser and system settings to determine the actual format that's going to be used.

Comment on lines 89 to 96
if (isTest) {
return new Intl.DateTimeFormat('en-US', { timeStyle: 'short' }).format(
date
);
}
return new Intl.DateTimeFormat(undefined, { timeStyle: 'short' }).format(
date
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (isTest) {
return new Intl.DateTimeFormat('en-US', { timeStyle: 'short' }).format(
date
);
}
return new Intl.DateTimeFormat(undefined, { timeStyle: 'short' }).format(
date
);
const DEFAULT_LOCALE = 'en-US'; //(as const on top of the file)
...
const locale = isTest ? DEFAULT_LOCALE : undefined;
return new Intl.DateTimeFormat(locale, { timeStyle: 'short' }).format(date);

);
} catch (err) {
logger.error('dateTimeShortFormat()', err);
return 'undefined';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. From what I see, the function throws only when locales or options contain invalid values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#exceptions.
    If so, should we catch the error at all? It can come only from our invalid configuration, not from the user input.

@kimlisa kimlisa enabled auto-merge March 28, 2024 14:49
@kimlisa kimlisa added this pull request to the merge queue Mar 28, 2024
Merged via the queue into master with commit 55ef4a2 Mar 28, 2024
34 checks passed
@kimlisa kimlisa deleted the lisa/date-time-short-format branch March 28, 2024 15:50
@public-teleport-github-review-bot

@kimlisa See the table below for backport results.

Branch Result
branch/v14 Create PR
branch/v15 Create PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/branch/v14 backport/branch/v15 no-changelog Indicates that a PR does not require a changelog entry size/sm ui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants