Homepage Demos Overview Downloads Tutorials Reference
Credits
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Search

TimeET.h

Go to the documentation of this file.
00001 #ifndef __TIME_ET_CLASS__
00002 #define __TIME_ET_CLASS__
00003 
00004 #include <iostream>
00005 #ifndef PLATFORM_APERIOS
00006 #include <sys/time.h>
00007 #else
00008 #include <MCOOP.h>
00009 //!would be defined by system - we redefine the same structure in case we're compiling for Aperios
00010 struct timeval {
00011   unsigned int tv_sec; //!< seconds
00012   long tv_usec;        //!< microseconds
00013 };
00014 //!would be defined by system - we redefine the same structure in case we're compiling for Aperios
00015 struct timezone {
00016   int     tz_minuteswest; //!< minutes west of Greenwich
00017   int     tz_dsttime;     //!< type of dst correction
00018 };
00019 #endif
00020 
00021 //!a nice class for handling time values with high precision
00022 /*@test negative times might not be handled properly */
00023 class TimeET {
00024   //! lets the class be displayed easily
00025   friend std::ostream& operator<<(std::ostream& o, const TimeET& t);
00026  public:
00027   //@{
00028   //!constructor
00029   TimeET() : tv() {
00030     Set();
00031   }
00032   TimeET(long ms) : tv() {
00033     Set(ms);
00034   }
00035   TimeET(long sec, long usec) : tv() {
00036     Set(sec,usec);
00037   }
00038   //!constructor, sepecify @a t seconds
00039   TimeET(double t) :tv() {
00040     Set(t);
00041   }
00042   //@}
00043 
00044   //!returns the difference between the current time and the time stored
00045   inline TimeET Age() const { return TimeET()-(*this); }
00046   //@{
00047   //!returns the time stored as seconds in a double
00048   inline double Value() const { return (double)tv.tv_sec+(double)tv.tv_usec/(double)us_per_sec; }
00049   //@}
00050   
00051   //@{
00052   //!sets the time stored in the class
00053   inline void Set(long ms) {
00054     Set(0,ms*us_per_ms);
00055   }
00056   inline void Set(long sec, long usec) {
00057     tv.tv_sec=sec+usec/us_per_sec;
00058     tv.tv_usec=usec%us_per_sec;;
00059   }
00060   inline void Set(double t) {
00061     tv.tv_sec=(long)t;
00062     tv.tv_usec=(long)((t-tv.tv_sec)*us_per_sec);
00063   }
00064   /*!@brief sets the time to the current time
00065    * @todo not getting timeofday on OPEN-R, is time since boot instead...*/
00066   inline void Set() {
00067 #ifndef PLATFORM_APERIOS
00068     gettimeofday(&tv,&tz);
00069 #else
00070     static struct SystemTime t;
00071     GetSystemTime(&t);
00072     Set(t.seconds,t.useconds);
00073 #endif
00074   }
00075   //@}
00076 
00077   //@{
00078   //!for comparing times
00079   inline bool operator<(long ms) { //what if ms is negative?
00080     unsigned int sec = ms/ms_per_sec;
00081     return tv.tv_sec<sec || tv.tv_sec==sec && tv.tv_usec<static_cast<long>((ms-sec*ms_per_sec)*us_per_ms);
00082   }
00083   inline bool operator<(double t) {
00084     return Value()<t;
00085   }
00086   inline bool operator<(const TimeET& t) {
00087     return tv.tv_sec<t.tv.tv_sec || tv.tv_sec==t.tv.tv_sec && tv.tv_usec<t.tv.tv_usec;
00088   }
00089   //@}
00090 
00091   //@{
00092   //!for doing doing math with time
00093   inline TimeET operator+(const TimeET& t) const {
00094     long usec = tv.tv_usec+t.tv.tv_usec;
00095     long sec = tv.tv_sec+t.tv.tv_sec+usec/us_per_sec;
00096     usec%=us_per_sec;
00097     return TimeET(sec,usec);
00098   }
00099   inline TimeET operator+=(const TimeET& t) {
00100     tv.tv_usec+=t.tv.tv_usec;
00101     tv.tv_sec+=t.tv.tv_sec+tv.tv_usec/us_per_sec;
00102     tv.tv_usec%=us_per_sec;
00103     return *this;
00104   }
00105   inline TimeET operator-(const TimeET& t) const {
00106     long usec = tv.tv_usec-t.tv.tv_usec;
00107     long sec = tv.tv_sec-t.tv.tv_sec+usec/us_per_sec;
00108     usec%=us_per_sec;
00109     if(usec<0) {
00110       usec+=us_per_sec;
00111       sec--;
00112     }
00113     return TimeET(sec,usec);
00114   }
00115   inline TimeET operator-=(const TimeET& t) {
00116     tv.tv_usec-=t.tv.tv_usec;
00117     tv.tv_sec=tv.tv_sec-t.tv.tv_sec+tv.tv_usec/us_per_sec;
00118     tv.tv_usec%=us_per_sec;
00119     if(tv.tv_usec<0) {
00120       tv.tv_usec+=us_per_sec;
00121       tv.tv_sec--;
00122     }
00123     return *this;
00124   }
00125   //@}
00126 
00127   static const long us_per_sec=1000000; //!< conversion factor for microseconds to seconds
00128   static const long ms_per_sec=1000;    //!< conversion factor for milliseconds to seconds
00129   static const long us_per_ms=1000;     //!< conversion factor for microseconds to milliseconds
00130  protected:
00131   timeval tv; //!< stores the time
00132   static struct timezone tz; //!< stores the timezone (not really used)
00133 };
00134 
00135 //@{
00136 //!for doing doing math with time
00137 inline TimeET operator+(long t1, const TimeET& t2) { return TimeET(t1)+t2; }
00138 inline TimeET operator-(double t1, const TimeET& t2) { return TimeET(t1)-t2; }
00139 //@}
00140 
00141 //! displays the value as text: secs~usecs
00142 inline std::ostream& operator<<(std::ostream& o, const TimeET& t) {
00143   return (o << t.tv.tv_sec << '~' << t.tv.tv_usec);
00144 }
00145 
00146 /*! @file
00147  * @brief Describes TimeET, a nice class for handling time values with high precision
00148  * @author ejt (Creator)
00149  *
00150  * $Author: ejt $
00151  * $Name: tekkotsu-1_4_1 $
00152  * $Revision: 1.2 $
00153  * $State: Exp $
00154  * $Date: 2003/04/18 05:15:11 $
00155  */
00156 
00157 #endif

Tekkotsu v1.4
Generated Sat Jul 19 00:06:32 2003 by Doxygen 1.3.2