00001 #ifndef _sys_Time_h
00002 #define _sys_Time_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdint.h>
00026 #include <limits>
00027 #include <iosfwd>
00028
00029 namespace qpid {
00030 namespace sys {
00031
00032 class Duration;
00033
00066 class AbsTime {
00067 static int64_t max() { return std::numeric_limits<int64_t>::max(); }
00068 int64_t time_ns;
00069
00070 friend class Duration;
00071
00072 public:
00073 inline AbsTime() {}
00074 inline AbsTime(const AbsTime& time0, const Duration& duration);
00075
00076
00077
00078 static AbsTime now();
00079 inline static AbsTime FarFuture();
00080 bool operator==(const AbsTime& t) const { return t.time_ns == time_ns; }
00081 template <class S> void serialize(S& s) { s(time_ns); }
00082
00083 friend bool operator<(const AbsTime& a, const AbsTime& b);
00084 friend bool operator>(const AbsTime& a, const AbsTime& b);
00085 friend std::ostream& operator << (std::ostream&, const AbsTime&);
00086 };
00087
00088 std::ostream& operator << (std::ostream&, const AbsTime&);
00089
00096 class Duration {
00097 static int64_t max() { return std::numeric_limits<int64_t>::max(); }
00098 int64_t nanosecs;
00099
00100 friend class AbsTime;
00101
00102 public:
00103 inline Duration(int64_t time0);
00104 inline explicit Duration(const AbsTime& time0);
00105 inline explicit Duration(const AbsTime& start, const AbsTime& finish);
00106 inline operator int64_t() const;
00107 };
00108
00109 std::ostream& operator << (std::ostream&, const Duration&);
00110
00111 AbsTime::AbsTime(const AbsTime& t, const Duration& d) :
00112 time_ns(d == Duration::max() ? max() : t.time_ns+d.nanosecs)
00113 {}
00114
00115 AbsTime AbsTime::FarFuture() { AbsTime ff; ff.time_ns = max(); return ff;}
00116
00117 inline AbsTime now() { return AbsTime::now(); }
00118
00119 inline bool operator<(const AbsTime& a, const AbsTime& b) { return a.time_ns < b.time_ns; }
00120 inline bool operator>(const AbsTime& a, const AbsTime& b) { return a.time_ns > b.time_ns; }
00121
00122 Duration::Duration(int64_t time0) :
00123 nanosecs(time0)
00124 {}
00125
00126 Duration::Duration(const AbsTime& time0) :
00127 nanosecs(time0.time_ns)
00128 {}
00129
00130 Duration::Duration(const AbsTime& start, const AbsTime& finish) :
00131 nanosecs(finish.time_ns - start.time_ns)
00132 {}
00133
00134 Duration::operator int64_t() const
00135 { return nanosecs; }
00136
00138 const Duration TIME_SEC = 1000*1000*1000;
00140 const Duration TIME_MSEC = 1000*1000;
00142 const Duration TIME_USEC = 1000;
00144 const Duration TIME_NSEC = 1;
00145
00147 const Duration TIME_INFINITE = std::numeric_limits<int64_t>::max();
00148
00150 const AbsTime FAR_FUTURE = AbsTime::FarFuture();
00151
00152 }}
00153
00154 #endif