00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef TIME_VAL_H
00015 #define TIME_VAL_H
00016
00017 #include <sys/time.h>
00018 #include <unistd.h>
00019
00020 #include <string>
00021 using std::string;
00022
00023 namespace ASSA {
00024
00030 class TimeVal : public timeval
00031 {
00032 public:
00033 enum {
00034 gmt,
00035 loc
00036 };
00037
00041 TimeVal ();
00042
00045 TimeVal (long sec_, long msec_);
00046
00049 TimeVal (double d_);
00050
00053 TimeVal (const timeval& tv_);
00054
00057 TimeVal (const TimeVal& tv_);
00058
00061 operator double () const;
00062
00064 void sec (long sec_) { tv_sec = sec_; }
00065
00067 long sec (void) const { return tv_sec; }
00068
00070 void msec (long msec_) { tv_usec = msec_; }
00071
00073 long msec (void) const { return tv_usec; }
00074
00078 long millisec () const;
00079
00081 void tz (int tz_) { m_tz = tz_; }
00082
00084 int tz (void) const { return m_tz; }
00085
00086 TimeVal& operator= (const TimeVal& tv_);
00087
00089 TimeVal& operator+= (const TimeVal& rhs_);
00090
00092 TimeVal& operator-= (const TimeVal& rhs_);
00093
00095 friend TimeVal operator+(const TimeVal& lhs_, const TimeVal& rhs_);
00096
00098 friend TimeVal operator-(const TimeVal& lhs_, const TimeVal& rhs_);
00099
00101 bool operator< (const TimeVal& rhs_) const;
00102
00104 bool operator==(const TimeVal& rhs_) const;
00105
00107 friend bool operator> (const TimeVal& lhs_, const TimeVal& rhs_);
00108
00110 friend bool operator!=(const TimeVal& lhs_, const TimeVal& rhs_);
00111
00113 friend bool operator<=(const TimeVal& lhs_, const TimeVal& rhs_);
00114
00116 friend bool operator>=(const TimeVal& lhs_, const TimeVal& rhs_);
00117
00128 string fmtString (const char* fmt_ = NULL) const;
00129
00132 string fmt_hh_mm_ss () const;
00133
00136 string fmt_hh_mm_ss_mls () const;
00137
00140 string fmt_mm_ss () const;
00141
00144 string fmt_mm_ss_mls () const;
00145
00148 string fmt_ss_mls () const;
00149
00153 void dump_to_log (const string& name_ = "") const;
00154
00157 static TimeVal zeroTime () { return m_zero; }
00158
00163 static TimeVal gettimeofday ();
00164
00165 protected:
00167 void init (long, long, int);
00168
00169 private:
00171 void normalize ();
00172
00173 private:
00175 int m_tz;
00176
00178 static TimeVal m_zero;
00179 };
00180
00181
00182
00183
00184 inline void
00185 TimeVal::
00186 init (long s_, long ms_, int tz_)
00187 {
00188 tv_sec = s_;
00189 tv_usec = ms_;
00190 m_tz = tz_;
00191 normalize ();
00192 }
00193
00194 inline
00195 TimeVal::
00196 TimeVal ()
00197 {
00198 init (0, 0, gmt);
00199 }
00200
00201 inline
00202 TimeVal::
00203 TimeVal (long sec_, long msec_)
00204 {
00205 init (sec_, msec_, gmt);
00206 }
00207
00208 inline
00209 TimeVal::
00210 TimeVal (double d_)
00211 : m_tz (gmt)
00212 {
00213 long l = long(d_);
00214 tv_sec = l;
00215 tv_usec = (long) ((d_ - double(l))*1000000.0);
00216 normalize();
00217 }
00218
00219 inline
00220 TimeVal::
00221 TimeVal (const timeval& tv_)
00222 {
00223 init (tv_.tv_sec, tv_.tv_usec, gmt);
00224 }
00225
00226 inline
00227 TimeVal::
00228 TimeVal (const TimeVal& tv_)
00229 {
00230 init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
00231 }
00232
00233 inline TimeVal
00234 TimeVal::
00235 gettimeofday ()
00236 {
00237 timeval tv;
00238 ::gettimeofday (&tv, 0);
00239 return tv;
00240 }
00241
00242 inline
00243 TimeVal::operator double () const
00244 {
00245 return tv_sec + tv_usec / 1000000.0;
00246 }
00247
00248 inline long
00249 TimeVal::
00250 millisec () const
00251 {
00252 return (msec () % 1000000) / 1000;
00253 }
00254
00255 inline string
00256 TimeVal::
00257 fmt_hh_mm_ss () const
00258 {
00259 return fmtString ("%T");
00260 }
00261
00262 inline string
00263 TimeVal::
00264 fmt_mm_ss () const
00265 {
00266 return fmtString ("%M:%S");
00267 }
00268
00269
00270
00271
00272
00273 inline TimeVal&
00274 TimeVal::
00275 operator=(const TimeVal& tv_)
00276 {
00277 init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
00278 return *this;
00279 }
00280
00281 inline TimeVal
00282 operator+(const TimeVal& lhs_, const TimeVal& rhs_)
00283 {
00284 TimeVal temp(lhs_);
00285 temp += rhs_;
00286 temp.normalize ();
00287 return temp;
00288 }
00289
00290 inline TimeVal
00291 operator-(const TimeVal& lhs_, const TimeVal& rhs_)
00292 {
00293 TimeVal temp(lhs_);
00294 temp -= rhs_;
00295 temp.normalize ();
00296 return temp;
00297 }
00298
00299 inline bool
00300 TimeVal::
00301 operator<(const TimeVal& rhs_) const
00302 {
00303 return (tv_sec < rhs_.tv_sec
00304 || (tv_sec == rhs_.tv_sec && tv_usec < rhs_.tv_usec) ) ;
00305 }
00306
00307 inline bool
00308 TimeVal::
00309 operator==(const TimeVal& rhs_) const
00310 {
00311 return !(*this < rhs_ || rhs_ < *this);
00312 }
00313
00314 inline bool
00315 operator> (const TimeVal& lhs_, const TimeVal& rhs_)
00316 {
00317 return rhs_ < lhs_;
00318 }
00319
00320 inline bool
00321 operator!=(const TimeVal& lhs_, const TimeVal& rhs_)
00322 {
00323 return !( lhs_ == rhs_ );
00324 }
00325
00326 inline bool
00327 operator<=(const TimeVal& lhs_, const TimeVal& rhs_)
00328 {
00329 return !(rhs_ < lhs_);
00330 }
00331
00332 inline bool
00333 operator>=(const TimeVal& lhs_, const TimeVal& rhs_)
00334 {
00335 return !(lhs_ < rhs_);
00336 }
00337
00338 }
00339
00340 #endif