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

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

This commit is contained in:
Linus Groh 2021-09-09 00:35:24 +01:00
parent 6607d1dfb1
commit d3fcf5a570
4 changed files with 77 additions and 0 deletions

View file

@ -49,6 +49,7 @@ void InstantPrototype::initialize(GlobalObject& global_object)
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);
define_native_function(vm.names.toZonedDateTimeISO, to_zoned_date_time_iso, 1, attr);
}
static Instant* typed_this(GlobalObject& global_object)
@ -568,4 +569,41 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_zoned_date_time)
return create_temporal_zoned_date_time(global_object, instant->nanoseconds(), *time_zone, *calendar);
}
// 8.3.18 Temporal.Instant.prototype.toZonedDateTimeISO ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tozoneddatetimeiso
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_zoned_date_time_iso)
{
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 Object, then
if (item.is_object()) {
// a. Let timeZoneProperty be ? Get(item, "timeZone").
auto time_zone_property = item.as_object().get(vm.names.timeZone);
if (vm.exception())
return {};
// b. If timeZoneProperty is not undefined, then
if (!time_zone_property.is_undefined()) {
// i. Set item to timeZoneProperty.
item = time_zone_property;
}
}
// 4. Let timeZone be ? ToTemporalTimeZone(item).
auto* time_zone = to_temporal_time_zone(global_object, item);
if (vm.exception())
return {};
// 5. Let calendar be ! GetISO8601Calendar().
auto* calendar = get_iso8601_calendar(global_object);
// 6. Return ? CreateTemporalZonedDateTime(instant.[[Nanoseconds]], timeZone, calendar).
return create_temporal_zoned_date_time(global_object, instant->nanoseconds(), *time_zone, *calendar);
}
}

View file

@ -34,6 +34,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time);
JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time_iso);
};
}