1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:37:44 +00:00

LibJS: Implement Date.prototype.setUTCMonth

This commit is contained in:
Timothy Flynn 2022-01-15 15:37:16 -05:00 committed by Andreas Kling
parent c71877b0a6
commit f2ffe3bf90
3 changed files with 80 additions and 1 deletions

View file

@ -77,7 +77,7 @@ void DatePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.setUTCHours, set_hours, 4, attr); // FIXME: see above
define_native_function(vm.names.setUTCMilliseconds, set_milliseconds, 1, attr); // FIXME: see above
define_native_function(vm.names.setUTCMinutes, set_minutes, 3, attr); // FIXME: see above
define_native_function(vm.names.setUTCMonth, set_month, 2, attr); // FIXME: see above
define_native_function(vm.names.setUTCMonth, set_utc_month, 2, attr);
define_native_function(vm.names.setUTCSeconds, set_utc_seconds, 2, attr);
define_native_function(vm.names.toDateString, to_date_string, 0, attr);
define_native_function(vm.names.toISOString, to_iso_string, 0, attr);
@ -619,6 +619,38 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_time)
return time;
}
// 21.4.4.33 Date.prototype.setUTCMonth ( month [ , date ] ), https://tc39.es/ecma262/#sec-date.prototype.setutcmonth
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_utc_month)
{
// 1. Let t be ? thisTimeValue(this value).
auto this_time = TRY(this_time_value(global_object, vm.this_value(global_object)));
auto time = local_time(this_time.as_double());
// 2. Let m be ? ToNumber(month).
auto month = TRY(vm.argument(0).to_number(global_object));
// 3. If date is not present, let dt be DateFromTime(t).
// 4. Else,
// a. Let dt be ? ToNumber(date).
auto date = TRY(argument_or_value(global_object, 1, date_from_time(time)));
// 5. Let newDate be MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t)).
auto year = Value(year_from_time(time));
auto day = make_day(global_object, year, month, date);
auto new_date = make_date(day, Value(time_within_day(time)));
// 6. Let v be TimeClip(newDate).
new_date = time_clip(global_object, new_date);
// 7. Set the [[DateValue]] internal slot of this Date object to v.
auto* this_object = MUST(typed_this_object(global_object));
this_object->set_date_value(new_date.as_double());
// 8. Return v.
return new_date;
}
// 21.4.4.34 Date.prototype.setUTCSeconds ( sec [ , ms ] ), https://tc39.es/ecma262/#sec-date.prototype.setutcseconds
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_utc_seconds)
{

View file

@ -46,6 +46,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(set_month);
JS_DECLARE_NATIVE_FUNCTION(set_seconds);
JS_DECLARE_NATIVE_FUNCTION(set_time);
JS_DECLARE_NATIVE_FUNCTION(set_utc_month);
JS_DECLARE_NATIVE_FUNCTION(set_utc_seconds);
JS_DECLARE_NATIVE_FUNCTION(to_date_string);
JS_DECLARE_NATIVE_FUNCTION(to_iso_string);

View file

@ -0,0 +1,46 @@
describe("errors", () => {
test("called on non-Date object", () => {
expect(() => {
Date.prototype.setUTCMonth();
}).toThrowWithMessage(TypeError, "Not an object of type Date");
});
test("called with non-numeric parameters", () => {
expect(() => {
new Date().setUTCMonth(Symbol.hasInstance);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
expect(() => {
new Date().setUTCMonth(1989, Symbol.hasInstance);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
});
});
describe("correct behavior", () => {
const d = new Date(2000, 2, 1);
test("basic functionality", () => {
d.setUTCMonth(8);
expect(d.getUTCMonth()).toBe(8);
d.setUTCMonth(9, 15);
expect(d.getUTCMonth()).toBe(9);
expect(d.getUTCDate()).toBe(15);
d.setUTCMonth("");
expect(d.getUTCMonth()).toBe(0);
d.setUTCMonth("a");
expect(d.getUTCMonth()).toBe(NaN);
});
test("NaN", () => {
d.setUTCMonth(NaN);
expect(d.getUTCMonth()).toBeNaN();
});
test("time clip", () => {
d.setUTCMonth(8.65e15);
expect(d.getUTCMonth()).toBeNaN();
});
});