00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef LIBMUTH_TIMEVAL_H
00020 #define LIBMUTH_TIMEVAL_H
00021
00022 #include <assert.h>
00023 #include "timeval_decls.h"
00024
00025
00026
00027
00028
00029 inline Timeval::Timeval() {
00030 }
00031
00032 inline Timeval::Timeval(const timeval &o) {
00033 assert(o.tv_usec < 1000000);
00034 tv_sec = o.tv_sec;
00035 tv_usec = o.tv_usec;
00036 }
00037
00038 inline Timeval::Timeval(void*) {
00039 #ifndef __WIN32__
00040
00041 ::gettimeofday(this, NULL);
00042 #else
00043 struct _timeb tb;
00044 _ftime(&tb);
00045 tv_sec = tb.time;
00046 tv_usec = 1000*tb.millitm;
00047 #endif
00048 }
00049
00050 inline Timeval::Timeval(time_t sec) {
00051 tv_sec = sec;
00052 tv_usec = 0;
00053 }
00054
00055 inline Timeval::Timeval(time_t sec, suseconds_t usec) {
00056 assert(usec < 1000000);
00057 tv_sec = sec;
00058 tv_usec = usec;
00059 }
00060
00061 inline Timeval::Timeval(float sec) {
00062 tv_sec = (time_t)sec;
00063 sec -= (float) tv_sec;
00064 tv_usec = (suseconds_t)(sec * 1000000);
00065 }
00066
00067 inline Timeval::Timeval(double sec) {
00068 tv_sec = (time_t)sec;
00069 sec -= (double) tv_sec;
00070 tv_usec = (suseconds_t)(sec * 1000000);
00071 }
00072
00073
00074
00075
00076
00077 inline Timeval &Timeval::operator=(const timeval &o) {
00078 assert(o.tv_usec < 1000000);
00079 tv_sec = o.tv_sec;
00080 tv_usec = o.tv_usec;
00081 return *this;
00082 }
00083
00084 inline Timeval &Timeval::operator=(time_t sec) {
00085 tv_sec = sec;
00086 tv_usec = 0;
00087 return *this;
00088 }
00089
00090 inline Timeval &Timeval::operator=(float sec) {
00091 tv_sec = (time_t)sec;
00092 sec -= (float)tv_sec;
00093 tv_usec = (suseconds_t)(sec * 10000000);
00094 return *this;
00095 }
00096
00097 inline Timeval &Timeval::operator=(double sec) {
00098 tv_sec = (time_t)sec;
00099 sec -= (double)tv_sec;
00100 tv_usec = (suseconds_t)(sec * 10000000);
00101 return *this;
00102 }
00103
00104
00105
00106
00107 inline void Timeval::gettimeofday() {
00108 #ifndef __WIN32__
00109
00110 ::gettimeofday(this, NULL);
00111 #else
00112 struct _timeb tb;
00113 _ftime(&tb);
00114 tv_sec = tb.time;
00115 tv_usec = 1000*tb.millitm;
00116 #endif
00117 }
00118
00119
00120
00121
00122
00123 inline Timeval::operator time_t() const {
00124 return tv_sec;
00125 }
00126
00127 inline Timeval::operator float() const {
00128 return (float)tv_sec + 0.000001 * (float)tv_usec;
00129 }
00130
00131 inline Timeval::operator double() const {
00132 return (double)tv_sec + 0.000001 * (double)tv_usec;
00133 }
00134
00135 inline unsigned int Timeval::milliseconds() const {
00136 return (1000*(unsigned int)tv_sec)+(((unsigned int)tv_usec)/1000);
00137 }
00138
00139
00140
00141
00142
00143 inline bool Timeval::operator>(const timeval &o) const {
00144 assert(o.tv_usec < 1000000);
00145 return tv_sec > o.tv_sec || (tv_sec == o.tv_sec && tv_usec > o.tv_usec);
00146 }
00147
00148 inline bool Timeval::operator>=(const timeval &o) const {
00149 assert(o.tv_usec < 1000000);
00150 return tv_sec > o.tv_sec ||
00151 (tv_sec == o.tv_sec && tv_usec >= o.tv_usec);
00152 }
00153
00154 inline bool Timeval::operator==(const timeval &o) const {
00155 assert(o.tv_usec < 1000000);
00156 return tv_sec == o.tv_sec && tv_usec == o.tv_usec;
00157 }
00158
00159 inline bool Timeval::operator<=(const timeval &o) const {
00160 assert(o.tv_usec < 1000000);
00161 return tv_sec < o.tv_sec ||
00162 (tv_sec == o.tv_sec && tv_usec <= o.tv_usec);
00163 }
00164
00165 inline bool Timeval::operator<(const timeval &o) const {
00166 assert(o.tv_usec < 1000000);
00167 return tv_sec < o.tv_sec || (tv_sec == o.tv_sec && tv_usec < o.tv_usec);
00168 }
00169
00170 inline bool Timeval::operator!=(const timeval &o) const {
00171 assert(o.tv_usec < 1000000);
00172 return tv_sec != o.tv_sec && tv_usec != o.tv_usec;
00173 }
00174
00175
00176
00177
00178
00179 inline Timeval Timeval::operator+(const timeval &o) const {
00180 assert(o.tv_usec < 1000000);
00181 const suseconds_t t(tv_usec + o.tv_usec);
00182 if(t < 1000000)
00183 return Timeval(tv_sec + o.tv_sec, t);
00184 return Timeval(tv_sec + o.tv_sec + 1, t - 1000000);
00185 }
00186
00187 inline Timeval Timeval::operator+(time_t sec) const {
00188 return Timeval(tv_sec + sec, tv_usec);
00189 }
00190
00191 inline Timeval Timeval::operator+(float sec) const {
00192
00193 return *this + Timeval(sec);
00194 }
00195
00196 inline Timeval Timeval::operator+(double sec) const {
00197
00198 return *this + Timeval(sec);
00199 }
00200
00201
00202
00203
00204
00205 inline Timeval &Timeval::operator+=(const timeval &o) {
00206 assert(o.tv_usec < 1000000);
00207 tv_sec += o.tv_sec;
00208 tv_usec += o.tv_usec;
00209 if(tv_usec >= 1000000) {
00210 ++tv_sec;
00211 tv_usec -= 1000000;
00212 }
00213 return *this;
00214 }
00215
00216 inline Timeval &Timeval::operator+=(time_t sec) {
00217 tv_sec += sec;
00218 return *this;
00219 }
00220
00221 inline Timeval &Timeval::operator+=(float sec) {
00222 {
00223 const time_t t((time_t)sec);
00224 tv_sec += t;
00225 sec -= (float)t;
00226 }
00227 tv_usec += (suseconds_t)(sec * 1000000);
00228 while(tv_usec >= 1000000) {
00229 tv_usec -= 1000000;
00230 ++tv_sec;
00231 }
00232 return *this;
00233 }
00234
00235 inline Timeval &Timeval::operator+=(double sec) {
00236 {
00237 const time_t t((time_t)sec);
00238 tv_sec += t;
00239 sec -= (double)t;
00240 }
00241 tv_usec += (suseconds_t)(sec * 1000000);
00242 while(tv_usec >= 1000000) {
00243 tv_usec -= 1000000;
00244 ++tv_sec;
00245 }
00246 return *this;
00247 }
00248
00249
00250
00251
00252
00253 inline Timeval Timeval::operator-(const timeval &o) const {
00254 assert(o.tv_usec < 1000000);
00255 if(tv_usec >= o.tv_usec)
00256 return Timeval(tv_sec - o.tv_sec, tv_usec - o.tv_usec);
00257 return Timeval(tv_sec - o.tv_sec - 1, 1000000 - o.tv_usec + tv_usec);
00258 }
00259
00260 inline Timeval Timeval::operator-(time_t sec) const {
00261 return Timeval(tv_sec - sec, tv_usec);
00262 }
00263
00264 inline Timeval Timeval::operator-(float sec) const {
00265
00266 return *this - Timeval(sec);
00267 }
00268
00269 inline Timeval Timeval::operator-(double sec) const {
00270
00271 return *this - Timeval(sec);
00272 }
00273
00274
00275
00276
00277
00278 inline Timeval &Timeval::operator-=(const timeval &o) {
00279 assert(o.tv_usec < 1000000);
00280 tv_sec -= o.tv_sec;
00281 if(tv_usec < o.tv_usec) {
00282 tv_usec += 10000000;
00283 tv_usec -= o.tv_usec;
00284 --tv_sec;
00285 } else
00286 tv_usec -= o.tv_usec;
00287 return *this;
00288 }
00289
00290 inline Timeval &Timeval::operator-=(time_t sec) {
00291 tv_sec -= sec;
00292 return *this;
00293 }
00294
00295 inline Timeval &Timeval::operator-=(float sec) {
00296 {
00297 time_t s((time_t)sec);
00298 tv_sec -= s;
00299 sec -= (float)s;
00300 }
00301 suseconds_t u((suseconds_t)(sec * 1000000));
00302 if(tv_usec < u) {
00303 tv_usec += 1000000;
00304 tv_usec -= u;
00305 --tv_sec;
00306 } else
00307 tv_usec -= u;
00308 return *this;
00309 }
00310
00311 inline Timeval &Timeval::operator-=(double sec) {
00312 {
00313 time_t s((time_t)sec);
00314 tv_sec -= s;
00315 sec -= (double)s;
00316 }
00317 suseconds_t u((suseconds_t)(sec * 1000000));
00318 if(tv_usec < u) {
00319 tv_usec += 1000000;
00320 tv_usec -= u;
00321 --tv_sec;
00322 } else
00323 tv_usec -= u;
00324 return *this;
00325 }
00326
00327 #endif