00001 // 00002 // Time.cpp 00003 // 00004 00005 00006 #include "sick_scan/tcp/Time.hpp" 00007 #include <sys/time.h> 00008 #include <time.h> 00009 #include "sick_scan/tcp/toolbox.hpp" // fuer "::toString()" 00010 00011 // 00012 // Duration 00013 // 00014 TimeDuration::TimeDuration() 00015 { 00016 m_duration = 0.0; 00017 } 00018 00019 00020 const double Time::m_secondFractionNTPtoNanoseconds (0.2328306436538696); // = 2^-32 * 1e9 00021 const double Time::m_nanosecondsToSecondFractionNTP (4.29496729600000); // = 2^32 * 1e-9 00022 const UINT64 Time::secondsFrom1900to1970 (2208988800UL); 00023 00024 // 00025 // Time 00026 // 00027 Time::Time() 00028 { 00029 set(0.0); 00030 } 00031 00032 Time::~Time() 00033 { 00034 } 00035 00036 // 00037 // Returns the time as a fractional value with up to 4 fraction digits. 00038 // Example: "1393926457.2141" 00039 // 00040 std::string Time::toString() const 00041 { 00042 double t = (double)m_time.tv_sec + ((double)m_time.tv_usec / 1000000.0); 00043 return ::toString(t, 4); 00044 } 00045 00046 // 00047 // Returns the time as a readble string, in local time. This includes 00048 // the microseconds. 00049 // Example: "Tue Mar 4 10:47:37 2014 165751 us" 00050 // 00051 std::string Time::toLongString() const 00052 { 00053 time_t seconds = m_time.tv_sec; 00054 struct tm* seconds_tm = localtime(&seconds); 00055 char* text = asctime(seconds_tm); 00056 std::string s = text; 00057 00058 // Microseconds 00059 std::string us = "000000" + ::toString((UINT32)m_time.tv_usec); 00060 us = us.substr(us.length() - 6, 6); 00061 s += " " + us + " us"; 00062 00063 return s; 00064 } 00065 00066 // 00067 // 00068 // 00069 void Time::set(double time) 00070 { 00071 m_time.tv_sec = (UINT32)time; 00072 m_time.tv_usec = (time - (double)((UINT32)time)) * 1000000; 00073 } 00074 00075 // 00076 // 00077 // 00078 void Time::set(timeval time) 00079 { 00080 m_time = time; 00081 } 00082 00083 00084 void Time::set(UINT64 ntpTime) 00085 { 00086 set(static_cast<UINT64>(ntpTime >> 32), static_cast<UINT32>(ntpTime)); 00087 } 00088 00089 // 00090 // 00091 // 00092 void Time::set(UINT64 ntpSeconds, UINT32 ntpFractionalSeconds) 00093 { 00094 // const UINT32 startUnixEpoche = 2208988800; // seconds from 1.1.1900 to 1.1.1900 00095 m_time.tv_sec = ntpSeconds - secondsFrom1900to1970; 00096 m_time.tv_usec = ntpFractionalSeconds * m_secondFractionNTPtoNanoseconds / 1000; 00097 } 00098 00102 double Time::seconds() 00103 { 00104 double s = (double)m_time.tv_sec + ((double)m_time.tv_usec / 1000000); 00105 return s; 00106 } 00107 00108 // 00109 // Zeit(spanne), in [ms]. 00110 // 00111 UINT32 Time::total_milliseconds() 00112 { 00113 UINT32 ms = (m_time.tv_sec * 1000) + (m_time.tv_usec / 1000); 00114 return ms; 00115 } 00116 00117 00118 00122 Time& Time::operator+=(const Time& other) 00123 { 00124 m_time.tv_usec += other.m_time.tv_usec; 00125 if (m_time.tv_usec > 1000000) 00126 { 00127 m_time.tv_sec++; 00128 m_time.tv_usec -= 1000000; 00129 } 00130 m_time.tv_sec += other.m_time.tv_sec; 00131 return *this; 00132 } 00133 00137 Time Time::operator+(const Time& other) const 00138 { 00139 Time t; 00140 t.m_time.tv_sec = m_time.tv_sec + other.m_time.tv_sec; 00141 t.m_time.tv_usec = (m_time.tv_usec + other.m_time.tv_usec); 00142 if (t.m_time.tv_usec > 1000000) 00143 { 00144 t.m_time.tv_sec++; 00145 t.m_time.tv_usec -= 1000000; 00146 } 00147 return t; 00148 } 00149 00153 Time Time::operator+(const TimeDuration& dur) const 00154 { 00155 Time td; 00156 td.set(dur.m_duration); 00157 00158 Time t; 00159 t = *this + td; 00160 00161 return t; 00162 } 00163 00164 00168 Time Time::now() 00169 { 00170 Time t; 00171 gettimeofday(&(t.m_time), NULL); 00172 00173 return t; 00174 } 00175 00176 // 00177 // True, wenn unser Zeitpunkt gleich oder spaeter als der andere ist. 00178 // 00179 bool Time::operator>=(const Time& other) const 00180 { 00181 if (m_time.tv_sec > other.m_time.tv_sec) 00182 { 00183 return true; 00184 } 00185 else if ((m_time.tv_sec == other.m_time.tv_sec) && 00186 (m_time.tv_usec >= other.m_time.tv_usec)) 00187 { 00188 return true; 00189 } 00190 00191 return false; 00192 } 00193 00194 // 00195 // True, wenn unser Zeitpunkt frueher als der andere ist. 00196 // 00197 bool Time::operator<(const Time& other) const 00198 { 00199 if (m_time.tv_sec < other.m_time.tv_sec) 00200 { 00201 // Unsere Sekunden sind kleiner 00202 return true; 00203 } 00204 else if ((m_time.tv_sec == other.m_time.tv_sec) && 00205 (m_time.tv_usec < other.m_time.tv_usec)) 00206 { 00207 // Sekunden sind gleich, aber unsere usec sind kleiner 00208 return true; 00209 } 00210 00211 return false; 00212 } 00213 00214 // 00215 // True, wenn unser Zeitpunkt gleich dem anderen ist. 00216 // 00217 bool Time::operator==(const Time& other) const 00218 { 00219 if ((m_time.tv_sec == other.m_time.tv_sec) && 00220 (m_time.tv_usec == other.m_time.tv_usec)) 00221 { 00222 // Gleich 00223 return true; 00224 } 00225 00226 return false; 00227 } 00228 00229 00230 Time Time::operator-(const Time& other) const 00231 { 00232 Time t; 00233 if (m_time.tv_sec > other.m_time.tv_sec) 00234 { 00235 t.m_time.tv_sec = m_time.tv_sec - other.m_time.tv_sec; 00236 UINT32 offset = 0; 00237 if (m_time.tv_usec < other.m_time.tv_usec) 00238 { 00239 t.m_time.tv_sec -= 1; 00240 offset = 1000000; 00241 } 00242 t.m_time.tv_usec = (m_time.tv_usec + offset) - other.m_time.tv_usec; 00243 } 00244 else if (m_time.tv_sec == other.m_time.tv_sec) 00245 { 00246 t.m_time.tv_sec = 0; 00247 if (m_time.tv_usec < other.m_time.tv_usec) 00248 { 00249 t.m_time.tv_usec = 0; 00250 } 00251 else 00252 { 00253 t.m_time.tv_usec = m_time.tv_usec - other.m_time.tv_usec; 00254 } 00255 } 00256 else 00257 { 00258 t.m_time.tv_sec = 0; 00259 t.m_time.tv_usec = 0; 00260 } 00261 return t; 00262 } 00263 00264 Time Time::operator-(const double& seconds) const 00265 { 00266 Time t1, t2; 00267 t1.set(seconds); 00268 t2 = *this - t1; 00269 return t2; 00270 /* 00271 if (m_time.tv_sec > other.m_time.tv_sec) 00272 { 00273 t.m_time.tv_sec = m_time.tv_sec - other.m_time.tv_sec; 00274 UINT32 offset = 0; 00275 if (m_time.tv_usec < other.m_time.tv_usec) 00276 { 00277 t.m_time.tv_sec -= 1; 00278 offset = 1000000; 00279 } 00280 t.m_time.tv_usec = (m_time.tv_usec + offset) - other.m_time.tv_usec; 00281 } 00282 else if (m_time.tv_sec == other.m_time.tv_sec) 00283 { 00284 t.m_time.tv_sec = 0; 00285 if (m_time.tv_usec < other.m_time.tv_usec) 00286 { 00287 t.m_time.tv_usec = 0; 00288 } 00289 else 00290 { 00291 t.m_time.tv_usec = m_time.tv_usec - other.m_time.tv_usec; 00292 } 00293 } 00294 else 00295 { 00296 t.m_time.tv_sec = 0; 00297 t.m_time.tv_usec = 0; 00298 } 00299 return t; 00300 */ 00301 } 00302