LibJS: Add toLocaleString(), toLocaleDateString(), toLocaleTimeString() to Date

These just return a "readable" implementation of the date for now.
This commit is contained in:
Nico Weber 2020-08-17 10:11:07 -04:00 committed by Andreas Kling
parent 8ebef785eb
commit a3908732ad
Notes: sideshowbarker 2024-07-19 03:28:55 +09:00
3 changed files with 41 additions and 0 deletions

View file

@ -52,6 +52,11 @@ public:
return String::format("%s %s", date_string().characters(), time_string().characters());
}
// FIXME: One day, implement real locale support. Until then, everyone gets what the Clock MenuApplet displays.
String locale_date_string() const { return m_datetime.to_string("%Y-%m-%d"); }
String locale_string() const { return m_datetime.to_string(); }
String locale_time_string() const { return m_datetime.to_string("%H:%M:%S"); }
virtual Value value_of() const override
{
return Value(static_cast<double>(m_datetime.timestamp() * 1000 + m_milliseconds));

View file

@ -67,6 +67,9 @@ void DatePrototype::initialize(GlobalObject& global_object)
define_native_function("getSeconds", get_seconds, 0, attr);
define_native_function("getTime", get_time, 0, attr);
define_native_function("toDateString", to_date_string, 0, attr);
define_native_function("toLocaleDateString", to_locale_date_string, 0, attr);
define_native_function("toLocaleString", to_locale_string, 0, attr);
define_native_function("toLocaleTimeString", to_locale_time_string, 0, attr);
define_native_function("toTimeString", to_time_string, 0, attr);
define_native_function("toString", to_string, 0, attr);
}
@ -166,6 +169,36 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_date_string)
return js_string(interpreter, move(string));
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string)
{
auto* this_object = typed_this(interpreter, global_object);
if (!this_object)
return {};
// FIXME: Optional locales, options params.
auto string = this_object->locale_date_string();
return js_string(interpreter, move(string));
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string)
{
auto* this_object = typed_this(interpreter, global_object);
if (!this_object)
return {};
// FIXME: Optional locales, options params.
auto string = this_object->locale_string();
return js_string(interpreter, move(string));
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
{
auto* this_object = typed_this(interpreter, global_object);
if (!this_object)
return {};
// FIXME: Optional locales, options params.
auto string = this_object->locale_time_string();
return js_string(interpreter, move(string));
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_time_string)
{
auto* this_object = typed_this(interpreter, global_object);

View file

@ -48,6 +48,9 @@ private:
JS_DECLARE_NATIVE_FUNCTION(get_seconds);
JS_DECLARE_NATIVE_FUNCTION(get_time);
JS_DECLARE_NATIVE_FUNCTION(to_date_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_date_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_time_string);
JS_DECLARE_NATIVE_FUNCTION(to_time_string);
JS_DECLARE_NATIVE_FUNCTION(to_string);
};