From 4e158327c868e5be964a8d6be8d557b9369756b1 Mon Sep 17 00:00:00 2001 From: Danny Su Date: Mon, 17 Mar 2025 14:04:30 -0700 Subject: [PATCH] Fix invalid timezone errors for valid timezones on Apple platforms. (#1611) (#1611) Summary: Original Author: chris@nschris.com Original Git: b8ad3c1117a71c86158bda9dae556237ecdfb585 Original Reviewed By: avp Original Revision: D70109465 This addresses issue https://github.com/facebook/hermes/issues/1607 where valid timezones were raising invalid time zone errors. The root of the issue is that hermes relies on `NSTimeZone.knownTimeZoneNames` to determine valid time zones, but this does not provide a complete list of all time zones NSTimeZone supports. `US/Eastern` is an example of a timezone that is not listed in knownTimeZoneNames but will generate a valid NSTimeZone. To address this problem this will first attempt to create an NSTimeZone before raising the range exception. If it's a valid time zone then it will get added to the validTimeZoneNames list, otherwise an exception will be raised. Pull Request resolved: https://github.com/facebook/hermes/pull/1611 Pulled By: lavenzg Reviewed By: lavenzg Differential Revision: D71332435 fbshipit-source-id: 3f8c11003d31736c28bab64a3362fc3cf5946379 --- lib/Platform/Intl/PlatformIntlApple.mm | 11 +++++++++-- test/hermes/intl/date-time-format-apple.js | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/Platform/Intl/PlatformIntlApple.mm b/lib/Platform/Intl/PlatformIntlApple.mm index 090bb952350..97b9e8f0b56 100644 --- a/lib/Platform/Intl/PlatformIntlApple.mm +++ b/lib/Platform/Intl/PlatformIntlApple.mm @@ -1462,8 +1462,15 @@ uint8_t getCurrencyDigits(std::u16string_view code) { timeZone = timeZoneIt->second.getString(); // b. If the result of IsValidTimeZoneName(timeZone) is false, then if (!isValidTimeZoneName(timeZone)) { - // i. Throw a RangeError exception. - return runtime.raiseRangeError("Incorrect timeZone information provided"); + if ([[NSTimeZone alloc] initWithName:u16StringToNSString(timeZone)] != + nil) { + validTimeZoneNames().update(timeZone); + } else { + // i. Throw a RangeError exception. + return runtime.raiseRangeError( + vm::TwineChar16("Incorrect timeZone information provided: ") + + vm::TwineChar16(timeZone.c_str())); + } } // c. Let timeZone be CanonicalizeTimeZoneName(timeZone). timeZone = canonicalizeTimeZoneName(timeZone); diff --git a/test/hermes/intl/date-time-format-apple.js b/test/hermes/intl/date-time-format-apple.js index 83e1c28e50b..ac498e35883 100644 --- a/test/hermes/intl/date-time-format-apple.js +++ b/test/hermes/intl/date-time-format-apple.js @@ -39,6 +39,9 @@ print(new Intl.DateTimeFormat('en-US', { timeStyle: 'long', timeZone: 'PST'}).fo print(new Intl.DateTimeFormat('en-US', { timeStyle: 'long', timeZone: 'EET'}).format(date)); // CHECK-NEXT: 5:45:00{{.+}}AM GMT+2 +print(new Intl.DateTimeFormat("en-US", { timeZone: "US/Eastern", timeZoneName: "short" }).resolvedOptions().timeZone); +// CHECK-NEXT: US/Eastern + try { print(new Intl.DateTimeFormat('en-US', { timeStyle: 'long', timeZone: 'XXX'}).format(date)); print("Succeeded");