react-native: Time zone offset not detected on Android device

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

  1. react-native -v: 0.46.0
  2. node -v:v8.2.1
  3. npm -v:3.10.10
  4. yarn --version:0.27.5

Then, specify:

  • Target Platform: iOS and Android
  • Development Operating System: macOs sierra 10.12.6
  • Build tools: Webstorm, issue only happens on Android devices (my app targets Android 7.0 and highers).

Steps to Reproduce

(Write your steps here:)

  1. Set your device time with a timezone offset (GMT +2 in my case);
  2. create a new Date (const date = new Date()šŸ˜‰
  3. call getTimezoneOffset() on your date.

Expected Behavior

I expected my time zone offset to have 120 as value.

Actual Behavior

I get a ā€œ0ā€ offset, as if my device timezone had not been detected. The only way to get my timezone detected is to add a call to ā€œtoLocaleString()ā€ function on my date, and then, my timezone offset is correct. I donā€™t undersand the interactions between ā€œgetTimezoneOffset()ā€ and ā€œtoLocaleString()ā€.

Reproducible Demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 19
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Hello there, this issue should be re-opened šŸ˜ƒ I can confirm this issue happens on Samsung devices (tested on Galaxy S7 & S8). On a Nexus 5X, there is no problem. Tested on RN v.0.50.3

šŸ‘ for re-opening the issue. Happens on Motorola devices too (along with the devices reported earlier). Tested till RN v0.51.0 Considering a large number of devices reported, this must be fixed asap.

I have noticed this, and it doesnā€™t happen on all Android devices. When it does happen though, itā€™s a really bad bug for the user.

For my app, I can consistently reproduce on a Samsung Galaxy S7 with Android 7.0 installed. Iā€™m aware that some of my appā€™s users have also experienced it on Huawei devices. All times displayed to the user in my app are off by three hours into the future. The device shows the correct time in the OS home screen, but times in the React Native app are off.

We are using React Native 49. Restarting the React Native app does not resolve the issue.

I am facing another weird problem with new Date() in some android devices only, not in iOS.

Suppose current time in my phone is 11:55 AM (GMT+5:30). Initially new Date( ).getMinutes() returns correct minutes (55 in this case), but when I go to Date & Time Settings in my Phone and change the time zone to GMT-04:00 (2:26 AM) ,I still get the old time in console (Reactotron console). Restarting the phone apparently seem to fix this bug.

Please note that I am not using chrome debugging for logging current time. I am using Reactotron for logging. I am using RN 0.55.4 and android device MI Redmi 4. But this is happening on Samsung devices as well.

I have tried using moment also but that too didnā€™t help. I donā€™t want to write a native module for such a trivial task

Any thoughts?

We just followed what was posted by @victoriaBoucher and for us it worked quite well:

Actual Behavior (ā€¦) The only way to get my timezone detected is to add a call to ā€œtoLocaleString()ā€ function on my date, and then, my timezone offset is correct. I donā€™t undersand the interactions between ā€œgetTimezoneOffset()ā€ and ā€œtoLocaleString()ā€.

So we just put this line of code in the file that defines our outermost <App /> component, so that it is run as soon as the App is imported:

// yes, this is a silly line hanging by itself
new Date().toLocaleString();

And that fixed things. No idea why, and if it handles all the scenarios, but we didnā€™t have any issues after that.

This issue is always reproducible in Samsung devices.

Same here, iā€™m using RN v0.55.3 on Android 7 and zebra devices. When app is in debug this issue happens almost alway in the fist 15 minutes, after this time the app get right time.

I (actually my customer šŸ˜¦ ) am having this issue with RN v0.53.3 on Android.

I can also confirm this. Only way to fix it after this happens is to reboot the phone.