summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-09-09 00:35:24 +0100
committerLinus Groh <mail@linusgroh.de>2021-09-09 08:52:48 +0100
commitd3fcf5a570f43b897de97916f03b7d706b211416 (patch)
tree469bafd8083b75f87dfdaeee482c5552b0c49d0f /Userland
parent6607d1dfb1fc19901ee9eb43f67345a8a64136e3 (diff)
downloadserenity-d3fcf5a570f43b897de97916f03b7d706b211416.zip
LibJS: Implement Temporal.Instant.prototype.toZonedDateTimeISO()
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp38
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toZonedDateTimeISO.js37
4 files changed, 77 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 2e33ebc392..7dbe2b197d 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -436,6 +436,7 @@ namespace JS {
P(toUpperCase) \
P(toUTCString) \
P(toZonedDateTime) \
+ P(toZonedDateTimeISO) \
P(trace) \
P(trim) \
P(trimEnd) \
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp
index e020790323..bb9f84995f 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp
@@ -49,6 +49,7 @@ void InstantPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.toJSON, to_json, 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.toZonedDateTimeISO, to_zoned_date_time_iso, 1, attr);
}
static Instant* typed_this(GlobalObject& global_object)
@@ -568,4 +569,41 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_zoned_date_time)
return create_temporal_zoned_date_time(global_object, instant->nanoseconds(), *time_zone, *calendar);
}
+// 8.3.18 Temporal.Instant.prototype.toZonedDateTimeISO ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tozoneddatetimeiso
+JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_zoned_date_time_iso)
+{
+ auto item = vm.argument(0);
+
+ // 1. Let instant be the this value.
+ // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
+ auto* instant = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. If Type(item) is Object, then
+ if (item.is_object()) {
+ // a. Let timeZoneProperty be ? Get(item, "timeZone").
+ auto time_zone_property = item.as_object().get(vm.names.timeZone);
+ if (vm.exception())
+ return {};
+
+ // b. If timeZoneProperty is not undefined, then
+ if (!time_zone_property.is_undefined()) {
+ // i. Set item to timeZoneProperty.
+ item = time_zone_property;
+ }
+ }
+
+ // 4. Let timeZone be ? ToTemporalTimeZone(item).
+ auto* time_zone = to_temporal_time_zone(global_object, item);
+ if (vm.exception())
+ return {};
+
+ // 5. Let calendar be ! GetISO8601Calendar().
+ auto* calendar = get_iso8601_calendar(global_object);
+
+ // 6. Return ? CreateTemporalZonedDateTime(instant.[[Nanoseconds]], timeZone, calendar).
+ return create_temporal_zoned_date_time(global_object, instant->nanoseconds(), *time_zone, *calendar);
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h
index 351d665b78..37e9438d71 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h
@@ -34,6 +34,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time);
+ JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time_iso);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toZonedDateTimeISO.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toZonedDateTimeISO.js
new file mode 100644
index 0000000000..7f4ac620da
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toZonedDateTimeISO.js
@@ -0,0 +1,37 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.Instant.prototype.toZonedDateTimeISO).toHaveLength(1);
+ });
+
+ // TODO: Un-skip when ParseTemporalTimeZoneString is fully implemented
+ test.skip("basic functionality", () => {
+ const instant = new Temporal.Instant(1625614921123456789n);
+ const zonedDateTime = instant.toZonedDateTimeISO("UTC");
+ expect(zonedDateTime.year).toBe(2021);
+ expect(zonedDateTime.month).toBe(7);
+ expect(zonedDateTime.day).toBe(6);
+ expect(zonedDateTime.hour).toBe(23);
+ expect(zonedDateTime.minute).toBe(42);
+ expect(zonedDateTime.second).toBe(1);
+ expect(zonedDateTime.millisecond).toBe(123);
+ expect(zonedDateTime.microsecond).toBe(456);
+ expect(zonedDateTime.nanosecond).toBe(789);
+ expect(zonedDateTime.calendar.id).toBe("iso8601");
+ expect(zonedDateTime.timeZone.id).toBe("UTC");
+ });
+
+ test("custom time zone object", () => {
+ const instant = new Temporal.Instant(1625614921123456789n);
+ const timeZone = new Temporal.TimeZone("UTC");
+ const zonedDateTime = instant.toZonedDateTimeISO({ timeZone });
+ expect(zonedDateTime.timeZone).toBe(timeZone);
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.Instant object", () => {
+ expect(() => {
+ Temporal.Instant.prototype.toZonedDateTimeISO.call("foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.Instant");
+ });
+});