mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-01 20:10:28 +00:00
LibJS: Implement Temporal.ZonedDateTime.prototype.monthCode
This commit is contained in:
parent
fd18dc782f
commit
30e27f6483
Notes:
sideshowbarker
2024-07-18 07:28:10 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/30e27f6483b Pull-request: https://github.com/SerenityOS/serenity/pull/9213 Reviewed-by: https://github.com/IdanHo ✅
3 changed files with 44 additions and 0 deletions
|
@ -34,6 +34,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
|
|||
define_native_accessor(vm.names.timeZone, time_zone_getter, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
|
||||
}
|
||||
|
||||
static ZonedDateTime* typed_this(GlobalObject& global_object)
|
||||
|
@ -129,4 +130,31 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::month_getter)
|
|||
return Value(calendar_month(global_object, calendar, *temporal_date_time));
|
||||
}
|
||||
|
||||
// 6.3.7 get Temporal.ZonedDateTime.prototype.monthCode, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.monthcode
|
||||
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::month_code_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 temporalDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar).
|
||||
auto* temporal_date_time = builtin_time_zone_get_plain_date_time_for(global_object, &time_zone, *instant, calendar);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 7. Return ? CalendarMonthCode(calendar, temporalDateTime).
|
||||
return js_string(vm, calendar_month_code(global_object, calendar, *temporal_date_time));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(time_zone_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(month_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
describe("correct behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
const timeZone = new Temporal.TimeZone("UTC");
|
||||
const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone);
|
||||
expect(zonedDateTime.monthCode).toBe("M07");
|
||||
});
|
||||
});
|
||||
|
||||
test("errors", () => {
|
||||
test("this value must be a Temporal.ZonedDateTime object", () => {
|
||||
expect(() => {
|
||||
Reflect.get(Temporal.ZonedDateTime.prototype, "monthCode", "foo");
|
||||
}).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue