summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2021-11-07 01:20:00 +0000
committerIdan Horowitz <idan.horowitz@gmail.com>2021-11-07 15:35:16 +0200
commit706296374b781225814529e0c3a07b4a5f0eb371 (patch)
treefc412c6cdd57abb9437ff6a660fc75f4fadc98df
parentac12581140e65d051fcf406b3b92ef4d7a95eb19 (diff)
downloadserenity-706296374b781225814529e0c3a07b4a5f0eb371.zip
LibJS: Implement Temporal.ZonedDateTime.prototype.equals
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp21
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp24
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.equals.js23
5 files changed, 70 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp
index 9c4c745d2c..46af02a2da 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp
@@ -646,4 +646,25 @@ ThrowCompletionOr<MarkedValueList> get_possible_instants_for(GlobalObject& globa
return { move(list) };
}
+// 11.6.18 TimeZoneEquals ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-timezoneequals
+ThrowCompletionOr<bool> time_zone_equals(GlobalObject& global_object, Object& one, Object& two)
+{
+ // 1. If one and two are the same Object value, return true.
+ if (&one == &two)
+ return true;
+
+ // 2. Let timeZoneOne be ? ToString(one).
+ auto time_zone_one = TRY(Value(&one).to_string(global_object));
+
+ // 3. Let timeZoneTwo be ? ToString(two).
+ auto time_zone_two = TRY(Value(&two).to_string(global_object));
+
+ // 4. If timeZoneOne is timeZoneTwo, return true.
+ if (time_zone_one == time_zone_two)
+ return true;
+
+ // 5. Return false.
+ return false;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h
index d0cb7b0b58..a3545cd987 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h
@@ -53,6 +53,7 @@ ThrowCompletionOr<PlainDateTime*> builtin_time_zone_get_plain_date_time_for(Glob
ThrowCompletionOr<Instant*> builtin_time_zone_get_instant_for(GlobalObject&, Value time_zone, PlainDateTime&, StringView disambiguation);
ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject&, Vector<Value> const& possible_instants, Value time_zone, PlainDateTime&, StringView disambiguation);
ThrowCompletionOr<MarkedValueList> get_possible_instants_for(GlobalObject&, Value time_zone, PlainDateTime&);
+ThrowCompletionOr<bool> time_zone_equals(GlobalObject&, Object& one, Object& two);
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 0224503483..34b764a98a 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
+ * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -68,6 +69,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.withPlainDate, with_plain_date, 1, attr);
define_native_function(vm.names.withTimeZone, with_time_zone, 1, attr);
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
+ define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.startOfDay, start_of_day, 0, attr);
define_native_function(vm.names.toInstant, to_instant, 0, attr);
@@ -808,6 +810,28 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::with_calendar)
return MUST(create_temporal_zoned_date_time(global_object, zoned_date_time->nanoseconds(), zoned_date_time->time_zone(), *calendar));
}
+// 6.3.40 Temporal.ZonedDateTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.equals
+JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::equals)
+{
+ // 1. Let zonedDateTime be the this value.
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
+ auto* zoned_date_time = TRY(typed_this_object(global_object));
+
+ // 3. Set other to ? ToTemporalZonedDateTime(other).
+ auto* other = TRY(to_temporal_zoned_date_time(global_object, vm.argument(0)));
+
+ // 4. If zonedDateTime.[[Nanoseconds]] ≠ other.[[Nanoseconds]], return false.
+ if (zoned_date_time->nanoseconds().big_integer() != other->nanoseconds().big_integer())
+ return Value(false);
+
+ // 5. If ? TimeZoneEquals(zonedDateTime.[[TimeZone]], other.[[TimeZone]]) is false, return false.
+ if (!TRY(time_zone_equals(global_object, zoned_date_time->time_zone(), other->time_zone())))
+ return Value(false);
+
+ // 6. Return ? CalendarEquals(zonedDateTime.[[Calendar]], other.[[Calendar]]).
+ return Value(TRY(calendar_equals(global_object, zoned_date_time->calendar(), other->calendar())));
+}
+
// 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
index 208a494f77..b0f268b6da 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
@@ -53,6 +53,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(with_plain_date);
JS_DECLARE_NATIVE_FUNCTION(with_time_zone);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
+ JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(start_of_day);
JS_DECLARE_NATIVE_FUNCTION(to_instant);
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.equals.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.equals.js
new file mode 100644
index 0000000000..6b8ba6f2af
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.equals.js
@@ -0,0 +1,23 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.ZonedDateTime.prototype.equals).toHaveLength(1);
+ });
+
+ test("basic functionality", () => {
+ const zonedDateTimeOne = new Temporal.ZonedDateTime(1n, new Temporal.TimeZone("UTC"));
+ const zonedDateTimeTwo = new Temporal.ZonedDateTime(2n, new Temporal.TimeZone("UTC"));
+
+ expect(zonedDateTimeOne.equals(zonedDateTimeOne)).toBeTrue();
+ expect(zonedDateTimeTwo.equals(zonedDateTimeTwo)).toBeTrue();
+ expect(zonedDateTimeOne.equals(zonedDateTimeTwo)).toBeFalse();
+ expect(zonedDateTimeTwo.equals(zonedDateTimeOne)).toBeFalse();
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.ZonedDateTime object", () => {
+ expect(() => {
+ Temporal.ZonedDateTime.prototype.equals.call("foo");
+ }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime");
+ });
+});