-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JSRangeErrorException - "Invalid timezone name!"] Fix Intl.DateTimeFormat bug with normalizing timeZone value #571
Changes from all commits
7991f8c
f5ace46
23616f7
692ef46
0846d3b
c31fe89
b68946c
bfb4de8
b483991
39953b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,16 @@ | |
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.TimeZone; | ||
import java.util.Optional; | ||
import java.util.List; | ||
import java.util.Arrays; | ||
import java.util.function.Predicate; | ||
|
||
public class PlatformDateTimeFormatterAndroid implements IPlatformDateTimeFormatter { | ||
private DateFormat mDateFormat = null; | ||
|
||
private List<String> allAvailableTimeZones = Arrays.asList(TimeZone.getAvailableIDs()); | ||
|
||
@Override | ||
public String format(double n) { | ||
return mDateFormat.format(new Date((long) n)); | ||
|
@@ -204,8 +210,21 @@ else if (needTime) | |
} | ||
|
||
@Override | ||
public boolean isValidTimeZone(String timeZone) { | ||
return TimeZone.getTimeZone(timeZone).getID().equals(timeZone); | ||
public String normalizeValidTimeZone(final String timeZone) throws JSRangeErrorException { | ||
Optional<String> normalizedValidTimeZone = allAvailableTimeZones.stream() | ||
.filter(new Predicate<String>() { | ||
@Override | ||
public boolean test(String tz) { | ||
return tz.equalsIgnoreCase(timeZone); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is incorrect. https://tc39.es/ecma402/#sec-case-sensitivity-and-case-mapping only allows mapping/folding ascii a-z <-> A-Z. equalsIgnoreCase is probably applying additional locale-specific comparisons. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest I don't find it critical. It's strange requirement. But ok, that's how it is. So I will prefer to leave it as it's right now (since it works, but with such small issues). I guess it will good enough to add test suites which will cover this case sensitivity issue in the future. And fix it. I will try to do my best and fix it according to ECMA standards and back with result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You just need to bring this back: Instead of using the built-in case insensitive comparison methods. |
||
} | ||
}).findFirst(); | ||
|
||
if(!normalizedValidTimeZone.isPresent()) { | ||
String errorMessage = timeZone + " is invalid timeZone"; | ||
throw new JSRangeErrorException(errorMessage); | ||
} | ||
|
||
return normalizedValidTimeZone.get(); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,17 @@ | |
import java.text.AttributedCharacterIterator; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.Optional; | ||
import java.util.List; | ||
import java.util.Arrays; | ||
import java.util.function.Predicate; | ||
|
||
public class PlatformDateTimeFormatterICU implements IPlatformDateTimeFormatter { | ||
private DateFormat mDateFormat = null; | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
private List<String> allAvailableTimeZones = Arrays.asList(TimeZone.getAvailableIDs()); | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
@Override | ||
public String format(double n) { | ||
|
@@ -269,8 +276,21 @@ public void configure( | |
|
||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
@Override | ||
public boolean isValidTimeZone(String timeZone) { | ||
return TimeZone.getTimeZone(timeZone).getID().equals(timeZone); | ||
public String normalizeValidTimeZone(final String timeZone) throws JSRangeErrorException { | ||
Optional<String> normalizedValidTimeZone = allAvailableTimeZones.stream() | ||
.filter(new Predicate<String>() { | ||
@Override | ||
public boolean test(String tz) { | ||
return tz.equalsIgnoreCase(timeZone); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
} | ||
}).findFirst(); | ||
|
||
if(!normalizedValidTimeZone.isPresent()) { | ||
String errorMessage = timeZone + " is invalid timeZone"; | ||
throw new JSRangeErrorException(errorMessage); | ||
} | ||
|
||
return normalizedValidTimeZone.get(); | ||
} | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just remove this try/catch, it doesn't do anything.