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