Commit 6b682148 authored by Frank Tang's avatar Frank Tang Committed by V8 LUCI CQ

[intl] Return "UTC" instead of "Etc/UTC" or "Etc/GMT" as DefaultTimeZone

Refactoring the code dealing with TimeZone Canonicalization.
Change CanonicalizeTimeZoneName from return MaybeHandle<String> to Handle<String>
Move TimeZoneId from JSDateTimeFormat to Intl and return Handle<String> instead of Handle<Object>


Bug: v8:13112
Change-Id: I678b0e0d407e5e4e9dd8b7120c0e99e7e2d9c5ea
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3833435Reviewed-by: 's avatarShu-yu Guo <syg@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/main@{#82505}
parent f1033c43
......@@ -2822,21 +2822,37 @@ bool IsUnicodeStringValidTimeZoneName(const icu::UnicodeString& id) {
}
} // namespace
MaybeHandle<String> Intl::CanonicalizeTimeZoneName(Isolate* isolate,
Handle<String> identifier) {
Handle<String> Intl::CanonicalizeTimeZoneID(Isolate* isolate,
const icu::UnicodeString& id) {
UErrorCode status = U_ZERO_ERROR;
std::string time_zone =
JSDateTimeFormat::CanonicalizeTimeZoneID(identifier->ToCString().get());
icu::UnicodeString time_zone_ustring =
icu::UnicodeString(time_zone.c_str(), -1, US_INV);
icu::UnicodeString canonical;
icu::TimeZone::getCanonicalID(time_zone_ustring, canonical, status);
icu::TimeZone::getCanonicalID(id, canonical, status);
CHECK(U_SUCCESS(status));
// In CLDR (http://unicode.org/cldr/trac/ticket/9943), Etc/UTC is made
// a separate timezone ID from Etc/GMT even though they're still the same
// timezone. We have Etc/UTC because 'UTC', 'Etc/Universal',
// 'Etc/Zulu' and others are turned to 'Etc/UTC' by ICU. Etc/GMT comes
// from Etc/GMT0, Etc/GMT+0, Etc/GMT-0, Etc/Greenwich.
// ecma402#sec-canonicalizetimezonename step 3
if (canonical == UNICODE_STRING_SIMPLE("Etc/UTC") ||
canonical == UNICODE_STRING_SIMPLE("Etc/GMT")) {
return isolate->factory()->UTC_string();
}
return Intl::ToString(isolate, canonical);
return Intl::ToString(isolate, canonical).ToHandleChecked();
}
Handle<String> Intl::CanonicalizeTimeZoneName(Isolate* isolate,
Handle<String> identifier) {
std::string time_zone =
JSDateTimeFormat::CanonicalizeTimeZoneID(identifier->ToCString().get());
return CanonicalizeTimeZoneID(
isolate, icu::UnicodeString(time_zone.c_str(), -1, US_INV));
}
Handle<String> Intl::TimeZoneId(Isolate* isolate, const icu::TimeZone& tz) {
icu::UnicodeString time_zone;
tz.getID(time_zone);
return Intl::CanonicalizeTimeZoneID(isolate, time_zone);
}
bool Intl::IsValidTimeZoneName(Isolate* isolate, Handle<String> id) {
......@@ -2945,16 +2961,8 @@ Handle<String> Intl::SourceString(Isolate* isolate, FormatRangeSource source) {
}
Handle<String> Intl::DefaultTimeZone(Isolate* isolate) {
icu::UnicodeString id;
{
std::unique_ptr<icu::TimeZone> tz(icu::TimeZone::createDefault());
tz->getID(id);
}
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString canonical;
icu::TimeZone::getCanonicalID(id, canonical, status);
DCHECK(U_SUCCESS(status));
return Intl::ToString(isolate, canonical).ToHandleChecked();
std::unique_ptr<icu::TimeZone> tz(icu::TimeZone::createDefault());
return TimeZoneId(isolate, *tz);
}
namespace {
......
......@@ -390,8 +390,13 @@ class Intl {
static std::vector<int64_t> GetTimeZonePossibleOffsetMilliseconds(
Isolate* isolate, int32_t time_zone_index, int64_t time_ms);
V8_WARN_UNUSED_RESULT static MaybeHandle<String> CanonicalizeTimeZoneName(
Isolate* isolate, Handle<String> identifier);
static Handle<String> CanonicalizeTimeZoneName(Isolate* isolate,
Handle<String> identifier);
static Handle<String> CanonicalizeTimeZoneID(
Isolate* isolate, const icu::UnicodeString& identifier);
static Handle<String> TimeZoneId(Isolate* isolate, const icu::TimeZone& tz);
// ecma402/#sec-coerceoptionstoobject
V8_WARN_UNUSED_RESULT static MaybeHandle<JSReceiver> CoerceOptionsToObject(
......
......@@ -495,37 +495,6 @@ int FractionalSecondDigitsFromPattern(const std::string& pattern) {
} // namespace
Handle<Object> JSDateTimeFormat::TimeZoneId(Isolate* isolate,
const icu::TimeZone& tz) {
Factory* factory = isolate->factory();
icu::UnicodeString time_zone;
tz.getID(time_zone);
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString canonical_time_zone;
icu::TimeZone::getCanonicalID(time_zone, canonical_time_zone, status);
Handle<Object> timezone_value;
if (U_SUCCESS(status)) {
// In CLDR (http://unicode.org/cldr/trac/ticket/9943), Etc/UTC is made
// a separate timezone ID from Etc/GMT even though they're still the same
// timezone. We have Etc/UTC because 'UTC', 'Etc/Universal',
// 'Etc/Zulu' and others are turned to 'Etc/UTC' by ICU. Etc/GMT comes
// from Etc/GMT0, Etc/GMT+0, Etc/GMT-0, Etc/Greenwich.
// ecma402#sec-canonicalizetimezonename step 3
if (canonical_time_zone == UNICODE_STRING_SIMPLE("Etc/UTC") ||
canonical_time_zone == UNICODE_STRING_SIMPLE("Etc/GMT")) {
timezone_value = factory->UTC_string();
} else {
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, timezone_value, Intl::ToString(isolate, canonical_time_zone),
Handle<Object>());
}
} else {
// Somehow on Windows we will reach here.
timezone_value = factory->undefined_value();
}
return timezone_value;
}
namespace {
Handle<String> GetCalendar(Isolate* isolate,
const icu::SimpleDateFormat& simple_date_format,
......@@ -557,8 +526,8 @@ Handle<String> GetCalendar(Isolate* isolate,
Handle<Object> GetTimeZone(Isolate* isolate,
const icu::SimpleDateFormat& simple_date_format) {
return JSDateTimeFormat::TimeZoneId(
isolate, simple_date_format.getCalendar()->getTimeZone());
return Intl::TimeZoneId(isolate,
simple_date_format.getCalendar()->getTimeZone());
}
} // namespace
......
......@@ -99,7 +99,6 @@ class JSDateTimeFormat
V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
Handle<Object> static TimeZoneId(Isolate* isolate, const icu::TimeZone& tz);
std::unique_ptr<icu::TimeZone> static CreateTimeZone(const char* timezone);
V8_EXPORT_PRIVATE static std::string CanonicalizeTimeZoneID(
......
......@@ -4519,7 +4519,7 @@ bool IsValidTimeZoneName(Isolate* isolate, Handle<String> time_zone) {
Handle<String> CanonicalizeTimeZoneName(Isolate* isolate,
Handle<String> identifier) {
return Intl::CanonicalizeTimeZoneName(isolate, identifier).ToHandleChecked();
return Intl::CanonicalizeTimeZoneName(isolate, identifier);
}
#else // V8_INTL_SUPPORT
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment