mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:57:45 +00:00
LibJS: Implement Temporal.ZonedDateTime.prototype.offsetNanoseconds
This commit is contained in:
parent
dd36593c72
commit
f937e9b966
4 changed files with 43 additions and 0 deletions
|
@ -283,6 +283,7 @@ namespace JS {
|
||||||
P(next) \
|
P(next) \
|
||||||
P(now) \
|
P(now) \
|
||||||
P(of) \
|
P(of) \
|
||||||
|
P(offsetNanoseconds) \
|
||||||
P(ownKeys) \
|
P(ownKeys) \
|
||||||
P(padEnd) \
|
P(padEnd) \
|
||||||
P(padStart) \
|
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.daysInYear, days_in_year_getter, {}, Attribute::Configurable);
|
||||||
define_native_accessor(vm.names.monthsInYear, months_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.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)
|
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);
|
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(days_in_year_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(in_leap_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…
Add table
Add a link
Reference in a new issue