Skip to content

feat(formatDate): add locale option to be extensible for absolute format #19157

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

Merged

Conversation

dkaushik95
Copy link
Contributor

Closes #

Currently absolute.format* doesn't work for our product since we allow users the option to switch to UTC time. Adding timeZone as a prop allows us to override the default timezone that Intl chooses.

Changelog

New

  • @carbon/utilities - Datetime.absolute now allows you to choose a timeZone which can be used to override the default timeZone while formatting datetime. This prop will be available to: formatTime, formatTime, format, and formatRange

Testing / Reviewing

  • Testers can try out the following use case:
  1. timestamp: 1744835151846
  2. use this timestamp to check the time difference between your locale and UTC
  3. eg:
datetime: 1744835151846
utc string: Apr 16, 2025, 8:25:51 PM
local string: Apr 16, 2025, 1:25:51 PM

Copy link

netlify bot commented Apr 17, 2025

Deploy Preview for v11-carbon-web-components ready!

Name Link
🔨 Latest commit 08832a8
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-web-components/deploys/681a2b52ebf6be00081a9326
😎 Deploy Preview https://deploy-preview-19157--v11-carbon-web-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Apr 17, 2025

Deploy Preview for v11-carbon-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 08832a8
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-react/deploys/681a2b524621df000861fa8b
😎 Deploy Preview https://deploy-preview-19157--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Apr 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.59%. Comparing base (5d1b860) to head (08832a8).
Report is 17 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #19157   +/-   ##
=======================================
  Coverage   84.59%   84.59%           
=======================================
  Files         380      380           
  Lines       14471    14471           
  Branches     4771     4737   -34     
=======================================
  Hits        12242    12242           
  Misses       2074     2074           
  Partials      155      155           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@nchevsky nchevsky left a comment

Choose a reason for hiding this comment

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

@dkaushik95 Can you add unit test coverage in absolute-test.js?

@dkaushik95
Copy link
Contributor Author

Thanks for the review @nchevsky . I have addressed all the review comments. Can you re-review it? Thanks!

@dkaushik95 dkaushik95 requested a review from nchevsky April 17, 2025 15:35
Comment on lines 28 to 43
timeZones.forEach((timeZone) => {
const dtf = new Intl.DateTimeFormat(locale, {
timeStyle: style,
timeZone,
});
const expectedOutput = dtf.format(date);

test(`${style} with timeZone ${timeZone} → ${expectedOutput}`, () => {
const actualOutput = absolute.formatTime(date, {
locale,
style,
timeZone,
});
expect(actualOutput).toBe(expectedOutput);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't put this within the timeStyle loop, as (a) it multiplies the number of iterations 4 → 12 and (b) timeStyle and timeZone aren't dependent on each other.

We don't even need to test three different time zones, for timeZone is simply passed through to Intl.DateTimeFormat regardless of its value. Some alternatives, in ascending order of complexity:

  1. Test any two time zones so that, even if one happens to match the host's, the other won't; or
  2. spy on Intl.DateTimeFormat and simply assert that absolute.format*() instantiated it with the given timeZone option; or
  3. dynamically select a time zone other than the host's, and test just that one.

I'd personally go with # 2 as it's more of a true unit test, but seeing how none of the preexisting tests are written this way, it might be less controversial to stick with # 1.

Whichever you choose, please replicate to all tests. 🙏🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, I chose the 1st option. I chose two timezones Tokyo and UTC so that it's always different for any host for at least one test. I also addressed the 2nd concern of using describe for timeZones so it should be consistent across all tests. I was trying different approaches and accidentally added both approaches :)

This will how the tests would show up.

At the end there will be 2 test cases for each function (8 iterations in all)

Screenshot 2025-04-21 at 8 58 21 AM

@preetibansalui
Copy link
Contributor

@dkaushik95 I approved the PR and then realized we missed updating the documentation for the newly added option. Could you please add the relevant details to the README file?

@dkaushik95
Copy link
Contributor Author

@preetibansalui Hello! I have made changes to add documentation to the readme. Can you give it a review? Thanks!

Copy link
Contributor

@preetibansalui preetibansalui left a comment

Choose a reason for hiding this comment

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

Looks good to me—just a small spelling correction needed.


For `absolute` functions, you can provide `timeZone` as an optional property.
This is usefull when (for example) you want to display utc time instead of a
local timezone.
Copy link
Contributor

@preetibansalui preetibansalui May 6, 2025

Choose a reason for hiding this comment

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

This is useful when (for example) you want to display UTC time instead of a
local time zone.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks! Fixed :)

@dkaushik95 dkaushik95 requested a review from preetibansalui May 12, 2025 15:44
Copy link
Contributor

@preetibansalui preetibansalui left a comment

Choose a reason for hiding this comment

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

Thanks for the changes, This LGTM 👍

@ariellalgilmore ariellalgilmore added this pull request to the merge queue May 12, 2025
Merged via the queue into carbon-design-system:main with commit 0ce2c4e May 12, 2025
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants