mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:17:44 +00:00
LibJS: Implement Temporal.ZonedDateTime.prototype.equals
This commit is contained in:
parent
ac12581140
commit
706296374b
5 changed files with 70 additions and 0 deletions
|
@ -646,4 +646,25 @@ ThrowCompletionOr<MarkedValueList> get_possible_instants_for(GlobalObject& globa
|
||||||
return { move(list) };
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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*> 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<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<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&);
|
bool is_valid_time_zone_numeric_utc_offset_syntax(String const&);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* 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.withPlainDate, with_plain_date, 1, attr);
|
||||||
define_native_function(vm.names.withTimeZone, with_time_zone, 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.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.valueOf, value_of, 0, attr);
|
||||||
define_native_function(vm.names.startOfDay, start_of_day, 0, attr);
|
define_native_function(vm.names.startOfDay, start_of_day, 0, attr);
|
||||||
define_native_function(vm.names.toInstant, to_instant, 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));
|
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
|
// 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof
|
||||||
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of)
|
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of)
|
||||||
{
|
{
|
||||||
|
|
|
@ -53,6 +53,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(with_plain_date);
|
JS_DECLARE_NATIVE_FUNCTION(with_plain_date);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(with_time_zone);
|
JS_DECLARE_NATIVE_FUNCTION(with_time_zone);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
|
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(start_of_day);
|
JS_DECLARE_NATIVE_FUNCTION(start_of_day);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_instant);
|
JS_DECLARE_NATIVE_FUNCTION(to_instant);
|
||||||
|
|
|
@ -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");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue