timeval.h

00001 /*
00002  *  Copyright (C) 2006  Helmut Grohne
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  * constructors
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         (void)this->gettimeofday();
00040 }
00041 
00042 inline Timeval::Timeval(time_t sec) {
00043         tv_sec = sec;
00044         tv_usec = 0;
00045 }
00046 
00047 inline Timeval::Timeval(time_t sec, suseconds_t usec) {
00048         assert(usec < 1000000);
00049         tv_sec = sec;
00050         tv_usec = usec;
00051 }
00052 
00053 inline Timeval::Timeval(float sec) {
00054         tv_sec = (time_t)sec;
00055         sec -= (float) tv_sec;
00056         tv_usec = (suseconds_t)(sec * 1000000);
00057 }
00058 
00059 inline Timeval::Timeval(double sec) {
00060         tv_sec = (time_t)sec;
00061         sec -= (double) tv_sec;
00062         tv_usec = (suseconds_t)(sec * 1000000);
00063 }
00064 
00065 /*
00066  * operator=
00067  */
00068 
00069 inline Timeval &Timeval::operator=(const timeval &o) {
00070         assert(o.tv_usec < 1000000);
00071         tv_sec = o.tv_sec;
00072         tv_usec = o.tv_usec;
00073         return *this;
00074 }
00075 
00076 inline Timeval &Timeval::operator=(time_t sec) {
00077         tv_sec = sec;
00078         tv_usec = 0;
00079         return *this;
00080 }
00081 
00082 inline Timeval &Timeval::operator=(float sec) {
00083         tv_sec = (time_t)sec;
00084         sec -= (float)tv_sec;
00085         tv_usec = (suseconds_t)(sec * 10000000);
00086         return *this;
00087 }
00088 
00089 inline Timeval &Timeval::operator=(double sec) {
00090         tv_sec = (time_t)sec;
00091         sec -= (double)tv_sec;
00092         tv_usec = (suseconds_t)(sec * 10000000);
00093         return *this;
00094 }
00095 
00096 /*
00097  * useful methods
00098  */
00099 inline Timeval &Timeval::gettimeofday() {
00100 #ifndef __WIN32__
00101         int r(::gettimeofday(this, NULL));
00102         /* possible errnos:
00103          * EFAULT: means that this is not an address => we should abort.
00104          * EINVAL: invalid timezone, NULL cannot be invalid.
00105          */
00106         assert(r == 0);
00107 #else
00108         struct _timeb tb;
00109         _ftime(&tb);
00110         tv_sec = tb.time;
00111         tv_usec = 1000*tb.millitm;
00112 #endif
00113         return *this;
00114 }
00115 
00116 /*
00117  * casting operators
00118  */
00119 
00120 inline Timeval::operator time_t() const {
00121         return tv_sec;
00122 }
00123 
00124 inline Timeval::operator float() const {
00125         return (float)tv_sec + 0.000001 * (float)tv_usec;
00126 }
00127 
00128 inline Timeval::operator double() const {
00129         return (double)tv_sec + 0.000001 * (double)tv_usec;
00130 }
00131 
00132 inline unsigned int Timeval::milliseconds() const {
00133         return (1000*(unsigned int)tv_sec)+(((unsigned int)tv_usec)/1000);
00134 }
00135 
00136 /*
00137  * comparison operators
00138  */
00139 
00140 inline bool Timeval::operator>(const timeval &o) const {
00141         assert(o.tv_usec < 1000000);
00142         return tv_sec > o.tv_sec || (tv_sec == o.tv_sec && tv_usec > o.tv_usec);
00143 }
00144 
00145 inline bool Timeval::operator>=(const timeval &o) const {
00146         assert(o.tv_usec < 1000000);
00147         return tv_sec > o.tv_sec ||
00148                         (tv_sec == o.tv_sec && tv_usec >= o.tv_usec);
00149 }
00150 
00151 inline bool Timeval::operator==(const timeval &o) const {
00152         assert(o.tv_usec < 1000000);
00153         return tv_sec == o.tv_sec && tv_usec == o.tv_usec;
00154 }
00155 
00156 inline bool Timeval::operator<=(const timeval &o) const {
00157         assert(o.tv_usec < 1000000);
00158         return tv_sec < o.tv_sec ||
00159                         (tv_sec == o.tv_sec && tv_usec <= o.tv_usec);
00160 }
00161 
00162 inline bool Timeval::operator<(const timeval &o) const {
00163         assert(o.tv_usec < 1000000);
00164         return tv_sec < o.tv_sec || (tv_sec == o.tv_sec && tv_usec < o.tv_usec);
00165 }
00166 
00167 inline bool Timeval::operator!=(const timeval &o) const {
00168         assert(o.tv_usec < 1000000);
00169         return tv_sec != o.tv_sec && tv_usec != o.tv_usec;
00170 }
00171 
00172 /*
00173  * operator+
00174  */
00175 
00176 inline Timeval Timeval::operator+(const timeval &o) const {
00177         assert(o.tv_usec < 1000000);
00178         const suseconds_t t(tv_usec + o.tv_usec);
00179         if(t < 1000000)
00180                 return Timeval(tv_sec + o.tv_sec, t);
00181         return Timeval(tv_sec + o.tv_sec + 1, t - 1000000);
00182 }
00183 
00184 inline Timeval Timeval::operator+(time_t sec) const {
00185         return Timeval(tv_sec + sec, tv_usec);
00186 }
00187 
00188 inline Timeval Timeval::operator+(float sec) const {
00189         /* TODO: more performance? */
00190         return *this + Timeval(sec);
00191 }
00192 
00193 inline Timeval Timeval::operator+(double sec) const {
00194         /* TODO: more performance? */
00195         return *this + Timeval(sec);
00196 }
00197 
00198 /*
00199  * operator+=
00200  */
00201 
00202 inline Timeval &Timeval::operator+=(const timeval &o) {
00203         assert(o.tv_usec < 1000000);
00204         tv_sec += o.tv_sec;
00205         tv_usec += o.tv_usec;
00206         if(tv_usec >= 1000000) {
00207                 ++tv_sec;
00208                 tv_usec -= 1000000;
00209         }
00210         return *this;
00211 }
00212 
00213 inline Timeval &Timeval::operator+=(time_t sec) {
00214         tv_sec += sec;
00215         return *this;
00216 }
00217 
00218 inline Timeval &Timeval::operator+=(float sec) {
00219         {
00220                 const time_t t((time_t)sec);
00221                 tv_sec += t;
00222                 sec -= (float)t;
00223         }
00224         tv_usec += (suseconds_t)(sec * 1000000);
00225         while(tv_usec >= 1000000) {
00226                 tv_usec -= 1000000;
00227                 ++tv_sec;
00228         }
00229         return *this;
00230 }
00231 
00232 inline Timeval &Timeval::operator+=(double sec) {
00233         {
00234                 const time_t t((time_t)sec);
00235                 tv_sec += t;
00236                 sec -= (double)t;
00237         }
00238         tv_usec += (suseconds_t)(sec * 1000000);
00239         while(tv_usec >= 1000000) {
00240                 tv_usec -= 1000000;
00241                 ++tv_sec;
00242         }
00243         return *this;
00244 }
00245 
00246 /*
00247  * operator-
00248  */
00249 
00250 inline Timeval Timeval::operator-(const timeval &o) const {
00251         assert(o.tv_usec < 1000000);
00252         if(tv_usec >= o.tv_usec)
00253                 return Timeval(tv_sec - o.tv_sec, tv_usec - o.tv_usec);
00254         return Timeval(tv_sec - o.tv_sec - 1, 1000000 - o.tv_usec + tv_usec);
00255 }
00256 
00257 inline Timeval Timeval::operator-(time_t sec) const {
00258         return Timeval(tv_sec - sec, tv_usec);
00259 }
00260 
00261 inline Timeval Timeval::operator-(float sec) const {
00262         /* TODO: more performance? */
00263         return *this - Timeval(sec);
00264 }
00265 
00266 inline Timeval Timeval::operator-(double sec) const {
00267         /* TODO: more performance? */
00268         return *this - Timeval(sec);
00269 }
00270 
00271 /*
00272  * operator-=
00273  */
00274 
00275 inline Timeval &Timeval::operator-=(const timeval &o) {
00276         assert(o.tv_usec < 1000000);
00277         tv_sec -= o.tv_sec;
00278         if(tv_usec < o.tv_usec) {
00279                 tv_usec += 10000000;
00280                 tv_usec -= o.tv_usec;
00281                 --tv_sec;
00282         } else
00283                 tv_usec -= o.tv_usec;
00284         return *this;
00285 }
00286 
00287 inline Timeval &Timeval::operator-=(time_t sec) {
00288         tv_sec -= sec;
00289         return *this;
00290 }
00291 
00292 inline Timeval &Timeval::operator-=(float sec) {
00293         {
00294                 time_t s((time_t)sec);
00295                 tv_sec -= s;
00296                 sec -= (float)s;
00297         }
00298         suseconds_t u((suseconds_t)(sec * 1000000));
00299         if(tv_usec < u) {
00300                 tv_usec += 1000000;
00301                 tv_usec -= u;
00302                 --tv_sec;
00303         } else
00304                 tv_usec -= u;
00305         return *this;
00306 }
00307 
00308 inline Timeval &Timeval::operator-=(double sec) {
00309         {
00310                 time_t s((time_t)sec);
00311                 tv_sec -= s;
00312                 sec -= (double)s;
00313         }
00314         suseconds_t u((suseconds_t)(sec * 1000000));
00315         if(tv_usec < u) {
00316                 tv_usec += 1000000;
00317                 tv_usec -= u;
00318                 --tv_sec;
00319         } else
00320                 tv_usec -= u;
00321         return *this;
00322 }
00323 
00324 #endif

Generated on Sat Feb 7 01:26:50 2009 by  doxygen 1.5.1