/home/vlg/develop/libASSA/libassa/assa/TimeVal.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                          TimeVal.h
00004 //------------------------------------------------------------------------------
00005 //  Copyright (c) 1999 by Vladislav Grinchenko
00006 //
00007 //  This library is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU Library General Public
00009 //  License as published by the Free Software Foundation; either
00010 //  version 2 of the License, or (at your option) any later version.
00011 //------------------------------------------------------------------------------
00012 //  Created: 09/28/1999
00013 //------------------------------------------------------------------------------
00014 #ifndef TIME_VAL_H
00015 #define TIME_VAL_H
00016 
00017 #include <sys/time.h>       // gettimeofday(3)
00018 #include <unistd.h>     // gettimeofday(3)
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 // Inlines
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 
00234 TimeVal::operator double () const
00235 { 
00236     return tv_sec + tv_usec / 1000000.0;
00237 }
00238 
00239 inline long
00240 TimeVal::
00241 millisec () const
00242 {
00243     return (msec () % 1000000) / 1000;
00244 }
00245 
00246 inline string
00247 TimeVal::
00248 fmt_hh_mm_ss () const
00249 {
00250     return fmtString ("%T");
00251 }
00252 
00253 inline string
00254 TimeVal::
00255 fmt_mm_ss () const
00256 {
00257     return fmtString ("%M:%S");
00258 }
00259 
00260 //------------------------------------------------------------------------------
00261 // Friend functions
00262 //------------------------------------------------------------------------------
00263 
00264 inline TimeVal&
00265 TimeVal::
00266 operator=(const TimeVal& tv_)
00267 {
00268     init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
00269     return *this;
00270 }
00271 
00272 inline TimeVal
00273 operator+(const TimeVal& lhs_, const TimeVal& rhs_)
00274 {
00275     TimeVal temp(lhs_);
00276     temp += rhs_;
00277     temp.normalize ();
00278     return temp;
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 bool 
00291 TimeVal::
00292 operator<(const TimeVal& rhs_) const
00293 {
00294     return (tv_sec < rhs_.tv_sec
00295         || (tv_sec == rhs_.tv_sec && tv_usec < rhs_.tv_usec) ) ;
00296 }
00297 
00298 inline bool 
00299 TimeVal::
00300 operator==(const TimeVal& rhs_) const
00301 {
00302     return !(*this < rhs_ || rhs_ < *this);
00303 }
00304 
00305 inline bool
00306 operator> (const TimeVal& lhs_, const TimeVal& rhs_)
00307 {
00308     return rhs_ < lhs_;
00309 }
00310 
00311 inline bool 
00312 operator!=(const TimeVal& lhs_, const TimeVal& rhs_)
00313 {
00314     return !( lhs_ == rhs_ );
00315 }
00316 
00317 inline bool
00318 operator<=(const TimeVal& lhs_, const TimeVal& rhs_)
00319 {
00320     return !(rhs_ < lhs_);
00321 }
00322 
00323 inline bool
00324 operator>=(const TimeVal& lhs_, const TimeVal& rhs_)
00325 {
00326     return !(lhs_ < rhs_);
00327 }
00328 
00329 } // end namespace ASSA
00330 
00331 #endif /* TIME_VAL_H */  

Generated on Sun Aug 13 15:08:00 2006 for libassa by  doxygen 1.4.6