diff --git a/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index bbdeeabe4c2..c8a2f92722b 100644 --- a/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -7,6 +7,8 @@ */ #include +#include +#include #include #include #include @@ -15,6 +17,7 @@ #include #include #include +#include namespace JS::Temporal { @@ -785,15 +788,56 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_string) } // 6.3.42 Temporal.ZonedDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tolocalestring -// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402. +// 15.12.8.1 Temporal.ZonedDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-properties-of-the-temporal-zoneddatetime-prototype-object JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_locale_string) { + auto& realm = *vm.current_realm(); + + auto locales = vm.argument(0); + auto options = vm.argument(1); + // 1. Let zonedDateTime be the this value. // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). auto zoned_date_time = TRY(typed_this_object(vm)); - // 3. Return TemporalZonedDateTimeToString(zonedDateTime, AUTO, AUTO, AUTO, AUTO). - return PrimitiveString::create(vm, temporal_zoned_date_time_to_string(zoned_date_time, Auto {}, ShowCalendar::Auto, ShowTimeZoneName::Auto, ShowOffset::Auto)); + // 3. Let timeZone be zonedDateTime.[[TimeZone]]. + auto time_zone = zoned_date_time->time_zone(); + + // 4. Let timeZoneParseResult be ? ParseTimeZoneIdentifier(timeZone). + auto time_zone_parse_result = TRY(parse_time_zone_identifier(vm, time_zone)); + + // 5. If timeZoneParseResult.[[OffsetMinutes]] is empty, then + if (!time_zone_parse_result.offset_minutes.has_value()) { + // a. Let timeZoneIdentifierRecord be GetAvailableNamedTimeZoneIdentifier(timeZone). + auto time_zone_identifier_record = Intl::get_available_named_time_zone_identifier(time_zone); + + // b. If timeZoneIdentifierRecord is empty, throw a RangeError exception. + if (!time_zone_identifier_record.has_value()) + return vm.throw_completion(ErrorType::TemporalInvalidTimeZoneName, time_zone); + + // c. Set timeZone to timeZoneIdentifierRecord.[[Identifier]]. + time_zone = time_zone_identifier_record->identifier; + } + // 6. Else, + else { + // a. Set timeZone to FormatOffsetTimeZoneIdentifier(timeZoneParseResult.[[OffsetMinutes]]). + time_zone = format_offset_time_zone_identifier(*time_zone_parse_result.offset_minutes); + } + + // 7. Let dateTimeFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, ANY, ALL, timeZone). + auto date_time_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Any, Intl::OptionDefaults::All, time_zone)); + + // 8. If zonedDateTime.[[Calendar]] is not "iso8601" and CalendarEquals(zonedDateTime.[[Calendar]], dateTimeFormat.[[Calendar]]) is false, then + if (zoned_date_time->calendar() != "iso8601"sv && !calendar_equals(zoned_date_time->calendar(), date_time_format->calendar())) { + // a. Throw a RangeError exception. + return vm.throw_completion(ErrorType::IntlTemporalInvalidCalendar, "Temporal.ZonedDateTime"sv, zoned_date_time->calendar(), date_time_format->calendar()); + } + + // 9. Let instant be ! CreateTemporalInstant(zonedDateTime.[[EpochNanoseconds]]). + auto instant = MUST(create_temporal_instant(vm, zoned_date_time->epoch_nanoseconds())); + + // 10. Return ? FormatDateTime(dateTimeFormat, instant). + return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_time_format, instant))); } // 6.3.43 Temporal.ZonedDateTime.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tojson diff --git a/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toLocaleString.js b/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toLocaleString.js index cd0d8d1829a..ad13f755f4c 100644 --- a/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toLocaleString.js +++ b/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toLocaleString.js @@ -4,9 +4,20 @@ describe("correct behavior", () => { }); test("basic functionality", () => { - const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300); + const plainDateTime = new Temporal.PlainDateTime( + 2021, + 11, + 3, + 1, + 33, + 5, + 100, + 200, + 300, + "gregory" + ); const zonedDateTime = plainDateTime.toZonedDateTime("UTC"); - expect(zonedDateTime.toLocaleString()).toBe("2021-11-03T01:33:05.1002003+00:00[UTC]"); + expect(zonedDateTime.toLocaleString()).toBe("11/3/2021, 1:33:5 AM UTC"); }); }); @@ -16,4 +27,27 @@ describe("errors", () => { Temporal.ZonedDateTime.prototype.toLocaleString.call("foo"); }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime"); }); + + test("Temporal object must have same calendar", () => { + const plainDateTime = new Temporal.PlainDateTime( + 2021, + 11, + 3, + 1, + 33, + 5, + 100, + 200, + 300, + "gregory" + ); + const zonedDateTime = plainDateTime.toZonedDateTime("UTC"); + + expect(() => { + zonedDateTime.toLocaleString("en", { calendar: "iso8601" }); + }).toThrowWithMessage( + RangeError, + "Cannot format Temporal.ZonedDateTime with calendar 'gregory' in locale with calendar 'iso8601'" + ); + }); });