From eadd0c40c987a493a5f6146eb8783e271bf8d1e9 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 25 Nov 2024 12:49:03 -0500 Subject: [PATCH] LibJS: Implement Temporal.ZonedDateTime.prototype.since/until --- .../LibJS/Runtime/Temporal/ZonedDateTime.cpp | 63 ++++ .../LibJS/Runtime/Temporal/ZonedDateTime.h | 1 + .../Temporal/ZonedDateTimePrototype.cpp | 30 ++ .../Runtime/Temporal/ZonedDateTimePrototype.h | 2 + .../ZonedDateTime.prototype.since.js | 317 ++++++++++++++++++ .../ZonedDateTime.prototype.until.js | 316 +++++++++++++++++ 6 files changed, 729 insertions(+) create mode 100644 Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.since.js create mode 100644 Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.until.js diff --git a/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp b/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp index 54a60906661..531962c09d7 100644 --- a/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp +++ b/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp @@ -524,6 +524,69 @@ ThrowCompletionOr difference_zoned_date_time_with_total(VM& return TRY(total_relative_duration(vm, difference, nanoseconds2, date_time, time_zone, calendar, unit)); } +// 6.5.9 DifferenceTemporalZonedDateTime ( operation, zonedDateTime, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalzoneddatetime +ThrowCompletionOr> difference_temporal_zoned_date_time(VM& vm, DurationOperation operation, ZonedDateTime const& zoned_date_time, Value other_value, Value options) +{ + // 1. Set other to ? ToTemporalZonedDateTime(other). + auto other = TRY(to_temporal_zoned_date_time(vm, other_value)); + + // 2. If CalendarEquals(zonedDateTime.[[Calendar]], other.[[Calendar]]) is false, then + if (!calendar_equals(zoned_date_time.calendar(), other->calendar())) { + // a. Throw a RangeError exception. + return vm.throw_completion(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, HOUR). + auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::DateTime, {}, Unit::Nanosecond, Unit::Hour)); + + // 5. If TemporalUnitCategory(settings.[[LargestUnit]]) is not DATE, then + if (temporal_unit_category(settings.largest_unit) != UnitCategory::Date) { + // a. Let internalDuration be DifferenceInstant(zonedDateTime.[[EpochNanoseconds]], other.[[EpochNanoseconds]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). + auto internal_duration = difference_instant(vm, zoned_date_time.epoch_nanoseconds()->big_integer(), other->epoch_nanoseconds()->big_integer(), settings.rounding_increment, settings.smallest_unit, settings.rounding_mode); + + // b. Let result be ! TemporalDurationFromInternal(internalDuration, settings.[[LargestUnit]]). + auto result = MUST(temporal_duration_from_internal(vm, internal_duration, settings.largest_unit)); + + // c. If operation is SINCE, set result to CreateNegatedTemporalDuration(result). + if (operation == DurationOperation::Since) + result = create_negated_temporal_duration(vm, result); + + // d. Return result. + return result; + } + + // 6. NOTE: To calculate differences in two different time zones, settings.[[LargestUnit]] must be a time unit, + // because day lengths can vary between time zones due to DST and other UTC offset shifts. + + // 7. If TimeZoneEquals(zonedDateTime.[[TimeZone]], other.[[TimeZone]]) is false, then + if (!time_zone_equals(zoned_date_time.time_zone(), other->time_zone())) { + // a. Throw a RangeError exception. + return vm.throw_completion(ErrorType::TemporalDifferentTimeZones); + } + + // 8. If zonedDateTime.[[EpochNanoseconds]] = other.[[EpochNanoseconds]], then + if (zoned_date_time.epoch_nanoseconds()->big_integer() == other->epoch_nanoseconds()->big_integer()) { + // 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)); + } + + // 9. Let internalDuration be ? DifferenceZonedDateTimeWithRounding(zonedDateTime.[[EpochNanoseconds]], other.[[EpochNanoseconds]], zonedDateTime.[[TimeZone]], zonedDateTime.[[Calendar]], settings.[[LargestUnit]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). + auto internal_duration = TRY(difference_zoned_date_time_with_rounding(vm, zoned_date_time.epoch_nanoseconds()->big_integer(), other->epoch_nanoseconds()->big_integer(), zoned_date_time.time_zone(), zoned_date_time.calendar(), settings.largest_unit, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode)); + + // 10. Let result be ? TemporalDurationFromInternal(internalDuration, HOUR). + auto result = TRY(temporal_duration_from_internal(vm, internal_duration, Unit::Hour)); + + // 11. If operation is SINCE, set result to CreateNegatedTemporalDuration(result). + if (operation == DurationOperation::Since) + result = create_negated_temporal_duration(vm, result); + + // 12. Return result. + return result; +} + // 6.5.10 AddDurationToZonedDateTime ( operation, zonedDateTime, temporalDurationLike, options ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtozoneddatetime ThrowCompletionOr> add_duration_to_zoned_date_time(VM& vm, ArithmeticOperation operation, ZonedDateTime const& zoned_date_time, Value temporal_duration_like, Value options) { diff --git a/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.h b/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.h index 0a692bdbac5..45e15445547 100644 --- a/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.h +++ b/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.h @@ -56,6 +56,7 @@ ThrowCompletionOr add_zoned_date_time(VM&, Crypto::Sig ThrowCompletionOr difference_zoned_date_time(VM&, Crypto::SignedBigInteger const& nanoseconds1, Crypto::SignedBigInteger const& nanoseconds2, StringView time_zone, StringView calendar, Unit largest_unit); ThrowCompletionOr difference_zoned_date_time_with_rounding(VM&, Crypto::SignedBigInteger const& nanoseconds1, Crypto::SignedBigInteger const& nanoseconds2, StringView time_zone, StringView calendar, Unit largest_unit, u64 rounding_increment, Unit smallest_unit, RoundingMode); ThrowCompletionOr difference_zoned_date_time_with_total(VM&, Crypto::SignedBigInteger const& nanoseconds1, Crypto::SignedBigInteger const& nanoseconds2, StringView time_zone, StringView calendar, Unit); +ThrowCompletionOr> difference_temporal_zoned_date_time(VM&, DurationOperation, ZonedDateTime const&, Value other, Value options); ThrowCompletionOr> add_duration_to_zoned_date_time(VM&, ArithmeticOperation, ZonedDateTime const&, Value temporal_duration_like, Value options); } diff --git a/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 8ddead5b57d..89e65ff198d 100644 --- a/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -64,6 +64,8 @@ void ZonedDateTimePrototype::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); @@ -373,6 +375,34 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::subtract) return TRY(add_duration_to_zoned_date_time(vm, ArithmeticOperation::Subtract, zoned_date_time, temporal_duration_like, options)); } +// 6.3.37 Temporal.ZonedDateTime.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.until +JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::until) +{ + auto other = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let zonedDateTime be the this value. + // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). + auto zoned_date_time = TRY(typed_this_object(vm)); + + // 3. Return ? DifferenceTemporalZonedDateTime(UNTIL, zonedDateTime, other, options). + return TRY(difference_temporal_zoned_date_time(vm, DurationOperation::Until, zoned_date_time, other, options)); +} + +// 6.3.38 Temporal.ZonedDateTime.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.since +JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::since) +{ + auto other = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let zonedDateTime be the this value. + // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). + auto zoned_date_time = TRY(typed_this_object(vm)); + + // 3. Return ? DifferenceTemporalZonedDateTime(SINCE, zonedDateTime, other, options). + return TRY(difference_temporal_zoned_date_time(vm, DurationOperation::Since, zoned_date_time, other, options)); +} + // 6.3.40 Temporal.ZonedDateTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.equals JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::equals) { diff --git a/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h index 6c1fc6a681c..fc79fc69245 100644 --- a/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h +++ b/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h @@ -53,6 +53,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(offset_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/ZonedDateTime/ZonedDateTime.prototype.since.js b/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.since.js new file mode 100644 index 00000000000..3fe148aef54 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.since.js @@ -0,0 +1,317 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.ZonedDateTime.prototype.since).toHaveLength(1); + }); + + test("basic functionality", () => { + const values = [ + [0n, 0n, "PT0S"], + [2345679011n, 123456789n, "PT2.222222222S"], + [123456789n, 0n, "PT0.123456789S"], + [0n, 123456789n, "-PT0.123456789S"], + [123456789123456789n, 0n, "PT34293H33M9.123456789S"], + [0n, 123456789123456789n, "-PT34293H33M9.123456789S"], + ]; + + for (const [arg, argOther, expected] of values) { + const zonedDateTime = new Temporal.ZonedDateTime(arg, "UTC"); + const other = new Temporal.ZonedDateTime(argOther, "UTC"); + expect(zonedDateTime.since(other).toString()).toBe(expected); + } + }); + + test("smallestUnit option", () => { + const zonedDateTime = new Temporal.ZonedDateTime(34401906007008009n, "UTC"); + const other = new Temporal.ZonedDateTime(0n, "UTC"); + const values = [ + ["year", "P1Y"], + ["month", "P13M"], + ["week", "P56W"], + ["day", "P398D"], + ["hour", "PT9556H"], + ["minute", "PT9556H5M"], + ["second", "PT9556H5M6S"], + ["millisecond", "PT9556H5M6.007S"], + ["microsecond", "PT9556H5M6.007008S"], + ["nanosecond", "PT9556H5M6.007008009S"], + ]; + + for (const [smallestUnit, expected] of values) { + expect(zonedDateTime.since(other, { smallestUnit }).toString()).toBe(expected); + } + }); + + test("largestUnit option", () => { + const zonedDateTime = new Temporal.ZonedDateTime(34401906007008009n, "UTC"); + const other = new Temporal.ZonedDateTime(0n, "UTC"); + const values = [ + ["year", "P1Y1M2DT4H5M6.007008009S"], + ["month", "P13M2DT4H5M6.007008009S"], + ["week", "P56W6DT4H5M6.007008009S"], + ["day", "P398DT4H5M6.007008009S"], + ["hour", "PT9556H5M6.007008009S"], + ["minute", "PT573365M6.007008009S"], + ["second", "PT34401906.007008009S"], + ["millisecond", "PT34401906.007008009S"], + ["microsecond", "PT34401906.007008009S"], + ["nanosecond", "PT34401906.007008008S"], + ]; + + for (const [largestUnit, expected] of values) { + expect(zonedDateTime.since(other, { largestUnit }).toString()).toBe(expected); + } + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.ZonedDateTime object", () => { + expect(() => { + Temporal.ZonedDateTime.prototype.since.call("foo", {}); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime"); + }); + + test("cannot compare dates from different calendars", () => { + const zonedDateTimeOne = new Temporal.ZonedDateTime(0n, "UTC", "iso8601"); + const zonedDateTimeTwo = new Temporal.ZonedDateTime(0n, "UTC", "gregory"); + + expect(() => { + zonedDateTimeOne.since(zonedDateTimeTwo); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars"); + }); + + test("cannot compare dates from different time zones", () => { + const zonedDateTimeOne = new Temporal.ZonedDateTime(0n, "UTC"); + const zonedDateTimeTwo = new Temporal.ZonedDateTime(0n, "America/New_York"); + + expect(() => { + zonedDateTimeOne.since(zonedDateTimeTwo, { largestUnit: "day" }); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different time zones"); + }); +}); + +describe("rounding modes", () => { + const earlier = new Temporal.ZonedDateTime( + 1546935756_123_456_789n /* 2019-01-08T08:22:36.123456789+00:00 */, + "UTC" + ); + const later = new Temporal.ZonedDateTime( + 1631018380_987_654_289n /* 2021-09-07T12:39:40.987654289+00:00 */, + "UTC" + ); + + test("'ceil' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P2Y"], + ["months", "P32M", "-P31M"], + ["weeks", "P140W", "-P139W"], + ["days", "P974D", "-P973D"], + ["hours", "PT23357H", "-PT23356H"], + ["minutes", "PT23356H18M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M4S"], + ["milliseconds", "PT23356H17M4.865S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864197S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "ceil"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'expand' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P140W", "-P140W"], + ["days", "P974D", "-P974D"], + ["hours", "PT23357H", "-PT23357H"], + ["minutes", "PT23356H18M", "-PT23356H18M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.865S", "-PT23356H17M4.865S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "expand"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'floor' rounding mode", () => { + const expected = [ + ["years", "P2Y", "-P3Y"], + ["months", "P31M", "-P32M"], + ["weeks", "P139W", "-P140W"], + ["days", "P973D", "-P974D"], + ["hours", "PT23356H", "-PT23357H"], + ["minutes", "PT23356H17M", "-PT23356H18M"], + ["seconds", "PT23356H17M4S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.865S"], + ["microseconds", "PT23356H17M4.864197S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "floor"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'trunc' rounding mode", () => { + const expected = [ + ["years", "P2Y", "-P2Y"], + ["months", "P31M", "-P31M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M4S", "-PT23356H17M4S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864197S", "-PT23356H17M4.864197S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "trunc"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfCeil' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864197S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfCeil"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfEven' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfEven"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfTrunc' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864197S", "-PT23356H17M4.864197S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfTrunc"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfExpand' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfExpand"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfFloor' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864197S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfFloor"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const sincePositive = later.since(earlier, { smallestUnit, roundingMode }); + expect(sincePositive.toString()).toBe(expectedPositive); + + const sinceNegative = earlier.since(later, { smallestUnit, roundingMode }); + expect(sinceNegative.toString()).toBe(expectedNegative); + }); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.until.js b/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.until.js new file mode 100644 index 00000000000..8d7c55c9655 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.until.js @@ -0,0 +1,316 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.ZonedDateTime.prototype.until).toHaveLength(1); + }); + + test("basic functionality", () => { + const values = [ + [0n, 0n, "PT0S"], + [123456789n, 2345679011n, "PT2.222222222S"], + [0n, 123456789n, "PT0.123456789S"], + [123456789n, 0n, "-PT0.123456789S"], + [0n, 123456789123456789n, "PT34293H33M9.123456789S"], + [123456789123456789n, 0n, "-PT34293H33M9.123456789S"], + ]; + for (const [arg, argOther, expected] of values) { + const zonedDateTime = new Temporal.ZonedDateTime(arg, "UTC"); + const other = new Temporal.ZonedDateTime(argOther, "UTC"); + expect(zonedDateTime.until(other).toString()).toBe(expected); + } + }); + + test("smallestUnit option", () => { + const zonedDateTime = new Temporal.ZonedDateTime(0n, "UTC"); + const other = new Temporal.ZonedDateTime(34401906007008009n, "UTC"); + const values = [ + ["year", "P1Y"], + ["month", "P13M"], + ["week", "P56W"], + ["day", "P398D"], + ["hour", "PT9556H"], + ["minute", "PT9556H5M"], + ["second", "PT9556H5M6S"], + ["millisecond", "PT9556H5M6.007S"], + ["microsecond", "PT9556H5M6.007008S"], + ["nanosecond", "PT9556H5M6.007008009S"], + ]; + + for (const [smallestUnit, expected] of values) { + expect(zonedDateTime.until(other, { smallestUnit }).toString()).toBe(expected); + } + }); + + test("largestUnit option", () => { + const zonedDateTime = new Temporal.ZonedDateTime(0n, "UTC"); + const other = new Temporal.ZonedDateTime(34401906007008009n, "UTC"); + const values = [ + ["year", "P1Y1M2DT4H5M6.007008009S"], + ["month", "P13M2DT4H5M6.007008009S"], + ["week", "P56W6DT4H5M6.007008009S"], + ["day", "P398DT4H5M6.007008009S"], + ["hour", "PT9556H5M6.007008009S"], + ["minute", "PT573365M6.007008009S"], + ["second", "PT34401906.007008009S"], + ["millisecond", "PT34401906.007008009S"], + ["microsecond", "PT34401906.007008009S"], + ["nanosecond", "PT34401906.007008008S"], + ]; + + for (const [largestUnit, expected] of values) { + expect(zonedDateTime.until(other, { largestUnit }).toString()).toBe(expected); + } + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.ZonedDateTime object", () => { + expect(() => { + Temporal.ZonedDateTime.prototype.until.call("foo", {}); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime"); + }); + + test("cannot compare dates from different calendars", () => { + const zonedDateTimeOne = new Temporal.ZonedDateTime(0n, "UTC", "iso8601"); + const zonedDateTimeTwo = new Temporal.ZonedDateTime(0n, "UTC", "gregory"); + + expect(() => { + zonedDateTimeOne.until(zonedDateTimeTwo); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars"); + }); + + test("cannot compare dates from different time zones", () => { + const zonedDateTimeOne = new Temporal.ZonedDateTime(0n, "UTC"); + const zonedDateTimeTwo = new Temporal.ZonedDateTime(0n, "America/New_York"); + + expect(() => { + zonedDateTimeOne.until(zonedDateTimeTwo, { largestUnit: "day" }); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different time zones"); + }); +}); + +describe("rounding modes", () => { + const earlier = new Temporal.ZonedDateTime( + 1546935756_123_456_789n /* 2019-01-08T08:22:36.123456789+00:00 */, + "UTC" + ); + const later = new Temporal.ZonedDateTime( + 1631018380_987_654_289n /* 2021-09-07T12:39:40.987654289+00:00 */, + "UTC" + ); + + test("'ceil' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P2Y"], + ["months", "P32M", "-P31M"], + ["weeks", "P140W", "-P139W"], + ["days", "P974D", "-P973D"], + ["hours", "PT23357H", "-PT23356H"], + ["minutes", "PT23356H18M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M4S"], + ["milliseconds", "PT23356H17M4.865S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864197S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "ceil"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'expand' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P140W", "-P140W"], + ["days", "P974D", "-P974D"], + ["hours", "PT23357H", "-PT23357H"], + ["minutes", "PT23356H18M", "-PT23356H18M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.865S", "-PT23356H17M4.865S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "expand"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'floor' rounding mode", () => { + const expected = [ + ["years", "P2Y", "-P3Y"], + ["months", "P31M", "-P32M"], + ["weeks", "P139W", "-P140W"], + ["days", "P973D", "-P974D"], + ["hours", "PT23356H", "-PT23357H"], + ["minutes", "PT23356H17M", "-PT23356H18M"], + ["seconds", "PT23356H17M4S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.865S"], + ["microseconds", "PT23356H17M4.864197S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "floor"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'trunc' rounding mode", () => { + const expected = [ + ["years", "P2Y", "-P2Y"], + ["months", "P31M", "-P31M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M4S", "-PT23356H17M4S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864197S", "-PT23356H17M4.864197S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "trunc"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfCeil' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864197S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfCeil"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfEven' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfEven"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfTrunc' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864197S", "-PT23356H17M4.864197S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfTrunc"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfExpand' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864198S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfExpand"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); + + test("'halfFloor' rounding mode", () => { + const expected = [ + ["years", "P3Y", "-P3Y"], + ["months", "P32M", "-P32M"], + ["weeks", "P139W", "-P139W"], + ["days", "P973D", "-P973D"], + ["hours", "PT23356H", "-PT23356H"], + ["minutes", "PT23356H17M", "-PT23356H17M"], + ["seconds", "PT23356H17M5S", "-PT23356H17M5S"], + ["milliseconds", "PT23356H17M4.864S", "-PT23356H17M4.864S"], + ["microseconds", "PT23356H17M4.864197S", "-PT23356H17M4.864198S"], + ["nanoseconds", "PT23356H17M4.8641975S", "-PT23356H17M4.8641975S"], + ]; + + const roundingMode = "halfFloor"; + expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => { + const untilPositive = earlier.until(later, { smallestUnit, roundingMode }); + expect(untilPositive.toString()).toBe(expectedPositive); + + const untilNegative = later.until(earlier, { smallestUnit, roundingMode }); + expect(untilNegative.toString()).toBe(expectedNegative); + }); + }); +});