diff options
author | Linus Groh <mail@linusgroh.de> | 2021-08-01 18:10:47 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-08-01 20:31:31 +0100 |
commit | 49c5f87274d5052dd771ebc7af7f74c8e167513c (patch) | |
tree | 00a7a59480f7b86a45735acaa85ee5d2c635e2fe /Userland/Libraries | |
parent | d022b74d3365323395b2697aee5bcbceffe54057 (diff) | |
download | serenity-49c5f87274d5052dd771ebc7af7f74c8e167513c.zip |
LibJS: Implement Temporal.ZonedDateTime.prototype.timeZone
Diffstat (limited to 'Userland/Libraries')
3 files changed, 30 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 0322846dd1..0d25026f60 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -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()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h index dcfcaf392b..4aca7e200b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h @@ -20,6 +20,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); + JS_DECLARE_NATIVE_FUNCTION(time_zone_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.timeZone.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.timeZone.js new file mode 100644 index 0000000000..e231bf96ce --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.timeZone.js @@ -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"); + }); +}); |