mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 17:58:18 +03:00
LibJS: Implement Temporal.ZonedDateTime.prototype.offsetNanoseconds
This commit is contained in:
parent
dd36593c72
commit
f937e9b966
Notes:
sideshowbarker
2024-07-18 07:27:00 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/f937e9b9663 Pull-request: https://github.com/SerenityOS/serenity/pull/9213 Reviewed-by: https://github.com/IdanHo ✅
@ -283,6 +283,7 @@ namespace JS {
|
||||
P(next) \
|
||||
P(now) \
|
||||
P(of) \
|
||||
P(offsetNanoseconds) \
|
||||
P(ownKeys) \
|
||||
P(padEnd) \
|
||||
P(padStart) \
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -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");
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user