Skip to content

Commit

Permalink
Fix Intl.DateTimeFormat TimeZone (#627)
Browse files Browse the repository at this point in the history
Summary:
<!--
  Thanks for submitting a pull request!
  We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The two fields below are mandatory.

  Before submitting a pull request, please make sure the following is done:

  1. Fork [the repository](https://github.com/facebook/hermes) and create your branch from `main`.
  2. If you've fixed a bug or added code that should be tested, add tests!
  3. Ensure it builds and the test suite passes. [tips](https://github.com/facebook/hermes/blob/HEAD/doc/BuildingAndRunning.md)
  4. Format your code with `.../hermes/utils/format.sh`
  5. If you haven't already, complete the CLA.
-->

Please feel free to edit anything as necessary.
Picking up where #571 left off

Error:
![intl_error](https://user-images.githubusercontent.com/30021449/139603461-2ae20e1d-7ead-4fe2-ab9a-da5f647b3d19.png)
Cause: #627 (comment)

_Coauthored with anton-patrushev_ 🎉
_A special thanks to hcwesson_ 🙏

<!--
  Explain the **motivation** for making this change.
  What existing problem does the pull request solve?
-->

Pull Request resolved: #627

Test Plan:
<!--
  Demonstrate the code is solid.
  Example: The exact commands you ran and their output,
  screenshots / videos if the pull request changes the user interface.
-->

Followed testing instructions provided by mhorowitz #571 (review)

![image](https://user-images.githubusercontent.com/30021449/139600222-316bb2e1-f718-4d15-b02e-281374a26eac.png)

Reviewed By: kodafb

Differential Revision: D32240632

Pulled By: neildhar

fbshipit-source-id: d9582d5908b22addb31516834a58649182da5c64
  • Loading branch information
calebmackdavenport authored and facebook-github-bot committed Nov 12, 2021
1 parent 6cbf43a commit 9238467
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.facebook.hermes.test;

import static org.fest.assertions.api.Assertions.assertThat;

import android.content.res.AssetManager;
import android.test.InstrumentationTestCase;
import java.io.BufferedReader;
Expand All @@ -27,4 +29,44 @@ public void testIntlFromAsset() throws IOException {
rt.evaluateJavaScript(script);
}
}

@Test
public void testDateTimeFormat() {
try (JSRuntime rt = JSRuntime.makeHermesRuntime()) {
rt.evaluateJavaScript(
new StringBuilder()
.append("var date = new Date('2021-08-13T14:00:00Z');\n")
.append("var formattedDate = Intl.DateTimeFormat('en-US', {\n")
.append("timeZone: 'America/New_York',\n")
.append("day: 'numeric',\n")
.append("month: 'numeric',\n")
.append("hour: 'numeric',\n")
.append("minute: 'numeric'\n")
.append("}).format(date);\n")
.toString());

String result = rt.getGlobalStringProperty("formattedDate");
assertThat(result).isEqualTo("8/13, 10:00 AM");
}
}

@Test
public void testDateTimeFormatCaseInsensitivity() {
try (JSRuntime rt = JSRuntime.makeHermesRuntime()) {
rt.evaluateJavaScript(
new StringBuilder()
.append("var date = new Date('2021-09-24T22:00:00Z');\n")
.append("var formattedDate = Intl.DateTimeFormat('en-US', {\n")
.append("timeZone: 'AmeRiCa/new_YORK',\n")
.append("day: 'numeric',\n")
.append("month: 'numeric',\n")
.append("hour: 'numeric',\n")
.append("minute: 'numeric'\n")
.append("}).format(date);\n")
.toString());

String result = rt.getGlobalStringProperty("formattedDate");
assertThat(result).isEqualTo("9/24, 6:00 PM");
}
}
}
50 changes: 25 additions & 25 deletions lib/Platform/Intl/java/com/facebook/hermes/intl/DateTimeFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
* This class represents the Java part of the Android Intl.DateTimeFormat implementation. The
Expand Down Expand Up @@ -138,6 +139,29 @@ private Object ToDateTimeOptions(Object options, String required, String default
return options;
}

public String normalizeTimeZoneName(String timeZoneName) {
StringBuilder normalized = new StringBuilder(timeZoneName.length());
int offset = 'a' - 'A';
for (int idx = 0; idx < timeZoneName.length(); idx++) {
char c = timeZoneName.charAt(idx);
if (c >= 'A' && c <= 'Z') {
normalized.append((char) (c + offset));
} else {
normalized.append(c);
}
}
return normalized.toString();
}

public String normalizeTimeZone(String timeZone) throws JSRangeErrorException {
for (String id : TimeZone.getAvailableIDs()) {
if (normalizeTimeZoneName(id).equals(normalizeTimeZoneName(timeZone))) {
return id;
}
}
throw new JSRangeErrorException("Invalid timezone name!");
}

private Object DefaultTimeZone() throws JSRangeErrorException {
return mPlatformDateTimeFormatter.getDefaultTimeZone(mResolvedLocaleObject);
}
Expand Down Expand Up @@ -244,10 +268,7 @@ private void initializeDateTimeFormat(List<String> locales, Map<String, Object>
if (JSObjects.isUndefined(timeZone)) {
timeZone = DefaultTimeZone();
} else {
String normalizedTimeZone = normalizeTimeZoneName(JSObjects.getJavaString(timeZone));
if (!isValidTimeZoneName(normalizedTimeZone)) {
throw new JSRangeErrorException("Invalid timezone name!");
}
timeZone = normalizeTimeZone(timeZone.toString());
}
mTimeZone = timeZone;

Expand Down Expand Up @@ -379,27 +400,6 @@ private void initializeDateTimeFormat(List<String> locales, Map<String, Object>
}
}

private String normalizeTimeZoneName(String timeZoneName) {
// https://tc39.es/ecma402/#sec-case-sensitivity-and-case-mapping
// Note that we should convert only upper case translation in ASCII range.
StringBuilder normalized = new StringBuilder(timeZoneName.length());
int offset = 'a' - 'A';
for (int idx = 0; idx < timeZoneName.length(); idx++) {
char c = timeZoneName.charAt(idx);
if (c >= 'a' && c <= 'z') {
normalized.append((char) (c - offset));
} else {
normalized.append(c);
}
}

return normalized.toString();
}

private boolean isValidTimeZoneName(String timeZone) {
return mPlatformDateTimeFormatter.isValidTimeZone(timeZone);
}

@DoNotStrip
public DateTimeFormat(List<String> locales, Map<String, Object> options)
throws JSRangeErrorException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,5 @@ String getDefaultCalendarName(ILocaleObject<?> mResolvedLocaleObject)

String getDefaultNumberingSystem(ILocaleObject<?> localeObject) throws JSRangeErrorException;

boolean isValidTimeZone(String timeZone);

String[] getAvailableLocales();
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ else if (needTime)
}
}

@Override
public boolean isValidTimeZone(String timeZone) {
return TimeZone.getTimeZone(timeZone).getID().equals(timeZone);
}

@Override
public String[] getAvailableLocales() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,6 @@ public void configure(
}
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean isValidTimeZone(String timeZone) {
return TimeZone.getTimeZone(timeZone).getID().equals(timeZone);
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public String[] getAvailableLocales() {
Expand Down

0 comments on commit 9238467

Please sign in to comment.