date-fns-tz icon indicating copy to clipboard operation
date-fns-tz copied to clipboard

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

Open patrik-csak opened this issue 2 years ago • 5 comments

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 avatar May 17 '22 01:05 patrik-csak

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

patrik-csak avatar May 17 '22 04:05 patrik-csak

'+02:00' should work.

marnusw avatar May 17 '22 07:05 marnusw

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 avatar May 17 '22 15:05 patrik-csak

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

marnusw avatar May 17 '22 16:05 marnusw

Here you go! https://github.com/marnusw/date-fns-tz/pull/187

patrik-csak avatar May 18 '22 00:05 patrik-csak

Hello, same issue with another time zone

const date  = new Date();

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

image

dancornilov avatar Jan 31 '23 13:01 dancornilov

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

marnusw avatar Jan 31 '23 13:01 marnusw

Closing this, continued in #187.

marnusw avatar Jan 31 '23 13:01 marnusw

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 ❤️

gunnartorfis avatar Feb 14 '23 19:02 gunnartorfis

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

jltxwesley avatar Jan 15 '24 23:01 jltxwesley