summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-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.offsetNanoseconds.js21
4 files changed, 43 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 8ead23741b..f5c40e2861 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(offsetNanoseconds) \
P(ownKeys) \
P(padEnd) \
P(padStart) \
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
index 5a2d6d6343..28e9ec16a5 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
@@ -54,6 +54,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable);
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);
}
static ZonedDateTime* typed_this(GlobalObject& global_object)
@@ -651,4 +652,23 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::in_leap_year_getter)
return calendar_in_leap_year(global_object, calendar, *temporal_date_time);
}
+// 6.3.28 get Temporal.ZonedDateTime.prototype.offsetNanoseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.offsetnanoseconds
+JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::offset_nanoseconds_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 timeZone be zonedDateTime.[[TimeZone]].
+ auto& time_zone = zoned_date_time->time_zone();
+
+ // 4. Let instant be ! CreateTemporalInstant(zonedDateTime.[[Nanoseconds]]).
+ auto* instant = create_temporal_instant(global_object, zoned_date_time->nanoseconds());
+
+ // 5. Return 𝔽(? GetOffsetNanosecondsFor(timeZone, instant)).
+ return Value(get_offset_nanoseconds_for(global_object, &time_zone, *instant));
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
index 1b8331b631..6010eff4bd 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
@@ -43,6 +43,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
+ JS_DECLARE_NATIVE_FUNCTION(offset_nanoseconds_getter);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.offsetNanoseconds.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.offsetNanoseconds.js
new file mode 100644
index 0000000000..41b14995e4
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.offsetNanoseconds.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.offsetNanoseconds).toBe(0);
+ });
+
+ test("custom offset", () => {
+ const timeZone = new Temporal.TimeZone("+01:30");
+ const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone);
+ expect(zonedDateTime.offsetNanoseconds).toBe(5400000000000);
+ });
+});
+
+test("errors", () => {
+ test("this value must be a Temporal.ZonedDateTime object", () => {
+ expect(() => {
+ Reflect.get(Temporal.ZonedDateTime.prototype, "offsetNanoseconds", "foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
+ });
+});