summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-05 22:13:12 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-05 23:15:27 +0100
commit82ab5da4db42fb70c1f0545b5780d7a93fd7c2b1 (patch)
tree1504d0a3fba92f846212ee40868b863eff32650e /Userland/Libraries/LibJS
parent6ce631b02378f224343018167f3e06d2cf712fa1 (diff)
downloadserenity-82ab5da4db42fb70c1f0545b5780d7a93fd7c2b1.zip
LibJS: Implement Temporal.ZonedDateTime.prototype.toPlainDateTime()
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp20
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toPlainDateTime.js29
3 files changed, 50 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
index af7179d2f8..ad355a8f32 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
@@ -64,6 +64,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.toInstant, to_instant, 0, attr);
define_native_function(vm.names.toPlainDate, to_plain_date, 0, attr);
define_native_function(vm.names.toPlainTime, to_plain_time, 0, attr);
+ define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 0, attr);
}
static ZonedDateTime* typed_this(GlobalObject& global_object)
@@ -772,4 +773,23 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_plain_time)
return create_temporal_time(global_object, temporal_date_time->iso_hour(), temporal_date_time->iso_minute(), temporal_date_time->iso_second(), temporal_date_time->iso_millisecond(), temporal_date_time->iso_microsecond(), temporal_date_time->iso_nanosecond());
}
+// 6.3.49 Temporal.ZonedDateTime.prototype.toPlainDateTime ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.toplaindatetime
+JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_plain_date_time)
+{
+ // 1. Let zonedDateTime be the this value.
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
+ auto* zoned_date_time = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Let timeZone be zonedDateTime.[[TimeZone]].
+ auto& time_zone = zoned_date_time->time_zone();
+
+ // 4. Let instant be ! CreateTemporalInstant(zonedDateTime.[[Nanoseconds]]).
+ auto* instant = create_temporal_instant(global_object, zoned_date_time->nanoseconds());
+
+ // 5. Return ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, zonedDateTime.[[Calendar]]).
+ return builtin_time_zone_get_plain_date_time_for(global_object, &time_zone, *instant, zoned_date_time->calendar());
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
index f91ed37cf5..6dd4cc8811 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
@@ -49,6 +49,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_instant);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
JS_DECLARE_NATIVE_FUNCTION(to_plain_time);
+ JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toPlainDateTime.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toPlainDateTime.js
new file mode 100644
index 0000000000..36671a784e
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toPlainDateTime.js
@@ -0,0 +1,29 @@
+describe("correct behavior", () => {
+ test("length is 0", () => {
+ expect(Temporal.ZonedDateTime.prototype.toPlainDateTime).toHaveLength(0);
+ });
+
+ test("basic functionality", () => {
+ const timeZone = new Temporal.TimeZone("UTC");
+ const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone);
+ const plainDateTime = zonedDateTime.toPlainDateTime();
+ expect(plainDateTime).toBeInstanceOf(Temporal.PlainDateTime);
+ expect(plainDateTime.year).toBe(2021);
+ expect(plainDateTime.month).toBe(7);
+ expect(plainDateTime.day).toBe(6);
+ expect(plainDateTime.hour).toBe(23);
+ expect(plainDateTime.minute).toBe(42);
+ expect(plainDateTime.second).toBe(1);
+ expect(plainDateTime.millisecond).toBe(0);
+ expect(plainDateTime.microsecond).toBe(0);
+ expect(plainDateTime.nanosecond).toBe(0);
+ });
+});
+
+test("errors", () => {
+ test("this value must be a Temporal.ZonedDateTime object", () => {
+ expect(() => {
+ Temporal.ZonedDateTime.prototype.toPlainDateTime.call("foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
+ });
+});