mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-02 22:30:31 +00:00
LibJS: Implement the ECMA-402 PlainDateTime.prototype.toLocaleString.js
This commit is contained in:
parent
224304cd56
commit
c96f6c396f
Notes:
github-actions[bot]
2024-11-29 08:53:10 +00:00
Author: https://github.com/trflynn89
Commit: c96f6c396f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2615
2 changed files with 26 additions and 5 deletions
|
@ -6,6 +6,8 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
||||||
|
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
|
||||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||||
|
@ -532,15 +534,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5.3.35 Temporal.PlainDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.tolocalestring
|
// 5.3.35 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.
|
// 15.12.4.1 Temporal.PlainDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-properties-of-the-temporal-plaindatetime-prototype-object
|
||||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_locale_string)
|
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_locale_string)
|
||||||
{
|
{
|
||||||
|
auto& realm = *vm.current_realm();
|
||||||
|
|
||||||
|
auto locales = vm.argument(0);
|
||||||
|
auto options = vm.argument(1);
|
||||||
|
|
||||||
// 1. Let dateTime be the this value.
|
// 1. Let dateTime be the this value.
|
||||||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||||
auto date_time = TRY(typed_this_object(vm));
|
auto date_time = TRY(typed_this_object(vm));
|
||||||
|
|
||||||
// 3. Return ISODateTimeToString(dateTime.[[ISODateTime]], dateTime.[[Calendar]], AUTO, AUTO).
|
// 3. Let dateFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, ANY, ALL).
|
||||||
return PrimitiveString::create(vm, iso_date_time_to_string(date_time->iso_date_time(), date_time->calendar(), Auto {}, ShowCalendar::Auto));
|
auto date_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Any, Intl::OptionDefaults::All));
|
||||||
|
|
||||||
|
// 4. Return ? FormatDateTime(dateFormat, dateTime).
|
||||||
|
return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_format, date_time)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5.3.36 Temporal.PlainDateTime.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.tojson
|
// 5.3.36 Temporal.PlainDateTime.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.tojson
|
||||||
|
|
|
@ -4,8 +4,19 @@ describe("correct behavior", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("basic functionality", () => {
|
test("basic functionality", () => {
|
||||||
const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300);
|
const plainDateTime = new Temporal.PlainDateTime(
|
||||||
expect(plainDateTime.toLocaleString()).toBe("2021-11-03T01:33:05.1002003");
|
2021,
|
||||||
|
11,
|
||||||
|
3,
|
||||||
|
1,
|
||||||
|
33,
|
||||||
|
5,
|
||||||
|
100,
|
||||||
|
200,
|
||||||
|
300,
|
||||||
|
"gregory"
|
||||||
|
);
|
||||||
|
expect(plainDateTime.toLocaleString()).toBe("11/3/2021, 1:33:05 AM");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue