mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:07:34 +00:00
AK: Make Time more usable
This commit is contained in:
parent
e510c41fd2
commit
340813e087
3 changed files with 53 additions and 1 deletions
|
@ -202,4 +202,18 @@ TEST_CASE(subtraction)
|
||||||
EXPECT_SUBTRACTION(-0x7fff'ffff'ffff'ffff, 999'999'995, 1, 999'999'996, (i64)-0x8000'0000'0000'0000, 0);
|
EXPECT_SUBTRACTION(-0x7fff'ffff'ffff'ffff, 999'999'995, 1, 999'999'996, (i64)-0x8000'0000'0000'0000, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(truncation)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(TIME(2, 800'000'000).to_truncated_seconds(), 2);
|
||||||
|
EXPECT_EQ(TIME(-2, -800'000'000).to_truncated_seconds(), -2);
|
||||||
|
|
||||||
|
EXPECT_EQ(TIME(0, 0).to_truncated_seconds(), 0);
|
||||||
|
EXPECT_EQ(TIME(1, 999'999'999).to_truncated_seconds(), 1);
|
||||||
|
EXPECT_EQ(TIME(1, 1'000'000'000).to_truncated_seconds(), 2);
|
||||||
|
EXPECT_EQ(TIME(-2, 0).to_truncated_seconds(), -2);
|
||||||
|
|
||||||
|
EXPECT_EQ(Time::min().to_truncated_seconds(), (i64)-0x8000'0000'0000'0000);
|
||||||
|
EXPECT_EQ(Time::max().to_truncated_seconds(), 0x7fff'ffff'ffff'ffff);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_MAIN(Time)
|
TEST_MAIN(Time)
|
||||||
|
|
29
AK/Time.cpp
29
AK/Time.cpp
|
@ -72,6 +72,10 @@ unsigned day_of_week(int year, unsigned month, int day)
|
||||||
return (year + year / 4 - year / 100 + year / 400 + seek_table[month - 1] + day) % 7;
|
return (year + year / 4 - year / 100 + year / 400 + seek_table[month - 1] + day) % 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Time Time::from_nanoseconds(i32 nanoseconds)
|
||||||
|
{
|
||||||
|
return Time::from_timespec({ 0, nanoseconds });
|
||||||
|
};
|
||||||
ALWAYS_INLINE static i32 sane_mod(i32& numerator, i32 denominator)
|
ALWAYS_INLINE static i32 sane_mod(i32& numerator, i32 denominator)
|
||||||
{
|
{
|
||||||
VERIFY(2 <= denominator && denominator <= 1'000'000'000);
|
VERIFY(2 <= denominator && denominator <= 1'000'000'000);
|
||||||
|
@ -102,6 +106,14 @@ Time Time::from_timeval(const struct timeval& tv)
|
||||||
return Time::from_half_sanitized(tv.tv_sec, extra_secs, usecs * 1'000);
|
return Time::from_half_sanitized(tv.tv_sec, extra_secs, usecs * 1'000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i64 Time::to_truncated_seconds() const
|
||||||
|
{
|
||||||
|
VERIFY(m_nanoseconds < 1'000'000'000);
|
||||||
|
if (m_seconds < 0 && m_nanoseconds)
|
||||||
|
return m_seconds + 1;
|
||||||
|
else
|
||||||
|
return m_seconds;
|
||||||
|
}
|
||||||
timespec Time::to_timespec() const
|
timespec Time::to_timespec() const
|
||||||
{
|
{
|
||||||
VERIFY(m_nanoseconds < 1'000'000'000);
|
VERIFY(m_nanoseconds < 1'000'000'000);
|
||||||
|
@ -172,6 +184,23 @@ Time Time::operator-(const Time& other) const
|
||||||
return Time { (m_seconds + 0x4000'0000'0000'0000) + 0x4000'0000'0000'0000, m_nanoseconds };
|
return Time { (m_seconds + 0x4000'0000'0000'0000) + 0x4000'0000'0000'0000, m_nanoseconds };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Time::operator<(const Time& other) const
|
||||||
|
{
|
||||||
|
return m_seconds < other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds < other.m_nanoseconds);
|
||||||
|
}
|
||||||
|
bool Time::operator<=(const Time& other) const
|
||||||
|
{
|
||||||
|
return m_seconds < other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds <= other.m_nanoseconds);
|
||||||
|
}
|
||||||
|
bool Time::operator>(const Time& other) const
|
||||||
|
{
|
||||||
|
return m_seconds > other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds > other.m_nanoseconds);
|
||||||
|
}
|
||||||
|
bool Time::operator>=(const Time& other) const
|
||||||
|
{
|
||||||
|
return m_seconds > other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds >= other.m_nanoseconds);
|
||||||
|
}
|
||||||
|
|
||||||
Time Time::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds)
|
Time Time::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds)
|
||||||
{
|
{
|
||||||
VERIFY(nanoseconds < 1'000'000'000);
|
VERIFY(nanoseconds < 1'000'000'000);
|
||||||
|
|
11
AK/Time.h
11
AK/Time.h
|
@ -82,15 +82,20 @@ inline int years_to_days_since_epoch(int year)
|
||||||
*/
|
*/
|
||||||
class Time {
|
class Time {
|
||||||
public:
|
public:
|
||||||
|
Time() = default;
|
||||||
Time(const Time&) = default;
|
Time(const Time&) = default;
|
||||||
|
|
||||||
static Time from_seconds(i64 seconds) { return Time(seconds, 0); };
|
static Time from_seconds(i64 seconds) { return Time(seconds, 0); }
|
||||||
|
static Time from_nanoseconds(i32 nanoseconds);
|
||||||
static Time from_timespec(const struct timespec&);
|
static Time from_timespec(const struct timespec&);
|
||||||
static Time from_timeval(const struct timeval&);
|
static Time from_timeval(const struct timeval&);
|
||||||
static Time min() { return Time(-0x8000'0000'0000'0000LL, 0); };
|
static Time min() { return Time(-0x8000'0000'0000'0000LL, 0); };
|
||||||
static Time zero() { return Time(0, 0); };
|
static Time zero() { return Time(0, 0); };
|
||||||
static Time max() { return Time(0x7fff'ffff'ffff'ffffLL, 999'999'999); };
|
static Time max() { return Time(0x7fff'ffff'ffff'ffffLL, 999'999'999); };
|
||||||
|
|
||||||
|
// Truncates "2.8 seconds" to 2 seconds.
|
||||||
|
// Truncates "-2.8 seconds" to -2 seconds.
|
||||||
|
i64 to_truncated_seconds() const;
|
||||||
timespec to_timespec() const;
|
timespec to_timespec() const;
|
||||||
timeval to_timeval() const;
|
timeval to_timeval() const;
|
||||||
|
|
||||||
|
@ -98,6 +103,10 @@ public:
|
||||||
bool operator!=(const Time& other) const { return !(*this == other); }
|
bool operator!=(const Time& other) const { return !(*this == other); }
|
||||||
Time operator+(const Time& other) const;
|
Time operator+(const Time& other) const;
|
||||||
Time operator-(const Time& other) const;
|
Time operator-(const Time& other) const;
|
||||||
|
bool operator<(const Time& other) const;
|
||||||
|
bool operator<=(const Time& other) const;
|
||||||
|
bool operator>(const Time& other) const;
|
||||||
|
bool operator>=(const Time& other) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit Time(i64 seconds, u32 nanoseconds)
|
explicit Time(i64 seconds, u32 nanoseconds)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue