LibJS: Implement Temporal.ZonedDateTime.prototype.startOfDay

This commit is contained in:
Timothy Flynn 2024-11-25 13:33:31 -05:00 committed by Andreas Kling
commit 6d78f1327e
Notes: github-actions[bot] 2024-11-26 10:03:18 +00:00
4 changed files with 101 additions and 35 deletions

View file

@ -79,6 +79,7 @@ void ZonedDateTimePrototype::initialize(Realm& realm)
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(realm, vm.names.toJSON, to_json, 0, attr);
define_native_function(realm, vm.names.valueOf, value_of, 0, attr);
define_native_function(realm, vm.names.startOfDay, start_of_day, 0, attr);
}
// 6.3.3 get Temporal.ZonedDateTime.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.calendarid
@ -808,4 +809,27 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of)
return vm.throw_completion<TypeError>(ErrorType::Convert, "Temporal.ZonedDateTime", "a primitive value");
}
// 6.3.45 Temporal.ZonedDateTime.prototype.startOfDay ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.startofday
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::start_of_day)
{
// 1. Let zonedDateTime be the this value.
// 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
auto zoned_date_time = TRY(typed_this_object(vm));
// 3. Let timeZone be zonedDateTime.[[TimeZone]].
auto const& time_zone = zoned_date_time->time_zone();
// 4. Let calendar be zonedDateTime.[[Calendar]].
auto const& calendar = zoned_date_time->calendar();
// 5. Let isoDateTime be GetISODateTimeFor(timeZone, zonedDateTime.[[EpochNanoseconds]]).
auto iso_date_time = get_iso_date_time_for(time_zone, zoned_date_time->epoch_nanoseconds()->big_integer());
// 6. Let epochNanoseconds be ? GetStartOfDay(timeZone, isoDateTime.[[ISODate]]).
auto epoch_nanoseconds = TRY(get_start_of_day(vm, time_zone, iso_date_time.iso_date));
// 7. Return ! CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar).
return MUST(create_temporal_zoned_date_time(vm, BigInt::create(vm, move(epoch_nanoseconds)), time_zone, calendar));
}
}

View file

@ -65,6 +65,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(start_of_day);
};
}