LibJS: Remove Core::DateTime logic from the Date object :^)
This commit is contained in:
parent
58ccca6a9d
commit
34a1dd4257
Notes:
sideshowbarker
2024-07-17 20:50:45 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/34a1dd42573 Pull-request: https://github.com/SerenityOS/serenity/pull/11913 Reviewed-by: https://github.com/linusg ✅ Reviewed-by: https://github.com/nico
2 changed files with 0 additions and 124 deletions
|
@ -16,24 +16,11 @@
|
|||
|
||||
namespace JS {
|
||||
|
||||
Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, i16 milliseconds, bool is_invalid)
|
||||
{
|
||||
return global_object.heap().allocate<Date>(global_object, datetime, milliseconds, is_invalid, *global_object.date_prototype());
|
||||
}
|
||||
|
||||
Date* Date::create(GlobalObject& global_object, double date_value)
|
||||
{
|
||||
return global_object.heap().allocate<Date>(global_object, date_value, *global_object.date_prototype());
|
||||
}
|
||||
|
||||
Date::Date(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& prototype)
|
||||
: Object(prototype)
|
||||
, m_datetime(datetime)
|
||||
, m_milliseconds(milliseconds)
|
||||
, m_is_invalid(is_invalid)
|
||||
{
|
||||
}
|
||||
|
||||
Date::Date(double date_value, Object& prototype)
|
||||
: Object(prototype)
|
||||
, m_date_value(date_value)
|
||||
|
@ -44,56 +31,6 @@ Date::~Date()
|
|||
{
|
||||
}
|
||||
|
||||
tm Date::to_utc_tm() const
|
||||
{
|
||||
time_t timestamp = m_datetime.timestamp();
|
||||
struct tm tm;
|
||||
gmtime_r(×tamp, &tm);
|
||||
return tm;
|
||||
}
|
||||
|
||||
int Date::utc_date() const
|
||||
{
|
||||
return to_utc_tm().tm_mday;
|
||||
}
|
||||
|
||||
int Date::utc_day() const
|
||||
{
|
||||
return to_utc_tm().tm_wday;
|
||||
}
|
||||
|
||||
int Date::utc_full_year() const
|
||||
{
|
||||
return to_utc_tm().tm_year + 1900;
|
||||
}
|
||||
|
||||
int Date::utc_hours() const
|
||||
{
|
||||
return to_utc_tm().tm_hour;
|
||||
}
|
||||
|
||||
int Date::utc_minutes() const
|
||||
{
|
||||
return to_utc_tm().tm_min;
|
||||
}
|
||||
|
||||
int Date::utc_month() const
|
||||
{
|
||||
return to_utc_tm().tm_mon;
|
||||
}
|
||||
|
||||
int Date::utc_seconds() const
|
||||
{
|
||||
return to_utc_tm().tm_sec;
|
||||
}
|
||||
|
||||
String Date::gmt_date_string() const
|
||||
{
|
||||
// Mon, 18 Dec 1995 17:28:35 GMT
|
||||
// FIXME: Note that we're totally cheating with the timezone part here..
|
||||
return datetime().to_string("%a, %e %b %Y %T GMT");
|
||||
}
|
||||
|
||||
String Date::iso_date_string() const
|
||||
{
|
||||
int year = year_from_time(m_date_value);
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Math.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
||||
namespace JS {
|
||||
|
@ -17,78 +15,19 @@ class Date final : public Object {
|
|||
JS_OBJECT(Date, Object);
|
||||
|
||||
public:
|
||||
static constexpr double time_clip = 8.64e15;
|
||||
|
||||
static Date* create(GlobalObject&, Core::DateTime, i16 milliseconds, bool is_invalid);
|
||||
static Date* create(GlobalObject&, double date_value);
|
||||
static Date* now(GlobalObject&);
|
||||
|
||||
Date(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& prototype);
|
||||
Date(double date_value, Object& prototype);
|
||||
virtual ~Date() override;
|
||||
|
||||
double date_value() const { return m_date_value; }
|
||||
void set_date_value(double value) { m_date_value = value; }
|
||||
|
||||
Core::DateTime& datetime() { return m_datetime; }
|
||||
const Core::DateTime& datetime() const { return m_datetime; }
|
||||
|
||||
int date() const { return datetime().day(); }
|
||||
int day() const { return datetime().weekday(); }
|
||||
int hours() const { return datetime().hour(); }
|
||||
i16 milliseconds() const { return m_milliseconds; }
|
||||
int minutes() const { return datetime().minute(); }
|
||||
int month() const { return datetime().month() - 1; }
|
||||
int seconds() const { return datetime().second(); }
|
||||
double time() const { return datetime().timestamp() * 1000.0 + milliseconds(); }
|
||||
int year() const { return datetime().year(); }
|
||||
|
||||
bool is_invalid() const { return m_is_invalid; }
|
||||
void set_is_invalid(bool value) { m_is_invalid = value; }
|
||||
|
||||
int utc_date() const;
|
||||
int utc_day() const;
|
||||
int utc_full_year() const;
|
||||
int utc_hours() const;
|
||||
int utc_milliseconds() const { return milliseconds(); }
|
||||
int utc_minutes() const;
|
||||
int utc_month() const;
|
||||
int utc_seconds() const;
|
||||
|
||||
void set_milliseconds(i16 milliseconds)
|
||||
{
|
||||
m_milliseconds = milliseconds;
|
||||
}
|
||||
|
||||
// FIXME: Support %04Y in Core::DateTime::to_string()
|
||||
String date_string() const { return String::formatted(m_datetime.to_string("%a %b %d {:04}"), m_datetime.year()); }
|
||||
// FIXME: Deal with timezones once SerenityOS has a working tzset(3)
|
||||
String time_string() const { return m_datetime.to_string("%T GMT+0000 (UTC)"); }
|
||||
String string() const
|
||||
{
|
||||
if (is_invalid())
|
||||
return "Invalid Date";
|
||||
|
||||
return String::formatted("{} {}", date_string(), time_string());
|
||||
}
|
||||
|
||||
String gmt_date_string() const;
|
||||
String iso_date_string() const;
|
||||
|
||||
// FIXME: One day, implement real locale support. Until then, everyone gets what the Clock Applet 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"); }
|
||||
|
||||
private:
|
||||
tm to_utc_tm() const;
|
||||
|
||||
double m_date_value { 0 }; // [[DateValue]]
|
||||
|
||||
Core::DateTime m_datetime;
|
||||
i16 m_milliseconds;
|
||||
|
||||
bool m_is_invalid { false };
|
||||
};
|
||||
|
||||
u16 day_within_year(double);
|
||||
|
|
Loading…
Add table
Reference in a new issue