mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 10:27:34 +00:00
LibJS: Store Date milliseconds as signed to support negative offsets
We need to support the range of -999 to 999.
This commit is contained in:
parent
17afe015a5
commit
46214f0657
3 changed files with 10 additions and 10 deletions
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
namespace JS {
|
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());
|
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;
|
struct timeval tv;
|
||||||
gettimeofday(&tv, nullptr);
|
gettimeofday(&tv, nullptr);
|
||||||
auto datetime = Core::DateTime::now();
|
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);
|
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)
|
: Object(prototype)
|
||||||
, m_datetime(datetime)
|
, m_datetime(datetime)
|
||||||
, m_milliseconds(milliseconds)
|
, m_milliseconds(milliseconds)
|
||||||
|
|
|
@ -15,10 +15,10 @@ class Date final : public Object {
|
||||||
JS_OBJECT(Date, Object);
|
JS_OBJECT(Date, Object);
|
||||||
|
|
||||||
public:
|
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&);
|
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;
|
virtual ~Date() override;
|
||||||
|
|
||||||
Core::DateTime& datetime() { return m_datetime; }
|
Core::DateTime& datetime() { return m_datetime; }
|
||||||
|
@ -27,7 +27,7 @@ public:
|
||||||
int date() const { return datetime().day(); }
|
int date() const { return datetime().day(); }
|
||||||
int day() const { return datetime().weekday(); }
|
int day() const { return datetime().weekday(); }
|
||||||
int hours() const { return datetime().hour(); }
|
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 minutes() const { return datetime().minute(); }
|
||||||
int month() const { return datetime().month() - 1; }
|
int month() const { return datetime().month() - 1; }
|
||||||
int seconds() const { return datetime().second(); }
|
int seconds() const { return datetime().second(); }
|
||||||
|
@ -46,7 +46,7 @@ public:
|
||||||
int utc_month() const;
|
int utc_month() const;
|
||||||
int utc_seconds() const;
|
int utc_seconds() const;
|
||||||
|
|
||||||
void set_milliseconds(u16 milliseconds)
|
void set_milliseconds(i16 milliseconds)
|
||||||
{
|
{
|
||||||
m_milliseconds = milliseconds;
|
m_milliseconds = milliseconds;
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ private:
|
||||||
tm to_utc_tm() const;
|
tm to_utc_tm() const;
|
||||||
|
|
||||||
Core::DateTime m_datetime;
|
Core::DateTime m_datetime;
|
||||||
u16 m_milliseconds;
|
i16 m_milliseconds;
|
||||||
bool m_is_invalid { false };
|
bool m_is_invalid { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -152,7 +152,7 @@ Value DateConstructor::construct(Function&)
|
||||||
|
|
||||||
auto create_invalid_date = [this]() {
|
auto create_invalid_date = [this]() {
|
||||||
auto datetime = Core::DateTime::create(1970, 1, 1, 0, 0, 0);
|
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);
|
return Date::create(global_object(), datetime, milliseconds, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ Value DateConstructor::construct(Function&)
|
||||||
// A timestamp since the epoch, in UTC.
|
// A timestamp since the epoch, in UTC.
|
||||||
double value_as_double = value.as_double();
|
double value_as_double = value.as_double();
|
||||||
auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(value_as_double / 1000));
|
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);
|
return Date::create(global_object(), datetime, milliseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue