00001 #ifndef ROBOT_TIME_H 00002 #define ROBOT_TIME_H 00003 00029 //$Id: Time.H 63 2007-01-18 21:39:32Z fwph $ 00030 #include <time.h> 00031 #include <sys/time.h> 00032 00033 namespace WURDE{ 00034 00044 class Time { 00045 00046 public: 00050 Time(); 00051 /* Copy constructor.*/ 00052 Time(const Time &m); 00057 Time(time_t value); 00061 Time(struct timeval _tv); 00062 00063 Time(struct timespec _ts); 00064 ~Time(); 00065 00066 /* Operators for comparison with other Time objects. These 00067 mostly ignore the timezone value, as Linux does not generally 00068 use it, and it should be irrelevant for our purposes anyway.*/ 00069 const Time &operator=(const Time &right); 00070 bool operator==(const Time &right) const; 00071 bool operator!=(const Time &right) const; 00072 bool operator<(const Time &right) const; 00073 bool operator>(const Time &right) const; 00074 bool operator<=(const Time &right) const; 00075 bool operator>=(const Time &right) const; 00076 Time operator+(const Time &right) const; 00077 Time operator-(const Time &right) const; 00078 00079 /* Operators for comparison with struct timevals */ 00080 const Time &operator=(const struct timeval &right); 00081 bool operator==(const struct timeval &right) const; 00082 bool operator!=(const struct timeval &right) const; 00083 bool operator<(const struct timeval &right) const; 00084 bool operator>(const struct timeval &right) const; 00085 bool operator<=(const struct timeval &right) const; 00086 bool operator>=(const struct timeval &right) const; 00087 Time operator+(const struct timeval &right) const; 00088 Time operator-(const struct timeval &right) const; 00089 00090 /* Operators for comparison with time_t values */ 00091 const Time &operator=(const time_t &right); 00092 bool operator==(const time_t &right) const; 00093 bool operator!=(const time_t &right) const; 00094 bool operator<(const time_t &right) const; 00095 bool operator>(const time_t &right) const; 00096 bool operator<=(const time_t &right) const; 00097 bool operator>=(const time_t &right) const; 00098 Time operator+(const time_t &right) const; 00099 Time operator-(const time_t &right) const; 00100 00101 /* Operators for use with struct timespec */ 00102 const Time &operator=(const struct timespec &right); 00103 bool operator==(const struct timespec &right) const; 00104 bool operator!=(const struct timespec &right) const; 00105 bool operator<(const struct timespec &right) const; 00106 bool operator>(const struct timespec &right) const; 00107 bool operator<=(const struct timespec &right) const; 00108 bool operator>=(const struct timespec &right) const; 00109 Time operator+(const struct timespec &right) const; 00110 Time operator-(const struct timespec &right) const; 00111 00112 00116 void now(){gettimeofday(&m_tv,&m_tz);} 00120 void zero(){m_tv.tv_sec=0;m_tv.tv_usec=0;} 00121 00125 time_t getSeconds() const {return m_tv.tv_sec;} 00126 00130 time_t getUSeconds() const { return m_tv.tv_usec; } 00131 00132 00136 void setSeconds(time_t sec){ m_tv.tv_sec=sec;} 00137 00141 void setUSeconds(time_t usec){m_tv.tv_usec=usec;} 00142 00146 struct timeval getTimeval(){return m_tv;} 00147 00148 struct timespec timespec(){ 00149 struct timespec retval; 00150 retval.tv_sec=m_tv.tv_sec; 00151 retval.tv_nsec=m_tv.tv_usec*1000; 00152 return retval; 00153 } 00154 00155 protected: 00156 /* Time value member variable */ 00157 struct timeval m_tv; 00158 /* Time zone member variable */ 00159 struct timezone m_tz; 00160 00161 }; 00162 00163 }; 00164 #endif