00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef OM_HGUARD_OMTIME_H
00024 #define OM_HGUARD_OMTIME_H
00025
00026 #ifdef HAVE_GETTIMEOFDAY
00027 #include <sys/time.h>
00028 #include <unistd.h>
00029 #endif
00030 #ifdef HAVE_FTIME
00031 #include <sys/timeb.h>
00032 #endif
00033 #include <time.h>
00034
00035 #include <xapian/types.h>
00036
00038 class OmTime {
00039 public:
00041 OmTime() : sec(0), usec(0) {}
00042
00044 static OmTime now();
00045
00047 OmTime(long int msec) : sec(msec / 1000), usec((msec % 1000) * 1000) {}
00048 OmTime(long int sec_, long int usec_) : sec(sec_), usec(usec_) {}
00049
00050 void operator+= (Xapian::timeout msecs) {
00051 usec += msecs * 1000;
00052 sec += usec / 1000000;
00053 usec %= 1000000;
00054 }
00055
00056 void operator+= (const OmTime &add) {
00057 usec += add.usec;
00058 sec += add.sec + usec / 1000000;
00059 usec %= 1000000;
00060 }
00061
00062 OmTime operator+ (Xapian::timeout msecs) const {
00063 OmTime result;
00064 result.usec = usec + msecs * 1000;
00065 result.sec = sec + result.usec / 1000000;
00066 result.usec %= 1000000;
00067 return result;
00068 }
00069
00070 OmTime operator+ (const OmTime &add) const {
00071 OmTime result;
00072 result.usec = usec + add.usec;
00073 result.sec = sec + add.sec + result.usec / 1000000;
00074 result.usec %= 1000000;
00075 return result;
00076 }
00077
00078 OmTime operator- (const OmTime &sub) const {
00079 OmTime result;
00080 result.usec = usec - sub.usec;
00081 result.sec = sec - sub.sec;
00082 if (result.usec < 0) {
00083 result.usec += 1000000;
00084 result.sec -= 1;
00085 }
00086 return result;
00087 }
00088
00089 bool operator> (const OmTime &rhs) const {
00090 if (sec > rhs.sec) return true;
00091 if (sec < rhs.sec) return false;
00092 return (usec > rhs.usec);
00093 }
00094
00095 bool is_set() const { return sec != 0 || usec != 0; }
00096
00097 double as_double() const {
00098 return double(sec) + (double(usec) / 1000000.0);
00099 }
00100
00101 long int sec;
00102 long int usec;
00103 };
00104
00105 inline OmTime
00106 OmTime::now()
00107 {
00108 OmTime result;
00109 #ifdef HAVE_GETTIMEOFDAY
00110 struct timeval tv;
00111 if (gettimeofday(&tv, 0) == 0) {
00112 result.sec = tv.tv_sec;
00113 result.usec = tv.tv_usec;
00114 return result;
00115 }
00116 #endif
00117 #ifdef FTIME_RETURNS_VOID
00118 struct timeb tp;
00119 ftime(&tp);
00120 result.sec = tp.time;
00121 result.usec = tp.millitm * 1000;
00122 return result;
00123 #else
00124 # ifdef HAVE_FTIME
00125 struct timeb tp;
00126 if (ftime(&tp) == 0) {
00127 result.sec = tp.time;
00128 result.usec = tp.millitm * 1000;
00129 return result;
00130 }
00131 # endif
00132 result.sec = time(NULL);
00133 result.usec = 0;
00134 return result;
00135 #endif
00136 }
00137
00138 #endif