diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-06-06 16:27:38 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-06 19:14:11 +0100 |
commit | 46214f0657c35f9af266cd38985fe787e1f429d0 (patch) | |
tree | 744af35a7d9aff2bc5f7ba145f668d949eaa55d0 /Userland/Libraries/LibJS | |
parent | 17afe015a5496643e441f1f0f9abc5cf6a4bcd25 (diff) | |
download | serenity-46214f0657c35f9af266cd38985fe787e1f429d0.zip |
LibJS: Store Date milliseconds as signed to support negative offsets
We need to support the range of -999 to 999.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Date.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Date.h | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/DateConstructor.cpp | 4 |
3 files changed, 10 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Date.cpp b/Userland/Libraries/LibJS/Runtime/Date.cpp index 39a07cdcc6..b56b8b7c8d 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.cpp +++ b/Userland/Libraries/LibJS/Runtime/Date.cpp @@ -14,7 +14,7 @@ namespace JS { -Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, u16 milliseconds, bool is_invalid) +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()); } @@ -24,11 +24,11 @@ Date* Date::now(GlobalObject& global_object) struct timeval tv; gettimeofday(&tv, nullptr); auto datetime = Core::DateTime::now(); - auto milliseconds = static_cast<u16>(tv.tv_usec / 1000); + auto milliseconds = static_cast<i16>(tv.tv_usec / 1000); return create(global_object, datetime, milliseconds); } -Date::Date(Core::DateTime datetime, u16 milliseconds, bool is_invalid, Object& prototype) +Date::Date(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& prototype) : Object(prototype) , m_datetime(datetime) , m_milliseconds(milliseconds) diff --git a/Userland/Libraries/LibJS/Runtime/Date.h b/Userland/Libraries/LibJS/Runtime/Date.h index fc75f2d280..00ca3be055 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.h +++ b/Userland/Libraries/LibJS/Runtime/Date.h @@ -15,10 +15,10 @@ class Date final : public Object { JS_OBJECT(Date, Object); public: - static Date* create(GlobalObject&, Core::DateTime, u16 milliseconds, bool is_invalid = false); + static Date* create(GlobalObject&, Core::DateTime, i16 milliseconds, bool is_invalid = false); static Date* now(GlobalObject&); - Date(Core::DateTime datetime, u16 milliseconds, bool is_invalid, Object& prototype); + Date(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& prototype); virtual ~Date() override; Core::DateTime& datetime() { return m_datetime; } @@ -27,7 +27,7 @@ public: int date() const { return datetime().day(); } int day() const { return datetime().weekday(); } int hours() const { return datetime().hour(); } - u16 milliseconds() const { return m_milliseconds; } + 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(); } @@ -46,7 +46,7 @@ public: int utc_month() const; int utc_seconds() const; - void set_milliseconds(u16 milliseconds) + void set_milliseconds(i16 milliseconds) { m_milliseconds = milliseconds; } @@ -79,7 +79,7 @@ private: tm to_utc_tm() const; Core::DateTime m_datetime; - u16 m_milliseconds; + i16 m_milliseconds; bool m_is_invalid { false }; }; diff --git a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp index 33c2bf5259..20349a0fa0 100644 --- a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -152,7 +152,7 @@ Value DateConstructor::construct(Function&) auto create_invalid_date = [this]() { auto datetime = Core::DateTime::create(1970, 1, 1, 0, 0, 0); - auto milliseconds = static_cast<u16>(0); + auto milliseconds = static_cast<i16>(0); return Date::create(global_object(), datetime, milliseconds, true); }; @@ -173,7 +173,7 @@ Value DateConstructor::construct(Function&) // A timestamp since the epoch, in UTC. double value_as_double = value.as_double(); auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(value_as_double / 1000)); - auto milliseconds = static_cast<u16>(fmod(value_as_double, 1000)); + auto milliseconds = static_cast<i16>(fmod(value_as_double, 1000)); return Date::create(global_object(), datetime, milliseconds); } |