marnusw / date-fns-tz

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

Invalid time value #191

Open eq0rip opened 2 years ago

eq0rip commented 2 years ago

While the library is working well on most parts. We recently started seeing error report on Sentry Invalid time value on some of the Android 6 devices. But we were concerned when recently we had the same case reported on Android 12 device too. We debugged on emulater for android 6 and were able to recreate the error and turns our tzTokenizeDate is returning monthName instead of monthNumber.

eq0rip commented 2 years ago

I have sent a PR if someone has same issue and wants to use the solution. https://github.com/marnusw/date-fns-tz/pull/190

dmytrokruhlyi commented 2 years ago

Having the same issue only on Android devices. I've found out that getTimezoneOffset function returns NaN in all cases (tried with my local timezone and some other timezones). My current package version is 1.3.4.

farzd commented 2 years ago

same, any fix?

luizfelipelaviola commented 2 years ago

Same here, working on iOS and in everywhere else. But on Android, it returns Date { NaN } (Invalid Time) and crashes the app.

dimakruhlyi commented 2 years ago

@luizfelipelaviola I've found a solution. In your android/app/build.gradle just replace the line def jscFlavor = 'org.webkit:android-jsc:+' with def jscFlavor = 'org.webkit:android-jsc-intl:+' and it works. There was an issue with Android build system.
You can find more details here.

luizfelipelaviola commented 2 years ago

@luizfelipelaviola I've found a solution. In your android/app/build.gradle just replace the line def jscFlavor = 'org.webkit:android-jsc:+' with def jscFlavor = 'org.webkit:android-jsc-intl:+' and it works. There was an issue with Android build system. You can find more details here.

Thanks for sharing that... But this is not working with Hermes. I found the updated documentation for date-fns-tz https://github.com/marnusw/date-fns-tz#usage-with-android Time to move on to another solution...

eq0rip commented 2 years ago

@luizfelipelaviola @dimakruhlyi @dmytrokruhlyi

have you tried my PR https://github.com/marnusw/date-fns-tz/pull/190

?

it's sorted on my side with this workaround

luizfelipelaviola commented 2 years ago

I'm using RN v0.66.3 for Android with Hermes. I tried the #190 from @eq0rip and editing the build.gradle as @dimakruhlyi said but both didn't work.

The solution was follow the @marnusw recommendation and start using polyfills by Format.JS on the date-fns-tz docs.

I've installed and added all polyfills and dependencies from Format.JS and everything is working very well right now.

Just install and put this on the top of your main index.tsx file:

import '@formatjs/intl-locale/polyfill'
import '@formatjs/intl-getcanonicallocales/polyfill'
import '@formatjs/intl-pluralrules/polyfill'
import '@formatjs/intl-pluralrules/locale-data/en'
import '@formatjs/intl-numberformat/polyfill'
import '@formatjs/intl-numberformat/locale-data/en'
import '@formatjs/intl-datetimeformat/polyfill'
import '@formatjs/intl-datetimeformat/locale-data/en'

Hope that helps someone else.

See also: https://github.com/formatjs/formatjs/issues/1967