00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #ifndef STIME_H
00031 #define STIME_H
00032 
00033 #include "w_defines.h"
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 extern "C" {
00044 #include <sys/time.h>
00045 }
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 #if     HAVE_CLOCK_GETTIME
00069 #ifndef USE_POSIX_TIME
00070 #define USE_POSIX_TIME 1
00071 #endif
00072 #elif   !HAVE_GETTIMEOFDAY
00073 #error  No suitable get-time system call. Cannot build.
00074 #endif
00075 
00076 class stime_t {
00077 protected:
00078 #ifdef USE_POSIX_TIME
00079     struct    timespec    _time;
00080 #else
00081     struct    timeval        _time;
00082 #endif
00083 
00084     
00085     void    gettime();
00086 
00087     void signs();
00088     void _normalize();
00089     void normalize();
00090 
00091     
00092     stime_t(time_t, long);        
00093 
00094 public:
00095     stime_t() {
00096         _time.tv_sec = 0;
00097 #ifdef USE_POSIX_TIME
00098         _time.tv_nsec = 0;
00099 #else
00100         _time.tv_usec = 0;
00101 #endif
00102     }
00103 
00104 #ifdef USE_POSIX_TIME
00105     stime_t(const struct timespec &ts);
00106 #endif
00107     stime_t(const struct timeval &tv);
00108 
00109     
00110     stime_t(int);
00111     stime_t(long);
00112 
00113     
00114     stime_t(double);
00115 
00116     
00117     bool    operator==(const stime_t &) const;
00118     bool    operator<(const stime_t &) const;
00119     bool    operator<=(const stime_t &) const;
00120     
00121     bool    operator!=(const stime_t &r) const { return !(*this == r); }
00122     bool    operator>(const stime_t &r) const { return !(*this <= r); }
00123     bool    operator>=(const stime_t &r) const { return !(*this < r); }
00124 
00125     
00126     stime_t    operator-() const;
00127 
00128     
00129     stime_t    operator+(const stime_t &r) const;
00130     stime_t    operator-(const stime_t &r) const;
00131 
00132     
00133      
00134     stime_t operator*(const int factor) const;
00135     stime_t operator/(const int factor) const;
00136     stime_t operator*(const double factor) const;
00137     stime_t operator/(const double factor) const;
00138 
00139     
00140     stime_t &operator+=(const stime_t &r);
00141     stime_t &operator-=(const stime_t &r);
00142 
00143      
00144 
00145     
00146 
00147     operator double() const;
00148     operator float() const;
00149 #ifdef USE_POSIX_TIME
00150     operator struct timespec() const;
00151 #endif
00152 
00153     
00154     operator struct timeval() const;
00155     
00156     
00157     
00158     long    secs() const;        
00159     long    msecs() const;        
00160     long    usecs() const;        
00161     long    nsecs() const;        
00162 
00163     
00164     static    stime_t sec(int seconds); 
00165     static    stime_t    usec(int micro_seconds, int seconds = 0);
00166     static    stime_t    msec(int milli_seconds, int seconds = 0);
00167     static    stime_t    nsec(int nano_seconds, int seconds = 0);
00168 
00169     
00170     static    stime_t now();
00171 
00172     ostream    &print(ostream &s) const;
00173     ostream &ctime(ostream &s) const;
00174 };
00175 
00176 
00177 
00178 
00179 
00180 class sinterval_t : public stime_t {
00181 public:
00182     
00183 
00184 
00185     sinterval_t() : stime_t() { }
00186 #ifdef USE_POSIX_TIME
00187     sinterval_t(const struct timespec &ts) : stime_t(ts) { }
00188 #endif
00189     sinterval_t(const struct timeval &tv) : stime_t(tv) { }
00190 
00191     sinterval_t(const stime_t &time) : stime_t(time) { }
00192     sinterval_t(int time) : stime_t(time) { }
00193     sinterval_t(long time) : stime_t(time) { }
00194     sinterval_t(double time) : stime_t(time) { }
00195 
00196     ostream    &print(ostream &s) const;
00197 };
00198 
00199 
00200 extern ostream &operator<<(ostream &s, const stime_t &t);
00201 extern ostream &operator<<(ostream &s, const sinterval_t &t);
00202 
00203 
00204 
00205 
00206 
00207 #endif