diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 42d334197ba..7fdf9da35a3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -38,6 +38,7 @@ void ZonedDateTimePrototype::initialize(Realm& realm) define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "Temporal.ZonedDateTime"_string), Attribute::Configurable); define_native_accessor(realm, vm.names.calendar, calendar_getter, {}, Attribute::Configurable); + define_native_accessor(realm, vm.names.calendarId, calendar_id_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.timeZone, time_zone_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.year, year_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.month, month_getter, {}, Attribute::Configurable); @@ -1401,4 +1402,17 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::get_iso_fields) return fields; } +// 6.3.3 get Temporal.ZonedDateTime.prototype.calendarId +// https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.calendarid +JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::calendar_id_getter) +{ + // 1. Let zonedDateTime be the this value. + // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). + auto zoned_date_time = TRY(typed_this_object(vm)); + + // 3. Return zonedDateTime.[[Calendar]].identifier + auto& calendar = static_cast(zoned_date_time->calendar()); + return PrimitiveString::create(vm, calendar.identifier()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h index 4ac0170f772..8708b98a932 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h @@ -23,6 +23,7 @@ private: explicit ZonedDateTimePrototype(Realm&); JS_DECLARE_NATIVE_FUNCTION(calendar_getter); + JS_DECLARE_NATIVE_FUNCTION(calendar_id_getter); JS_DECLARE_NATIVE_FUNCTION(time_zone_getter); JS_DECLARE_NATIVE_FUNCTION(year_getter); JS_DECLARE_NATIVE_FUNCTION(month_getter); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.calendarId.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.calendarId.js new file mode 100644 index 00000000000..7fcdf6f3e98 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.calendarId.js @@ -0,0 +1,19 @@ +describe("correct behavior", () => { + test("calendarId basic functionality", () => { + const calendar = "iso8601"; + const zonedDateTime = new Temporal.ZonedDateTime( + 0n, + Temporal.TimeZone.from("UTC"), + Temporal.Calendar.from(calendar) + ); + expect(zonedDateTime.calendarId).toBe("iso8601"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.ZonedDateTime object", () => { + expect(() => { + Reflect.get(Temporal.ZonedDateTime.prototype, "calendarId", "foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime"); + }); +});