1 #ifndef MSP_TIME_TIMESTAMP_H_
2 #define MSP_TIME_TIMESTAMP_H_
4 #include <msp/core/mspcore_api.h>
12 Represents a moment in time. The main source of TimeStamps is the now()
15 For representing user-specified times, use the DateTime class.
17 class MSPCORE_API TimeStamp
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;
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) { }
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; }
35 time_t to_unixtime() const { return usec/1000000LL; }
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); }
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; }
50 explicit operator bool() const { return usec>0; }
52 static TimeStamp from_unixtime(time_t t) { return TimeStamp(t*1000000LL); }