Explorar o código

LibJS: Implement Date.prototype.toString()

Linus Groh %!s(int64=5) %!d(string=hai) anos
pai
achega
dc9b1226ac

+ 11 - 0
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<double>(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));
+}
+
 }

+ 1 - 0
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&);
 };
 
 }