summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp21
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.from.js17
2 files changed, 34 insertions, 4 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
index 9259d168d5..d5113409ff 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
@@ -8,10 +8,13 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
+#include <LibJS/Runtime/Temporal/Instant.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
#include <LibJS/Runtime/Temporal/PlainDateTime.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
+#include <LibJS/Runtime/Temporal/TimeZone.h>
+#include <LibJS/Runtime/Temporal/ZonedDateTime.h>
namespace JS::Temporal {
@@ -89,10 +92,20 @@ PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* opt
}
// b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
- // i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
- // ii. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]).
- // iii. Return ! CreateTemporalDate(plainDateTime.[[ISOYear]], plainDateTime.[[ISOMonth]], plainDateTime.[[ISODay]], plainDateTime.[[Calendar]]).
- // TODO
+ if (is<ZonedDateTime>(item_object)) {
+ auto& zoned_date_time = static_cast<ZonedDateTime&>(item_object);
+
+ // i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
+ auto* instant = create_temporal_instant(global_object, zoned_date_time.nanoseconds());
+
+ // ii. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]).
+ auto* plain_date_time = builtin_time_zone_get_plain_date_time_for(global_object, &zoned_date_time.time_zone(), *instant, zoned_date_time.calendar());
+ if (vm.exception())
+ return {};
+
+ // iii. Return ! CreateTemporalDate(plainDateTime.[[ISOYear]], plainDateTime.[[ISOMonth]], plainDateTime.[[ISODay]], plainDateTime.[[Calendar]]).
+ return create_temporal_date(global_object, plain_date_time->iso_year(), plain_date_time->iso_month(), plain_date_time->iso_day(), plain_date_time->calendar());
+ }
// c. If item has an [[InitializedTemporalDateTime]] internal slot, then
if (is<PlainDateTime>(item_object)) {
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.from.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.from.js
index d6f5730656..6519880961 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.from.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.from.js
@@ -11,6 +11,23 @@ describe("correct behavior", () => {
expect(createdPlainDate.day).toBe(26);
});
+ test("PlainDateTime instance argument", () => {
+ const plainDateTime = new Temporal.PlainDateTime(2021, 7, 26, 1, 2, 3);
+ const createdPlainDate = Temporal.PlainDate.from(plainDateTime);
+ expect(createdPlainDate.year).toBe(2021);
+ expect(createdPlainDate.month).toBe(7);
+ expect(createdPlainDate.day).toBe(26);
+ });
+
+ test("ZonedDateTime instance argument", () => {
+ const timeZone = new Temporal.TimeZone("UTC");
+ const zonedDateTime = new Temporal.ZonedDateTime(1627318123456789000n, timeZone);
+ const createdPlainDate = Temporal.PlainDate.from(zonedDateTime);
+ expect(createdPlainDate.year).toBe(2021);
+ expect(createdPlainDate.month).toBe(7);
+ expect(createdPlainDate.day).toBe(26);
+ });
+
// Un-skip once ParseISODateTime & ParseTemporalDateString are implemented
test.skip("PlainDate string argument", () => {
const createdPlainDate = Temporal.PlainDate.from("2021-07-26");