summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2021-11-10 00:15:50 +0000
committerLinus Groh <mail@linusgroh.de>2021-11-10 12:56:56 +0000
commit6856b6168aa5002a36402bc123e280a77a05d29a (patch)
treefd27e0ebb4a187c907ee368749511d27792fb820 /Userland/Libraries
parenta9ad993e780930970d7611b23ea79df22abec9d5 (diff)
downloadserenity-6856b6168aa5002a36402bc123e280a77a05d29a.zip
LibJS: Implement Temporal.ZonedDateTime.prototype.toLocaleString
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp13
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toLocaleString.js20
3 files changed, 34 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
index de82aedc34..da32e8ec8e 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
@@ -71,6 +71,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.withCalendar, with_calendar, 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.startOfDay, start_of_day, 0, attr);
define_native_function(vm.names.toInstant, to_instant, 0, attr);
@@ -862,6 +863,18 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_string)
return js_string(vm, TRY(temporal_zoned_date_time_to_string(global_object, *zoned_date_time, precision.precision, show_calendar, show_time_zone, show_offset, precision.increment, precision.unit, rounding_mode)));
}
+// 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.
+JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_locale_string)
+{
+ // 1. Let zonedDateTime be the this value.
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
+ auto* zoned_date_time = TRY(typed_this_object(global_object));
+
+ // 3. Return ? TemporalZonedDateTimeToString(zonedDateTime, "auto", "auto", "auto", "auto").
+ return js_string(vm, TRY(temporal_zoned_date_time_to_string(global_object, *zoned_date_time, "auto"sv, "auto"sv, "auto"sv, "auto"sv)));
+}
+
// 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
index a290750734..297423704f 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
@@ -55,6 +55,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
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(start_of_day);
JS_DECLARE_NATIVE_FUNCTION(to_instant);
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toLocaleString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toLocaleString.js
new file mode 100644
index 0000000000..7890060806
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toLocaleString.js
@@ -0,0 +1,20 @@
+describe("correct behavior", () => {
+ test("length is 0", () => {
+ expect(Temporal.ZonedDateTime.prototype.toLocaleString).toHaveLength(0);
+ });
+
+ test("basic functionality", () => {
+ const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300);
+ const timeZone = new Temporal.TimeZone("UTC");
+ const zonedDateTime = plainDateTime.toZonedDateTime(timeZone);
+ expect(zonedDateTime.toLocaleString()).toBe("2021-11-03T01:33:05.1002003+00:00[UTC]");
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.ZonedDateTime object", () => {
+ expect(() => {
+ Temporal.ZonedDateTime.prototype.toLocaleString.call("foo");
+ }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime");
+ });
+});