mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:47:45 +00:00
LibJS: Add Date.prototype.{get, set}Year()
This commit is contained in:
parent
96ee5e36ed
commit
09233b9e41
3 changed files with 43 additions and 0 deletions
|
@ -133,6 +133,7 @@ namespace JS {
|
||||||
P(getUTCMinutes) \
|
P(getUTCMinutes) \
|
||||||
P(getUTCMonth) \
|
P(getUTCMonth) \
|
||||||
P(getUTCSeconds) \
|
P(getUTCSeconds) \
|
||||||
|
P(getYear) \
|
||||||
P(global) \
|
P(global) \
|
||||||
P(globalThis) \
|
P(globalThis) \
|
||||||
P(groups) \
|
P(groups) \
|
||||||
|
@ -207,6 +208,7 @@ namespace JS {
|
||||||
P(setMinutes) \
|
P(setMinutes) \
|
||||||
P(setPrototypeOf) \
|
P(setPrototypeOf) \
|
||||||
P(setSeconds) \
|
P(setSeconds) \
|
||||||
|
P(setYear) \
|
||||||
P(shift) \
|
P(shift) \
|
||||||
P(sign) \
|
P(sign) \
|
||||||
P(sin) \
|
P(sin) \
|
||||||
|
|
|
@ -42,6 +42,8 @@ void DatePrototype::initialize(GlobalObject& global_object)
|
||||||
define_native_function(vm.names.getDay, get_day, 0, attr);
|
define_native_function(vm.names.getDay, get_day, 0, attr);
|
||||||
define_native_function(vm.names.getFullYear, get_full_year, 0, attr);
|
define_native_function(vm.names.getFullYear, get_full_year, 0, attr);
|
||||||
define_native_function(vm.names.setFullYear, set_full_year, 3, attr);
|
define_native_function(vm.names.setFullYear, set_full_year, 3, attr);
|
||||||
|
define_native_function(vm.names.getYear, get_year, 0, attr);
|
||||||
|
define_native_function(vm.names.setYear, set_year, 1, attr);
|
||||||
define_native_function(vm.names.getHours, get_hours, 0, attr);
|
define_native_function(vm.names.getHours, get_hours, 0, attr);
|
||||||
define_native_function(vm.names.setHours, set_hours, 4, attr);
|
define_native_function(vm.names.setHours, set_hours, 4, attr);
|
||||||
define_native_function(vm.names.getMilliseconds, get_milliseconds, 0, attr);
|
define_native_function(vm.names.getMilliseconds, get_milliseconds, 0, attr);
|
||||||
|
@ -161,6 +163,43 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_full_year)
|
||||||
return Value(this_object->time());
|
return Value(this_object->time());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_year)
|
||||||
|
{
|
||||||
|
auto* this_object = typed_this(vm, global_object);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if (this_object->is_invalid())
|
||||||
|
return js_nan();
|
||||||
|
|
||||||
|
return Value(this_object->year() - 1900);
|
||||||
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_year)
|
||||||
|
{
|
||||||
|
auto* this_object = typed_this(vm, global_object);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto& datetime = this_object->datetime();
|
||||||
|
|
||||||
|
auto new_year_value = vm.argument(0).to_number(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
if (!new_year_value.is_finite_number()) {
|
||||||
|
this_object->set_is_invalid(true);
|
||||||
|
return js_nan();
|
||||||
|
}
|
||||||
|
auto new_year = new_year_value.as_i32();
|
||||||
|
if (new_year >= 0 && new_year <= 99)
|
||||||
|
new_year += 1900;
|
||||||
|
|
||||||
|
datetime.set_time(new_year, datetime.month(), datetime.day(), datetime.hour(), datetime.minute(), datetime.second());
|
||||||
|
this_object->set_is_invalid(false);
|
||||||
|
|
||||||
|
return Value(this_object->time());
|
||||||
|
}
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours)
|
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours)
|
||||||
{
|
{
|
||||||
auto* this_object = typed_this(vm, global_object);
|
auto* this_object = typed_this(vm, global_object);
|
||||||
|
|
|
@ -23,6 +23,8 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_day);
|
JS_DECLARE_NATIVE_FUNCTION(get_day);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_full_year);
|
JS_DECLARE_NATIVE_FUNCTION(get_full_year);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(set_full_year);
|
JS_DECLARE_NATIVE_FUNCTION(set_full_year);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(get_year);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(set_year);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_hours);
|
JS_DECLARE_NATIVE_FUNCTION(get_hours);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(set_hours);
|
JS_DECLARE_NATIVE_FUNCTION(set_hours);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_milliseconds);
|
JS_DECLARE_NATIVE_FUNCTION(get_milliseconds);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue