diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZoneConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZoneConstructor.cpp index 3ffd8c2f29..bb7b533a36 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZoneConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZoneConstructor.cpp @@ -25,6 +25,9 @@ void TimeZoneConstructor::initialize(GlobalObject& global_object) // 11.3.1 Temporal.TimeZone.prototype, https://tc39.es/proposal-temporal/#sec-temporal-timezone-prototype define_direct_property(vm.names.prototype, global_object.temporal_time_zone_prototype(), 0); + u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.from, from, 1, attr); + define_direct_property(vm.names.length, Value(1), Attribute::Configurable); } @@ -79,4 +82,13 @@ Value TimeZoneConstructor::construct(FunctionObject& new_target) return create_temporal_time_zone(global_object, canonical, &new_target); } +// 11.3.2 Temporal.TimeZone.from ( item ) +JS_DEFINE_NATIVE_FUNCTION(TimeZoneConstructor::from) +{ + auto item = vm.argument(0); + + // 1. Return ? ToTemporalTimeZone(item). + return to_temporal_time_zone(global_object, item); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZoneConstructor.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZoneConstructor.h index 621a50ef5e..385a9b0438 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZoneConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZoneConstructor.h @@ -23,6 +23,8 @@ public: private: virtual bool has_constructor() const override { return true; } + + JS_DECLARE_NATIVE_FUNCTION(from); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js new file mode 100644 index 0000000000..f16e3f6d88 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.from.js @@ -0,0 +1,15 @@ +describe("normal behavior", () => { + test("length is 1", () => { + expect(Temporal.TimeZone.from).toHaveLength(1); + }); + + test("basic functionality", () => { + const timeZone = new Temporal.TimeZone("UTC"); + const timeZoneLike = {}; + const zonedDateTimeLike = { timeZone: {} }; + expect(Temporal.TimeZone.from(timeZone)).toBe(timeZone); + expect(Temporal.TimeZone.from(timeZoneLike)).toBe(timeZoneLike); + expect(Temporal.TimeZone.from(zonedDateTimeLike)).toBe(zonedDateTimeLike.timeZone); + // TODO: test from("string") once ParseTemporalTimeZoneString is working + }); +});