|
@@ -65,6 +65,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
|
|
define_native_function(vm.names.add, add, 1, attr);
|
|
define_native_function(vm.names.add, add, 1, attr);
|
|
define_native_function(vm.names.subtract, subtract, 1, attr);
|
|
define_native_function(vm.names.subtract, subtract, 1, attr);
|
|
define_native_function(vm.names.until, until, 1, attr);
|
|
define_native_function(vm.names.until, until, 1, attr);
|
|
|
|
+ define_native_function(vm.names.since, since, 1, attr);
|
|
define_native_function(vm.names.round, round, 1, attr);
|
|
define_native_function(vm.names.round, round, 1, attr);
|
|
define_native_function(vm.names.equals, equals, 1, attr);
|
|
define_native_function(vm.names.equals, equals, 1, attr);
|
|
define_native_function(vm.names.toString, to_string, 0, attr);
|
|
define_native_function(vm.names.toString, to_string, 0, attr);
|
|
@@ -561,6 +562,63 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::until)
|
|
return TRY(create_temporal_duration(global_object, round_result.years, round_result.months, round_result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds));
|
|
return TRY(create_temporal_duration(global_object, round_result.years, round_result.months, round_result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// 5.3.29 Temporal.PlainDateTime.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.since
|
|
|
|
+JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::since)
|
|
|
|
+{
|
|
|
|
+ // 1. Let dateTime be the this value.
|
|
|
|
+ // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
|
|
|
+ auto* date_time = TRY(typed_this_object(global_object));
|
|
|
|
+
|
|
|
|
+ // 3. Set other to ? ToTemporalDateTime(other).
|
|
|
|
+ auto* other = TRY(to_temporal_date_time(global_object, vm.argument(0)));
|
|
|
|
+
|
|
|
|
+ // 4. If ? CalendarEquals(dateTime.[[Calendar]], other.[[Calendar]]) is false, throw a RangeError exception.
|
|
|
|
+ if (!TRY(calendar_equals(global_object, date_time->calendar(), other->calendar())))
|
|
|
|
+ return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalDifferentCalendars);
|
|
|
|
+
|
|
|
|
+ // 5. Set options to ? GetOptionsObject(options).
|
|
|
|
+ auto* options = TRY(get_options_object(global_object, vm.argument(1)));
|
|
|
|
+
|
|
|
|
+ // 6. Let smallestUnit be ? ToSmallestTemporalUnit(options, « », "nanosecond").
|
|
|
|
+ auto smallest_unit = TRY(to_smallest_temporal_unit(global_object, *options, {}, "nanosecond"sv));
|
|
|
|
+
|
|
|
|
+ // 7. Let defaultLargestUnit be ! LargerOfTwoTemporalUnits("day", smallestUnit).
|
|
|
|
+ auto default_largest_unit = larger_of_two_temporal_units("day"sv, *smallest_unit);
|
|
|
|
+
|
|
|
|
+ // 8. Let largestUnit be ? ToLargestTemporalUnit(options, « », "auto", defaultLargestUnit).
|
|
|
|
+ auto largest_unit = TRY(to_largest_temporal_unit(global_object, *options, {}, "auto"sv, default_largest_unit));
|
|
|
|
+
|
|
|
|
+ // 9. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit).
|
|
|
|
+ TRY(validate_temporal_unit_range(global_object, largest_unit, *smallest_unit));
|
|
|
|
+
|
|
|
|
+ // 10. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
|
|
|
|
+ auto rounding_mode = TRY(to_temporal_rounding_mode(global_object, *options, "trunc"sv));
|
|
|
|
+
|
|
|
|
+ // 11. Set roundingMode to ! NegateTemporalRoundingMode(roundingMode).
|
|
|
|
+ rounding_mode = negate_temporal_rounding_mode(rounding_mode);
|
|
|
|
+
|
|
|
|
+ // 12. Let maximum be ! MaximumTemporalDurationRoundingIncrement(smallestUnit).
|
|
|
|
+ auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
|
|
|
|
+
|
|
|
|
+ // 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
|
|
|
|
+ auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false));
|
|
|
|
+
|
|
|
|
+ // 14. Let diff be ? DifferenceISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], dateTime.[[Calendar]], largestUnit, options).
|
|
|
|
+ auto diff = TRY(difference_iso_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), date_time->calendar(), largest_unit, options));
|
|
|
|
+
|
|
|
|
+ // 15. Let relativeTo be ! CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]).
|
|
|
|
+ auto* relative_to = MUST(create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()));
|
|
|
|
+
|
|
|
|
+ // 16. Let roundResult be ? RoundDuration(diff.[[Years]], diff.[[Months]], diff.[[Weeks]], diff.[[Days]], diff.[[Hours]], diff.[[Minutes]], diff.[[Seconds]], diff.[[Milliseconds]], diff.[[Microseconds]], diff.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode, relativeTo).
|
|
|
|
+ auto round_result = TRY(round_duration(global_object, diff.years, diff.months, diff.weeks, diff.days, diff.hours, diff.minutes, diff.seconds, diff.milliseconds, diff.microseconds, diff.nanoseconds, rounding_increment, *smallest_unit, rounding_mode, relative_to));
|
|
|
|
+
|
|
|
|
+ // 17. Let result be ! BalanceDuration(roundResult.[[Days]], roundResult.[[Hours]], roundResult.[[Minutes]], roundResult.[[Seconds]], roundResult.[[Milliseconds]], roundResult.[[Microseconds]], roundResult.[[Nanoseconds]], largestUnit).
|
|
|
|
+ auto result = MUST(balance_duration(global_object, round_result.days, round_result.hours, round_result.minutes, round_result.seconds, round_result.milliseconds, round_result.microseconds, *js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)round_result.nanoseconds)), largest_unit));
|
|
|
|
+
|
|
|
|
+ // 18. Return ? CreateTemporalDuration(−roundResult.[[Years]], −roundResult.[[Months]], −roundResult.[[Weeks]], −result.[[Days]], −result.[[Hours]], −result.[[Minutes]], −result.[[Seconds]], −result.[[Milliseconds]], −result.[[Microseconds]], −result.[[Nanoseconds]]).
|
|
|
|
+ return TRY(create_temporal_duration(global_object, -round_result.years, -round_result.months, -round_result.weeks, -result.days, -result.hours, -result.minutes, -result.seconds, -result.milliseconds, -result.microseconds, -result.nanoseconds));
|
|
|
|
+}
|
|
|
|
+
|
|
// 5.3.30 Temporal.PlainDateTime.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.round
|
|
// 5.3.30 Temporal.PlainDateTime.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.round
|
|
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::round)
|
|
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::round)
|
|
{
|
|
{
|