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

'Invalid time zone specified' when calling formatInTimeZone with time zone offset and time zone tokens #186

Closed
patrik-csak opened this issue May 17, 2022 · 10 comments

Comments

@patrik-csak
Copy link
Contributor

Given the following documentation (emphasis mine):

formatInTimeZone

This function takes a Date instance in the system's local time or an ISO8601 string, and an IANA time zone name or offset string.

I expect the following to work:

formatInTimeZone("1986-04-04T10:32:55.123Z", "+2", "dd.MM.yyyy HH:mm zzzz");

What actually happens:

RangeError: Invalid time zone specified: +2
    at new DateTimeFormat (<anonymous>)
    at getDTF (/home/runner/tmp-nodejs/node_modules/date-fns-tz/_lib/tzIntlTimeZoneName/index.js:36:10)
    at tzIntlTimeZoneName (/home/runner/tmp-nodejs/node_modules/date-fns-tz/_lib/tzIntlTimeZoneName/index.js:14:13)
    at Object.z (/home/runner/tmp-nodejs/node_modules/date-fns-tz/format/formatters/index.js:105:35)
    at /home/runner/tmp-nodejs/node_modules/date-fns-tz/format/index.js:344:75
    at Array.reduce (<anonymous>)
    at format (/home/runner/tmp-nodejs/node_modules/date-fns-tz/format/index.js:337:25)
    at formatInTimeZone (/home/runner/tmp-nodejs/node_modules/date-fns-tz/formatInTimeZone/index.js:41:30)
    at Object.<anonymous> (/home/runner/tmp-nodejs/index.js:4:13)

To reproduce, run the code in this Replit Repl

@patrik-csak
Copy link
Contributor Author

After a little more research, it looks like maybe it's not possible to convert a numerical offset to a named offset

@marnusw
Copy link
Owner

marnusw commented May 17, 2022

'+02:00' should work.

@patrik-csak
Copy link
Contributor Author

Thanks for the reply, @marnusw

'+02:00' doesn't work. I get the same error:

RangeError: Invalid time zone specified: +02:00

I updated the Repl to use all numerical offset variations and none work

@patrik-csak patrik-csak reopened this May 17, 2022
@marnusw
Copy link
Owner

marnusw commented May 17, 2022

I will take a look. If you have the time, a PR with failing unit tests will help me sort this out quicker.

@patrik-csak
Copy link
Contributor Author

Here you go! #187

@dancornilov
Copy link

Hello, same issue with another time zone

const date  = new Date();

formatInTimeZone(date, 'Eastern Standard Time', 'yyyy-MM-dd HH:mm:ss zzz');

image

@marnusw
Copy link
Owner

marnusw commented Jan 31, 2023

@dancornilov you have to specify the IANA time zone name: America/New_York

@marnusw
Copy link
Owner

marnusw commented Jan 31, 2023

Closing this, continued in #187.

@marnusw marnusw closed this as completed Jan 31, 2023
@gunnartorfis
Copy link

gunnartorfis commented Feb 14, 2023

Hey, we've been getting hundreds of Sentry errors related to this issue but are quite stuck on how to approach this. It seems like the time zone name is definitely in IANA.

Our largest customer group is in Iceland and we've been getting:
Invalid time zone specified: Atlantic/Reykjavik

But also for

  • Europe/Lisbon
  • Europe/Prague
  • America/Mexico_City
  • Africa/Johannesburg
  • Asia/Katmandu
  • Asia/Dubai

We're calling formatInTimezone based on the "company" timezone:

export const getCompanyTimezone = () => {
  const company = getCompany();

  const companyTimezone = company?.connections.location?.timeZone;

  if (companyTimezone && isValidTimezone(companyTimezone)) {
    return companyTimezone;
  }

  return getUserTimezone();
};

Here's the isValidTimezone function that has been implemented just for this issue, without any luck.

const isValidTimezone = (tz: string) => {
  if (!Intl?.DateTimeFormat().resolvedOptions().timeZone) {
    // This one has never been reported
    Sentry.captureException(new Error('Intl.DateTimeFormat().resolvedOptions().timeZone is undefined'), {
      extra: {
        tz,
        resolvedOptions: JSON.stringify(Intl.DateTimeFormat().resolvedOptions())
      }
    });
    return false;
  }

  try {
    Intl.DateTimeFormat(undefined, { timeZone: tz });
    return true;
  } catch (ex) {
    return false;
  }
};

I'm not sure how to approach this so any help is much appreciated ❤️

@jltxwesley
Copy link

Hey @gunnartorfis, did you end up solving this issue?

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

No branches or pull requests

5 participants