summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-04 23:18:19 +0100
committerAndreas Kling <kling@serenityos.org>2021-08-05 19:19:40 +0200
commit6c345c8107bef3bcb81dcb303b0d0b7e126f04b1 (patch)
tree07bd7af522f0dd345fc700ef305463559ccf8e11 /Userland
parentf937e9b96638f806d54a4062bbc6253cb0775156 (diff)
downloadserenity-6c345c8107bef3bcb81dcb303b0d0b7e126f04b1.zip
LibJS: Implement Temporal.ZonedDateTime.prototype.offset
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp2
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h2
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp20
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.offset.js21
6 files changed, 45 insertions, 2 deletions
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<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, TimeZone& time_zone, Instant& instant)
+Optional<String> 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<String> builtin_time_zone_get_offset_string_for(GlobalObject&, TimeZone&, Instant&);
+Optional<String> 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");
+ });
+});