]> git.tdb.fi Git - libs/core.git/blob - source/time/timestamp.h
Add move semantics to Variant
[libs/core.git] / source / time / timestamp.h
1 #ifndef MSP_TIME_TIMESTAMP_H_
2 #define MSP_TIME_TIMESTAMP_H_
3
4 #include <msp/core/mspcore_api.h>
5 #include "timedelta.h"
6 #include "rawtime.h"
7
8 namespace Msp {
9 namespace Time {
10
11 /**
12 Represents a moment in time.  The main source of TimeStamps is the now()
13 function.
14
15 For representing user-specified times, use the DateTime class.
16 */
17 class MSPCORE_API TimeStamp
18 {
19 private:
20         RawTime usec = 0;
21
22 public:
23         /** Construct a TimeStamp that represents an arbitarily distant point in the
24         past.  It's guaranteed to be less than any valid timestamp. */
25         TimeStamp() = default;
26
27         /** Constructs a TimeStamp from a plain number.  The purpose of this is to allow
28         serialization together with the raw() function. */
29         explicit TimeStamp(RawTime u): usec(u) { }
30
31         /** Returns the raw number stored inside the TimeStamp.  This value should be
32         considered opaque and only be used for serialization. */
33         RawTime raw() const { return usec; }
34
35         time_t to_unixtime() const { return usec/1000000LL; }
36
37         TimeStamp operator+(const TimeDelta &t) const { return TimeStamp(usec+t.raw()); }
38         TimeStamp &operator+=(const TimeDelta &t) { usec += t.raw(); return *this; }
39         TimeStamp operator-(const TimeDelta &t) const { return TimeStamp(usec-t.raw()); }
40         TimeStamp &operator-=(const TimeDelta &t) { usec -= t.raw(); return *this; }
41         TimeDelta operator-(const TimeStamp &t) const { return TimeDelta(usec-t.usec); }
42
43         bool operator>=(const TimeStamp &t) const { return usec>=t.usec; }
44         bool operator>(const TimeStamp &t) const { return usec>t.usec; }
45         bool operator<=(const TimeStamp &t) const { return usec<=t.usec; }
46         bool operator<(const TimeStamp &t) const { return usec<t.usec; }
47         bool operator==(const TimeStamp &t) const { return usec==t.usec; }
48         bool operator!=(const TimeStamp &t) const { return usec!=t.usec; }
49
50         explicit operator bool() const { return usec>0; }
51
52         static TimeStamp from_unixtime(time_t t) { return TimeStamp(t*1000000LL); }
53 };
54
55 } // namespace Time
56 } // namespace Msp
57
58 #endif