From 6f75dcc7b8e77c988164718a2b95caa4b3fc1715 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 1 Aug 2021 17:58:08 +0100 Subject: [PATCH] LibJS: Handle ZonedDateTime in ToTemporalInstant --- Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp | 10 ++++++++-- .../Tests/builtins/Temporal/Instant/Instant.from.js | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp index af1d00abdd..46963fd4f1 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace JS::Temporal { @@ -81,9 +82,14 @@ Instant* to_temporal_instant(GlobalObject& global_object, Value item) // i. Return item. return &static_cast(item.as_object()); } - // TODO: + // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then - // i. Return ! CreateTemporalInstant(item.[[Nanoseconds]]). + if (is(item.as_object())) { + auto& zoned_date_time = static_cast(item.as_object()); + + // i. Return ! CreateTemporalInstant(item.[[Nanoseconds]]). + return create_temporal_instant(global_object, zoned_date_time.nanoseconds()); + } } // 2. Let string be ? ToString(item). diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js index ea13fd2fa0..e182a91620 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js @@ -8,6 +8,12 @@ describe("correct behavior", () => { expect(Temporal.Instant.from(instant).epochNanoseconds).toBe(123n); }); + test("ZonedDateTime instance argument", () => { + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = new Temporal.ZonedDateTime(123n, timeZone); + expect(Temporal.Instant.from(zonedDateTime).epochNanoseconds).toBe(123n); + }); + // Un-skip once ParseISODateTime & ParseTemporalTimeZoneString are implemented test.skip("Instant string argument", () => { expect(Temporal.Instant.from("1975-02-02T14:25:36.123456789Z").epochNanoseconds).toBe(