From 697e68e68f7600015ea2b73cce022ae36ecd6087 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 27 Nov 2024 16:18:40 -0500 Subject: [PATCH] LibJS: Implement the ECMA-402 PlainDate.prototype.toLocaleString.js --- .../Runtime/Temporal/PlainDatePrototype.cpp | 16 +++++++++++++--- .../PlainDate.prototype.toLocaleString.js | 5 +---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index c9530bdcc56..2668c42ed01 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -6,6 +6,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include +#include #include #include #include @@ -475,15 +477,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_string) } // 3.3.31 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tolocalestring -// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402. +// 15.12.3.1 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-temporal.plaindate.prototype.tolocalestring JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string) { + auto& realm = *vm.current_realm(); + + auto locales = vm.argument(0); + auto options = vm.argument(1); + // 1. Let temporalDate be the this value. // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). auto temporal_date = TRY(typed_this_object(vm)); - // 3. Return TemporalDateToString(temporalDate, AUTO). - return PrimitiveString::create(vm, temporal_date_to_string(temporal_date, ShowCalendar::Auto)); + // 3. Let dateFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, DATE, DATE). + auto date_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Date, Intl::OptionDefaults::Date)); + + // 4. Return ? FormatDateTime(dateFormat, temporalDate). + return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_format, temporal_date))); } // 3.3.32 Temporal.PlainDate.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tojson diff --git a/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toLocaleString.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toLocaleString.js index 22023e26b31..55293f4c0c6 100644 --- a/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toLocaleString.js +++ b/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toLocaleString.js @@ -6,11 +6,8 @@ describe("correct behavior", () => { test("basic functionality", () => { let plainDate; - plainDate = new Temporal.PlainDate(2021, 7, 6); - expect(plainDate.toLocaleString()).toBe("2021-07-06"); - plainDate = new Temporal.PlainDate(2021, 7, 6, "gregory"); - expect(plainDate.toLocaleString()).toBe("2021-07-06[u-ca=gregory]"); + expect(plainDate.toLocaleString()).toBe("7/6/2021"); }); });