00001 00025 /* === S T A R T =========================================================== */ 00026 00027 #ifndef __ETL__CLOCK_GETTIMEOFDAY_H 00028 #define __ETL__CLOCK_GETTIMEOFDAY_H 00029 00030 /* === H E A D E R S ======================================================= */ 00031 00032 #include <sys/time.h> 00033 #include <cmath> 00034 00035 /* === M A C R O S ========================================================= */ 00036 00037 /* === T Y P E D E F S ===================================================== */ 00038 00039 /* === C L A S S E S & S T R U C T S ======================================= */ 00040 00041 _ETL_BEGIN_NAMESPACE 00042 00043 class clock_desc_gettimeofday 00044 { 00045 public: 00046 typedef double value_type; 00047 00048 inline static bool realtime() 00049 { return true; } 00050 00051 inline static bool proctime() 00052 { return false; } 00053 00054 inline static value_type 00055 one_second() 00056 { return 1.0f; } 00057 00058 inline static value_type precision() 00059 { return one_second()/(value_type)1000000.0f; } 00060 00061 inline static const char *description() 00062 { return "UNIX gettimeofday()"; }; 00063 00064 protected: 00065 class timestamp : public timeval 00066 { 00067 timestamp(int sec, int usec) 00068 { tv_sec=sec; tv_usec=usec; } 00069 00070 friend class clock_desc_gettimeofday; 00071 public: 00072 timestamp() { } 00073 00074 00075 inline timestamp operator-(const timestamp &rhs)const 00076 { 00077 timestamp ret; 00078 ret.tv_usec=tv_usec-rhs.tv_usec; 00079 00080 if(ret.tv_usec<0) 00081 { 00082 ret.tv_sec=tv_sec-rhs.tv_sec-1; 00083 ret.tv_usec+=1000000; 00084 } 00085 else 00086 ret.tv_sec=tv_sec-rhs.tv_sec; 00087 return ret; 00088 } 00089 00090 inline timestamp operator+(timestamp rhs)const 00091 { 00092 rhs.tv_usec+=tv_usec; 00093 00094 if(rhs.tv_usec>1000000) 00095 { 00096 rhs.tv_sec+=tv_sec+1; 00097 rhs.tv_usec-=1000000; 00098 } 00099 else 00100 rhs.tv_sec+=tv_sec; 00101 return rhs; 00102 } 00103 00104 inline bool operator<(const timestamp &rhs)const 00105 { return tv_sec<rhs.tv_sec || tv_usec<rhs.tv_usec; } 00106 00107 inline bool operator==(const timestamp &rhs)const 00108 { return tv_usec==rhs.tv_usec && tv_sec==rhs.tv_sec; } 00109 00110 inline bool operator!=(const timestamp &rhs)const 00111 { return tv_usec!=rhs.tv_usec || tv_sec!=rhs.tv_sec; } 00112 }; 00113 00114 static void 00115 get_current_time(timestamp &x) 00116 { gettimeofday(&x,NULL);} 00117 00118 static timestamp 00119 get_current_time() 00120 { timestamp ret; get_current_time(ret); return ret; } 00121 00122 static value_type 00123 timestamp_to_seconds(const timestamp &x) 00124 { return (value_type)x.tv_sec + precision()*x.tv_usec; } 00125 00126 static timestamp 00127 seconds_to_timestamp(const value_type &x) 00128 { return timestamp((int)floor(x), (int)((x-floor(x))/precision()+0.5)); } 00129 }; 00130 00131 _ETL_END_NAMESPACE 00132 00133 /* === E N D =============================================================== */ 00134 00135 #endif 00136