mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:37:46 +00:00
LibJS: Add fast path TimeZone conversion to PlainDate#toZonedDateTime
This is a normative chane in the Temporal spec.
See: fcab1af
This commit is contained in:
parent
707f12f927
commit
35c9e324b4
2 changed files with 42 additions and 12 deletions
|
@ -525,23 +525,33 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_zoned_date_time)
|
||||||
|
|
||||||
// 3. If Type(item) is Object, then
|
// 3. If Type(item) is Object, then
|
||||||
if (item.is_object()) {
|
if (item.is_object()) {
|
||||||
// a. Let timeZoneLike be ? Get(item, "timeZone").
|
// a. If item has an [[InitializedTemporalTimeZone]] internal slot, then
|
||||||
auto time_zone_like = TRY(item.as_object().get(vm.names.timeZone));
|
if (is<TimeZone>(item.as_object())) {
|
||||||
|
// i. Let timeZone be item.
|
||||||
// b. If timeZoneLike is undefined, then
|
time_zone = &item.as_object();
|
||||||
if (time_zone_like.is_undefined()) {
|
|
||||||
// i. Let timeZone be ? ToTemporalTimeZone(item).
|
|
||||||
time_zone = TRY(to_temporal_time_zone(vm, item));
|
|
||||||
|
|
||||||
// ii. Let temporalTime be undefined.
|
// ii. Let temporalTime be undefined.
|
||||||
}
|
}
|
||||||
// c. Else,
|
// b. Else,
|
||||||
else {
|
else {
|
||||||
// i. Let timeZone be ? ToTemporalTimeZone(timeZoneLike).
|
// i. Let timeZoneLike be ? Get(item, "timeZone").
|
||||||
time_zone = TRY(to_temporal_time_zone(vm, time_zone_like));
|
auto time_zone_like = TRY(item.as_object().get(vm.names.timeZone));
|
||||||
|
|
||||||
// ii. Let temporalTime be ? Get(item, "plainTime").
|
// ii. If timeZoneLike is undefined, then
|
||||||
temporal_time_value = TRY(item.as_object().get(vm.names.plainTime));
|
if (time_zone_like.is_undefined()) {
|
||||||
|
// 1. Let timeZone be ? ToTemporalTimeZone(item).
|
||||||
|
time_zone = TRY(to_temporal_time_zone(vm, item));
|
||||||
|
|
||||||
|
// 2. Let temporalTime be undefined.
|
||||||
|
}
|
||||||
|
// iii. Else,
|
||||||
|
else {
|
||||||
|
// 1. Let timeZone be ? ToTemporalTimeZone(timeZoneLike).
|
||||||
|
time_zone = TRY(to_temporal_time_zone(vm, time_zone_like));
|
||||||
|
|
||||||
|
// 2. Let temporalTime be ? Get(item, "plainTime").
|
||||||
|
temporal_time_value = TRY(item.as_object().get(vm.names.plainTime));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 4. Else,
|
// 4. Else,
|
||||||
|
|
|
@ -74,6 +74,26 @@ describe("correct behavior", () => {
|
||||||
expect(zonedDateTime.calendar).toBe(plainDate.calendar);
|
expect(zonedDateTime.calendar).toBe(plainDate.calendar);
|
||||||
expect(zonedDateTime.timeZone.id).toBe("UTC");
|
expect(zonedDateTime.timeZone.id).toBe("UTC");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("time zone fast path returns if it is passed a Temporal.TimeZone instance", () => {
|
||||||
|
const plainDate = new Temporal.PlainDate(2021, 7, 6);
|
||||||
|
|
||||||
|
// This is obseravble via there being no property lookups (avoiding a "timeZone" property lookup in this case)
|
||||||
|
let madeObservableHasPropertyLookup = false;
|
||||||
|
class TimeZone extends Temporal.TimeZone {
|
||||||
|
constructor() {
|
||||||
|
super("UTC");
|
||||||
|
}
|
||||||
|
|
||||||
|
get timeZone() {
|
||||||
|
madeObservableHasPropertyLookup = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const timeZone = new TimeZone();
|
||||||
|
plainDate.toZonedDateTime(timeZone);
|
||||||
|
expect(madeObservableHasPropertyLookup).toBeFalse();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("errors", () => {
|
describe("errors", () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue