marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support
MIT License
1.08k stars 116 forks source link

`RangeError: Invalid time zone specified:` error caused by usage of `formatInTimeZone` function for some folks #264

Open wordswithchung opened 10 months ago

wordswithchung commented 10 months ago

Our DataDog was reporting this error -- RangeError: Invalid time zone specified: -- for about 15 different users internationally. The time zones it would list are definitely not invalid. Some included:

The stack trace pointed to getTimeZoneOffSet:

RangeError: Invalid time zone specified: Asia/Manila
  at getTimeZoneOffset 
  at Object.X
  at <anonymous>
  at Array.reduce 
  at format_format
  at formatInTimeZone

It's failing at tzParseTimezone, particularly where it checks isValidTimezoneIANAString(timezoneString). That function relies on the existence of Intl.DateTimeFormat to determine if a time zone is valid or not. Which doesn't work for everyone.

var validIANATimezoneCache = {}
function isValidTimezoneIANAString(timeZoneString) {
  if (validIANATimezoneCache[timeZoneString]) return true
  try {
    new Intl.DateTimeFormat(undefined, { timeZone: timeZoneString })
    validIANATimezoneCache[timeZoneString] = true
    return true
  } catch (error) {
    return false
  }
}

Would it be possible to use a different way to check in isValidTimezoneIANAString? Thank you! 🙇

sathishdevelops commented 10 months ago

Still facing the same issue for this Timezone: Asia/Kolkata, Any update or workaround for this?

CaptainJeff commented 9 months ago

Same issue happening for me in react native

tao-qian commented 8 months ago

We are having the same issue in React Native for Asia/Kolkata

tao-qian commented 8 months ago

After doing some research, my impression is that Intl.DateTimeFormat is system specific, and the Asia/Kolkata issue only happens on iOS (at least based on the errors we have seen in Sentry).

Based on https://github.com/facebook/hermes/issues/1100, we decided to manually map Asia/Kolkata to Asia/Calcutta in our code before passing the timezone to date-fns-tz. And that seemed to have helped.

toduyemi commented 7 months ago

I just had this error thrown. After wrestling with it for hours and tracing the issue to the same line as OC, I realized/found the problem was not with my timezone parameter, but with my date string (the first parameter) which was a string. I don't know why this error was getting thrown in my case, but it was very misleading for the error to flag where it did with the message it did. After changing to a UTC epoch timestamp (number), I haave has zero problems, including the timezones all of the above commenters have listed.

gabs-passarinho-garcia commented 4 months ago

Here we are having the same issue with America/Sao_Paulo and America/Cuiaba timezones

brianpooe commented 4 months ago

having the same issue with Africa/Johannesburg