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
00031 #include "Time.h"
00032 #include <ostream>
00033
00034 const long Time::MILLION = 1000000;
00035
00036 Time::Time( const struct timeval& t )
00037 {
00038 tv.tv_sec = t.tv_sec;
00039 tv.tv_usec = t.tv_usec;
00040 fix();
00041 }
00042
00043 Time::Time( long sec, long usec )
00044 {
00045 tv.tv_sec = sec;
00046 tv.tv_usec = usec;
00047 fix();
00048 }
00049
00050 Time::Time( const Time& t )
00051 {
00052 tv.tv_sec = t.tv.tv_sec;
00053 tv.tv_usec = t.tv.tv_usec;
00054 }
00055
00056 void
00057 Time::operator=( const Time& t )
00058 {
00059 if ( &t == this ) return;
00060 tv.tv_sec = t.tv.tv_sec;
00061 tv.tv_usec = t.tv.tv_usec;
00062 }
00063
00064 Time
00065 Time::operator+(const Time& t) const
00066 {
00067 return Time( tv.tv_sec + t.tv.tv_sec , tv.tv_usec + t.tv.tv_usec );
00068 }
00069
00070 Time
00071 Time::operator-(const Time& t) const
00072 {
00073 return Time( tv.tv_sec - t.tv.tv_sec , tv.tv_usec - t.tv.tv_usec );
00074 }
00075
00076 Time
00077 Time::operator*(long s) const
00078 {
00079 return Time( tv.tv_sec * s , tv.tv_usec * s );
00080 }
00081
00082 Time
00083 Time::operator/(long s) const
00084 {
00085
00086 return Time( tv.tv_sec / s ,
00087 ( MILLION * ( tv.tv_sec % s ) + tv.tv_usec ) / s );
00088 }
00089
00090 Time&
00091 Time::operator+=(const Time& t)
00092 {
00093 tv.tv_sec += t.tv.tv_sec;
00094 tv.tv_usec += t.tv.tv_usec;
00095 fix();
00096 return *this;
00097 }
00098
00099 Time&
00100 Time::operator-=(const Time& t)
00101 {
00102 tv.tv_sec -= t.tv.tv_sec;
00103 tv.tv_usec -= t.tv.tv_usec;
00104 fix();
00105 return *this;
00106 }
00107
00108 Time&
00109 Time::operator*=(long s)
00110 {
00111 tv.tv_sec *= s;
00112 tv.tv_usec *= s;
00113 fix();
00114 return *this;
00115 }
00116
00117 bool
00118 Time::operator==(const Time& t) const
00119 {
00120 if ( tv.tv_sec != t.tv.tv_sec ) return false;
00121 if ( tv.tv_usec != t.tv.tv_usec ) return false;
00122 return true;
00123 }
00124
00125 bool
00126 Time::operator<(const Time& t) const
00127 {
00128 if ( tv.tv_sec < t.tv.tv_sec ) return true;
00129 if ( tv.tv_sec > t.tv.tv_sec ) return false;
00130 if ( tv.tv_usec < t.tv.tv_usec ) return true;
00131 return false;
00132 }
00133
00134 bool
00135 Time::operator>(const Time& t) const
00136 {
00137 if ( tv.tv_sec > t.tv.tv_sec ) return true;
00138 if ( tv.tv_sec < t.tv.tv_sec ) return false;
00139 if ( tv.tv_usec > t.tv.tv_usec ) return true;
00140 return false;
00141 }
00142
00143 bool
00144 Time::operator<=(const Time& t) const
00145 {
00146 return ! operator>(t);
00147 }
00148
00149 bool
00150 Time::operator>=(const Time& t) const
00151 {
00152 return ! operator<(t);
00153 }
00154
00155
00156 void
00157 Time::fix()
00158 {
00159 if ( ( tv.tv_usec > MILLION ) || ( tv.tv_usec < -MILLION ) )
00160 {
00161
00162 tv.tv_sec += ( tv.tv_usec / MILLION );
00163 tv.tv_usec %= MILLION;
00164 }
00165 if ( tv.tv_sec >= 0 && tv.tv_usec < 0 )
00166 {
00167 tv.tv_usec += MILLION;
00168 tv.tv_sec -= 1;
00169 }
00170 else if ( tv.tv_sec < 0 && tv.tv_usec > 0 )
00171 {
00172 tv.tv_usec -= MILLION;
00173 tv.tv_sec += 1;
00174 }
00175 }
00176
00177 Time
00178 operator+(const struct timeval& a, const Time& b)
00179 {
00180 return Time( a.tv_sec + b.tv.tv_sec , a.tv_usec + b.tv.tv_usec );
00181 }
00182
00183 Time
00184 operator-(const struct timeval& a, const Time& b)
00185 {
00186 return Time( a.tv_sec - b.tv.tv_sec , a.tv_usec - b.tv.tv_usec );
00187 }
00188
00189 Time
00190 operator*(long a, const Time& b)
00191 {
00192 return Time( a * b.tv.tv_sec , a * b.tv.tv_usec );
00193 }
00194
00195 bool
00196 operator==(const struct timeval& a, const Time& b)
00197 {
00198 if ( a.tv_sec != b.tv.tv_sec ) return false;
00199 if ( a.tv_usec != b.tv.tv_usec ) return false;
00200 return true;
00201 }
00202
00203 bool
00204 operator<(const struct timeval& a, const Time& b)
00205 {
00206 if ( a.tv_sec < b.tv.tv_sec ) return true;
00207 if ( a.tv_sec > b.tv.tv_sec ) return false;
00208 if ( a.tv_usec < b.tv.tv_usec ) return true;
00209 return false;
00210 }
00211
00212 bool
00213 operator>(const struct timeval& a, const Time& b)
00214 {
00215 if ( a.tv_sec > b.tv.tv_sec ) return true;
00216 if ( a.tv_sec < b.tv.tv_sec ) return false;
00217 if ( a.tv_usec > b.tv.tv_usec ) return true;
00218 return false;
00219 }
00220
00221 std::ostream& operator<<(std::ostream& stream, const Time& t)
00222 {
00223 stream << "{" << t.tv.tv_sec << "," << t.tv.tv_usec << "}";
00224 return stream;
00225 }