00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00033
00034 #ifndef GROOVX_RUTZ_TIME_H_UTC20050626084020_DEFINED
00035 #define GROOVX_RUTZ_TIME_H_UTC20050626084020_DEFINED
00036
00037 #include <sys/time.h>
00038
00039
00040
00041
00042
00043 namespace rutz
00044 {
00045 class time;
00046 }
00047
00049
00050 class rutz::time
00051 {
00052 public:
00054 time() throw() : m_timeval()
00055 {
00056 reset();
00057 }
00058
00060 time(const timeval& t) throw() : m_timeval(t)
00061 {
00062 normalize(this->m_timeval);
00063 }
00064
00066 time(unsigned long s, long us) throw() : m_timeval()
00067 {
00068 reset(s, us);
00069 }
00070
00071 explicit time(double ss) throw() : m_timeval()
00072 {
00073 const unsigned long s = (unsigned long)(ss);
00074 const long us = long((ss - s) * 1000000);
00075 reset(s, us);
00076 }
00077
00079 time(const time& x) throw() : m_timeval(x.m_timeval) {}
00080
00082 ~time() throw() {}
00083
00085 static time wall_clock_now() throw();
00086
00088 static time user_rusage() throw();
00089
00091 static time sys_rusage() throw();
00092
00094 static time rusage_now() throw()
00095 { return rutz::time::user_rusage() + rutz::time::sys_rusage(); }
00096
00098 void reset(unsigned long s = 0, long us = 0) throw()
00099 {
00100 m_timeval.tv_sec = s;
00101 m_timeval.tv_usec = us;
00102 normalize(this->m_timeval);
00103 }
00104
00106 time& operator=(const time& x) throw()
00107 {
00108 m_timeval = x.m_timeval;
00109 return *this;
00110 }
00111
00113 time operator+(const time& t2) const throw()
00114 {
00115 timeval result;
00116 result.tv_sec = this->m_timeval.tv_sec + t2.m_timeval.tv_sec;
00117 result.tv_usec = this->m_timeval.tv_usec + t2.m_timeval.tv_usec;
00118
00119 normalize(result);
00120
00121 return time(result);
00122 }
00123
00125 time& operator+=(const time& t2) throw()
00126 {
00127 m_timeval.tv_sec += t2.m_timeval.tv_sec;
00128 m_timeval.tv_usec += t2.m_timeval.tv_usec;
00129
00130 normalize(this->m_timeval);
00131
00132 return *this;
00133 }
00134
00136 time operator-(const time& t2) const throw()
00137 {
00138 timeval result;
00139 result.tv_sec = this->m_timeval.tv_sec - t2.m_timeval.tv_sec;
00140 result.tv_usec = this->m_timeval.tv_usec - t2.m_timeval.tv_usec;
00141
00142 normalize(result);
00143
00144 return time(result);
00145 }
00146
00148 time& operator-=(const time& t2) throw()
00149 {
00150 m_timeval.tv_sec -= t2.m_timeval.tv_sec;
00151 m_timeval.tv_usec -= t2.m_timeval.tv_usec;
00152
00153 normalize(this->m_timeval);
00154
00155 return *this;
00156 }
00157
00159 bool operator<(const time& t2) const throw()
00160 {
00161 return (m_timeval.tv_sec < t2.m_timeval.tv_sec)
00162 ||
00163 (m_timeval.tv_sec == t2.m_timeval.tv_sec
00164 && m_timeval.tv_usec < t2.m_timeval.tv_usec);
00165 }
00166
00168 bool operator<=(const time& t2) const throw()
00169 {
00170 return (m_timeval.tv_sec <= t2.m_timeval.tv_sec)
00171 ||
00172 (m_timeval.tv_sec == t2.m_timeval.tv_sec
00173 && m_timeval.tv_usec <= t2.m_timeval.tv_usec);
00174 }
00175
00177 bool operator==(const time& t2) const throw()
00178 {
00179 return (m_timeval.tv_sec == t2.m_timeval.tv_sec
00180 && m_timeval.tv_usec == t2.m_timeval.tv_usec);
00181 }
00182
00184 bool operator>(const time& t2) const throw()
00185 {
00186 return (m_timeval.tv_sec > t2.m_timeval.tv_sec)
00187 ||
00188 (m_timeval.tv_sec == t2.m_timeval.tv_sec
00189 && m_timeval.tv_usec > t2.m_timeval.tv_usec);
00190 }
00191
00193 bool operator>=(const time& t2) const throw()
00194 {
00195 return (m_timeval.tv_sec >= t2.m_timeval.tv_sec)
00196 ||
00197 (m_timeval.tv_sec == t2.m_timeval.tv_sec
00198 && m_timeval.tv_usec >= t2.m_timeval.tv_usec);
00199 }
00200
00202 double sec() const throw()
00203 {
00204 return double(m_timeval.tv_sec) + m_timeval.tv_usec / 1000000.0;
00205 }
00206
00208 double msec() const throw()
00209 {
00210 return (m_timeval.tv_sec * 1000.0) + (m_timeval.tv_usec / 1000.0);
00211 }
00212
00214 double usec() const throw()
00215 {
00216 return (m_timeval.tv_sec * 1000000.0) + double(m_timeval.tv_usec);
00217 }
00218
00220 const timeval& tval() const throw()
00221 {
00222 return m_timeval;
00223 }
00224
00226 operator const timeval&() const throw()
00227 {
00228 return m_timeval;
00229 }
00230
00231 private:
00233 static void normalize(timeval& t) throw()
00234 {
00235 long s = t.tv_usec / 1000000;
00236
00237 if (s != 0)
00238 {
00239 t.tv_sec += s;
00240 t.tv_usec -= (s * 1000000);
00241 }
00242 }
00243
00244 timeval m_timeval;
00245 };
00246
00247 static const char __attribute__((used)) vcid_groovx_rutz_time_h_utc20050626084020[] = "$Id: time.h 10096 2007-08-07 19:08:11Z rjpeters $ $HeadURL: file:
00248 #endif // !GROOVX_RUTZ_TIME_H_UTC20050626084020_DEFINED