]> git.tdb.fi Git - libs/core.git/blob - source/time/timestamp.h
Make Time::sleep void
[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         /**
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         */
26         TimeStamp(): usec(0) { }
27
28         /**
29         Constructs a TimeStamp from a plain number.  The purpose of this is to allow
30         serialization together with the raw() function.
31         */
32         explicit TimeStamp(RawTime u): usec(u) { }
33
34         /**
35         Returns the raw number stored inside the TimeStamp.  This value should be
36         considered opaque and only be used for serialization.
37         */
38         RawTime raw() const { return usec; }
39
40         time_t to_unixtime() const { return usec/1000000LL; }
41
42         TimeStamp operator+(const TimeDelta &t) const  { return TimeStamp(usec+t.raw()); }
43         TimeStamp &operator+=(const TimeDelta &t)      { usec+=t.raw(); return *this; }
44         TimeStamp operator-(const TimeDelta &t) const  { return TimeStamp(usec-t.raw()); }
45         TimeStamp &operator-=(const TimeDelta &t)      { usec-=t.raw(); return *this; }
46         TimeDelta operator-(const TimeStamp &t) const  { return TimeDelta(usec-t.usec); }
47
48         bool      operator>=(const TimeStamp &t) const { return usec>=t.usec; }
49         bool      operator>(const TimeStamp &t) const  { return usec>t.usec; }
50         bool      operator<=(const TimeStamp &t) const { return usec<=t.usec; }
51         bool      operator<(const TimeStamp &t) const  { return usec<t.usec; }
52         bool      operator==(const TimeStamp &t) const { return usec==t.usec; }
53         bool      operator!=(const TimeStamp &t) const { return usec!=t.usec; }
54
55         operator const void *() const                  { return usec>0 ? this : 0; }
56
57 #ifndef WIN32
58         operator timeval() const { return rawtime_to_timeval(usec); }
59         operator timespec() const { return rawtime_to_timespec(usec); }
60 #endif
61
62         static TimeStamp from_unixtime(time_t t) { return TimeStamp(t*1000000LL); }
63 };
64
65 } // namespace Time
66 } // namespace Msp
67
68 #endif