summaryrefslogtreecommitdiff
path: root/AK/Time.h
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2021-03-07 23:34:13 -0700
committerAndreas Kling <kling@serenityos.org>2021-03-08 15:29:11 +0100
commit183b2e71ba8d85293db493cab27b8adb4af54981 (patch)
tree9bf440fa5c076572fed966cb04fa5840a2f5fb08 /AK/Time.h
parent42bc229500049ef816a45ce32a19d8987918e6a4 (diff)
downloadserenity-183b2e71ba8d85293db493cab27b8adb4af54981.zip
AK: Take advantage of constexpr in Time and add time conversion methods
By making the Time constructor constexpr we can optimize creating a Time instance from hardcoded values. Also add more functions to convert between Time and various time units.
Diffstat (limited to 'AK/Time.h')
-rw-r--r--AK/Time.h42
1 files changed, 39 insertions, 3 deletions
diff --git a/AK/Time.h b/AK/Time.h
index 20633f1070..e3ecb2724a 100644
--- a/AK/Time.h
+++ b/AK/Time.h
@@ -84,9 +84,35 @@ class Time {
public:
Time() = default;
Time(const Time&) = default;
+ Time& operator=(const Time&) = default;
- static Time from_seconds(i64 seconds) { return Time(seconds, 0); }
- static Time from_nanoseconds(i32 nanoseconds);
+ Time(Time&& other)
+ : m_seconds(exchange(other.m_seconds, 0))
+ , m_nanoseconds(exchange(other.m_nanoseconds, 0))
+ {
+ }
+ Time& operator=(Time&& other)
+ {
+ if (this != &other) {
+ m_seconds = exchange(other.m_seconds, 0);
+ m_nanoseconds = exchange(other.m_nanoseconds, 0);
+ }
+ return *this;
+ }
+
+ constexpr static Time from_seconds(i64 seconds) { return Time(seconds, 0); }
+ constexpr static Time from_nanoseconds(i64 nanoseconds)
+ {
+ return Time(nanoseconds / 1'000'000'000, nanoseconds % 1'000'000'000);
+ }
+ constexpr static Time from_microseconds(i64 microseconds)
+ {
+ return Time(microseconds / 1'000'000, (microseconds % 1'000'000) * 1'000);
+ }
+ constexpr static Time from_milliseconds(i64 milliseconds)
+ {
+ return Time(milliseconds / 1'000, (milliseconds % 1'000) * 1'000'000);
+ }
static Time from_timespec(const struct timespec&);
static Time from_timeval(const struct timeval&);
static Time min() { return Time(-0x8000'0000'0000'0000LL, 0); };
@@ -96,20 +122,30 @@ public:
// Truncates "2.8 seconds" to 2 seconds.
// Truncates "-2.8 seconds" to -2 seconds.
i64 to_truncated_seconds() const;
+ i64 to_truncated_milliseconds() const;
+ i64 to_truncated_microseconds() const;
+ i64 to_seconds() const;
+ i64 to_milliseconds() const;
+ i64 to_microseconds() const;
+ i64 to_nanoseconds() const;
timespec to_timespec() const;
timeval to_timeval() const;
+ bool is_zero() const { return !m_seconds && !m_nanoseconds; }
+
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);
Time operator-(const Time& other) const;
+ Time& operator-=(const Time& other);
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)
+ constexpr explicit Time(i64 seconds, u32 nanoseconds)
: m_seconds(seconds)
, m_nanoseconds(nanoseconds)
{