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

utcToZonedTime returns wrong value for Android #179

Closed
Gabi1M opened this issue Apr 19, 2022 · 6 comments
Closed

utcToZonedTime returns wrong value for Android #179

Gabi1M opened this issue Apr 19, 2022 · 6 comments

Comments

@Gabi1M
Copy link

Gabi1M commented Apr 19, 2022

Hi all,

I'm trying to parse an ISO 8601 string to zoned date time, but on android it returns a wrong value. On iOS it works just fine.
The string I'm trying to parse is 2022-04-19T19:00:00+05:00.

I'm using the following versions:

  • date-fns: 2.28.0
  • date-fns-tz: 1.1.2

The code I'm testing with is:

import { parseISO } from 'date-fns';
import { format, utcToZonedTime } from 'date-fns-tz';

const test = '2022-04-19T19:00:00+05:00';
const parsed = parseISO(test);
const result = utcToZonedTime(parsed, TIME_ZONE);

console.log(`Date to parse: ${test}`);
console.log(`Parsed: ${format(parsed, 'dd MMMM yyyy HH:mm zzz')}`);
console.log(`Zoned: ${format(result, 'dd MMMM yyyy HH:mm zzz')}`);

My local timezone is: Europe/Bucharest GMT+2/GMT+3 DST
TIME_ZONE is: Asia/Karachi GMT + 5

The output is as follows:

  • iOS
    Date to parse: 2022-04-19T19:00:00+05:00
    Parsed: 19 April 2022 17:00 GMT+3
    Zoned: 19 April 2022 19:00 GMT+3
  • Android
    Date to parse: 2022-04-19T19:00:00+05:00
    Parsed: 19 April 2022 17:00 '
    Zoned: 19 April 2022 07:00 '

Instead of outputing 19 for the hour, it outputs 07, regardless of HH being specified in the format string. This interferes with date calculations in our app and is producing different behaviours on iOS and Android.
I have tried on multiple android versions, on both real devices and emulator, and the result is consistent on all of them.

@marnusw
Copy link
Owner

marnusw commented Apr 19, 2022

Please see if the info in #59 solves your issue and if so perhaps consider contributing it to the readme since it seems to be a recurring problem. You could also try a polyfill for Intl on Android: https://formatjs.io/docs/polyfills

@Gabi1M
Copy link
Author

Gabi1M commented Apr 19, 2022

Thanks for the response.
Indeed, the issue you linked above solves the problem if the app doesn't use hermes. Sadly, we do use it, and disabling it is not really an option.
We do use this polyfill to include the necessary features

@marnusw
Copy link
Owner

marnusw commented Apr 20, 2022

That is unfortunate. Still, this does seem like an "Inlt doesn't work on Android" problem rather than a problem with date-fns-tz. It might be useful to try using the Intl API functions directly first until you can get that to work in an app using Hermes. I won't have time to figure this out in the near future. If you solve it please post the solution!

@marnusw
Copy link
Owner

marnusw commented Apr 28, 2022

@Gabi1M have you managed to get Intl working on Android with Hermes? I was wondering if it might work to delete window.Intl before adding a polyfill. That way everything should happen in JS. It'll be messy, but it might work in the meantime.

@marnusw
Copy link
Owner

marnusw commented Apr 28, 2022

I've added a note to the readme on usage with Android. Closing this until there is Intl support for Hermes.

@marnusw marnusw closed this as completed Apr 28, 2022
@jean-marc-cp
Copy link

Hermes has support for Intl on both iOS and Android as of RN 70.
facebook/hermes#23 (comment)

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

3 participants