mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:37:34 +00:00
LibJS: Implement Date.prototype.to{Date,Time}String()
This commit is contained in:
parent
dc9b1226ac
commit
3fcea71538
3 changed files with 31 additions and 2 deletions
|
@ -36,6 +36,11 @@ public:
|
||||||
|
|
||||||
Core::DateTime& datetime() { return m_datetime; }
|
Core::DateTime& datetime() { return m_datetime; }
|
||||||
u16 milliseconds() { return m_milliseconds; }
|
u16 milliseconds() { return m_milliseconds; }
|
||||||
|
|
||||||
|
String date_string() { 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)"); }
|
||||||
|
|
||||||
virtual Value value_of() const override
|
virtual Value value_of() const override
|
||||||
{
|
{
|
||||||
return Value(static_cast<double>(m_datetime.timestamp() * 1000 + m_milliseconds));
|
return Value(static_cast<double>(m_datetime.timestamp() * 1000 + m_milliseconds));
|
||||||
|
|
|
@ -58,6 +58,8 @@ DatePrototype::DatePrototype()
|
||||||
put_native_function("getMonth", get_month);
|
put_native_function("getMonth", get_month);
|
||||||
put_native_function("getSeconds", get_seconds);
|
put_native_function("getSeconds", get_seconds);
|
||||||
put_native_function("getTime", get_time);
|
put_native_function("getTime", get_time);
|
||||||
|
put_native_function("toDateString", to_date_string);
|
||||||
|
put_native_function("toTimeString", to_time_string);
|
||||||
put_native_function("toString", to_string);
|
put_native_function("toString", to_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,13 +149,33 @@ Value DatePrototype::get_time(Interpreter& interpreter)
|
||||||
return Value(static_cast<double>(seconds * 1000 + milliseconds));
|
return Value(static_cast<double>(seconds * 1000 + milliseconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value DatePrototype::to_date_string(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
auto* this_object = this_date_from_interpreter(interpreter);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
auto string = this_object->date_string();
|
||||||
|
return js_string(interpreter.heap(), move(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value DatePrototype::to_time_string(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
auto* this_object = this_date_from_interpreter(interpreter);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
auto string = this_object->time_string();
|
||||||
|
return js_string(interpreter.heap(), move(string));
|
||||||
|
}
|
||||||
|
|
||||||
Value DatePrototype::to_string(Interpreter& interpreter)
|
Value DatePrototype::to_string(Interpreter& interpreter)
|
||||||
{
|
{
|
||||||
auto* this_object = this_date_from_interpreter(interpreter);
|
auto* this_object = this_date_from_interpreter(interpreter);
|
||||||
if (!this_object)
|
if (!this_object)
|
||||||
return {};
|
return {};
|
||||||
// FIXME: Deal with timezones once SerenityOS has a working tzset(3)
|
auto string = String::format(
|
||||||
auto string = this_object->datetime().to_string("%a %b %d %Y %T GMT+0000 (UTC)");
|
"%s %s",
|
||||||
|
this_object->date_string().characters(),
|
||||||
|
this_object->time_string().characters());
|
||||||
return js_string(interpreter.heap(), move(string));
|
return js_string(interpreter.heap(), move(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,8 @@ private:
|
||||||
static Value get_month(Interpreter&);
|
static Value get_month(Interpreter&);
|
||||||
static Value get_seconds(Interpreter&);
|
static Value get_seconds(Interpreter&);
|
||||||
static Value get_time(Interpreter&);
|
static Value get_time(Interpreter&);
|
||||||
|
static Value to_date_string(Interpreter&);
|
||||||
|
static Value to_time_string(Interpreter&);
|
||||||
static Value to_string(Interpreter&);
|
static Value to_string(Interpreter&);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue