1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:07:45 +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

@ -436,6 +436,7 @@ namespace JS {
P(toUpperCase) \ P(toUpperCase) \
P(toUTCString) \ P(toUTCString) \
P(toZonedDateTime) \ P(toZonedDateTime) \
P(toZonedDateTimeISO) \
P(trace) \ P(trace) \
P(trim) \ P(trim) \
P(trimEnd) \ P(trimEnd) \

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.toJSON, to_json, 0, attr);
define_native_function(vm.names.valueOf, value_of, 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.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) 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); 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(to_json);
JS_DECLARE_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time); JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time);
JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time_iso);
}; };
} }

View file

@ -0,0 +1,37 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.Instant.prototype.toZonedDateTimeISO).toHaveLength(1);
});
// TODO: Un-skip when ParseTemporalTimeZoneString is fully implemented
test.skip("basic functionality", () => {
const instant = new Temporal.Instant(1625614921123456789n);
const zonedDateTime = instant.toZonedDateTimeISO("UTC");
expect(zonedDateTime.year).toBe(2021);
expect(zonedDateTime.month).toBe(7);
expect(zonedDateTime.day).toBe(6);
expect(zonedDateTime.hour).toBe(23);
expect(zonedDateTime.minute).toBe(42);
expect(zonedDateTime.second).toBe(1);
expect(zonedDateTime.millisecond).toBe(123);
expect(zonedDateTime.microsecond).toBe(456);
expect(zonedDateTime.nanosecond).toBe(789);
expect(zonedDateTime.calendar.id).toBe("iso8601");
expect(zonedDateTime.timeZone.id).toBe("UTC");
});
test("custom time zone object", () => {
const instant = new Temporal.Instant(1625614921123456789n);
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = instant.toZonedDateTimeISO({ timeZone });
expect(zonedDateTime.timeZone).toBe(timeZone);
});
});
describe("errors", () => {
test("this value must be a Temporal.Instant object", () => {
expect(() => {
Temporal.Instant.prototype.toZonedDateTimeISO.call("foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.Instant");
});
});