diff options
4 files changed, 28 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 7bf829fd98..efc86cb2ae 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -348,6 +348,7 @@ namespace JS { P(toLocaleString) \ P(toLocaleTimeString) \ P(toLowerCase) \ + P(toPlainDate) \ P(toString) \ P(toTemporalInstant) \ P(toTimeString) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 6dcecbec83..1cb2fe38eb 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -5,6 +5,7 @@ */ #include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Temporal/PlainDate.h> #include <LibJS/Runtime/Temporal/PlainDateTime.h> #include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h> @@ -29,6 +30,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.valueOf, value_of, 0, attr); + define_native_function(vm.names.toPlainDate, to_plain_date, 0, attr); } static PlainDateTime* typed_this(GlobalObject& global_object) @@ -65,4 +67,17 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of) return {}; } +// 5.3.37 Temporal.PlainDateTime.prototype.toPlainDate ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.toplaindate +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_date) +{ + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto* date_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return ? CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]). + return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index 6d17233dc0..28eadc2422 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -21,6 +21,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); JS_DECLARE_NATIVE_FUNCTION(value_of); + JS_DECLARE_NATIVE_FUNCTION(to_plain_date); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainDate.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainDate.js new file mode 100644 index 0000000000..7723de2a3b --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainDate.js @@ -0,0 +1,11 @@ +describe("normal behavior", () => { + test("length is 0", () => { + expect(Temporal.PlainDateTime.prototype.toPlainDate).toHaveLength(0); + }); + + test("basic functionality", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 7, 23, 0, 32, 18, 123, 456, 789); + const plainDate = plainDateTime.toPlainDate(); + expect(plainDate.equals(new Temporal.PlainDate(2021, 7, 23))).toBeTrue(); + }); +}); |