question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GetFormatterParts failing on latest Firefox Nightly

See original GitHub issue

From https://bugzilla.mozilla.org/show_bug.cgi?id=1742572:

Standalone test case:

function GetFormatterParts(timeZone, epochMilliseconds){
  const formatter = new Intl.DateTimeFormat("en-us", {
    timeZone,
    hour12: false,
    era: "short",
    year: "numeric",
    month: "numeric",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric"
  });
  const datetime = formatter.format(new Date(epochMilliseconds));
  const [date, fullYear, time] = datetime.split(/,\s+/);
  const [month, day] = date.split(" ");
  const [year, era] = fullYear.split(" ");
  const [hour, minute, second] = time.split(":");
  return {
    year: era === "BC" ? -year + 1 : +year,
    month: +month,
    day: +day,
    hour: hour === "24" ? 0 : +hour,
    minute: +minute,
    second: +second
  };
}

GetFormatterParts("America/Los_Angeles", Date.now());

GetFormatterParts expects that the formatted date-time string can be split into three parts through the regular expression /,\s+/. This worked before the update, because the formatted string looked like "11 23, 2021 AD, 05:00:00". But after the update to ICU 70, which includes an update to CLDR 40, the string now looks like "11/23/2021 A, 05:00:00", i.e. can no longer be separated into three parts when searching for a comma.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
marco-ccommented, Dec 6, 2021

@justingrant mind releasing a new version with this fix?

1reaction
ptomatocommented, Nov 24, 2021

I was thinking of a change for proposal-temporal such as this one:

--- a/polyfill/lib/ecmascript.mjs
+++ b/polyfill/lib/ecmascript.mjs
@@ -2310,14 +2310,14 @@ export const ES = ObjectAssign({}, ES2020, {
   },
   GetFormatterParts: (timeZone, epochMilliseconds) => {
     const formatter = getIntlDateTimeFormatEnUsForTimeZone(timeZone);
-    // FIXME: can this use formatToParts instead?
-    const datetime = formatter.format(new Date(epochMilliseconds));
-    const [date, fullYear, time] = datetime.split(/,\s+/);
-    const [month, day] = date.split(' ');
-    const [year, era] = fullYear.split(' ');
-    const [hour, minute, second] = time.split(':');
+    const values = {};
+    const parts = formatter.formatToParts(new Date(epochMilliseconds));
+    for (const { type, value } of parts) {
+      if (type !== 'literal') values[type] = value;
+    }
+    const { era, year, month, day, hour, minute, second } = values;
     return {
-      year: era === 'BC' ? -year + 1 : +year,
+      year: era.startsWith('B') ? -year + 1 : +year, // era 'B' or 'BC'
       month: +month,
       day: +day,
       hour: hour === '24' ? 0 : +hour, // bugs.chromium.org/p/chromium/issues/detail?id=1045791

We already use formatToParts in the calendar code so maybe it shouldn’t be a problem. On the other hand, it’s only used for non-ISO calendars, and this would require it more generally for ISO calendar usage as well. I’m not sure what formatToParts is transpiled to for old browsers that don’t support it. For the reference code in proposal-temporal I’m not concerned about old browsers (it’s been generally supported since 2018 or so) but for the polyfill it might matter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firefox Nightly Error Collection - Mozilla Support
Firefox Nightly automatically collects and sends Mozilla information about JavaScript errors that occur within the browser itself (not on web pages).
Read more >
Firefox Nightly 110.0a1, See All New Features ... - Mozilla
See what landed recently in Firefox Nightly! Release Notes tell you what's new in Firefox. As always, we welcome your feedback.
Read more >
Firefox hangs or is not responding - How to fix
When Firefox hangs, it stops responding to your clicks and doesn't seem to do anything. This article covers the various solutions depending on...
Read more >
Fix problems connecting to websites after updating Firefox
This article describes how to troubleshoot problems connecting to websites that start immediately after updating Firefox to a new version.
Read more >
Firefox can't load websites but other browsers can
We'll explain errors such as "Server not found" or "Unable to connect" and how to fix problems where Firefox can't access websites but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found