]> git.saurik.com Git - apple/system_cmds.git/blob - CPPUtil/UtilAbsTime.hpp
8fe56b099df831ee4438b5cb4491ad6a8f3ed514
[apple/system_cmds.git] / CPPUtil / UtilAbsTime.hpp
1 //
2 // UtilAbsTime.hpp
3 // CPPUtil
4 //
5 // Created by James McIlree on 4/14/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
7 //
8
9 #ifndef __CPPUtil__UtilAbsTime__
10 #define __CPPUtil__UtilAbsTime__
11
12 class NanoTime;
13
14 class AbsTime {
15 protected:
16 uint64_t _time;
17
18 public:
19 // Minimum and Maximum possible values
20 static const AbsTime BEGINNING_OF_TIME;
21 static const AbsTime END_OF_TIME;
22
23 static AbsTime now();
24
25 AbsTime() : _time(0ULL) {}
26 explicit AbsTime(uint64_t t) : _time(t) {}
27
28 bool operator==(const AbsTime& rhs) const { return this->_time == rhs._time; }
29 bool operator!=(const AbsTime &rhs) const { return !(*this == rhs); }
30
31 bool operator<(const AbsTime& rhs) const { return this->_time < rhs._time; }
32 bool operator<=(const AbsTime& rhs) const { return this->_time <= rhs._time; }
33 bool operator>(const AbsTime& rhs) const { return this->_time > rhs._time; }
34 bool operator>=(const AbsTime& rhs) const { return this->_time >= rhs._time; }
35
36 // We do not want to be able to mutate AbsTime(s)
37 // without type enforcement, but it is useful to be able
38 // to say "if (time == 0) {}", so we have value based
39 // operators for comparison
40 bool operator==(uint64_t value) const { return this->_time == value; }
41 bool operator!=(uint64_t value) const { return !(*this == value); }
42
43 bool operator<(uint64_t value) const { return this->_time < value; }
44 bool operator<=(uint64_t value) const { return this->_time <= value; }
45 bool operator>(uint64_t value) const { return this->_time > value; }
46 bool operator>=(uint64_t value) const { return this->_time >= value; }
47
48 AbsTime operator+(const AbsTime& rhs) const { return AbsTime(_time + rhs._time); }
49 AbsTime operator-(const AbsTime& rhs) const { return AbsTime(_time - rhs._time); }
50 AbsTime operator*(const AbsTime& rhs) const { return AbsTime(_time * rhs._time); }
51 AbsTime operator/(const AbsTime& rhs) const { return AbsTime(_time / rhs._time); }
52
53 AbsTime& operator+=(const AbsTime& rhs) { _time += rhs._time; return *this; }
54
55 NanoTime nano_time() const; // NOTE! Uses system mach_timebase_info, potentially expensive conversion costs.
56 NanoTime nano_time(mach_timebase_info_data_t timebase_info) const;
57
58 uint64_t value() const { return _time; }
59 double double_value() const { return (double)_time; }
60 };
61
62 #endif /* defined(__CPPUtil__UtilAbsTime__) */