LibJS: Implement Temporal.ZonedDateTime.prototype.era
This commit is contained in:
parent
f59e4d6738
commit
b59e9260db
Notes:
sideshowbarker
2024-07-18 05:11:30 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/b59e9260db2 Pull-request: https://github.com/SerenityOS/serenity/pull/9647 Reviewed-by: https://github.com/IdanHo ✅
3 changed files with 55 additions and 0 deletions
|
@ -59,6 +59,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
|
|||
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.offsetNanoseconds, offset_nanoseconds_getter, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.offset, offset_getter, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.era, era_getter, {}, Attribute::Configurable);
|
||||
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
||||
|
@ -704,6 +705,33 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::offset_getter)
|
|||
return js_string(vm, move(*offset_string));
|
||||
}
|
||||
|
||||
// 15.6.10.2 get Temporal.ZonedDateTime.prototype.era, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.era
|
||||
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::era_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. Let calendar be zonedDateTime.[[Calendar]].
|
||||
auto& calendar = zoned_date_time->calendar();
|
||||
|
||||
// 6. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar).
|
||||
auto* plain_date_time = builtin_time_zone_get_plain_date_time_for(global_object, &time_zone, *instant, calendar);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 7. Return ? CalendarEra(calendar, plainDateTime).
|
||||
return calendar_era(global_object, calendar, *plain_date_time);
|
||||
}
|
||||
|
||||
// 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof
|
||||
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of)
|
||||
{
|
||||
|
|
|
@ -45,6 +45,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(offset_nanoseconds_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(offset_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(era_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_instant);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
describe("correct behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
const timeZone = new Temporal.TimeZone("UTC");
|
||||
const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone);
|
||||
expect(zonedDateTime.era).toBeUndefined();
|
||||
});
|
||||
|
||||
test("calendar with custom era function", () => {
|
||||
const timeZone = new Temporal.TimeZone("UTC");
|
||||
const calendar = {
|
||||
era() {
|
||||
return "foo";
|
||||
},
|
||||
};
|
||||
const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone, calendar);
|
||||
expect(zonedDateTime.era).toBe("foo");
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.ZonedDateTime object", () => {
|
||||
expect(() => {
|
||||
Reflect.get(Temporal.ZonedDateTime.prototype, "era", "foo");
|
||||
}).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue