diff options
author | Linus Groh <mail@linusgroh.de> | 2020-03-31 18:43:45 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-31 21:19:21 +0200 |
commit | 632231cc0c1520f51b713df2b55ef3a34513d496 (patch) | |
tree | 6d13dbb62e67ea4011aeea2ef998e3585c647b15 /Libraries | |
parent | 839beb52f372c90ae258ae4a9496c5a854351be2 (diff) | |
download | serenity-632231cc0c1520f51b713df2b55ef3a34513d496.zip |
js: Implement print function for Date objects
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/Date.h | 9 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/DatePrototype.cpp | 5 |
2 files changed, 8 insertions, 6 deletions
diff --git a/Libraries/LibJS/Runtime/Date.h b/Libraries/LibJS/Runtime/Date.h index 875f8bf5d2..557af7ce2c 100644 --- a/Libraries/LibJS/Runtime/Date.h +++ b/Libraries/LibJS/Runtime/Date.h @@ -35,11 +35,16 @@ public: virtual ~Date() override; Core::DateTime& datetime() { return m_datetime; } + const Core::DateTime& datetime() const { return m_datetime; } u16 milliseconds() { return m_milliseconds; } - String date_string() { return m_datetime.to_string("%a %b %d %Y"); } + String date_string() const { return m_datetime.to_string("%a %b %d %Y"); } // FIXME: Deal with timezones once SerenityOS has a working tzset(3) - String time_string() { return m_datetime.to_string("%T GMT+0000 (UTC)"); } + String time_string() const { return m_datetime.to_string("%T GMT+0000 (UTC)"); } + String string() const + { + return String::format("%s %s", date_string().characters(), time_string().characters()); + } virtual Value value_of() const override { diff --git a/Libraries/LibJS/Runtime/DatePrototype.cpp b/Libraries/LibJS/Runtime/DatePrototype.cpp index c4cf3a6599..eb58b7b64c 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -172,10 +172,7 @@ Value DatePrototype::to_string(Interpreter& interpreter) auto* this_object = this_date_from_interpreter(interpreter); if (!this_object) return {}; - auto string = String::format( - "%s %s", - this_object->date_string().characters(), - this_object->time_string().characters()); + auto string = this_object->string(); return js_string(interpreter.heap(), move(string)); } |