00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _BASE_TIME_
00026 #define _BASE_TIME_
00027
00028 #include <base/base>
00029 #include <base/Object>
00030
00031 namespace base {
00032
00033 class Serializer;
00034
00035
00036 class Time : public Object
00037 {
00038 public:
00039 Time();
00040 Time(const Time& t) { secs=t.secs; micros=t.micros; }
00041 Time(long secs, long micros) { this->secs=secs; this->micros=micros; }
00042 Time(Real secs);
00043 virtual ~Time();
00044
00045 virtual String className() const { return String("Time"); }
00046
00047 bool isPast() const { return less(*this,now()); }
00048
00049 Time& operator=(const Time& t) { secs=t.secs; micros=t.micros; return *this; }
00050 bool operator==(const Time& t) const { return equal(*this,t); }
00051 Time& operator-=(const Time& t);
00052 Time& operator+=(const Time& t);
00053 Time& operator*=(Real s);
00054 Time& operator/=(Real s) { return operator*=(1.0/s); }
00055
00056 Real seconds() const { return Real(secs) + Real(micros)/Real(microsPerSec); }
00057
00058 static const long microsPerSec = 1000000;
00059 static const Time& now();
00060 static bool isPast(const Time& t) { return less(t,now()); }
00061
00062 bool equals(const Time& t, Time eps = Time(0,10));
00063
00064 static bool less(const Time& t1, const Time& t2);
00065 static bool greater(const Time& t1, const Time& t2);
00066
00067 static void sleep(const Time& t);
00068 static void sleep(long secs, long micros);
00069 static void sleep(Real secs) { sleep(Time(secs)); }
00070
00071 static long timeResolution() { return resolution; }
00072
00073 void serialize(Serializer& s);
00074
00075 long secs, micros;
00076
00077 protected:
00078 static Time* time;
00079
00080 static long baseSecs;
00081 static long baseMicros;
00082 static Time lastNow;
00083 static long resolution;
00084
00085 static bool equal(const Time& t1, const Time& t2) { return (t1.secs==t2.secs) && (t1.micros==t2.micros); }
00086 void normalize();
00087
00088 };
00089
00090 std::ostream& operator<<(std::ostream& out, const Time& t);
00091
00092 inline Time operator-(const Time& t1, const Time& t2)
00093 { Time r(t1); r-=t2; return r; }
00094
00095 inline Time operator+(const Time& t1, const Time& t2)
00096 { Time r(t1); r+=t2; return r; }
00097
00098 inline Time operator*(const Time& t, Real s)
00099 { Time r(t); r*=s; return r; }
00100
00101 inline Time operator*(Real s, const Time& t)
00102 { Time r(t); r*=s; return r; }
00103
00104 inline Time operator/(const Time& t, Real s)
00105 { Time r(t); r/=s; return r; }
00106
00107 inline bool Time::equals(const Time& t, Time eps)
00108 {
00109 if (Time::equal(*this, t)) return true;
00110
00111 if (Time::greater(*this, t))
00112 return Time::less(*this - t, eps);
00113 return Time::less(t - *this, eps);
00114
00115 }
00116
00117 inline bool operator<(const Time& t1, const Time& t2)
00118 { return Time::less(t1,t2); }
00119
00120 inline bool operator>(const Time& t1, const Time& t2)
00121 { return Time::greater(t1,t2); }
00122
00123
00124
00125 }
00126
00127 #endif