summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2021-02-27 23:54:10 +0100
committerAndreas Kling <kling@serenityos.org>2021-03-02 08:36:08 +0100
commit340813e0873336fb88f909d6f29f7fcf2147efcb (patch)
tree3d2d77f5518e6938dfb1d7149051a056dbfee405
parente510c41fd2b26bacc197b200d309c09a4a6ecf2c (diff)
downloadserenity-340813e0873336fb88f909d6f29f7fcf2147efcb.zip
AK: Make Time more usable
-rw-r--r--AK/Tests/TestTime.cpp14
-rw-r--r--AK/Time.cpp29
-rw-r--r--AK/Time.h11
3 files changed, 53 insertions, 1 deletions
diff --git a/AK/Tests/TestTime.cpp b/AK/Tests/TestTime.cpp
index e63fab35cc..0e79338b6e 100644
--- a/AK/Tests/TestTime.cpp
+++ b/AK/Tests/TestTime.cpp
@@ -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)
diff --git a/AK/Time.cpp b/AK/Time.cpp
index a00e59fa7f..1f4eb9ec93 100644
--- a/AK/Time.cpp
+++ b/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;
}
+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);
@@ -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);
@@ -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);
diff --git a/AK/Time.h b/AK/Time.h
index 0a5e86e1b9..7f86a2afaf 100644
--- a/AK/Time.h
+++ b/AK/Time.h
@@ -82,15 +82,20 @@ 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;
@@ -98,6 +103,10 @@ public:
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)