From d5eaefe87b315cd9a5f8fcd047b98ca4606dcdb6 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sun, 23 Aug 2020 13:27:14 -0400 Subject: LibJS: Move datetime access out of DatePrototype How Date keeps time internally should be an implementation detail of Date, so move it behind accessors. No behavior change. --- Libraries/LibJS/Runtime/Date.h | 12 +++++++++++- Libraries/LibJS/Runtime/DatePrototype.cpp | 24 ++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Libraries/LibJS/Runtime/Date.h b/Libraries/LibJS/Runtime/Date.h index cfd573875e..3fd7d23147 100644 --- a/Libraries/LibJS/Runtime/Date.h +++ b/Libraries/LibJS/Runtime/Date.h @@ -42,7 +42,17 @@ public: Core::DateTime& datetime() { return m_datetime; } const Core::DateTime& datetime() const { return m_datetime; } - u16 milliseconds() { return m_milliseconds; } + + int date() const { return datetime().day(); } + int day() const { return datetime().weekday(); } + int full_year() const { return datetime().year(); } + int hours() const { return datetime().hour(); } + u16 milliseconds() const { return m_milliseconds; } + int minutes() const { return datetime().minute(); } + int month() const { return datetime().month() - 1; } + int seconds() const { return datetime().second(); } + double time() const { return datetime().timestamp() * 1000.0 + milliseconds(); } + int year() const { return datetime().day(); } String date_string() const { return m_datetime.to_string("%a %b %d %Y"); } // FIXME: Deal with timezones once SerenityOS has a working tzset(3) diff --git a/Libraries/LibJS/Runtime/DatePrototype.cpp b/Libraries/LibJS/Runtime/DatePrototype.cpp index 510f2b3b66..77837c3351 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -91,8 +91,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_date) auto* this_object = typed_this(interpreter, global_object); if (!this_object) return {}; - auto date = this_object->datetime().day(); - return Value(static_cast(date)); + return Value(static_cast(this_object->date())); } JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_day) @@ -100,8 +99,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_day) auto* this_object = typed_this(interpreter, global_object); if (!this_object) return {}; - auto day = this_object->datetime().weekday(); - return Value(static_cast(day)); + return Value(static_cast(this_object->day())); } JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_full_year) @@ -109,8 +107,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_full_year) auto* this_object = typed_this(interpreter, global_object); if (!this_object) return {}; - auto full_year = this_object->datetime().year(); - return Value(static_cast(full_year)); + return Value(static_cast(this_object->full_year())); } JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours) @@ -118,8 +115,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours) auto* this_object = typed_this(interpreter, global_object); if (!this_object) return {}; - auto hours = this_object->datetime().hour(); - return Value(static_cast(hours)); + return Value(static_cast(this_object->hours())); } JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_milliseconds) @@ -127,8 +123,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_milliseconds) auto* this_object = typed_this(interpreter, global_object); if (!this_object) return {}; - auto milliseconds = this_object->milliseconds(); - return Value(static_cast(milliseconds)); + return Value(static_cast(this_object->milliseconds())); } JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_minutes) @@ -136,8 +131,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_minutes) auto* this_object = typed_this(interpreter, global_object); if (!this_object) return {}; - auto minutes = this_object->datetime().minute(); - return Value(static_cast(minutes)); + return Value(static_cast(this_object->minutes())); } JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_month) @@ -145,8 +139,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_month) auto* this_object = typed_this(interpreter, global_object); if (!this_object) return {}; - auto months = this_object->datetime().month() - 1; - return Value(static_cast(months)); + return Value(static_cast(this_object->month())); } JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_seconds) @@ -154,8 +147,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_seconds) auto* this_object = typed_this(interpreter, global_object); if (!this_object) return {}; - auto seconds = this_object->datetime().second(); - return Value(static_cast(seconds)); + return Value(static_cast(this_object->seconds())); } JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_time) -- cgit v1.2.3