From 477f00aced40fe28e2513c012b0f1e013d1b8a49 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 24 Nov 2024 16:42:50 -0500 Subject: [PATCH] LibJS: Implement Temporal.Instant.prototype.until/since --- Libraries/LibJS/Runtime/Temporal/Instant.cpp | 39 ++++ Libraries/LibJS/Runtime/Temporal/Instant.h | 2 + .../Runtime/Temporal/InstantPrototype.cpp | 31 +++ .../LibJS/Runtime/Temporal/InstantPrototype.h | 2 + .../Instant/Instant.prototype.since.js | 190 ++++++++++++++++++ .../Instant/Instant.prototype.until.js | 190 ++++++++++++++++++ 6 files changed, 454 insertions(+) create mode 100644 Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.since.js create mode 100644 Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.until.js diff --git a/Libraries/LibJS/Runtime/Temporal/Instant.cpp b/Libraries/LibJS/Runtime/Temporal/Instant.cpp index 7731a74bb55..037de6d9c2b 100644 --- a/Libraries/LibJS/Runtime/Temporal/Instant.cpp +++ b/Libraries/LibJS/Runtime/Temporal/Instant.cpp @@ -179,6 +179,19 @@ ThrowCompletionOr add_instant(VM& vm, Crypto::SignedBi return result; } +// 8.5.6 DifferenceInstant ( ns1, ns2, roundingIncrement, smallestUnit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-differenceinstant +InternalDuration difference_instant(VM& vm, Crypto::SignedBigInteger const& nanoseconds1, Crypto::SignedBigInteger const& nanoseconds2, u64 rounding_increment, Unit smallest_unit, RoundingMode rounding_mode) +{ + // 1. Let timeDuration be TimeDurationFromEpochNanosecondsDifference(ns2, ns1). + auto time_duration = time_duration_from_epoch_nanoseconds_difference(nanoseconds2, nanoseconds1); + + // 2. Set timeDuration to ! RoundTimeDuration(timeDuration, roundingIncrement, smallestUnit, roundingMode). + time_duration = MUST(round_time_duration(vm, time_duration, Crypto::UnsignedBigInteger { rounding_increment }, smallest_unit, rounding_mode)); + + // 3. Return ! CombineDateAndTimeDuration(ZeroDateDuration(), timeDuration). + return MUST(combine_date_and_time_duration(vm, zero_date_duration(vm), move(time_duration))); +} + // 8.5.7 RoundTemporalInstant ( ns, increment, unit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundtemporalinstant Crypto::SignedBigInteger round_temporal_instant(Crypto::SignedBigInteger const& nanoseconds, u64 increment, Unit unit, RoundingMode rounding_mode) { @@ -228,6 +241,32 @@ String temporal_instant_to_string(Instant const& instant, Optional t return MUST(String::formatted("{}{}", date_time_string, time_zone_string)); } +// 8.5.9 DifferenceTemporalInstant ( operation, instant, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalinstant +ThrowCompletionOr> difference_temporal_instant(VM& vm, DurationOperation operation, Instant const& instant, Value other_value, Value options) +{ + // 1. Set other to ? ToTemporalInstant(other). + auto other = TRY(to_temporal_instant(vm, other_value)); + + // 2. Let resolvedOptions be ? GetOptionsObject(options). + auto resolved_options = TRY(get_options_object(vm, options)); + + // 3. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, TIME, « », NANOSECOND, SECOND). + auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::Time, {}, Unit::Nanosecond, Unit::Second)); + + // 4. Let internalDuration be DifferenceInstant(instant.[[EpochNanoseconds]], other.[[EpochNanoseconds]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). + auto internal_duration = difference_instant(vm, instant.epoch_nanoseconds()->big_integer(), other->epoch_nanoseconds()->big_integer(), settings.rounding_increment, settings.smallest_unit, settings.rounding_mode); + + // 5. Let result be ! TemporalDurationFromInternal(internalDuration, settings.[[LargestUnit]]). + auto result = MUST(temporal_duration_from_internal(vm, internal_duration, settings.largest_unit)); + + // 6. If operation is SINCE, set result to CreateNegatedTemporalDuration(result). + if (operation == DurationOperation::Since) + result = create_negated_temporal_duration(vm, result); + + // 7. Return result. + return result; +} + // 8.5.10 AddDurationToInstant ( operation, instant, temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtoinstant ThrowCompletionOr> add_duration_to_instant(VM& vm, ArithmeticOperation operation, Instant const& instant, Value temporal_duration_like) { diff --git a/Libraries/LibJS/Runtime/Temporal/Instant.h b/Libraries/LibJS/Runtime/Temporal/Instant.h index e7264ca229a..6f87e9bc968 100644 --- a/Libraries/LibJS/Runtime/Temporal/Instant.h +++ b/Libraries/LibJS/Runtime/Temporal/Instant.h @@ -61,8 +61,10 @@ ThrowCompletionOr> create_temporal_instant(VM&, BigInt const& e ThrowCompletionOr> to_temporal_instant(VM&, Value item); i8 compare_epoch_nanoseconds(Crypto::SignedBigInteger const& epoch_nanoseconds_one, Crypto::SignedBigInteger const& epoch_nanoseconds_two); ThrowCompletionOr add_instant(VM&, Crypto::SignedBigInteger const& epoch_nanoseconds, TimeDuration const&); +InternalDuration difference_instant(VM&, Crypto::SignedBigInteger const& nanoseconds1, Crypto::SignedBigInteger const& nanoseconds2, u64 rounding_increment, Unit smallest_unit, RoundingMode); Crypto::SignedBigInteger round_temporal_instant(Crypto::SignedBigInteger const& nanoseconds, u64 increment, Unit, RoundingMode); String temporal_instant_to_string(Instant const&, Optional time_zone, SecondsStringPrecision::Precision); +ThrowCompletionOr> difference_temporal_instant(VM&, DurationOperation, Instant const&, Value other, Value options); ThrowCompletionOr> add_duration_to_instant(VM&, ArithmeticOperation, Instant const&, Value temporal_duration_like); } diff --git a/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 185483edadf..f0e1fce7a79 100644 --- a/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include @@ -34,6 +35,8 @@ void InstantPrototype::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); @@ -95,6 +98,34 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::subtract) return TRY(add_duration_to_instant(vm, ArithmeticOperation::Subtract, instant, temporal_duration_like)); } +// 8.3.7 Temporal.Instant.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.until +JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until) +{ + auto other = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let instant be the this value. + // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). + auto instant = TRY(typed_this_object(vm)); + + // 3. Return ? DifferenceTemporalInstant(UNTIL, instant, other, options). + return TRY(difference_temporal_instant(vm, DurationOperation::Until, instant, other, options)); +} + +// 8.3.8 Temporal.Instant.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.since +JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since) +{ + auto other = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let instant be the this value. + // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). + auto instant = TRY(typed_this_object(vm)); + + // 3. Return ? DifferenceTemporalInstant(SINCE, instant, other, options). + return TRY(difference_temporal_instant(vm, DurationOperation::Since, instant, other, options)); +} + // 8.3.10 Temporal.Instant.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.equals JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::equals) { diff --git a/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h b/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h index a1283b04a75..f2d72ec9713 100644 --- a/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h +++ b/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h @@ -27,6 +27,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(epoch_nanoseconds_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); diff --git a/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.since.js b/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.since.js new file mode 100644 index 00000000000..6c1205712ce --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.since.js @@ -0,0 +1,190 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Instant.prototype.since).toHaveLength(1); + }); + + test("basic functionality", () => { + const instant1 = new Temporal.Instant(1625614920000000000n); + const instant2 = new Temporal.Instant(0n); + expect(instant1.since(instant2).seconds).toBe(1625614920); + expect(instant1.since(instant2, { largestUnit: "hour" }).hours).toBe(451559); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.Instant object", () => { + expect(() => { + Temporal.Instant.prototype.since.call("foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant"); + }); +}); + +describe("rounding modes", () => { + const earlier = new Temporal.Instant( + 217178610_123_456_789n /* 1976-11-18T15:23:30.123456789Z */ + ); + const later = new Temporal.Instant( + 1572345998_271_986_289n /* 2019-10-29T10:46:38.271986289Z */ + ); + const largestUnit = "hours"; + + test("'ceil' rounding mode", () => { + const expected = [ + ["hours", "PT376436H", "-PT376435H"], + ["minutes", "PT376435H24M", "-PT376435H23M"], + ["seconds", "PT376435H23M9S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.148S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.148529S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "ceil"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { largestUnit, smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'expand' rounding mode", () => { + const expected = [ + ["hours", "PT376436H", "-PT376436H"], + ["minutes", "PT376435H24M", "-PT376435H24M"], + ["seconds", "PT376435H23M9S", "-PT376435H23M9S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "expand"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { largestUnit, smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'floor' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376436H"], + ["minutes", "PT376435H23M", "-PT376435H24M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M9S"], + ["milliseconds", "PT376435H23M8.148S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.148529S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "floor"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { largestUnit, smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfCeil' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.148529S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfCeil"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { largestUnit, smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfEven' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfEven"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { largestUnit, smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfExpand' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfExpand"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { largestUnit, smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfFloor' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.148529S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfFloor"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { largestUnit, smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfTrunc' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.148529S", "-PT376435H23M8.148529S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfTrunc"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { largestUnit, smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.until.js b/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.until.js new file mode 100644 index 00000000000..6c7b824fcd1 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.until.js @@ -0,0 +1,190 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Instant.prototype.until).toHaveLength(1); + }); + + test("basic functionality", () => { + const instant1 = new Temporal.Instant(0n); + const instant2 = new Temporal.Instant(1625614920000000000n); + expect(instant1.until(instant2).seconds).toBe(1625614920); + expect(instant1.until(instant2, { largestUnit: "hour" }).hours).toBe(451559); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.Instant object", () => { + expect(() => { + Temporal.Instant.prototype.until.call("foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant"); + }); +}); + +describe("rounding modes", () => { + const earlier = new Temporal.Instant( + 217178610_123_456_789n /* 1976-11-18T15:23:30.123456789Z */ + ); + const later = new Temporal.Instant( + 1572345998_271_986_289n /* 2019-10-29T10:46:38.271986289Z */ + ); + const largestUnit = "hours"; + + test("'ceil' rounding mode", () => { + const expected = [ + ["hours", "PT376436H", "-PT376435H"], + ["minutes", "PT376435H24M", "-PT376435H23M"], + ["seconds", "PT376435H23M9S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.148S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.148529S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "ceil"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { largestUnit, smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'expand' rounding mode", () => { + const expected = [ + ["hours", "PT376436H", "-PT376436H"], + ["minutes", "PT376435H24M", "-PT376435H24M"], + ["seconds", "PT376435H23M9S", "-PT376435H23M9S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "expand"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { largestUnit, smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'floor' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376436H"], + ["minutes", "PT376435H23M", "-PT376435H24M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M9S"], + ["milliseconds", "PT376435H23M8.148S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.148529S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "floor"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { largestUnit, smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfCeil' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.148529S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfCeil"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { largestUnit, smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfEven' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfEven"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { largestUnit, smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfExpand' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.14853S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfExpand"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { largestUnit, smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfFloor' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.148529S", "-PT376435H23M8.14853S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfFloor"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { largestUnit, smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfTrunc' rounding mode", () => { + const expected = [ + ["hours", "PT376435H", "-PT376435H"], + ["minutes", "PT376435H23M", "-PT376435H23M"], + ["seconds", "PT376435H23M8S", "-PT376435H23M8S"], + ["milliseconds", "PT376435H23M8.149S", "-PT376435H23M8.149S"], + ["microseconds", "PT376435H23M8.148529S", "-PT376435H23M8.148529S"], + ["nanoseconds", "PT376435H23M8.1485295S", "-PT376435H23M8.1485295S"], + ]; + + const roundingMode = "halfTrunc"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { largestUnit, smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { largestUnit, smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); +});