1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:57:35 +00:00

LibJS: Implement Temporal.PlainDateTime.prototype.toLocaleString

This commit is contained in:
Luke Wilde 2021-11-03 01:57:57 +00:00 committed by Linus Groh
parent b2bbd4d2b8
commit 2a98b521b4
3 changed files with 32 additions and 0 deletions

View file

@ -64,6 +64,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.subtract, subtract, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.toZonedDateTime, to_zoned_date_time, 1, attr);
define_native_function(vm.names.toPlainDate, to_plain_date, 0, attr);
@ -496,6 +497,18 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_string)
return js_string(vm, TRY(temporal_date_time_to_string(global_object, result.year, result.month, result.day, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond, &date_time->calendar(), precision.precision, show_calendar)));
}
// 5.3.33 Temporal.PlainDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.tolocalestring
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_locale_string)
{
// 1. Let dateTime be the this value.
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
auto* date_time = TRY(typed_this_object(global_object));
// 3. Return ? TemporalDateTimeToString(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], dateTime.[[Calendar]], "auto", "auto").
return js_string(vm, TRY(temporal_date_time_to_string(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), &date_time->calendar(), "auto"sv, "auto"sv)));
}
// 5.3.35 Temporal.PlainDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of)
{

View file

@ -48,6 +48,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(subtract);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);

View file

@ -0,0 +1,18 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainDateTime.prototype.toLocaleString).toHaveLength(0);
});
test("basic functionality", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300);
expect(plainDateTime.toLocaleString()).toBe("2021-11-03T01:33:05.1002003");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainDateTime object", () => {
expect(() => {
Temporal.PlainDateTime.prototype.toLocaleString.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime");
});
});