00001 #ifndef __TIME_ET_CLASS__
00002 #define __TIME_ET_CLASS__
00003
00004 #include <iostream>
00005 #ifndef PLATFORM_APERIOS
00006 #include <sys/time.h>
00007 #else
00008 #include <MCOOP.h>
00009
00010 struct timeval {
00011 unsigned int tv_sec;
00012 long tv_usec;
00013 };
00014
00015 struct timezone {
00016 int tz_minuteswest;
00017 int tz_dsttime;
00018 };
00019 #endif
00020
00021
00022
00023 class TimeET {
00024
00025 friend std::ostream& operator<<(std::ostream& o, const TimeET& t);
00026 public:
00027
00028
00029 TimeET() : tv() {
00030 Set();
00031 }
00032 TimeET(long ms) : tv() {
00033 Set(ms);
00034 }
00035 TimeET(long sec, long usec) : tv() {
00036 Set(sec,usec);
00037 }
00038
00039 TimeET(double t) :tv() {
00040 Set(t);
00041 }
00042
00043
00044
00045 inline TimeET Age() const { return TimeET()-(*this); }
00046
00047
00048 inline double Value() const { return (double)tv.tv_sec+(double)tv.tv_usec/(double)us_per_sec; }
00049
00050
00051
00052
00053 inline void Set(long ms) {
00054 Set(0,ms*us_per_ms);
00055 }
00056 inline void Set(long sec, long usec) {
00057 tv.tv_sec=sec+usec/us_per_sec;
00058 tv.tv_usec=usec%us_per_sec;;
00059 }
00060 inline void Set(double t) {
00061 tv.tv_sec=(long)t;
00062 tv.tv_usec=(long)((t-tv.tv_sec)*us_per_sec);
00063 }
00064
00065
00066 inline void Set() {
00067 #ifndef PLATFORM_APERIOS
00068 gettimeofday(&tv,&tz);
00069 #else
00070 static struct SystemTime t;
00071 GetSystemTime(&t);
00072 Set(t.seconds,t.useconds);
00073 #endif
00074 }
00075
00076
00077
00078
00079 inline bool operator<(long ms) {
00080 unsigned int sec = ms/ms_per_sec;
00081 return tv.tv_sec<sec || tv.tv_sec==sec && tv.tv_usec<static_cast<long>((ms-sec*ms_per_sec)*us_per_ms);
00082 }
00083 inline bool operator<(double t) {
00084 return Value()<t;
00085 }
00086 inline bool operator<(const TimeET& t) {
00087 return tv.tv_sec<t.tv.tv_sec || tv.tv_sec==t.tv.tv_sec && tv.tv_usec<t.tv.tv_usec;
00088 }
00089
00090
00091
00092
00093 inline TimeET operator+(const TimeET& t) const {
00094 long usec = tv.tv_usec+t.tv.tv_usec;
00095 long sec = tv.tv_sec+t.tv.tv_sec+usec/us_per_sec;
00096 usec%=us_per_sec;
00097 return TimeET(sec,usec);
00098 }
00099 inline TimeET operator+=(const TimeET& t) {
00100 tv.tv_usec+=t.tv.tv_usec;
00101 tv.tv_sec+=t.tv.tv_sec+tv.tv_usec/us_per_sec;
00102 tv.tv_usec%=us_per_sec;
00103 return *this;
00104 }
00105 inline TimeET operator-(const TimeET& t) const {
00106 long usec = tv.tv_usec-t.tv.tv_usec;
00107 long sec = tv.tv_sec-t.tv.tv_sec+usec/us_per_sec;
00108 usec%=us_per_sec;
00109 if(usec<0) {
00110 usec+=us_per_sec;
00111 sec--;
00112 }
00113 return TimeET(sec,usec);
00114 }
00115 inline TimeET operator-=(const TimeET& t) {
00116 tv.tv_usec-=t.tv.tv_usec;
00117 tv.tv_sec=tv.tv_sec-t.tv.tv_sec+tv.tv_usec/us_per_sec;
00118 tv.tv_usec%=us_per_sec;
00119 if(tv.tv_usec<0) {
00120 tv.tv_usec+=us_per_sec;
00121 tv.tv_sec--;
00122 }
00123 return *this;
00124 }
00125
00126
00127 static const long us_per_sec=1000000;
00128 static const long ms_per_sec=1000;
00129 static const long us_per_ms=1000;
00130 protected:
00131 timeval tv;
00132 static struct timezone tz;
00133 };
00134
00135
00136
00137 inline TimeET operator+(long t1, const TimeET& t2) { return TimeET(t1)+t2; }
00138 inline TimeET operator-(double t1, const TimeET& t2) { return TimeET(t1)-t2; }
00139
00140
00141
00142 inline std::ostream& operator<<(std::ostream& o, const TimeET& t) {
00143 return (o << t.tv.tv_sec << '~' << t.tv.tv_usec);
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 #endif