Переглянути джерело

LibJS: Implement Temporal.TimeZone.prototype.getOffsetStringFor()

Linus Groh 4 роки тому
батько
коміт
c4123d8aad

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -183,6 +183,7 @@ namespace JS {
     P(getMinutes)                            \
     P(getMonth)                              \
     P(getOffsetNanosecondsFor)               \
+    P(getOffsetStringFor)                    \
     P(getOwnPropertyDescriptor)              \
     P(getOwnPropertyDescriptors)             \
     P(getOwnPropertyNames)                   \

+ 14 - 0
Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp

@@ -410,6 +410,20 @@ double get_offset_nanoseconds_for(GlobalObject& global_object, Object& time_zone
     return offset_nanoseconds;
 }
 
+// 11.6.12 BuiltinTimeZoneGetOffsetStringFor ( timeZone, instant )
+Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, TimeZone& time_zone, Instant& instant)
+{
+    auto& vm = global_object.vm();
+
+    // 1. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
+    auto offset_nanoseconds = get_offset_nanoseconds_for(global_object, time_zone, instant);
+    if (vm.exception())
+        return {};
+
+    // 2. Return ! FormatTimeZoneOffsetString(offsetNanoseconds).
+    return format_time_zone_offset_string(offset_nanoseconds);
+}
+
 // 11.6.13 BuiltinTimeZoneGetPlainDateTimeFor ( timeZone, instant, calendar ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetplaindatetimefor
 PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject& global_object, Object& time_zone, Instant& instant, Object& calendar)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h

@@ -47,6 +47,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&, Object& time_zone, Instant&);
+Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject&, TimeZone&, Instant&);
 PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject&, Object& time_zone, Instant&, Object& calendar);
 
 bool is_valid_time_zone_numeric_utc_offset_syntax(String const&);

+ 22 - 0
Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp

@@ -27,6 +27,7 @@ void TimeZonePrototype::initialize(GlobalObject& global_object)
     u8 attr = Attribute::Writable | Attribute::Configurable;
     define_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable);
     define_native_function(vm.names.getOffsetNanosecondsFor, get_offset_nanoseconds_for, 1, attr);
+    define_native_function(vm.names.getOffsetStringFor, get_offset_string_for, 1, attr);
     define_native_function(vm.names.toString, to_string, 0, attr);
     define_native_function(vm.names.toJSON, to_json, 0, attr);
 
@@ -79,6 +80,27 @@ JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::get_offset_nanoseconds_for)
     return Value((double)get_iana_time_zone_offset_nanoseconds(instant->nanoseconds(), time_zone->identifier()));
 }
 
+// 11.4.5 Temporal.TimeZone.prototype.getOffsetStringFor ( instant ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.getoffsetstringfor
+JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::get_offset_string_for)
+{
+    // 1. Let timeZone be the this value.
+    // 2. Perform ? RequireInternalSlot(timeZone, [[InitializedTemporalTimeZone]]).
+    auto* time_zone = typed_this(global_object);
+    if (vm.exception())
+        return {};
+
+    // 3. Set instant to ? ToTemporalInstant(instant).
+    auto* instant = to_temporal_instant(global_object, vm.argument(0));
+    if (vm.exception())
+        return {};
+
+    // 4. Return ? BuiltinTimeZoneGetOffsetStringFor(timeZone, instant).
+    auto offset_string = builtin_time_zone_get_offset_string_for(global_object, *time_zone, *instant);
+    if (vm.exception())
+        return {};
+    return js_string(vm, move(*offset_string));
+}
+
 // 11.4.11 Temporal.TimeZone.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.tostring
 JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_string)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h

@@ -21,6 +21,7 @@ public:
 private:
     JS_DECLARE_NATIVE_FUNCTION(id_getter);
     JS_DECLARE_NATIVE_FUNCTION(get_offset_nanoseconds_for);
+    JS_DECLARE_NATIVE_FUNCTION(get_offset_string_for);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
 };

+ 25 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.getOffsetStringFor.js

@@ -0,0 +1,25 @@
+describe("correct behavior", () => {
+    test("length is 1", () => {
+        expect(Temporal.TimeZone.prototype.getOffsetStringFor).toHaveLength(1);
+    });
+
+    test("basic functionality", () => {
+        const timeZone = new Temporal.TimeZone("UTC");
+        const instant = new Temporal.Instant(0n);
+        expect(timeZone.getOffsetStringFor(instant)).toBe("+00:00");
+    });
+
+    test("custom offset", () => {
+        const timeZone = new Temporal.TimeZone("+01:30");
+        const instant = new Temporal.Instant(0n);
+        expect(timeZone.getOffsetStringFor(instant)).toBe("+01:30");
+    });
+});
+
+test("errors", () => {
+    test("this value must be a Temporal.TimeZone object", () => {
+        expect(() => {
+            Temporal.TimeZone.prototype.getOffsetStringFor.call("foo");
+        }).toThrowWithMessage(TypeError, "Not a Temporal.TimeZone");
+    });
+});