mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
LibJS: Implement Temporal.Instant.prototype.until/since
This commit is contained in:
parent
1d67f28e72
commit
477f00aced
Notes:
github-actions[bot]
2024-11-25 12:34:06 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/477f00aced4 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2557 Reviewed-by: https://github.com/shannonbooth ✅
6 changed files with 454 additions and 0 deletions
|
@ -179,6 +179,19 @@ ThrowCompletionOr<Crypto::SignedBigInteger> 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<StringView> 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<GC::Ref<Duration>> 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<GC::Ref<Instant>> add_duration_to_instant(VM& vm, ArithmeticOperation operation, Instant const& instant, Value temporal_duration_like)
|
||||
{
|
||||
|
|
|
@ -61,8 +61,10 @@ ThrowCompletionOr<GC::Ref<Instant>> create_temporal_instant(VM&, BigInt const& e
|
|||
ThrowCompletionOr<GC::Ref<Instant>> to_temporal_instant(VM&, Value item);
|
||||
i8 compare_epoch_nanoseconds(Crypto::SignedBigInteger const& epoch_nanoseconds_one, Crypto::SignedBigInteger const& epoch_nanoseconds_two);
|
||||
ThrowCompletionOr<Crypto::SignedBigInteger> 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<StringView> time_zone, SecondsStringPrecision::Precision);
|
||||
ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_instant(VM&, DurationOperation, Instant const&, Value other, Value options);
|
||||
ThrowCompletionOr<GC::Ref<Instant>> add_duration_to_instant(VM&, ArithmeticOperation, Instant const&, Value temporal_duration_like);
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||
#include <LibJS/Runtime/Temporal/InstantPrototype.h>
|
||||
#include <LibJS/Runtime/Temporal/TimeZone.h>
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue