summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-08-23 13:27:14 -0400
committerAndreas Kling <kling@serenityos.org>2020-08-23 22:00:05 +0200
commitd5eaefe87b315cd9a5f8fcd047b98ca4606dcdb6 (patch)
tree785aabb38e51839815f1f590fe5b4c808daf6a98
parentf0ef283f3c6c16e593a6773e9b9ea542507db43b (diff)
downloadserenity-d5eaefe87b315cd9a5f8fcd047b98ca4606dcdb6.zip
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.
-rw-r--r--Libraries/LibJS/Runtime/Date.h12
-rw-r--r--Libraries/LibJS/Runtime/DatePrototype.cpp24
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<double>(date));
+ return Value(static_cast<double>(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<double>(day));
+ return Value(static_cast<double>(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<double>(full_year));
+ return Value(static_cast<double>(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<double>(hours));
+ return Value(static_cast<double>(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<double>(milliseconds));
+ return Value(static_cast<double>(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<double>(minutes));
+ return Value(static_cast<double>(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<double>(months));
+ return Value(static_cast<double>(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<double>(seconds));
+ return Value(static_cast<double>(this_object->seconds()));
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_time)