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

Add date and time utils to use in downstream projects #1726

Merged
merged 1 commit into from
Oct 2, 2024

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Oct 1, 2024

This PR brings and consolidates in the shared library a few time and date related utilities that we have defined in client and LMS over time:

So far we have been copying the few things that were needed in both places, but we are going to need the functions around relative times, which would duplicate most of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The module is called time.ts in client and date.ts in LMS, so I decided to call it date-and-time.ts.

Copy link

codecov bot commented Oct 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (137e4dc) to head (41868eb).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1726   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           66        67    +1     
  Lines         1121      1195   +74     
  Branches       439       449   +10     
=========================================
+ Hits          1121      1195   +74     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@acelaya
Copy link
Contributor Author

acelaya commented Oct 1, 2024

I have draft PRs in client and LMS already using these.

Comment on lines +261 to +278
export function formatDateTime(
date: Date | string,
options?: FormatDateTimeOptions,
): string {
return format(
typeof date === 'string' ? new Date(date) : date,
{
year: 'numeric',
month: 'short',
day: '2-digit',
weekday: options?.includeWeekday ? 'long' : undefined,
hour: '2-digit',
minute: '2-digit',
},
options?.Intl,
);
}
Copy link
Contributor Author

@acelaya acelaya Oct 1, 2024

Choose a reason for hiding this comment

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

This function consolidates formatDate from client, which includes the weekday, and formatDateTime from LMS, which returns the same format but without the weekday.

The formatDateTime name seems more correct, and the dynamic inclusion of the weekday is now handled by an optional config option.

src/index.ts Outdated Show resolved Hide resolved
src/util/date-and-time.ts Show resolved Hide resolved
src/util/test/date-and-time-test.js Outdated Show resolved Hide resolved
src/util/test/date-and-time-test.js Outdated Show resolved Hide resolved
it('returned function cancels the timer', () => {
const date = new Date().toISOString();
const callback = sandbox.stub();
const cancel = decayingInterval(date, callback);
Copy link
Member

Choose a reason for hiding this comment

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

We could modernize this API in future by returning an AbortSignal. This would make the meaning of the return value more obvious and potentially simplify use in other browser APIs.

@acelaya acelaya merged commit a09451d into main Oct 2, 2024
4 checks passed
@acelaya acelaya deleted the date-and-time-utils branch October 2, 2024 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants