mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:57:35 +00:00
LibJS: Implement Temporal.ZonedDateTime.compare
This commit is contained in:
parent
c9ec3295d9
commit
ac12581140
3 changed files with 31 additions and 0 deletions
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
@ -31,6 +32,7 @@ void ZonedDateTimeConstructor::initialize(GlobalObject& global_object)
|
||||||
|
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_native_function(vm.names.from, from, 1, attr);
|
define_native_function(vm.names.from, from, 1, attr);
|
||||||
|
define_native_function(vm.names.compare, compare, 2, attr);
|
||||||
|
|
||||||
define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
|
define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
|
||||||
}
|
}
|
||||||
|
@ -97,4 +99,17 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimeConstructor::from)
|
||||||
return TRY(to_temporal_zoned_date_time(global_object, item, options));
|
return TRY(to_temporal_zoned_date_time(global_object, item, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 6.2.3 Temporal.ZonedDateTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.compare
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimeConstructor::compare)
|
||||||
|
{
|
||||||
|
// 1. Set one to ? ToTemporalZonedDateTime(one).
|
||||||
|
auto* one = TRY(to_temporal_zoned_date_time(global_object, vm.argument(0)));
|
||||||
|
|
||||||
|
// 2. Set two to ? ToTemporalZonedDateTime(two).
|
||||||
|
auto* two = TRY(to_temporal_zoned_date_time(global_object, vm.argument(1)));
|
||||||
|
|
||||||
|
// 3. Return 𝔽(! CompareEpochNanoseconds(one.[[Nanoseconds]], two.[[Nanoseconds]])).
|
||||||
|
return Value(compare_epoch_nanoseconds(one->nanoseconds(), two->nanoseconds()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ private:
|
||||||
virtual bool has_constructor() const override { return true; }
|
virtual bool has_constructor() const override { return true; }
|
||||||
|
|
||||||
JS_DECLARE_NATIVE_FUNCTION(from);
|
JS_DECLARE_NATIVE_FUNCTION(from);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(compare);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("length is 2", () => {
|
||||||
|
expect(Temporal.ZonedDateTime.compare).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
const zonedDateTimeOne = new Temporal.ZonedDateTime(1n, new Temporal.TimeZone("UTC"));
|
||||||
|
const zonedDateTimeTwo = new Temporal.ZonedDateTime(2n, new Temporal.TimeZone("UTC"));
|
||||||
|
|
||||||
|
expect(Temporal.ZonedDateTime.compare(zonedDateTimeOne, zonedDateTimeOne)).toBe(0);
|
||||||
|
expect(Temporal.ZonedDateTime.compare(zonedDateTimeTwo, zonedDateTimeTwo)).toBe(0);
|
||||||
|
expect(Temporal.ZonedDateTime.compare(zonedDateTimeOne, zonedDateTimeTwo)).toBe(-1);
|
||||||
|
expect(Temporal.ZonedDateTime.compare(zonedDateTimeTwo, zonedDateTimeOne)).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue