1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:47:44 +00:00

LibJS: Implement Temporal.ZonedDateTime.prototype.timeZone

This commit is contained in:
Linus Groh 2021-08-01 18:10:47 +01:00
parent d022b74d33
commit 49c5f87274
3 changed files with 30 additions and 0 deletions

View file

@ -27,6 +27,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.ZonedDateTime"), Attribute::Configurable);
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.timeZone, time_zone_getter, {}, Attribute::Configurable);
}
static ZonedDateTime* typed_this(GlobalObject& global_object)
@ -55,4 +56,17 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::calendar_getter)
return Value(&zoned_date_time->calendar());
}
// 6.3.4 get Temporal.ZonedDateTime.prototype.timeZone, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.timezone
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::time_zone_getter)
{
// 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. Return zonedDateTime.[[TimeZone]].
return Value(&zoned_date_time->time_zone());
}
}

View file

@ -20,6 +20,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(time_zone_getter);
};
}

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone);
expect(zonedDateTime.timeZone).toBe(timeZone);
});
});
test("errors", () => {
test("this value must be a Temporal.ZonedDateTime object", () => {
expect(() => {
Reflect.get(Temporal.ZonedDateTime.prototype, "timeZone", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
});
});