react-native-device-info: Time zone never updates

I found an issue where if you change the device’s timezone and then do DeviceInfo.getTimezone() it will result in the time zone it was originally when app initially loaded and not the changed timezone.

react-native-device-info: 0.9.8 react-native: 0.46.3 Platform: iOS and Android

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 11
  • Comments: 15

Most upvoted comments

@sam1463 I wouldn’t mind adding it if a PR was submitted 😃

Ok, here are the highlights:

// iOS bridge
//  ....
RCT_EXPORT_METHOD(getTimeZone:(RCTResponseSenderBlock)callback)
{
  callback(@[@{@"timeZone": [self timeZone]}]);
}
- (NSString*)timeZone
{
  NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
  return localTimeZone.name;
}
// ...
// Android bridge
// ...
@ReactMethod
public void getTimeZone(final Callback callback) {
    try {
        WritableMap map = Arguments.createMap();
        map.putString("timeZone", TimeZone.getDefault().getID());
        callback.invoke(map);
    } catch (Exception e) {
        WritableMap map = Arguments.createMap();
        map.putString("error", "Sorry, could not get time zone. Please try again.");
        callback.invoke(map);
    }
}
// ...
// react-native side
// ...
const TimeZoneManager = NativeModules.TimeZoneManager;
// ...
function getTimeZone(callback) {
  TimeZoneManager.getTimeZone(res => callback(res.timeZone));
}
// ...