Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

TimeET.h

Go to the documentation of this file.
00001 #ifndef __TIME_ET_CLASS__
00002 #define __TIME_ET_CLASS__
00003 
00004 #ifdef PLATFORM_APERIOS
00005 #include <MCOOP.h>
00006 #endif
00007 #include <iostream>
00008 #include <sys/time.h>
00009 
00010 //!a nice class for handling time values with high precision
00011 /*@test negative times might not be handled properly */
00012 class TimeET {
00013   //! lets the class be displayed easily
00014   friend std::ostream& operator<<(std::ostream& o, const TimeET& t);
00015   
00016 public:
00017   //@{
00018   //!constructor
00019   TimeET() : tv() {
00020     Set();
00021   }
00022   TimeET(long ms) : tv() {
00023     Set(ms);
00024   }
00025   TimeET(time_t sec, long usec) : tv() {
00026     Set(sec,usec);
00027   }
00028   TimeET(const timeval& tval) : tv(tval) {}
00029   TimeET(const timespec& tspec) : tv() {
00030     Set(tspec.tv_sec,tspec.tv_nsec/ns_per_us);
00031   }
00032   //!constructor, sepecify @a t seconds
00033   TimeET(double t) :tv() {
00034     Set(t);
00035   }
00036   //@}
00037   
00038   //!returns the difference between the current time and the time stored
00039   inline TimeET Age() const { return TimeET()-(*this); }
00040   //@{
00041   //!returns the time stored as seconds in a double
00042   inline double Value() const { return (double)tv.tv_sec+(double)tv.tv_usec/(double)us_per_sec; }
00043   //! returns the time as a timeval system construct
00044   inline operator timeval &() { return tv; }
00045   //! returns the time as a const timeval system construct
00046   inline operator const timeval &() const { return tv; }
00047   //! returns the time as a timespec system construct (though the nanosecond resolution isn't actually retained)
00048   inline operator timespec() {
00049     timespec tspec={tv.tv_sec,tv.tv_usec*ns_per_us};
00050     return tspec;
00051   }
00052   //! returns the seconds portion (not rounded)
00053   inline time_t getSeconds() const { return tv.tv_sec; }
00054   //! returns the millisecond representation (includes both seconds and microseconds contribution); pass 0 to round down, 1000 to round up, 500 to round nearest
00055   inline long getMilliseconds(long round=us_per_ms/2) const { return tv.tv_sec*ms_per_sec + (tv.tv_usec+round)/us_per_ms; }
00056   //! returns the microseconds portion (doesn't include seconds)
00057   inline long getMicroPortion() const { return tv.tv_usec; }
00058   //@}
00059   
00060   //@{
00061   //!sets the time stored in the class in terms of milliseconds
00062   inline void Set(long ms) {
00063     Set(0,ms*us_per_ms);
00064   }
00065   //!sets the time in terms of seconds and microseconds (aka timeval)
00066   inline void Set(time_t sec, long usec) {
00067     tv.tv_sec=sec+usec/us_per_sec;
00068     tv.tv_usec=usec%us_per_sec;;
00069   }
00070   //!sets the time in terms of floating-point seconds
00071   inline void Set(double t) {
00072     tv.tv_sec=(long)t;
00073     tv.tv_usec=(long)((t-tv.tv_sec)*us_per_sec);
00074   }
00075   /*!@brief sets the time to the current time
00076    * @todo not getting timeofday on OPEN-R, is time since boot instead...*/
00077   inline void Set() {
00078 #ifdef PLATFORM_APERIOS
00079     static struct SystemTime t;
00080     GetSystemTime(&t);
00081     Set(t.seconds,t.useconds);
00082 #else
00083     gettimeofday(&tv,&tz);
00084 #endif
00085   }
00086   //@}
00087   
00088   //@{
00089   //!for comparing times
00090   inline bool operator<(long ms) const { //what if ms is negative?
00091     time_t sec = ms/ms_per_sec;
00092     return tv.tv_sec<sec || (tv.tv_sec==sec && tv.tv_usec<static_cast<long>((ms-sec*ms_per_sec)*us_per_ms));
00093   }
00094   inline bool operator<(double t) const {
00095     return Value()<t;
00096   }
00097   inline bool operator<(const TimeET& t) const {
00098     return tv.tv_sec<t.tv.tv_sec || (tv.tv_sec==t.tv.tv_sec && tv.tv_usec<t.tv.tv_usec);
00099   }
00100   //@}
00101   
00102   //@{
00103   //!for doing doing math with time
00104   inline TimeET operator+(const TimeET& t) const {
00105     long usec = tv.tv_usec+t.tv.tv_usec;
00106     long sec = tv.tv_sec+t.tv.tv_sec+usec/us_per_sec;
00107     usec%=us_per_sec;
00108     return TimeET(sec,usec);
00109   }
00110   inline TimeET& operator+=(const TimeET& t) {
00111     tv.tv_usec+=t.tv.tv_usec;
00112     tv.tv_sec+=t.tv.tv_sec+tv.tv_usec/us_per_sec;
00113     tv.tv_usec%=us_per_sec;
00114     return *this;
00115   }
00116   inline TimeET operator-(const TimeET& t) const {
00117     long usec = tv.tv_usec-t.tv.tv_usec;
00118     long sec = tv.tv_sec-t.tv.tv_sec+usec/us_per_sec;
00119     usec%=us_per_sec;
00120     if(usec<0) {
00121       usec+=us_per_sec;
00122       sec--;
00123     }
00124     return TimeET(sec,usec);
00125   }
00126   inline TimeET& operator-=(const TimeET& t) {
00127     tv.tv_usec-=t.tv.tv_usec;
00128     tv.tv_sec=tv.tv_sec-t.tv.tv_sec+tv.tv_usec/us_per_sec;
00129     tv.tv_usec%=us_per_sec;
00130     if(tv.tv_usec<0) {
00131       tv.tv_usec+=us_per_sec;
00132       tv.tv_sec--;
00133     }
00134     return *this;
00135   }
00136   inline TimeET operator*(double x) const {
00137     if(x>1 || x<-1) {
00138       double usec = tv.tv_usec*x;
00139       long carry=static_cast<long>(usec/us_per_sec);
00140       long sec = static_cast<long>(tv.tv_sec*x)+carry;
00141       usec-=carry*us_per_sec;
00142       return TimeET(sec,static_cast<long>(usec));
00143     } else {
00144       double secv=tv.tv_sec*x;
00145       long sec=static_cast<long>(secv);
00146       double carry=secv-sec;
00147       long usec=static_cast<long>(tv.tv_usec*x+carry*us_per_sec);
00148       return TimeET(sec,usec);
00149     }
00150   }
00151   inline TimeET& operator*=(double x) {
00152     if(x>1 || x<-1) {
00153       double usec = tv.tv_usec*x;
00154       long carry=static_cast<long>(usec/us_per_sec);
00155       tv.tv_sec = static_cast<long>(tv.tv_sec*x)+carry;
00156       tv.tv_usec=static_cast<long>(usec-carry*us_per_sec);
00157       return *this;
00158     } else {
00159       double secv=tv.tv_sec*x;
00160       tv.tv_sec=static_cast<long>(secv);
00161       double carry=secv-tv.tv_sec;
00162       tv.tv_usec=static_cast<long>(tv.tv_usec*x+carry*us_per_sec);
00163       return *this;
00164     }
00165   }
00166   inline TimeET operator/(double x) const {
00167     if(x>1 || x<-1) {
00168       double secv=tv.tv_sec/x;
00169       long sec=static_cast<long>(secv);
00170       double carry=secv-sec;
00171       long usec=static_cast<long>(tv.tv_usec/x+carry*us_per_sec);
00172       return TimeET(sec,usec);
00173     } else {
00174       double usec = tv.tv_usec/x;
00175       long carry=static_cast<long>(usec/us_per_sec);
00176       long sec = static_cast<long>(tv.tv_sec/x)+carry;
00177       usec-=carry*us_per_sec;
00178       return TimeET(sec,static_cast<long>(usec));
00179     }
00180   }
00181   inline TimeET& operator/=(double x) {
00182     if(x>1 || x<-1) {
00183       double secv=tv.tv_sec/x;
00184       tv.tv_sec=static_cast<long>(secv);
00185       double carry=secv-tv.tv_sec;
00186       tv.tv_usec=static_cast<long>(tv.tv_usec/x+carry*us_per_sec);
00187       return *this;
00188     } else {
00189       double usec = tv.tv_usec/x;
00190       long carry=static_cast<long>(usec/us_per_sec);
00191       tv.tv_sec = static_cast<long>(tv.tv_sec/x)+carry;
00192       tv.tv_usec=static_cast<long>(usec-carry*us_per_sec);
00193       return *this;
00194     }
00195   }
00196   //@}
00197   
00198   static const long us_per_sec=1000000; //!< conversion factor for microseconds to seconds
00199   static const long ms_per_sec=1000;    //!< conversion factor for milliseconds to seconds
00200   static const long us_per_ms=1000;     //!< conversion factor for microseconds to milliseconds
00201   static const long ns_per_us=1000;     //!< conversion factor for nanoseconds to microseconds
00202   
00203 protected:
00204   timeval tv; //!< stores the time
00205   static struct timezone tz; //!< stores the timezone (not really used)
00206 };
00207 
00208 //@{
00209 //!for doing doing math with time
00210 inline TimeET operator+(long t1, const TimeET& t2) { return TimeET(t1)+t2; }
00211 inline TimeET operator-(long t1, const TimeET& t2) { return TimeET(t1)-t2; }
00212 inline TimeET operator+(double t1, const TimeET& t2) { return TimeET(t1)+t2; }
00213 inline TimeET operator-(double t1, const TimeET& t2) { return TimeET(t1)-t2; }
00214 inline TimeET operator*(double x, const TimeET& t) { return t*x; }
00215 //@}
00216 
00217 //! displays the value as text: secs~usecs
00218 inline std::ostream& operator<<(std::ostream& o, const TimeET& t) {
00219   o << t.tv.tv_sec << '.';
00220   o.width(6);
00221   o.fill('0');
00222   o << t.tv.tv_usec;
00223   o.fill(' ');
00224   return o;
00225 }
00226 
00227 /*! @file
00228 * @brief Describes TimeET, a nice class for handling time values with high precision
00229 * @author ejt (Creator)
00230 */
00231 
00232 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:52 2016 by Doxygen 1.6.3