summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-07-31 21:54:35 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-01 10:24:38 +0100
commitc4123d8aadc467afe7238b8102abe6ebf25b29a0 (patch)
tree3c489338e35bf17e6a1bb6264fe6af2c4756d16a /Userland/Libraries
parentf987c1146448a5b25c3942a600eaa6832d339087 (diff)
downloadserenity-c4123d8aadc467afe7238b8102abe6ebf25b29a0.zip
LibJS: Implement Temporal.TimeZone.prototype.getOffsetStringFor()
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp14
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp22
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.getOffsetStringFor.js25
6 files changed, 64 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index ad5569f174..2ec53018fb 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/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) \
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp
index c785645662..9a3baeb1a9 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp
+++ b/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)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h
index 1821c11669..be79fc491c 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h
+++ b/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&);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp
index 6b24d3db21..5f2d9120c6 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp
+++ b/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)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h
index e69c5751cc..e7177f4820 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h
+++ b/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);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.getOffsetStringFor.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.getOffsetStringFor.js
new file mode 100644
index 0000000000..e6250cfeee
--- /dev/null
+++ b/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");
+ });
+});