From dc9b1226ac8b5ee2305ab849b3dbdaa0627fc135 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 30 Mar 2020 18:32:00 +0100 Subject: [PATCH] LibJS: Implement Date.prototype.toString() --- Libraries/LibJS/Runtime/DatePrototype.cpp | 11 +++++++++++ Libraries/LibJS/Runtime/DatePrototype.h | 1 + 2 files changed, 12 insertions(+) diff --git a/Libraries/LibJS/Runtime/DatePrototype.cpp b/Libraries/LibJS/Runtime/DatePrototype.cpp index 9be2217c96..0bd6e260ed 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -58,6 +58,7 @@ DatePrototype::DatePrototype() put_native_function("getMonth", get_month); put_native_function("getSeconds", get_seconds); put_native_function("getTime", get_time); + put_native_function("toString", to_string); } DatePrototype::~DatePrototype() @@ -146,4 +147,14 @@ Value DatePrototype::get_time(Interpreter& interpreter) return Value(static_cast(seconds * 1000 + milliseconds)); } +Value DatePrototype::to_string(Interpreter& interpreter) +{ + auto* this_object = this_date_from_interpreter(interpreter); + if (!this_object) + return {}; + // FIXME: Deal with timezones once SerenityOS has a working tzset(3) + auto string = this_object->datetime().to_string("%a %b %d %Y %T GMT+0000 (UTC)"); + return js_string(interpreter.heap(), move(string)); +} + } diff --git a/Libraries/LibJS/Runtime/DatePrototype.h b/Libraries/LibJS/Runtime/DatePrototype.h index 9ed93a092c..69205f2802 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.h +++ b/Libraries/LibJS/Runtime/DatePrototype.h @@ -47,6 +47,7 @@ private: static Value get_month(Interpreter&); static Value get_seconds(Interpreter&); static Value get_time(Interpreter&); + static Value to_string(Interpreter&); }; }