common/omtime.h

Go to the documentation of this file.
00001 /* omtime.h: Class representing a time to subsecond accuracy
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2002 Ananova Ltd
00005  * Copyright 2003,2005,2006 Olly Betts
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
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>   /* time */
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 /* OM_HGUARD_OMTIME_H */

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.