|
@@ -84,7 +84,7 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatConstructor::supported_locales_of)
|
|
|
// 11.1.2 CreateDateTimeFormat ( newTarget, locales, options, required, defaults ), https://tc39.es/ecma402/#sec-createdatetimeformat
|
|
|
ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm, FunctionObject& new_target, Value locales_value, Value options_value, OptionRequired required, OptionDefaults defaults)
|
|
|
{
|
|
|
- // 1. Let dateTimeFormat be ? OrdinaryCreateFromConstructor(newTarget, "%DateTimeFormat.prototype%", « [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]], [[Weekday]], [[Era]], [[Year]], [[Month]], [[Day]], [[DayPeriod]], [[Hour]], [[Minute]], [[Second]], [[FractionalSecondDigits]], [[TimeZoneName]], [[HourCycle]], [[DateStyle]], [[TimeStyle]], [[Pattern]], [[RangePatterns]], [[BoundFormat]] »).
|
|
|
+ // 1. Let dateTimeFormat be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.DateTimeFormat.prototype%", « [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]], [[HourCycle]], [[DateStyle]], [[TimeStyle]], [[DateTimeFormat]], [[BoundFormat]] »).
|
|
|
auto date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(vm, new_target, &Intrinsics::intl_date_time_format_prototype));
|
|
|
|
|
|
// 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
|
|
@@ -143,37 +143,35 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
// 16. Set opt.[[hc]] to hourCycle.
|
|
|
opt.hc = locale_key_from_value(hour_cycle);
|
|
|
|
|
|
- // 17. Let localeData be %DateTimeFormat%.[[LocaleData]].
|
|
|
- // 18. Let r be ResolveLocale(%DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %DateTimeFormat%.[[RelevantExtensionKeys]], localeData).
|
|
|
+ // 17. Let r be ResolveLocale(%Intl.DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %Intl.DateTimeFormat%.[[RelevantExtensionKeys]], %Intl.DateTimeFormat%.[[LocaleData]]).
|
|
|
auto result = resolve_locale(requested_locales, opt, DateTimeFormat::relevant_extension_keys());
|
|
|
|
|
|
- // 19. Set dateTimeFormat.[[Locale]] to r.[[locale]].
|
|
|
+ // 18. Set dateTimeFormat.[[Locale]] to r.[[Locale]].
|
|
|
date_time_format->set_locale(move(result.locale));
|
|
|
|
|
|
- // 20. Let resolvedCalendar be r.[[ca]].
|
|
|
- // 21. Set dateTimeFormat.[[Calendar]] to resolvedCalendar.
|
|
|
+ // 19. Let resolvedCalendar be r.[[ca]].
|
|
|
+ // 20. Set dateTimeFormat.[[Calendar]] to resolvedCalendar.
|
|
|
if (auto* resolved_calendar = result.ca.get_pointer<String>())
|
|
|
date_time_format->set_calendar(move(*resolved_calendar));
|
|
|
|
|
|
- // 22. Set dateTimeFormat.[[NumberingSystem]] to r.[[nu]].
|
|
|
+ // 21. Set dateTimeFormat.[[NumberingSystem]] to r.[[nu]].
|
|
|
if (auto* resolved_numbering_system = result.nu.get_pointer<String>())
|
|
|
date_time_format->set_numbering_system(move(*resolved_numbering_system));
|
|
|
|
|
|
- // 23. Let dataLocale be r.[[dataLocale]].
|
|
|
+ // 22. Let resolvedLocaleData be r.[[LocaleData]].
|
|
|
|
|
|
- // 24. Let dataLocaleData be localeData.[[<dataLocale>]].
|
|
|
Optional<Unicode::HourCycle> hour_cycle_value;
|
|
|
Optional<bool> hour12_value;
|
|
|
|
|
|
- // 25. If hour12 is true, then
|
|
|
- // a. Let hc be dataLocaleData.[[hourCycle12]].
|
|
|
- // 26. Else if hour12 is false, then
|
|
|
- // a. Let hc be dataLocaleData.[[hourCycle24]].
|
|
|
+ // 23. If hour12 is true, then
|
|
|
+ // a. Let hc be resolvedLocaleData.[[hourCycle12]].
|
|
|
+ // 24. Else if hour12 is false, then
|
|
|
+ // a. Let hc be resolvedLocaleData.[[hourCycle24]].
|
|
|
if (hour12.is_boolean()) {
|
|
|
// NOTE: We let LibUnicode figure out the appropriate hour cycle.
|
|
|
hour12_value = hour12.as_bool();
|
|
|
}
|
|
|
- // 27. Else,
|
|
|
+ // 25. Else,
|
|
|
else {
|
|
|
// a. Assert: hour12 is undefined.
|
|
|
VERIFY(hour12.is_undefined());
|
|
@@ -182,30 +180,27 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
if (auto* resolved_hour_cycle = result.hc.get_pointer<String>())
|
|
|
hour_cycle_value = Unicode::hour_cycle_from_string(*resolved_hour_cycle);
|
|
|
|
|
|
- // c. If hc is null, set hc to dataLocaleData.[[hourCycle]].
|
|
|
+ // c. If hc is null, set hc to resolvedLocaleData.[[hourCycle]].
|
|
|
if (!hour_cycle_value.has_value())
|
|
|
hour_cycle_value = Unicode::default_hour_cycle(date_time_format->locale());
|
|
|
}
|
|
|
|
|
|
- // 28. Set dateTimeFormat.[[HourCycle]] to hc.
|
|
|
- date_time_format->hour_cycle = hour_cycle_value;
|
|
|
-
|
|
|
- // 29. Let timeZone be ? Get(options, "timeZone").
|
|
|
+ // 26. Let timeZone be ? Get(options, "timeZone").
|
|
|
auto time_zone_value = TRY(options->get(vm.names.timeZone));
|
|
|
String time_zone;
|
|
|
|
|
|
- // 30. If timeZone is undefined, then
|
|
|
+ // 27. If timeZone is undefined, then
|
|
|
if (time_zone_value.is_undefined()) {
|
|
|
- // a. Set timeZone to DefaultTimeZone().
|
|
|
+ // a. Set timeZone to SystemTimeZoneIdentifier().
|
|
|
time_zone = system_time_zone_identifier();
|
|
|
}
|
|
|
- // 31. Else,
|
|
|
+ // 28. Else,
|
|
|
else {
|
|
|
// a. Set timeZone to ? ToString(timeZone).
|
|
|
time_zone = TRY(time_zone_value.to_string(vm));
|
|
|
}
|
|
|
|
|
|
- // 32. If IsTimeZoneOffsetString(timeZone) is true, then
|
|
|
+ // 29. If IsTimeZoneOffsetString(timeZone) is true, then
|
|
|
bool is_time_zone_offset_string = JS::is_time_zone_offset_string(time_zone);
|
|
|
|
|
|
if (is_time_zone_offset_string) {
|
|
@@ -222,7 +217,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
// d. Let offsetNanoseconds be ParseTimeZoneOffsetString(timeZone).
|
|
|
auto offset_nanoseconds = parse_time_zone_offset_string(time_zone);
|
|
|
|
|
|
- // e. Let offsetMinutes be offsetNanoseconds / (6 × 10^10).
|
|
|
+ // e. Let offsetMinutes be offsetNanoseconds / (6 × 10**10).
|
|
|
auto offset_minutes = offset_nanoseconds / 60'000'000'000;
|
|
|
|
|
|
// f. Assert: offsetMinutes is an integer.
|
|
@@ -231,7 +226,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
// g. Set timeZone to FormatOffsetTimeZoneIdentifier(offsetMinutes).
|
|
|
time_zone = format_offset_time_zone_identifier(offset_minutes);
|
|
|
}
|
|
|
- // 33. Else
|
|
|
+ // 30. Else,
|
|
|
else {
|
|
|
// a. Let timeZoneIdentifierRecord be GetAvailableNamedTimeZoneIdentifier(timeZone).
|
|
|
auto time_zone_identifier_record = get_available_named_time_zone_identifier(time_zone);
|
|
@@ -244,29 +239,29 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
time_zone = time_zone_identifier_record->primary_identifier;
|
|
|
}
|
|
|
|
|
|
- // 35. Set dateTimeFormat.[[TimeZone]] to timeZone.
|
|
|
+ // 31. Set dateTimeFormat.[[TimeZone]] to timeZone.
|
|
|
date_time_format->set_time_zone(time_zone);
|
|
|
|
|
|
// NOTE: ICU requires time zone offset strings to be of the form "GMT+00:00"
|
|
|
if (is_time_zone_offset_string)
|
|
|
time_zone = MUST(String::formatted("GMT{}", time_zone));
|
|
|
|
|
|
- // 36. Let formatOptions be a new Record.
|
|
|
+ // 32. Let formatOptions be a new Record.
|
|
|
Unicode::CalendarPattern format_options {};
|
|
|
|
|
|
- // 37. Set formatOptions.[[hourCycle]] to hc.
|
|
|
+ // 33. Set formatOptions.[[hourCycle]] to hc.
|
|
|
format_options.hour_cycle = hour_cycle_value;
|
|
|
format_options.hour12 = hour12_value;
|
|
|
|
|
|
- // 38. Let hasExplicitFormatComponents be false.
|
|
|
+ // 34. Let hasExplicitFormatComponents be false.
|
|
|
// NOTE: Instead of using a boolean, we track any explicitly provided component name for nicer exception messages.
|
|
|
PropertyKey const* explicit_format_component = nullptr;
|
|
|
|
|
|
- // 39. For each row of Table 6, except the header row, in table order, do
|
|
|
+ // 35. For each row of Table 16, except the header row, in table order, do
|
|
|
TRY(for_each_calendar_field(vm, format_options, [&](auto& option, PropertyKey const& property, auto const& values) -> ThrowCompletionOr<void> {
|
|
|
using ValueType = typename RemoveReference<decltype(option)>::ValueType;
|
|
|
|
|
|
- // a. Let prop be the name given in the Property column of the row.
|
|
|
+ // a. Let prop be the name given in the Property column of the current row.
|
|
|
|
|
|
// b. If prop is "fractionalSecondDigits", then
|
|
|
if constexpr (IsIntegral<ValueType>) {
|
|
@@ -284,7 +279,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
}
|
|
|
// c. Else,
|
|
|
else {
|
|
|
- // i. Let values be a List whose elements are the strings given in the Values column of the row.
|
|
|
+ // i. Let values be a List whose elements are the strings given in the Values column of the current row.
|
|
|
// ii. Let value be ? GetOption(options, prop, string, values, undefined).
|
|
|
auto value = TRY(get_option(vm, *options, property, OptionType::String, values, Empty {}));
|
|
|
|
|
@@ -301,24 +296,26 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
return {};
|
|
|
}));
|
|
|
|
|
|
- // 40. Let matcher be ? GetOption(options, "formatMatcher", string, « "basic", "best fit" », "best fit").
|
|
|
- matcher = TRY(get_option(vm, *options, vm.names.formatMatcher, OptionType::String, AK::Array { "basic"sv, "best fit"sv }, "best fit"sv));
|
|
|
+ // 36. Let formatMatcher be ? GetOption(options, "formatMatcher", string, « "basic", "best fit" », "best fit").
|
|
|
+ [[maybe_unused]] auto format_matcher = TRY(get_option(vm, *options, vm.names.formatMatcher, OptionType::String, AK::Array { "basic"sv, "best fit"sv }, "best fit"sv));
|
|
|
|
|
|
- // 41. Let dateStyle be ? GetOption(options, "dateStyle", string, « "full", "long", "medium", "short" », undefined).
|
|
|
+ // 37. Let dateStyle be ? GetOption(options, "dateStyle", string, « "full", "long", "medium", "short" », undefined).
|
|
|
auto date_style = TRY(get_option(vm, *options, vm.names.dateStyle, OptionType::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
|
|
|
|
|
|
- // 42. Set dateTimeFormat.[[DateStyle]] to dateStyle.
|
|
|
+ // 38. Set dateTimeFormat.[[DateStyle]] to dateStyle.
|
|
|
if (!date_style.is_undefined())
|
|
|
date_time_format->set_date_style(date_style.as_string().utf8_string_view());
|
|
|
|
|
|
- // 43. Let timeStyle be ? GetOption(options, "timeStyle", string, « "full", "long", "medium", "short" », undefined).
|
|
|
+ // 39. Let timeStyle be ? GetOption(options, "timeStyle", string, « "full", "long", "medium", "short" », undefined).
|
|
|
auto time_style = TRY(get_option(vm, *options, vm.names.timeStyle, OptionType::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
|
|
|
|
|
|
- // 44. Set dateTimeFormat.[[TimeStyle]] to timeStyle.
|
|
|
+ // 40. Set dateTimeFormat.[[TimeStyle]] to timeStyle.
|
|
|
if (!time_style.is_undefined())
|
|
|
date_time_format->set_time_style(time_style.as_string().utf8_string_view());
|
|
|
|
|
|
- // 45. If dateStyle is not undefined or timeStyle is not undefined, then
|
|
|
+ OwnPtr<Unicode::DateTimeFormat> formatter;
|
|
|
+
|
|
|
+ // 41. If dateStyle is not undefined or timeStyle is not undefined, then
|
|
|
if (date_time_format->has_date_style() || date_time_format->has_time_style()) {
|
|
|
// a. If hasExplicitFormatComponents is true, then
|
|
|
if (explicit_format_component != nullptr) {
|
|
@@ -338,18 +335,17 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
return vm.throw_completion<TypeError>(ErrorType::IntlInvalidDateTimeFormatOption, "dateStyle"sv, "time"sv);
|
|
|
}
|
|
|
|
|
|
- // d. Let styles be dataLocaleData.[[styles]].[[<resolvedCalendar>]].
|
|
|
+ // d. Let styles be resolvedLocaleData.[[styles]].[[<resolvedCalendar>]].
|
|
|
// e. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles).
|
|
|
- auto formatter = Unicode::DateTimeFormat::create_for_date_and_time_style(
|
|
|
+ formatter = Unicode::DateTimeFormat::create_for_date_and_time_style(
|
|
|
date_time_format->locale(),
|
|
|
time_zone,
|
|
|
format_options.hour_cycle,
|
|
|
format_options.hour12,
|
|
|
date_time_format->date_style(),
|
|
|
date_time_format->time_style());
|
|
|
- date_time_format->set_formatter(move(formatter));
|
|
|
}
|
|
|
- // 46. Else,
|
|
|
+ // 42. Else,
|
|
|
else {
|
|
|
// a. Let needDefaults be true.
|
|
|
bool needs_defaults = true;
|
|
@@ -359,7 +355,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
// i. For each property name prop of « "weekday", "year", "month", "day" », do
|
|
|
auto check_property_value = [&](auto const& value) {
|
|
|
// 1. Let value be formatOptions.[[<prop>]].
|
|
|
- // 2. If value is not undefined, let needDefaults be false.
|
|
|
+ // 2. If value is not undefined, set needDefaults to false.
|
|
|
if (value.has_value())
|
|
|
needs_defaults = false;
|
|
|
};
|
|
@@ -375,7 +371,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
// i. For each property name prop of « "dayPeriod", "hour", "minute", "second", "fractionalSecondDigits" », do
|
|
|
auto check_property_value = [&](auto const& value) {
|
|
|
// 1. Let value be formatOptions.[[<prop>]].
|
|
|
- // 2. If value is not undefined, let needDefaults be false.
|
|
|
+ // 2. If value is not undefined, set needDefaults to false.
|
|
|
if (value.has_value())
|
|
|
needs_defaults = false;
|
|
|
};
|
|
@@ -413,45 +409,28 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
|
|
|
set_property_value(format_options.second);
|
|
|
}
|
|
|
|
|
|
- // f. Let formats be dataLocaleData.[[formats]].[[<resolvedCalendar>]].
|
|
|
- // g. If matcher is "basic", then
|
|
|
+ // f. Let formats be resolvedLocaleData.[[formats]].[[<resolvedCalendar>]].
|
|
|
+ // g. If formatMatcher is "basic", then
|
|
|
// i. Let bestFormat be BasicFormatMatcher(formatOptions, formats).
|
|
|
// h. Else,
|
|
|
// i. Let bestFormat be BestFitFormatMatcher(formatOptions, formats).
|
|
|
- auto formatter = Unicode::DateTimeFormat::create_for_pattern_options(
|
|
|
+ formatter = Unicode::DateTimeFormat::create_for_pattern_options(
|
|
|
date_time_format->locale(),
|
|
|
time_zone,
|
|
|
format_options);
|
|
|
- date_time_format->set_formatter(move(formatter));
|
|
|
}
|
|
|
|
|
|
- // 47. For each row in Table 6, except the header row, in table order, do
|
|
|
- date_time_format->for_each_calendar_field_zipped_with(date_time_format->formatter().chosen_pattern(), [&](auto& date_time_format_field, auto const& best_format_field) {
|
|
|
- // a. Let prop be the name given in the Property column of the row.
|
|
|
- // b. If bestFormat has a field [[<prop>]], then
|
|
|
- if (best_format_field.has_value()) {
|
|
|
- // i. Let p be bestFormat.[[<prop>]].
|
|
|
- // ii. Set dateTimeFormat's internal slot whose name is the Internal Slot column of the row to p.
|
|
|
- date_time_format_field = best_format_field;
|
|
|
- }
|
|
|
- });
|
|
|
+ // 43. Set dateTimeFormat.[[DateTimeFormat]] to bestFormat.
|
|
|
+ date_time_format->set_date_time_format(formatter->chosen_pattern());
|
|
|
|
|
|
- // 48. If dateTimeFormat.[[Hour]] is undefined, then
|
|
|
- if (!date_time_format->hour.has_value()) {
|
|
|
- // a. Set dateTimeFormat.[[HourCycle]] to undefined.
|
|
|
- date_time_format->hour_cycle.clear();
|
|
|
- }
|
|
|
+ // 44. If bestFormat has a field [[hour]], then
|
|
|
+ // a. Set dateTimeFormat.[[HourCycle]] to hc.
|
|
|
+ // NOTE: The [[HourCycle]] is stored and accessed from [[DateTimeFormat]].
|
|
|
|
|
|
- // 49. If dateTimeFormat.[[HourCycle]] is "h11" or "h12", then
|
|
|
- // a. Let pattern be bestFormat.[[pattern12]].
|
|
|
- // b. Let rangePatterns be bestFormat.[[rangePatterns12]].
|
|
|
- // 50. Else,
|
|
|
- // a. Let pattern be bestFormat.[[pattern]].
|
|
|
- // b. Let rangePatterns be bestFormat.[[rangePatterns]].
|
|
|
- // 51. Set dateTimeFormat.[[Pattern]] to pattern.
|
|
|
- // 52. Set dateTimeFormat.[[RangePatterns]] to rangePatterns.
|
|
|
+ // Non-standard, create an ICU number formatter for this Intl object.
|
|
|
+ date_time_format->set_formatter(formatter.release_nonnull());
|
|
|
|
|
|
- // 53. Return dateTimeFormat.
|
|
|
+ // 45. Return dateTimeFormat.
|
|
|
return date_time_format;
|
|
|
}
|
|
|
|