mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibJS: Implement Temporal.ZonedDateTime.prototype.eraYear
This commit is contained in:
parent
b59e9260db
commit
a8329272cc
Notes:
sideshowbarker
2024-07-18 05:11:27 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/a8329272ccf Pull-request: https://github.com/SerenityOS/serenity/pull/9647 Reviewed-by: https://github.com/IdanHo ✅
3 changed files with 55 additions and 0 deletions
|
@ -60,6 +60,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
|
|||
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);
|
||||
define_native_accessor(vm.names.eraYear, era_year_getter, {}, Attribute::Configurable);
|
||||
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
||||
|
@ -732,6 +733,33 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::era_getter)
|
|||
return calendar_era(global_object, calendar, *plain_date_time);
|
||||
}
|
||||
|
||||
// 15.6.10.3 get Temporal.ZonedDateTime.prototype.eraYear, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.erayear
|
||||
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::era_year_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 ? CalendarEraYear(calendar, plainDateTime).
|
||||
return calendar_era_year(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)
|
||||
{
|
||||
|
|
|
@ -46,6 +46,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(offset_nanoseconds_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(offset_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(era_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(era_year_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.eraYear).toBeUndefined();
|
||||
});
|
||||
|
||||
test("calendar with custom eraYear function", () => {
|
||||
const timeZone = new Temporal.TimeZone("UTC");
|
||||
const calendar = {
|
||||
eraYear() {
|
||||
return 123;
|
||||
},
|
||||
};
|
||||
const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone, calendar);
|
||||
expect(zonedDateTime.eraYear).toBe(123);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.ZonedDateTime object", () => {
|
||||
expect(() => {
|
||||
Reflect.get(Temporal.ZonedDateTime.prototype, "eraYear", "foo");
|
||||
}).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue