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