1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:57:35 +00:00

LibJS: Implement Temporal.Instant.prototype.toZonedDateTime()

This commit is contained in:
Linus Groh 2021-09-09 00:27:19 +01:00
parent 1c78ff1b9f
commit 6607d1dfb1
5 changed files with 116 additions and 0 deletions

View file

@ -22,6 +22,7 @@ public:
virtual ~Instant() override = default;
[[nodiscard]] BigInt const& nanoseconds() const { return m_nanoseconds; }
[[nodiscard]] BigInt& nanoseconds() { return m_nanoseconds; }
private:
virtual void visit_edges(Visitor&) override;

View file

@ -8,10 +8,12 @@
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/Duration.h>
#include <LibJS/Runtime/Temporal/Instant.h>
#include <LibJS/Runtime/Temporal/InstantPrototype.h>
#include <LibJS/Runtime/Temporal/TimeZone.h>
#include <LibJS/Runtime/Temporal/ZonedDateTime.h>
namespace JS::Temporal {
@ -46,6 +48,7 @@ void InstantPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.toZonedDateTime, to_zoned_date_time, 1, attr);
}
static Instant* typed_this(GlobalObject& global_object)
@ -509,4 +512,60 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::value_of)
return {};
}
// 8.3.17 Temporal.Instant.prototype.toZonedDateTime ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tozoneddatetime
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_zoned_date_time)
{
auto item = vm.argument(0);
// 1. Let instant be the this value.
// 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
auto* instant = typed_this(global_object);
if (vm.exception())
return {};
// 3. If Type(item) is not Object, then
if (!item.is_object()) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, item);
return {};
}
// 4. Let calendarLike be ? Get(item, "calendar").
auto calendar_like = item.as_object().get(vm.names.calendar);
if (vm.exception())
return {};
// 5. If calendarLike is undefined, then
if (calendar_like.is_undefined()) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.calendar.as_string());
return {};
}
// 6. Let calendar be ? ToTemporalCalendar(calendarLike).
auto* calendar = to_temporal_calendar(global_object, calendar_like);
if (vm.exception())
return {};
// 7. Let temporalTimeZoneLike be ? Get(item, "timeZone").
auto temporal_time_zone_like = item.as_object().get(vm.names.timeZone);
if (vm.exception())
return {};
// 8. If temporalTimeZoneLike is undefined, then
if (temporal_time_zone_like.is_undefined()) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.timeZone.as_string());
return {};
}
// 9. Let timeZone be ? ToTemporalTimeZone(temporalTimeZoneLike).
auto* time_zone = to_temporal_time_zone(global_object, temporal_time_zone_like);
if (vm.exception())
return {};
// 10. Return ? CreateTemporalZonedDateTime(instant.[[Nanoseconds]], timeZone, calendar).
return create_temporal_zoned_date_time(global_object, instant->nanoseconds(), *time_zone, *calendar);
}
}

View file

@ -33,6 +33,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(to_zoned_date_time);
};
}