mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-03 08:08:43 +00:00
LibJS: Implement Temporal.ZonedDateTime.prototype.since/until
This commit is contained in:
parent
336efa5e3f
commit
eadd0c40c9
Notes:
github-actions[bot]
2024-11-26 10:03:39 +00:00
Author: https://github.com/trflynn89
Commit: eadd0c40c9
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2577
Reviewed-by: https://github.com/shannonbooth ✅
6 changed files with 729 additions and 0 deletions
|
@ -524,6 +524,69 @@ ThrowCompletionOr<Crypto::BigFraction> 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<GC::Ref<Duration>> 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<RangeError>(ErrorType::TemporalDifferentCalendars);
|
||||
}
|
||||
|
||||
// 3. Let resolvedOptions be ? GetOptionsObject(options).
|
||||
auto resolved_options = TRY(get_options_object(vm, options));
|
||||
|
||||
// 4. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, DATETIME, « », NANOSECOND, 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<RangeError>(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<GC::Ref<ZonedDateTime>> add_duration_to_zoned_date_time(VM& vm, ArithmeticOperation operation, ZonedDateTime const& zoned_date_time, Value temporal_duration_like, Value options)
|
||||
{
|
||||
|
|
|
@ -56,6 +56,7 @@ ThrowCompletionOr<Crypto::SignedBigInteger> add_zoned_date_time(VM&, Crypto::Sig
|
|||
ThrowCompletionOr<InternalDuration> difference_zoned_date_time(VM&, Crypto::SignedBigInteger const& nanoseconds1, Crypto::SignedBigInteger const& nanoseconds2, StringView time_zone, StringView calendar, Unit largest_unit);
|
||||
ThrowCompletionOr<InternalDuration> 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<Crypto::BigFraction> difference_zoned_date_time_with_total(VM&, Crypto::SignedBigInteger const& nanoseconds1, Crypto::SignedBigInteger const& nanoseconds2, StringView time_zone, StringView calendar, Unit);
|
||||
ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_zoned_date_time(VM&, DurationOperation, ZonedDateTime const&, Value other, Value options);
|
||||
ThrowCompletionOr<GC::Ref<ZonedDateTime>> add_duration_to_zoned_date_time(VM&, ArithmeticOperation, ZonedDateTime const&, Value temporal_duration_like, Value options);
|
||||
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue