diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index aa1780eab7..51aef9e12e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -67,6 +67,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.toPlainTime, to_plain_time, 0, attr); define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 0, attr); define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr); + define_native_function(vm.names.toPlainMonthDay, to_plain_month_day, 0, attr); define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); } @@ -834,6 +835,43 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_plain_year_month) return year_month_from_fields(global_object, calendar, *fields); } +// 6.3.51 Temporal.ZonedDateTime.prototype.toPlainMonthDay ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.toplainmonthday +JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_plain_month_day) +{ + // 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. Let calendar be zonedDateTime.[[Calendar]]. + auto& calendar = zoned_date_time->calendar(); + + // 6. Let temporalDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar). + auto* temporal_date_time = builtin_time_zone_get_plain_date_time_for(global_object, &time_zone, *instant, calendar); + if (vm.exception()) + return {}; + + // 7. Let fieldNames be ? CalendarFields(calendar, « "day", "monthCode" »). + auto field_names = calendar_fields(global_object, calendar, { "day"sv, "monthCode"sv }); + if (vm.exception()) + return {}; + + // 8. Let fields be ? PrepareTemporalFields(temporalDateTime, fieldNames, «»). + auto* fields = prepare_temporal_fields(global_object, *temporal_date_time, field_names, {}); + if (vm.exception()) + return {}; + + // 9. Return ? MonthDayFromFields(calendar, fields). + return month_day_from_fields(global_object, calendar, *fields); +} + // 6.3.52 Temporal.ZonedDateTime.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.getisofields JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::get_iso_fields) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h index 81257afe27..e07769543b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h @@ -51,6 +51,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(to_plain_time); JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time); JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month); + JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toPlainMonthDay.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toPlainMonthDay.js new file mode 100644 index 0000000000..7189d01b04 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toPlainMonthDay.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.ZonedDateTime.prototype.toPlainMonthDay).toHaveLength(0); + }); + + test("basic functionality", () => { + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone); + const plainMonthDay = zonedDateTime.toPlainMonthDay(); + expect(plainMonthDay.calendar).toBe(zonedDateTime.calendar); + expect(plainMonthDay.monthCode).toBe("M07"); + expect(plainMonthDay.day).toBe(6); + }); +});