diff --git a/Libraries/LibJS/Runtime/Date.h b/Libraries/LibJS/Runtime/Date.h index ec816b1a015..d97bcdbe3fc 100644 --- a/Libraries/LibJS/Runtime/Date.h +++ b/Libraries/LibJS/Runtime/Date.h @@ -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(m_datetime.timestamp() * 1000 + m_milliseconds)); diff --git a/Libraries/LibJS/Runtime/DatePrototype.cpp b/Libraries/LibJS/Runtime/DatePrototype.cpp index 6aa8725eac0..b098ff47d3d 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -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); diff --git a/Libraries/LibJS/Runtime/DatePrototype.h b/Libraries/LibJS/Runtime/DatePrototype.h index e74a445049f..7efb094623e 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.h +++ b/Libraries/LibJS/Runtime/DatePrototype.h @@ -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); };