Skip to content

Commit

Permalink
AK: Make Time more usable
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWiederhake authored and awesomekling committed Mar 2, 2021
1 parent e510c41 commit 340813e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
14 changes: 14 additions & 0 deletions AK/Tests/TestTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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)
29 changes: 29 additions & 0 deletions AK/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Time Time::from_nanoseconds(i32 nanoseconds)
{
return Time::from_timespec({ 0, nanoseconds });
};
ALWAYS_INLINE static i32 sane_mod(i32& numerator, i32 denominator)
{
VERIFY(2 <= denominator && denominator <= 1'000'000'000);
Expand Down Expand Up @@ -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);
}

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
{
VERIFY(m_nanoseconds < 1'000'000'000);
Expand Down Expand Up @@ -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 };
}

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)
{
VERIFY(nanoseconds < 1'000'000'000);
Expand Down
11 changes: 10 additions & 1 deletion AK/Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,31 @@ inline int years_to_days_since_epoch(int year)
*/
class Time {
public:
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_timeval(const struct timeval&);
static Time min() { return Time(-0x8000'0000'0000'0000LL, 0); };
static Time zero() { return Time(0, 0); };
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;
timeval to_timeval() const;

bool operator==(const Time& other) const { return this->m_seconds == other.m_seconds && this->m_nanoseconds == other.m_nanoseconds; }
bool operator!=(const Time& other) const { return !(*this == other); }
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:
explicit Time(i64 seconds, u32 nanoseconds)
Expand Down

0 comments on commit 340813e

Please sign in to comment.