Go to the documentation of this file.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
00032
00033
00034
00035
00036
00037 #ifndef TIMESTAMP_ERIK_H__
00038 #define TIMESTAMP_ERIK_H__
00039
00040 #include <sys/time.h>
00041 #include <iostream>
00042 #include <stdio.h>
00043
00044 namespace util{class timestamp;}
00045
00046 static std::ostream& operator<< (std::ostream&, const util::timestamp&);
00047
00048 namespace util
00049 {
00078 class timestamp
00079 {
00080 int seconds;
00081 int microseconds;
00082
00087 void fixme()
00088 {
00089 if (microseconds < 0)
00090 {
00091 seconds --;
00092 microseconds += 1000000;
00093 }
00094 else if (microseconds > 1000000)
00095 {
00096 seconds ++;
00097 microseconds -= 1000000;
00098 }
00099 }
00100
00101 public:
00102
00104 timestamp()
00105 {
00106 struct timeval tv;
00107 gettimeofday (&tv, NULL);
00108 seconds = tv.tv_sec;
00109 microseconds = tv.tv_usec;
00110 }
00111
00113 timestamp(int s, int us)
00114 :seconds(s), microseconds(us)
00115 {}
00116
00117
00118 timestamp operator+ (const timestamp& b)
00119 {
00120 timestamp ret (this->seconds + b.seconds,
00121 this->microseconds+b.microseconds);
00122 ret.fixme();
00123 return ret;
00124 }
00125
00126 timestamp operator- (const timestamp& b)
00127 {
00128 timestamp ret (this->seconds - b.seconds,
00129 this->microseconds - b.microseconds);
00130 ret.fixme();
00131 return ret;
00132 }
00133
00134 const timestamp& operator += (const timestamp& b)
00135 {
00136 this->seconds += b.seconds;
00137 this->microseconds += b.microseconds;
00138
00139 this->fixme();
00140 return *this;
00141 }
00142
00143 const timestamp& operator -= (const timestamp& b)
00144 {
00145 this->seconds -= b.seconds;
00146 this->microseconds -= b.microseconds;
00147
00148 this->fixme();
00149 return *this;
00150 }
00151
00152 friend std::ostream& ::operator<< (std::ostream&, const util::timestamp&);
00153 };
00154
00155 }
00156
00157 static std::ostream& operator<< (std::ostream& out, const util::timestamp& t)
00158 {
00159 char microsecval[7];
00160
00161 out<<t.seconds<<'.';
00162
00163 sprintf(microsecval, "%06d", t.microseconds);
00164
00165 out<<microsecval;
00166
00167 return out;
00168 }
00169
00170
00171 #endif