LibJS: Implement Temporal.ZonedDateTime.prototype.offsetNanoseconds

This commit is contained in:
Linus Groh 2021-08-04 23:17:07 +01:00 committed by Andreas Kling
parent dd36593c72
commit f937e9b966
Notes: sideshowbarker 2024-07-18 07:27:00 +09:00
4 changed files with 43 additions and 0 deletions

View File

@ -283,6 +283,7 @@ namespace JS {
P(next) \
P(now) \
P(of) \
P(offsetNanoseconds) \
P(ownKeys) \
P(padEnd) \
P(padStart) \

View File

@ -54,6 +54,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.offsetNanoseconds, offset_nanoseconds_getter, {}, Attribute::Configurable);
}
static ZonedDateTime* typed_this(GlobalObject& global_object)
@ -651,4 +652,23 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::in_leap_year_getter)
return calendar_in_leap_year(global_object, calendar, *temporal_date_time);
}
// 6.3.28 get Temporal.ZonedDateTime.prototype.offsetNanoseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.offsetnanoseconds
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::offset_nanoseconds_getter)
{
// 1. Let zonedDateTime be the this value.
// 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
auto* zoned_date_time = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let timeZone be zonedDateTime.[[TimeZone]].
auto& time_zone = zoned_date_time->time_zone();
// 4. Let instant be ! CreateTemporalInstant(zonedDateTime.[[Nanoseconds]]).
auto* instant = create_temporal_instant(global_object, zoned_date_time->nanoseconds());
// 5. Return 𝔽(? GetOffsetNanosecondsFor(timeZone, instant)).
return Value(get_offset_nanoseconds_for(global_object, &time_zone, *instant));
}
}

View File

@ -43,6 +43,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(offset_nanoseconds_getter);
};
}

View File

@ -0,0 +1,21 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone);
expect(zonedDateTime.offsetNanoseconds).toBe(0);
});
test("custom offset", () => {
const timeZone = new Temporal.TimeZone("+01:30");
const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone);
expect(zonedDateTime.offsetNanoseconds).toBe(5400000000000);
});
});
test("errors", () => {
test("this value must be a Temporal.ZonedDateTime object", () => {
expect(() => {
Reflect.get(Temporal.ZonedDateTime.prototype, "offsetNanoseconds", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
});
});