mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Implement Temporal.PlainDateTime.prototype.since/until
This commit is contained in:
parent
85ffacacde
commit
906d951104
Notes:
github-actions[bot]
2024-11-24 10:45:21 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/906d9511044 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2544
6 changed files with 244 additions and 0 deletions
|
@ -367,6 +367,42 @@ ThrowCompletionOr<Crypto::BigFraction> difference_plain_date_time_with_total(VM&
|
|||
return TRY(total_relative_duration(vm, diff, dest_epoch_ns, iso_date_time1, {}, calendar, unit));
|
||||
}
|
||||
|
||||
// 5.5.15 DifferenceTemporalPlainDateTime ( operation, dateTime, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplaindatetime
|
||||
ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_date_time(VM& vm, DurationOperation operation, PlainDateTime const& date_time, Value other_value, Value options)
|
||||
{
|
||||
// 1. Set other to ? ToTemporalDateTime(other).
|
||||
auto other = TRY(to_temporal_date_time(vm, other_value));
|
||||
|
||||
// 2. If CalendarEquals(dateTime.[[Calendar]], other.[[Calendar]]) is false, throw a RangeError exception.
|
||||
if (!calendar_equals(date_time.calendar(), other->calendar()))
|
||||
return vm.throw_completion<RangeError>(ErrorType::TemporalDifferentCalendars);
|
||||
|
||||
// 3. Let resolvedOptions be ? GetOptionsObject(options).
|
||||
auto resolved_options = TRY(get_options_object(vm, options));
|
||||
|
||||
// 4. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, DATETIME, « », NANOSECOND, DAY).
|
||||
auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::DateTime, {}, Unit::Nanosecond, Unit::Day));
|
||||
|
||||
// 5. If CompareISODateTime(dateTime.[[ISODateTime]], other.[[ISODateTime]]) = 0, then
|
||||
if (compare_iso_date_time(date_time.iso_date_time(), other->iso_date_time()) == 0) {
|
||||
// a. Return ! CreateTemporalDuration(0, 0, 0, 0, 0, 0, 0, 0, 0, 0).
|
||||
return MUST(create_temporal_duration(vm, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
// 6. Let internalDuration be ? DifferencePlainDateTimeWithRounding(dateTime.[[ISODateTime]], other.[[ISODateTime]], dateTime.[[Calendar]], settings.[[LargestUnit]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]).
|
||||
auto internal_duration = TRY(difference_plain_date_time_with_rounding(vm, date_time.iso_date_time(), other->iso_date_time(), date_time.calendar(), settings.largest_unit, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode));
|
||||
|
||||
// 7. Let result be ? TemporalDurationFromInternal(internalDuration, settings.[[LargestUnit]]).
|
||||
auto result = TRY(temporal_duration_from_internal(vm, internal_duration, settings.largest_unit));
|
||||
|
||||
// 8. If operation is SINCE, set result to CreateNegatedTemporalDuration(result).
|
||||
if (operation == DurationOperation::Since)
|
||||
result = create_negated_temporal_duration(vm, result);
|
||||
|
||||
// 9. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
// 5.5.16 AddDurationToDateTime ( operation, dateTime, temporalDurationLike, options ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtodatetime
|
||||
ThrowCompletionOr<GC::Ref<PlainDateTime>> add_duration_to_date_time(VM& vm, ArithmeticOperation operation, PlainDateTime const& date_time, Value temporal_duration_like, Value options)
|
||||
{
|
||||
|
|
|
@ -44,6 +44,7 @@ ISODateTime round_iso_date_time(ISODateTime const&, u64 increment, Unit, Roundin
|
|||
ThrowCompletionOr<InternalDuration> difference_iso_date_time(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit);
|
||||
ThrowCompletionOr<InternalDuration> difference_plain_date_time_with_rounding(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit, u64 rounding_increment, Unit smallest_unit, RoundingMode);
|
||||
ThrowCompletionOr<Crypto::BigFraction> difference_plain_date_time_with_total(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit);
|
||||
ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_date_time(VM&, DurationOperation, PlainDateTime const&, Value other, Value options);
|
||||
ThrowCompletionOr<GC::Ref<PlainDateTime>> add_duration_to_date_time(VM&, ArithmeticOperation, PlainDateTime const&, Value temporal_duration_like, Value options);
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h>
|
||||
|
||||
namespace JS::Temporal {
|
||||
|
@ -55,6 +56,8 @@ void PlainDateTimePrototype::initialize(Realm& realm)
|
|||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(realm, vm.names.add, add, 1, attr);
|
||||
define_native_function(realm, vm.names.subtract, subtract, 1, attr);
|
||||
define_native_function(realm, vm.names.until, until, 1, attr);
|
||||
define_native_function(realm, vm.names.since, since, 1, attr);
|
||||
define_native_function(realm, vm.names.equals, equals, 1, attr);
|
||||
define_native_function(realm, vm.names.toString, to_string, 0, attr);
|
||||
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
|
||||
|
@ -244,6 +247,34 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::subtract)
|
|||
return TRY(add_duration_to_date_time(vm, ArithmeticOperation::Subtract, date_time, temporal_duration_like, options));
|
||||
}
|
||||
|
||||
// 5.3.30 Temporal.PlainDateTime.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.until
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::until)
|
||||
{
|
||||
auto other = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let dateTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||
auto date_time = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? DifferenceTemporalPlainDateTime(UNTIL, dateTime, other, options).
|
||||
return TRY(difference_temporal_plain_date_time(vm, DurationOperation::Until, date_time, other, options));
|
||||
}
|
||||
|
||||
// 5.3.31 Temporal.PlainDateTime.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.since
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::since)
|
||||
{
|
||||
auto other = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let dateTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||
auto date_time = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? DifferenceTemporalPlainDateTime(SINCE, dateTime, other, options).
|
||||
return TRY(difference_temporal_plain_date_time(vm, DurationOperation::Since, date_time, other, options));
|
||||
}
|
||||
|
||||
// 5.3.33 Temporal.PlainDateTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.equals
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::equals)
|
||||
{
|
||||
|
|
|
@ -47,6 +47,8 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(add);
|
||||
JS_DECLARE_NATIVE_FUNCTION(subtract);
|
||||
JS_DECLARE_NATIVE_FUNCTION(until);
|
||||
JS_DECLARE_NATIVE_FUNCTION(since);
|
||||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDateTime.prototype.since).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const values = [
|
||||
[[0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0], "PT0S"],
|
||||
[[2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9], "P394DT1H1M1.001001001S"],
|
||||
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 1, 0, 0, 0, 0, 0, 0], "P399DT4H5M6.007008009S"],
|
||||
[[0, 1, 1, 0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8, 9], "-P399DT4H5M6.007008009S"],
|
||||
[
|
||||
[0, 12, 31, 23, 59, 59, 999, 999, 999],
|
||||
[0, 1, 1, 0, 0, 0, 0, 0, 0],
|
||||
"P365DT23H59M59.999999999S",
|
||||
],
|
||||
[
|
||||
[0, 1, 1, 0, 0, 0, 0, 0, 0],
|
||||
[0, 12, 31, 23, 59, 59, 999, 999, 999],
|
||||
"-P365DT23H59M59.999999999S",
|
||||
],
|
||||
];
|
||||
for (const [args, argsOther, expected] of values) {
|
||||
const plainDateTime = new Temporal.PlainDateTime(...args);
|
||||
const other = new Temporal.PlainDateTime(...argsOther);
|
||||
expect(plainDateTime.since(other).toString()).toBe(expected);
|
||||
}
|
||||
});
|
||||
|
||||
test("smallestUnit option", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
const other = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0);
|
||||
const values = [
|
||||
["year", "P1Y"],
|
||||
["month", "P13M"],
|
||||
["week", "P57W"],
|
||||
["day", "P399D"],
|
||||
["hour", "P399DT4H"],
|
||||
["minute", "P399DT4H5M"],
|
||||
["second", "P399DT4H5M6S"],
|
||||
["millisecond", "P399DT4H5M6.007S"],
|
||||
["microsecond", "P399DT4H5M6.007008S"],
|
||||
["nanosecond", "P399DT4H5M6.007008009S"],
|
||||
];
|
||||
for (const [smallestUnit, expected] of values) {
|
||||
expect(plainDateTime.since(other, { smallestUnit }).toString()).toBe(expected);
|
||||
}
|
||||
});
|
||||
|
||||
test("largestUnit option", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
const other = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0);
|
||||
const values = [
|
||||
["year", "P1Y1M2DT4H5M6.007008009S"],
|
||||
["month", "P13M2DT4H5M6.007008009S"],
|
||||
["week", "P57WT4H5M6.007008009S"],
|
||||
["day", "P399DT4H5M6.007008009S"],
|
||||
["hour", "PT9580H5M6.007008009S"],
|
||||
["minute", "PT574805M6.007008009S"],
|
||||
["second", "PT34488306.007008009S"],
|
||||
["millisecond", "PT34488306.007008009S"],
|
||||
["microsecond", "PT34488306.007008009S"],
|
||||
["nanosecond", "PT34488306.007008008S"],
|
||||
];
|
||||
for (const [largestUnit, expected] of values) {
|
||||
expect(plainDateTime.since(other, { largestUnit }).toString()).toBe(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainDateTime object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainDateTime.prototype.since.call("foo", {});
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime");
|
||||
});
|
||||
|
||||
test("cannot compare dates from different calendars", () => {
|
||||
const args = [1970, 1, 1, 0, 0, 0, 0, 0, 0];
|
||||
const plainDateTimeOne = new Temporal.PlainDateTime(...args, "iso8601");
|
||||
const plainDateTimeTwo = new Temporal.PlainDateTime(...args, "gregory");
|
||||
|
||||
expect(() => {
|
||||
plainDateTimeOne.since(plainDateTimeTwo);
|
||||
}).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,87 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDateTime.prototype.until).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const values = [
|
||||
[[0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0], "PT0S"],
|
||||
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9, 10], "P394DT1H1M1.001001001S"],
|
||||
[[0, 1, 1, 0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8, 9], "P399DT4H5M6.007008009S"],
|
||||
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 1, 0, 0, 0, 0, 0, 0], "-P399DT4H5M6.007008009S"],
|
||||
[
|
||||
[0, 1, 1, 0, 0, 0, 0, 0, 0],
|
||||
[0, 12, 31, 23, 59, 59, 999, 999, 999],
|
||||
"P365DT23H59M59.999999999S",
|
||||
],
|
||||
[
|
||||
[0, 12, 31, 23, 59, 59, 999, 999, 999],
|
||||
[0, 1, 1, 0, 0, 0, 0, 0, 0],
|
||||
"-P365DT23H59M59.999999999S",
|
||||
],
|
||||
];
|
||||
for (const [args, argsOther, expected] of values) {
|
||||
const plainDateTime = new Temporal.PlainDateTime(...args);
|
||||
const other = new Temporal.PlainDateTime(...argsOther);
|
||||
expect(plainDateTime.until(other).toString()).toBe(expected);
|
||||
}
|
||||
});
|
||||
|
||||
test("smallestUnit option", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0);
|
||||
const other = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
const values = [
|
||||
["year", "P1Y"],
|
||||
["month", "P13M"],
|
||||
["week", "P57W"],
|
||||
["day", "P399D"],
|
||||
["hour", "P399DT4H"],
|
||||
["minute", "P399DT4H5M"],
|
||||
["second", "P399DT4H5M6S"],
|
||||
["millisecond", "P399DT4H5M6.007S"],
|
||||
["microsecond", "P399DT4H5M6.007008S"],
|
||||
["nanosecond", "P399DT4H5M6.007008009S"],
|
||||
];
|
||||
for (const [smallestUnit, expected] of values) {
|
||||
expect(plainDateTime.until(other, { smallestUnit }).toString()).toBe(expected);
|
||||
}
|
||||
});
|
||||
|
||||
test("largestUnit option", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0);
|
||||
const other = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
const values = [
|
||||
["year", "P1Y1M2DT4H5M6.007008009S"],
|
||||
["month", "P13M2DT4H5M6.007008009S"],
|
||||
["week", "P57WT4H5M6.007008009S"],
|
||||
["day", "P399DT4H5M6.007008009S"],
|
||||
["hour", "PT9580H5M6.007008009S"],
|
||||
["minute", "PT574805M6.007008009S"],
|
||||
["second", "PT34488306.007008009S"],
|
||||
["millisecond", "PT34488306.007008009S"],
|
||||
["microsecond", "PT34488306.007008009S"],
|
||||
["nanosecond", "PT34488306.007008008S"],
|
||||
];
|
||||
for (const [largestUnit, expected] of values) {
|
||||
expect(plainDateTime.until(other, { largestUnit }).toString()).toBe(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainDateTime object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainDateTime.prototype.until.call("foo", {});
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime");
|
||||
});
|
||||
|
||||
test("cannot compare dates from different calendars", () => {
|
||||
const args = [1970, 1, 1, 0, 0, 0, 0, 0, 0];
|
||||
const plainDateTimeOne = new Temporal.PlainDateTime(...args, "iso8601");
|
||||
const plainDateTimeTwo = new Temporal.PlainDateTime(...args, "gregory");
|
||||
|
||||
expect(() => {
|
||||
plainDateTimeOne.until(plainDateTimeTwo);
|
||||
}).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars");
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue