Time.cpp
Go to the documentation of this file.
1 //
2 // Time.cpp
3 //
4 
5 
6 #include "sick_scan/tcp/Time.hpp"
7 #include <sys/time.h>
8 #include <time.h>
9 #include "sick_scan/tcp/toolbox.hpp" // fuer "::toString()"
10 
11 //
12 // Duration
13 //
15 {
16  m_duration = 0.0;
17 }
18 
19 
20 const double Time::m_secondFractionNTPtoNanoseconds (0.2328306436538696); // = 2^-32 * 1e9
21 const double Time::m_nanosecondsToSecondFractionNTP (4.29496729600000); // = 2^32 * 1e-9
22 const UINT64 Time::secondsFrom1900to1970 (2208988800UL);
23 
24 //
25 // Time
26 //
28 {
29  set(0.0);
30 }
31 
33 {
34 }
35 
36 //
37 // Returns the time as a fractional value with up to 4 fraction digits.
38 // Example: "1393926457.2141"
39 //
40 std::string Time::toString() const
41 {
42  double t = (double)m_time.tv_sec + ((double)m_time.tv_usec / 1000000.0);
43  return ::toString(t, 4);
44 }
45 
46 //
47 // Returns the time as a readble string, in local time. This includes
48 // the microseconds.
49 // Example: "Tue Mar 4 10:47:37 2014 165751 us"
50 //
51 std::string Time::toLongString() const
52 {
53  time_t seconds = m_time.tv_sec;
54  struct tm* seconds_tm = localtime(&seconds);
55  char* text = asctime(seconds_tm);
56  std::string s = text;
57 
58  // Microseconds
59  std::string us = "000000" + ::toString((UINT32)m_time.tv_usec);
60  us = us.substr(us.length() - 6, 6);
61  s += " " + us + " us";
62 
63  return s;
64 }
65 
66 //
67 //
68 //
69 void Time::set(double time)
70 {
71  m_time.tv_sec = (UINT32)time;
72  m_time.tv_usec = (time - (double)((UINT32)time)) * 1000000;
73 }
74 
75 //
76 //
77 //
78 void Time::set(timeval time)
79 {
80  m_time = time;
81 }
82 
83 
84 void Time::set(UINT64 ntpTime)
85 {
86  set(static_cast<UINT64>(ntpTime >> 32), static_cast<UINT32>(ntpTime));
87 }
88 
89 //
90 //
91 //
92 void Time::set(UINT64 ntpSeconds, UINT32 ntpFractionalSeconds)
93 {
94 // const UINT32 startUnixEpoche = 2208988800; // seconds from 1.1.1900 to 1.1.1900
95  m_time.tv_sec = ntpSeconds - secondsFrom1900to1970;
96  m_time.tv_usec = ntpFractionalSeconds * m_secondFractionNTPtoNanoseconds / 1000;
97 }
98 
103 {
104  double s = (double)m_time.tv_sec + ((double)m_time.tv_usec / 1000000);
105  return s;
106 }
107 
108 //
109 // Zeit(spanne), in [ms].
110 //
112 {
113  UINT32 ms = (m_time.tv_sec * 1000) + (m_time.tv_usec / 1000);
114  return ms;
115 }
116 
117 
118 
122 Time& Time::operator+=(const Time& other)
123 {
124  m_time.tv_usec += other.m_time.tv_usec;
125  if (m_time.tv_usec > 1000000)
126  {
127  m_time.tv_sec++;
128  m_time.tv_usec -= 1000000;
129  }
130  m_time.tv_sec += other.m_time.tv_sec;
131  return *this;
132 }
133 
137 Time Time::operator+(const Time& other) const
138 {
139  Time t;
140  t.m_time.tv_sec = m_time.tv_sec + other.m_time.tv_sec;
141  t.m_time.tv_usec = (m_time.tv_usec + other.m_time.tv_usec);
142  if (t.m_time.tv_usec > 1000000)
143  {
144  t.m_time.tv_sec++;
145  t.m_time.tv_usec -= 1000000;
146  }
147  return t;
148 }
149 
154 {
155  Time td;
156  td.set(dur.m_duration);
157 
158  Time t;
159  t = *this + td;
160 
161  return t;
162 }
163 
164 
169 {
170  Time t;
171  gettimeofday(&(t.m_time), NULL);
172 
173  return t;
174 }
175 
176 //
177 // True, wenn unser Zeitpunkt gleich oder spaeter als der andere ist.
178 //
179 bool Time::operator>=(const Time& other) const
180 {
181  if (m_time.tv_sec > other.m_time.tv_sec)
182  {
183  return true;
184  }
185  else if ((m_time.tv_sec == other.m_time.tv_sec) &&
186  (m_time.tv_usec >= other.m_time.tv_usec))
187  {
188  return true;
189  }
190 
191  return false;
192 }
193 
194 //
195 // True, wenn unser Zeitpunkt frueher als der andere ist.
196 //
197 bool Time::operator<(const Time& other) const
198 {
199  if (m_time.tv_sec < other.m_time.tv_sec)
200  {
201  // Unsere Sekunden sind kleiner
202  return true;
203  }
204  else if ((m_time.tv_sec == other.m_time.tv_sec) &&
205  (m_time.tv_usec < other.m_time.tv_usec))
206  {
207  // Sekunden sind gleich, aber unsere usec sind kleiner
208  return true;
209  }
210 
211  return false;
212 }
213 
214 //
215 // True, wenn unser Zeitpunkt gleich dem anderen ist.
216 //
217 bool Time::operator==(const Time& other) const
218 {
219  if ((m_time.tv_sec == other.m_time.tv_sec) &&
220  (m_time.tv_usec == other.m_time.tv_usec))
221  {
222  // Gleich
223  return true;
224  }
225 
226  return false;
227 }
228 
229 
230 Time Time::operator-(const Time& other) const
231 {
232  Time t;
233  if (m_time.tv_sec > other.m_time.tv_sec)
234  {
235  t.m_time.tv_sec = m_time.tv_sec - other.m_time.tv_sec;
236  UINT32 offset = 0;
237  if (m_time.tv_usec < other.m_time.tv_usec)
238  {
239  t.m_time.tv_sec -= 1;
240  offset = 1000000;
241  }
242  t.m_time.tv_usec = (m_time.tv_usec + offset) - other.m_time.tv_usec;
243  }
244  else if (m_time.tv_sec == other.m_time.tv_sec)
245  {
246  t.m_time.tv_sec = 0;
247  if (m_time.tv_usec < other.m_time.tv_usec)
248  {
249  t.m_time.tv_usec = 0;
250  }
251  else
252  {
253  t.m_time.tv_usec = m_time.tv_usec - other.m_time.tv_usec;
254  }
255  }
256  else
257  {
258  t.m_time.tv_sec = 0;
259  t.m_time.tv_usec = 0;
260  }
261  return t;
262 }
263 
264 Time Time::operator-(const double& seconds) const
265 {
266  Time t1, t2;
267  t1.set(seconds);
268  t2 = *this - t1;
269  return t2;
270 /*
271  if (m_time.tv_sec > other.m_time.tv_sec)
272  {
273  t.m_time.tv_sec = m_time.tv_sec - other.m_time.tv_sec;
274  UINT32 offset = 0;
275  if (m_time.tv_usec < other.m_time.tv_usec)
276  {
277  t.m_time.tv_sec -= 1;
278  offset = 1000000;
279  }
280  t.m_time.tv_usec = (m_time.tv_usec + offset) - other.m_time.tv_usec;
281  }
282  else if (m_time.tv_sec == other.m_time.tv_sec)
283  {
284  t.m_time.tv_sec = 0;
285  if (m_time.tv_usec < other.m_time.tv_usec)
286  {
287  t.m_time.tv_usec = 0;
288  }
289  else
290  {
291  t.m_time.tv_usec = m_time.tv_usec - other.m_time.tv_usec;
292  }
293  }
294  else
295  {
296  t.m_time.tv_sec = 0;
297  t.m_time.tv_usec = 0;
298  }
299  return t;
300 */
301 }
302 
double seconds()
Definition: Time.cpp:102
static const UINT64 secondsFrom1900to1970
Definition: Time.hpp:72
Time & operator+=(const Time &other)
Definition: Time.cpp:122
Time operator-(const Time &other) const
Definition: Time.cpp:230
bool operator==(const Time &other) const
Definition: Time.cpp:217
Definition: Time.hpp:44
static const double m_nanosecondsToSecondFractionNTP
Definition: Time.hpp:78
uint32_t UINT32
timeval m_time
Definition: Time.hpp:75
Time operator+(const TimeDuration &dur) const
Definition: Time.cpp:153
Time()
Definition: Time.cpp:27
bool operator<(const Time &other) const
Definition: Time.cpp:197
void set(double time)
Definition: Time.cpp:69
TimeDuration()
Definition: Time.cpp:14
double m_duration
Definition: Time.hpp:26
static const double m_secondFractionNTPtoNanoseconds
Definition: Time.hpp:77
std::string toString(INT32 value)
Definition: toolbox.cpp:275
std::string toLongString() const
Definition: Time.cpp:51
UINT32 total_milliseconds()
Definition: Time.cpp:111
static Time now()
Definition: Time.cpp:168
static sick_scan::SickScanCommonTcp * s
uint64_t UINT64
~Time()
Definition: Time.cpp:32
bool operator>=(const Time &other) const
Definition: Time.cpp:179
std::string toString() const
Definition: Time.cpp:40


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Tue Jul 9 2019 04:55:32