TimeET.h
Go to the documentation of this file.00001 #ifndef __TIME_ET_CLASS__
00002 #define __TIME_ET_CLASS__
00003
00004 #ifdef PLATFORM_APERIOS
00005 #include <MCOOP.h>
00006 #endif
00007 #include <iostream>
00008 #include <sys/time.h>
00009
00010
00011
00012 class TimeET {
00013
00014 friend std::ostream& operator<<(std::ostream& o, const TimeET& t);
00015
00016 public:
00017
00018
00019 TimeET() : tv() {
00020 Set();
00021 }
00022 TimeET(long ms) : tv() {
00023 Set(ms);
00024 }
00025 TimeET(time_t sec, long usec) : tv() {
00026 Set(sec,usec);
00027 }
00028 TimeET(const timeval& tval) : tv(tval) {}
00029 TimeET(const timespec& tspec) : tv() {
00030 Set(tspec.tv_sec,tspec.tv_nsec/ns_per_us);
00031 }
00032
00033 TimeET(double t) :tv() {
00034 Set(t);
00035 }
00036
00037
00038
00039 inline TimeET Age() const { return TimeET()-(*this); }
00040
00041
00042 inline double Value() const { return (double)tv.tv_sec+(double)tv.tv_usec/(double)us_per_sec; }
00043
00044 inline operator timeval &() { return tv; }
00045
00046 inline operator const timeval &() const { return tv; }
00047
00048 inline operator timespec() {
00049 timespec tspec={tv.tv_sec,tv.tv_usec*ns_per_us};
00050 return tspec;
00051 }
00052
00053 inline time_t getSeconds() const { return tv.tv_sec; }
00054
00055 inline long getMilliseconds(long round=us_per_ms/2) const { return tv.tv_sec*ms_per_sec + (tv.tv_usec+round)/us_per_ms; }
00056
00057 inline long getMicroPortion() const { return tv.tv_usec; }
00058
00059
00060
00061
00062 inline void Set(long ms) {
00063 Set(0,ms*us_per_ms);
00064 }
00065
00066 inline void Set(time_t sec, long usec) {
00067 tv.tv_sec=sec+usec/us_per_sec;
00068 tv.tv_usec=usec%us_per_sec;;
00069 }
00070
00071 inline void Set(double t) {
00072 tv.tv_sec=(long)t;
00073 tv.tv_usec=(long)((t-tv.tv_sec)*us_per_sec);
00074 }
00075
00076
00077 inline void Set() {
00078 #ifdef PLATFORM_APERIOS
00079 static struct SystemTime t;
00080 GetSystemTime(&t);
00081 Set(t.seconds,t.useconds);
00082 #else
00083 gettimeofday(&tv,&tz);
00084 #endif
00085 }
00086
00087
00088
00089
00090 inline bool operator<(long ms) const {
00091 time_t sec = ms/ms_per_sec;
00092 return tv.tv_sec<sec || (tv.tv_sec==sec && tv.tv_usec<static_cast<long>((ms-sec*ms_per_sec)*us_per_ms));
00093 }
00094 inline bool operator<(double t) const {
00095 return Value()<t;
00096 }
00097 inline bool operator<(const TimeET& t) const {
00098 return tv.tv_sec<t.tv.tv_sec || (tv.tv_sec==t.tv.tv_sec && tv.tv_usec<t.tv.tv_usec);
00099 }
00100
00101
00102
00103
00104 inline TimeET operator+(const TimeET& t) const {
00105 long usec = tv.tv_usec+t.tv.tv_usec;
00106 long sec = tv.tv_sec+t.tv.tv_sec+usec/us_per_sec;
00107 usec%=us_per_sec;
00108 return TimeET(sec,usec);
00109 }
00110 inline TimeET& operator+=(const TimeET& t) {
00111 tv.tv_usec+=t.tv.tv_usec;
00112 tv.tv_sec+=t.tv.tv_sec+tv.tv_usec/us_per_sec;
00113 tv.tv_usec%=us_per_sec;
00114 return *this;
00115 }
00116 inline TimeET operator-(const TimeET& t) const {
00117 long usec = tv.tv_usec-t.tv.tv_usec;
00118 long sec = tv.tv_sec-t.tv.tv_sec+usec/us_per_sec;
00119 usec%=us_per_sec;
00120 if(usec<0) {
00121 usec+=us_per_sec;
00122 sec--;
00123 }
00124 return TimeET(sec,usec);
00125 }
00126 inline TimeET& operator-=(const TimeET& t) {
00127 tv.tv_usec-=t.tv.tv_usec;
00128 tv.tv_sec=tv.tv_sec-t.tv.tv_sec+tv.tv_usec/us_per_sec;
00129 tv.tv_usec%=us_per_sec;
00130 if(tv.tv_usec<0) {
00131 tv.tv_usec+=us_per_sec;
00132 tv.tv_sec--;
00133 }
00134 return *this;
00135 }
00136 inline TimeET operator*(double x) const {
00137 if(x>1 || x<-1) {
00138 double usec = tv.tv_usec*x;
00139 long carry=static_cast<long>(usec/us_per_sec);
00140 long sec = static_cast<long>(tv.tv_sec*x)+carry;
00141 usec-=carry*us_per_sec;
00142 return TimeET(sec,static_cast<long>(usec));
00143 } else {
00144 double secv=tv.tv_sec*x;
00145 long sec=static_cast<long>(secv);
00146 double carry=secv-sec;
00147 long usec=static_cast<long>(tv.tv_usec*x+carry*us_per_sec);
00148 return TimeET(sec,usec);
00149 }
00150 }
00151 inline TimeET& operator*=(double x) {
00152 if(x>1 || x<-1) {
00153 double usec = tv.tv_usec*x;
00154 long carry=static_cast<long>(usec/us_per_sec);
00155 tv.tv_sec = static_cast<long>(tv.tv_sec*x)+carry;
00156 tv.tv_usec=static_cast<long>(usec-carry*us_per_sec);
00157 return *this;
00158 } else {
00159 double secv=tv.tv_sec*x;
00160 tv.tv_sec=static_cast<long>(secv);
00161 double carry=secv-tv.tv_sec;
00162 tv.tv_usec=static_cast<long>(tv.tv_usec*x+carry*us_per_sec);
00163 return *this;
00164 }
00165 }
00166 inline TimeET operator/(double x) const {
00167 if(x>1 || x<-1) {
00168 double secv=tv.tv_sec/x;
00169 long sec=static_cast<long>(secv);
00170 double carry=secv-sec;
00171 long usec=static_cast<long>(tv.tv_usec/x+carry*us_per_sec);
00172 return TimeET(sec,usec);
00173 } else {
00174 double usec = tv.tv_usec/x;
00175 long carry=static_cast<long>(usec/us_per_sec);
00176 long sec = static_cast<long>(tv.tv_sec/x)+carry;
00177 usec-=carry*us_per_sec;
00178 return TimeET(sec,static_cast<long>(usec));
00179 }
00180 }
00181 inline TimeET& operator/=(double x) {
00182 if(x>1 || x<-1) {
00183 double secv=tv.tv_sec/x;
00184 tv.tv_sec=static_cast<long>(secv);
00185 double carry=secv-tv.tv_sec;
00186 tv.tv_usec=static_cast<long>(tv.tv_usec/x+carry*us_per_sec);
00187 return *this;
00188 } else {
00189 double usec = tv.tv_usec/x;
00190 long carry=static_cast<long>(usec/us_per_sec);
00191 tv.tv_sec = static_cast<long>(tv.tv_sec/x)+carry;
00192 tv.tv_usec=static_cast<long>(usec-carry*us_per_sec);
00193 return *this;
00194 }
00195 }
00196
00197
00198 static const long us_per_sec=1000000;
00199 static const long ms_per_sec=1000;
00200 static const long us_per_ms=1000;
00201 static const long ns_per_us=1000;
00202
00203 protected:
00204 timeval tv;
00205 static struct timezone tz;
00206 };
00207
00208
00209
00210 inline TimeET operator+(long t1, const TimeET& t2) { return TimeET(t1)+t2; }
00211 inline TimeET operator-(long t1, const TimeET& t2) { return TimeET(t1)-t2; }
00212 inline TimeET operator+(double t1, const TimeET& t2) { return TimeET(t1)+t2; }
00213 inline TimeET operator-(double t1, const TimeET& t2) { return TimeET(t1)-t2; }
00214 inline TimeET operator*(double x, const TimeET& t) { return t*x; }
00215
00216
00217
00218 inline std::ostream& operator<<(std::ostream& o, const TimeET& t) {
00219 o << t.tv.tv_sec << '.';
00220 o.width(6);
00221 o.fill('0');
00222 o << t.tv.tv_usec;
00223 o.fill(' ');
00224 return o;
00225 }
00226
00227
00228
00229
00230
00231
00232 #endif