LibJS: Implement the ECMA-402 PlainTime.prototype.toLocaleString.js

This commit is contained in:
Timothy Flynn 2024-11-27 16:30:12 -05:00 committed by Andreas Kling
parent ac0292b18f
commit 224304cd56
Notes: github-actions[bot] 2024-11-29 08:53:17 +00:00
2 changed files with 14 additions and 3 deletions

View file

@ -6,6 +6,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
#include <LibJS/Runtime/Temporal/Duration.h>
#include <LibJS/Runtime/Temporal/PlainTimePrototype.h>
@ -316,14 +318,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_string)
}
// 4.3.17 Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tolocalestring
// 15.12.6.1 Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-temporal.plaintime.prototype.tolocalestring
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_locale_string)
{
auto& realm = *vm.current_realm();
auto locales = vm.argument(0);
auto options = vm.argument(1);
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
auto temporal_time = TRY(typed_this_object(vm));
// 3. Return TimeRecordToString(temporalTime.[[Time]], AUTO).
return PrimitiveString::create(vm, time_record_to_string(temporal_time->time(), Auto {}));
// 3. Let dateFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, TIME, TIME).
auto date_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Time, Intl::OptionDefaults::Time));
// 4. Return ? FormatDateTime(dateFormat, temporalTime).
return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_format, temporal_time)));
}
// 4.3.18 Temporal.PlainTime.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tojson

View file

@ -5,7 +5,7 @@ describe("correct behavior", () => {
test("basic functionality", () => {
const plainTime = new Temporal.PlainTime(18, 14, 47, 123, 456, 789);
expect(plainTime.toLocaleString()).toBe("18:14:47.123456789");
expect(plainTime.toLocaleString()).toBe("6:14:47 PM");
});
});