00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: TimeStamp.cpp 1230 2014-11-16 02:32:01Z sella $";
00010
00011 #include "TimeStamp.h"
00012
00013 #include <sys/time.h>
00014 #include <stdint.h>
00015 #include <stdlib.h>
00016
00017 using namespace sella::util;
00018
00019 TimeStamp::TimeStamp(const struct timeval &tv) throw () :
00020 stamp((tv.tv_sec * 1000000) + tv.tv_usec)
00021 { }
00022
00023 TimeStamp::TimeStamp(int64_t usec) throw () :
00024 stamp(usec)
00025 { }
00026
00027 TimeStamp::TimeStamp() throw () :
00028 stamp(TimeStamp::now())
00029 { }
00030
00031 TimeStamp::TimeStamp(const TimeStamp &other) throw () :
00032 stamp(other.stamp)
00033 { }
00034
00035 TimeStamp::~TimeStamp() throw () { }
00036
00037 TimeStamp & TimeStamp::operator = (const TimeStamp &other) throw () {
00038 if (this != &other) {
00039 this->stamp = other.stamp;
00040 }
00041 return *this;
00042 }
00043
00044 TimeStamp & TimeStamp::operator = (const struct timeval &tv) throw () {
00045 this->stamp = (tv.tv_sec * 1000000) + tv.tv_usec;
00046
00047 return *this;
00048 }
00049
00050 TimeStamp & TimeStamp::operator = (int64_t micro) throw () {
00051 this->stamp = micro;
00052
00053 return *this;
00054 }
00055
00056 TimeStamp & TimeStamp::operator += (const TimeStamp &other) throw () {
00057 if (this != &other) {
00058 this->stamp += other.stamp;
00059 }
00060 return *this;
00061 }
00062
00063 TimeStamp & TimeStamp::operator += (const struct timeval &tv) throw () {
00064 this->stamp += (tv.tv_sec * 1000000) + tv.tv_usec;
00065
00066 return *this;
00067 }
00068
00069 TimeStamp & TimeStamp::operator += (int64_t micro) throw () {
00070 this->stamp += micro;
00071
00072 return *this;
00073 }
00074
00075 TimeStamp & TimeStamp::operator -= (const TimeStamp &other) throw () {
00076 if (this != &other) {
00077 this->stamp -= other.stamp;
00078 }
00079 return *this;
00080 }
00081
00082 TimeStamp & TimeStamp::operator -= (const struct timeval &tv) throw () {
00083 this->stamp -= (tv.tv_sec * 1000000) + tv.tv_usec;
00084
00085 return *this;
00086 }
00087
00088 TimeStamp & TimeStamp::operator -= (int64_t micro) throw () {
00089 this->stamp -= micro;
00090
00091 return *this;
00092 }
00093
00094 TimeStamp & TimeStamp::operator *= (const TimeStamp &other) throw () {
00095 if (this != &other) {
00096 this->stamp *= other.stamp;
00097 }
00098 return *this;
00099 }
00100
00101 TimeStamp & TimeStamp::operator *= (const struct timeval &tv) throw () {
00102 this->stamp *= (tv.tv_sec * 1000000) + tv.tv_usec;
00103
00104 return *this;
00105 }
00106
00107 TimeStamp & TimeStamp::operator *= (int64_t micro) throw () {
00108 this->stamp *= micro;
00109
00110 return *this;
00111 }
00112
00113 TimeStamp & TimeStamp::operator /= (const TimeStamp &other) throw () {
00114 if (this != &other) {
00115 this->stamp /= other.stamp;
00116 }
00117 return *this;
00118 }
00119
00120 TimeStamp & TimeStamp::operator /= (const struct timeval &tv) throw () {
00121 this->stamp /= (tv.tv_sec * 1000000) + tv.tv_usec;
00122
00123 return *this;
00124 }
00125
00126 TimeStamp & TimeStamp::operator /= (int64_t micro) throw () {
00127 this->stamp /= micro;
00128
00129 return *this;
00130 }
00131
00132 TimeStamp TimeStamp::operator + (const TimeStamp &other) throw () {
00133 return TimeStamp(this->stamp + other.stamp);
00134 }
00135
00136 TimeStamp TimeStamp::operator + (const struct timeval &tv) throw () {
00137 return TimeStamp(this->stamp + ((tv.tv_sec * 1000000) + tv.tv_usec));
00138 }
00139
00140 TimeStamp TimeStamp::operator + (int64_t micro) throw () {
00141 return TimeStamp(this->stamp + micro);
00142 }
00143
00144 TimeStamp TimeStamp::operator - (const TimeStamp &other) throw () {
00145 return TimeStamp(this->stamp - other.stamp);
00146 }
00147
00148 TimeStamp TimeStamp::operator - (const struct timeval &tv) throw () {
00149 return TimeStamp(this->stamp - ((tv.tv_sec * 1000000) + tv.tv_usec));
00150 }
00151
00152 TimeStamp TimeStamp::operator - (int64_t micro) throw () {
00153 return TimeStamp(this->stamp - micro);
00154 }
00155
00156 TimeStamp TimeStamp::operator * (const TimeStamp &other) throw () {
00157 return TimeStamp(this->stamp * other.stamp);
00158 }
00159
00160 TimeStamp TimeStamp::operator * (const struct timeval &tv) throw () {
00161 return TimeStamp(this->stamp * ((tv.tv_sec * 1000000) + tv.tv_usec));
00162 }
00163
00164 TimeStamp TimeStamp::operator * (int64_t micro) throw () {
00165 return TimeStamp(this->stamp * micro);
00166 }
00167
00168 TimeStamp TimeStamp::operator / (const TimeStamp &other) throw () {
00169 return TimeStamp(this->stamp / other.stamp);
00170 }
00171
00172 TimeStamp TimeStamp::operator / (const struct timeval &tv) throw () {
00173 return TimeStamp(this->stamp / ((tv.tv_sec * 1000000) + tv.tv_usec));
00174 }
00175
00176 TimeStamp TimeStamp::operator / (int64_t micro) throw () {
00177 return TimeStamp(this->stamp / micro);
00178 }
00179
00180 bool TimeStamp::operator == (const TimeStamp &other) const throw () {
00181 return this->stamp == other.stamp;
00182 }
00183
00184 bool TimeStamp::operator != (const TimeStamp &other) const throw () {
00185 return this->stamp != other.stamp;
00186 }
00187
00188 bool TimeStamp::operator >= (const TimeStamp &other) const throw () {
00189 return this->stamp >= other.stamp;
00190 }
00191
00192 bool TimeStamp::operator <= (const TimeStamp &other) const throw () {
00193 return this->stamp <= other.stamp;
00194 }
00195
00196 bool TimeStamp::operator > (const TimeStamp &other) const throw () {
00197 return this->stamp > other.stamp;
00198 }
00199
00200 bool TimeStamp::operator < (const TimeStamp &other) const throw () {
00201 return this->stamp < other.stamp;
00202 }
00203
00204 void TimeStamp::update() throw () {
00205 this->stamp = TimeStamp::now();
00206 }
00207
00208 int64_t TimeStamp::now() throw () {
00209 struct timeval tv;
00210
00211 gettimeofday(&tv, NULL);
00212
00213 return (tv.tv_sec * 1000000) + tv.tv_usec;
00214 }
00215
00216 int64_t TimeStamp::getEpochMicroseconds() const throw () {
00217 return this->stamp;
00218 }
00219
00220 int64_t TimeStamp::getEpochSeconds() const throw () {
00221 return this->stamp / 1000000;
00222 }
00223
00224
00225
00226