mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 13:57:34 +00:00
js: Implement print function for Date objects
This commit is contained in:
parent
839beb52f3
commit
632231cc0c
3 changed files with 23 additions and 12 deletions
|
@ -35,11 +35,16 @@ public:
|
||||||
virtual ~Date() override;
|
virtual ~Date() override;
|
||||||
|
|
||||||
Core::DateTime& datetime() { return m_datetime; }
|
Core::DateTime& datetime() { return m_datetime; }
|
||||||
|
const Core::DateTime& datetime() const { 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"); }
|
String date_string() const { return m_datetime.to_string("%a %b %d %Y"); }
|
||||||
// FIXME: Deal with timezones once SerenityOS has a working tzset(3)
|
// 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
|
virtual Value value_of() const override
|
||||||
{
|
{
|
||||||
|
|
|
@ -172,10 +172,7 @@ 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 {};
|
||||||
auto string = String::format(
|
auto string = this_object->string();
|
||||||
"%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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
#include <LibJS/Parser.h>
|
#include <LibJS/Parser.h>
|
||||||
#include <LibJS/Runtime/Array.h>
|
#include <LibJS/Runtime/Array.h>
|
||||||
|
#include <LibJS/Runtime/Date.h>
|
||||||
#include <LibJS/Runtime/Function.h>
|
#include <LibJS/Runtime/Function.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
#include <LibJS/Runtime/PrimitiveString.h>
|
#include <LibJS/Runtime/PrimitiveString.h>
|
||||||
|
@ -113,6 +114,11 @@ static void print_function(const JS::Object* function, HashTable<JS::Object*>&)
|
||||||
printf("\033[34;1m[%s]\033[0m", function->class_name());
|
printf("\033[34;1m[%s]\033[0m", function->class_name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void print_date(const JS::Object* date, HashTable<JS::Object*>&)
|
||||||
|
{
|
||||||
|
printf("\033[34;1mDate %s\033[0m", static_cast<const JS::Date*>(date)->string().characters());
|
||||||
|
}
|
||||||
|
|
||||||
void print_value(JS::Value value, HashTable<JS::Object*>& seen_objects)
|
void print_value(JS::Value value, HashTable<JS::Object*>& seen_objects)
|
||||||
{
|
{
|
||||||
if (value.is_object()) {
|
if (value.is_object()) {
|
||||||
|
@ -128,11 +134,14 @@ void print_value(JS::Value value, HashTable<JS::Object*>& seen_objects)
|
||||||
if (value.is_array())
|
if (value.is_array())
|
||||||
return print_array(static_cast<const JS::Array*>(value.as_object()), seen_objects);
|
return print_array(static_cast<const JS::Array*>(value.as_object()), seen_objects);
|
||||||
|
|
||||||
if (value.is_object() && value.as_object()->is_function())
|
if (value.is_object()) {
|
||||||
return print_function(value.as_object(), seen_objects);
|
auto* object = value.as_object();
|
||||||
|
if (object->is_function())
|
||||||
if (value.is_object())
|
return print_function(object, seen_objects);
|
||||||
return print_object(value.as_object(), seen_objects);
|
if (object->is_date())
|
||||||
|
return print_date(object, seen_objects);
|
||||||
|
return print_object(object, seen_objects);
|
||||||
|
}
|
||||||
|
|
||||||
if (value.is_string())
|
if (value.is_string())
|
||||||
printf("\033[31;1m");
|
printf("\033[31;1m");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue