diff --git a/Userland/Libraries/LibJS/Runtime/Date.cpp b/Userland/Libraries/LibJS/Runtime/Date.cpp index c96addb14e..39a07cdcc6 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.cpp +++ b/Userland/Libraries/LibJS/Runtime/Date.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include namespace JS { @@ -17,6 +19,15 @@ Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, u16 mil return global_object.heap().allocate(global_object, datetime, milliseconds, is_invalid, *global_object.date_prototype()); } +Date* Date::now(GlobalObject& global_object) +{ + struct timeval tv; + gettimeofday(&tv, nullptr); + auto datetime = Core::DateTime::now(); + auto milliseconds = static_cast(tv.tv_usec / 1000); + return create(global_object, datetime, milliseconds); +} + Date::Date(Core::DateTime datetime, u16 milliseconds, bool is_invalid, Object& prototype) : Object(prototype) , m_datetime(datetime) diff --git a/Userland/Libraries/LibJS/Runtime/Date.h b/Userland/Libraries/LibJS/Runtime/Date.h index fd17c68481..fc75f2d280 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.h +++ b/Userland/Libraries/LibJS/Runtime/Date.h @@ -16,6 +16,7 @@ class Date final : public Object { public: static Date* create(GlobalObject&, Core::DateTime, u16 milliseconds, bool is_invalid = false); + static Date* now(GlobalObject&); Date(Core::DateTime datetime, u16 milliseconds, bool is_invalid, Object& prototype); virtual ~Date() override; diff --git a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp index 9d21a7907f..33c2bf5259 100644 --- a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -141,22 +141,14 @@ DateConstructor::~DateConstructor() Value DateConstructor::call() { - auto date = construct(*this); - if (!date.is_object()) - return {}; - return js_string(heap(), static_cast(date.as_object()).string()); + return js_string(heap(), Date::now(global_object())->string()); } Value DateConstructor::construct(Function&) { auto& vm = this->vm(); - if (vm.argument_count() == 0) { - struct timeval tv; - gettimeofday(&tv, nullptr); - auto datetime = Core::DateTime::now(); - auto milliseconds = static_cast(tv.tv_usec / 1000); - return Date::create(global_object(), datetime, milliseconds); - } + if (vm.argument_count() == 0) + return Date::now(global_object()); auto create_invalid_date = [this]() { auto datetime = Core::DateTime::create(1970, 1, 1, 0, 0, 0);