facebook / hermes

A JavaScript engine optimized for running React Native.
https://hermesengine.dev/
MIT License
9.67k stars 618 forks source link

toLocaleString date function returns the wrong date for "Asia/Singapore" timezone and 1981 and older years #1485

Open nlyzhov-spotnana opened 3 weeks ago

nlyzhov-spotnana commented 3 weeks ago

Description

Hello. Recently we got a bug for showing dates, where the date was shown with -1 day. During the investigation, the issue is found only in Asia/Singapore timezone and only for dates 1981 and older. The reason for this bug is that toLocaleString converts the timezone wrong, it gives 11.30 pm of the previous day instead of 12.00 am of the current day. This bug is only on mobile, the web shows the correct date. The date returned from (new Date('1979-01-24T00:00:00').toLocaleString() : Web: 01/24/1979, 12:00:00 AM Mobile: 01/23/1979, 23:30:00 PM

Steps to reproduce

  1. Install the application for iPhone or Android (reproducible on both)
  2. Use this component to reproduce:
    
    import { StyleSheet, View, Text } from 'react-native';

const styles = StyleSheet.create({ titleContainer: { marginTop: 100, }, });

export default function HomeScreen() { const currentDateString = new Date('2024-08-01T00:00:00').toString(); const currentDateLocale = (new Date('2024-08-01T00:00:00')).toLocaleString('en-US');

const oldDateIString = new Date('1981-01-01T00:00:00').toString();
const oldLocale = (new Date('1981-01-01T00:00:00')).toLocaleString('en-US');

return (

currentDateString: {currentDateString} currentDateLocale: {currentDateLocale} ---------------------- oldDateString: {oldDateString} oldLocale: {oldLocale}

); }

3. Go to Mac settings and switch manually the timezone to `Singapore`

<img width="696" alt="image" src="https://github.com/user-attachments/assets/7cf1e463-9538-4bf2-9f65-296e809260b4">

4. Refresh the app

6. Check the `oldLocale` variable in the app: it shows `oldLocale: 12/31/1980, 11:30:00 PM`
<img width="489" alt="image" src="https://github.com/user-attachments/assets/56e353a5-2e62-49ff-8c8e-14d66b262b85">

### React Native Version

0.72 - 0.74

### Affected Platforms

Runtime - Android, Runtime - iOS

### Output of `npx react-native info`

```text
System:
  OS: macOS 14.5
  CPU: (12) arm64 Apple M3 Pro
  Memory: 87.89 MB / 36.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.19.0
    path: ~/.nvm/versions/node/v18.19.0/bin/node
  Yarn:
    version: 1.22.21
    path: /opt/homebrew/bin/yarn
  npm:
    version: 10.2.3
    path: ~/.nvm/versions/node/v18.19.0/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /Users/nlyzhov/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11330709
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 21.0.4
    path: /usr/bin/javac
  Ruby:
    version: 3.1.4
    path: /Users/nlyzhov/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.5
    wanted: 0.74.5
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

info React Native v0.75.1 is now available (your project is running on v0.74.5).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.75.1
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.74.5
info For more info, check out "https://reactnative.dev/docs/upgrading?os=macos".

Stacktrace or Logs

No crash or failure

Reproducer

https://snack.expo.dev/pJ9xRMNJBfGg39ajN7uDv

Screenshots and Videos

image
react-native-bot commented 3 weeks ago
:warning: Add or Reformat Version Info
:information_source: We could not find or parse the version number of React Native in your issue report. Please use the template, and report your version including major, minor, and patch numbers - e.g. 0.70.2
react-native-bot commented 3 weeks ago
:warning: Add or Reformat Version Info
:information_source: We could not find or parse the version number of React Native in your issue report. Please use the template, and report your version including major, minor, and patch numbers - e.g. 0.70.2
cipolleschi commented 3 weeks ago

I think this is. bug in Hermes which implements I18n. @cortinico how can we move this to the hermes repo?

neildhar commented 3 weeks ago

Hey @nlyzhov-spotnana, thanks for reporting this. I can reproduce the behaviour you're seeing. The issue seems to be that the timezone in Singapore changed in 1982 from +0730 to +0800. When we initially construct the date, we use the current timezone of +0800, but when converting it back to a localised string, the Intl implementation correctly uses the older timezone of +0730.

We'll look into this.