From 6c345c8107bef3bcb81dcb303b0d0b7e126f04b1 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 4 Aug 2021 23:18:19 +0100 Subject: LibJS: Implement Temporal.ZonedDateTime.prototype.offset --- .../Libraries/LibJS/Runtime/CommonPropertyNames.h | 1 + .../Libraries/LibJS/Runtime/Temporal/TimeZone.cpp | 2 +- .../Libraries/LibJS/Runtime/Temporal/TimeZone.h | 2 +- .../Runtime/Temporal/ZonedDateTimePrototype.cpp | 20 ++++++++++++++++++++ .../LibJS/Runtime/Temporal/ZonedDateTimePrototype.h | 1 + .../ZonedDateTime/ZonedDateTime.prototype.offset.js | 21 +++++++++++++++++++++ 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.offset.js (limited to 'Userland/Libraries/LibJS') diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index f5c40e2861..5157dd0957 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -283,6 +283,7 @@ namespace JS { P(next) \ P(now) \ P(of) \ + P(offset) \ P(offsetNanoseconds) \ P(ownKeys) \ P(padEnd) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp index 627276cc79..42beb7c217 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp @@ -416,7 +416,7 @@ double get_offset_nanoseconds_for(GlobalObject& global_object, Value time_zone, } // 11.6.12 BuiltinTimeZoneGetOffsetStringFor ( timeZone, instant ) -Optional builtin_time_zone_get_offset_string_for(GlobalObject& global_object, TimeZone& time_zone, Instant& instant) +Optional builtin_time_zone_get_offset_string_for(GlobalObject& global_object, Object& time_zone, Instant& instant) { auto& vm = global_object.vm(); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h index 664f6c594b..b173193bac 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h @@ -43,7 +43,7 @@ double parse_time_zone_offset_string(GlobalObject&, String const&); String format_time_zone_offset_string(double offset_nanoseconds); Object* to_temporal_time_zone(GlobalObject&, Value temporal_time_zone_like); double get_offset_nanoseconds_for(GlobalObject&, Value time_zone, Instant&); -Optional builtin_time_zone_get_offset_string_for(GlobalObject&, TimeZone&, Instant&); +Optional builtin_time_zone_get_offset_string_for(GlobalObject&, Object& time_zone, Instant&); PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject&, Value time_zone, Instant&, Object& calendar); bool is_valid_time_zone_numeric_utc_offset_syntax(String const&); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 28e9ec16a5..f96070a902 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -55,6 +55,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.offsetNanoseconds, offset_nanoseconds_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.offset, offset_getter, {}, Attribute::Configurable); } static ZonedDateTime* typed_this(GlobalObject& global_object) @@ -671,4 +672,23 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::offset_nanoseconds_getter) return Value(get_offset_nanoseconds_for(global_object, &time_zone, *instant)); } +// 6.3.29 get Temporal.ZonedDateTime.prototype.offset, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.offset +JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::offset_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. Let instant be ! CreateTemporalInstant(zonedDateTime.[[Nanoseconds]]). + auto* instant = create_temporal_instant(global_object, zoned_date_time->nanoseconds()); + + // 4. Return ? BuiltinTimeZoneGetOffsetStringFor(zonedDateTime.[[TimeZone]], instant). + auto offset_string = builtin_time_zone_get_offset_string_for(global_object, zoned_date_time->time_zone(), *instant); + if (vm.exception()) + return {}; + return js_string(vm, move(*offset_string)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h index 6010eff4bd..da58688653 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h @@ -44,6 +44,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter); JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter); JS_DECLARE_NATIVE_FUNCTION(offset_nanoseconds_getter); + JS_DECLARE_NATIVE_FUNCTION(offset_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.offset.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.offset.js new file mode 100644 index 0000000000..91c6b366d0 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.offset.js @@ -0,0 +1,21 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone); + expect(zonedDateTime.offset).toBe("+00:00"); + }); + + test("custom offset", () => { + const timeZone = new Temporal.TimeZone("+01:30"); + const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone); + expect(zonedDateTime.offset).toBe("+01:30"); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.ZonedDateTime object", () => { + expect(() => { + Reflect.get(Temporal.ZonedDateTime.prototype, "offset", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime"); + }); +}); -- cgit v1.2.3