Browse Source

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.
Nico Weber 4 years ago
parent
commit
d5eaefe87b
2 changed files with 19 additions and 17 deletions
  1. 11 1
      Libraries/LibJS/Runtime/Date.h
  2. 8 16
      Libraries/LibJS/Runtime/DatePrototype.cpp

+ 11 - 1
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)

+ 8 - 16
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)