TimeStamp.cc
Go to the documentation of this file.
1 
44 
45 #ifndef WIN32
46 #include <sys/time.h>
47 #endif
48 #include <time.h>
49 
50 namespace crl {
51 namespace multisense {
52 namespace details {
53 namespace utility {
54 
55 
56 #if defined (WIN32)
57 
58 // Windows probably doesn't provider timeradd or timersub macros, so provide
59 // a definition here
60 #ifndef timeradd
61 #define timeradd(a, b, result) \
62 do \
63 { \
64  (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
65  (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
66  if ((result)->tv_usec >= 1000000) \
67  { \
68  (result)->tv_sec++; \
69  (result)->tv_usec -= 1000000; \
70  } \
71 } while (0)
72 #endif
73 
74 #ifndef timersub
75 #define timersub(a, b, result) \
76 do \
77 { \
78  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
79  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
80  if ((result)->tv_usec < 0) \
81  { \
82  (result)->tv_sec--; \
83  (result)->tv_usec += 1000000; \
84  } \
85 } while (0)
86 #endif
87 
88 //
89 // The FILETIME structure in Windows measures time in 100-nanosecond intervals
90 // since 1601-Jan-01. The timeval structure measures time in second intervals
91 // since 1970-Jan-01. This function computes the number of seconds (as a double)
92 // that we need to apply as an offset to ensure that clock times are all tracked
93 // from the same epoch.
94 static ULARGE_INTEGER initOffsetSecondsSince1970 ()
95 {
96  SYSTEMTIME epochTimeAsSystemTime = { 1970, 1, 0, 1, 0, 0, 0, 0 };
97  FILETIME epochTimeAsFileTime;
98  SystemTimeToFileTime (&epochTimeAsSystemTime, &epochTimeAsFileTime);
99 
100  ULARGE_INTEGER epochTime;
101  epochTime.LowPart = epochTimeAsFileTime.dwLowDateTime;
102  epochTime.HighPart = epochTimeAsFileTime.dwHighDateTime;
103 
104  return epochTime;
105 }
106 
107 ULARGE_INTEGER TimeStamp::offsetSecondsSince1970 = initOffsetSecondsSince1970 ();
108 
109 #endif
110 
111 /*
112  * Constructor. Empty. We rely on the getter methods to do
113  * things that are more useful.
114  */
116 {
117  this->time.tv_sec = 0;
118  this->time.tv_usec = 0;
119 }
120 
121 /*
122  * Constructor. Initializes with the specified
123  */
124 TimeStamp::TimeStamp(int32_t seconds, int32_t microSeconds)
125 {
126  this->time.tv_sec = seconds;
127  this->time.tv_usec = microSeconds;
128 }
129 
130 /*
131  * Constructor. Initializes with the specified
132  */
133 TimeStamp::TimeStamp(int64_t nanoseconds)
134 {
135  this->time.tv_sec = (long)(nanoseconds / 1000000000);
136 
137  int64_t usec = (nanoseconds - (static_cast<int64_t>(this->time.tv_sec) * 1000000000)) / 1000;
138  this->time.tv_usec = static_cast<int32_t>(usec);
139 }
140 
141 /*
142  * Constructor. Initializes with the specified timestamp value.
143  */
144 TimeStamp::TimeStamp(const struct timeval& value)
145 {
146  this->set(value);
147 }
148 
149 /*
150  * Sets this timestamp equal to the timestamp specified.
151  */
152 void TimeStamp::set(const struct timeval& value)
153 {
154  this->time.tv_sec = value.tv_sec;
155  this->time.tv_usec = value.tv_usec;
156 }
157 
158 /*
159  * Sets this timestamp to a specific time
160  */
161 void TimeStamp::set(int32_t seconds, int32_t microSeconds)
162 {
163  this->time.tv_sec = seconds;
164  this->time.tv_usec = microSeconds;
165 }
166 
167 #ifndef SENSORPOD_FIRMWARE
168 
169 /*
170  * This routine will get the current time (as gettimeofday()) and
171  * store it off. It is the normal way of initializing time. Notice
172  * that there may be large time skips when you call this routine, due
173  * to time synchronization jumps.
174  *
175  * Notice that the timestamp returned by this object *is* atomic. It
176  * will not change, even if time synchronization skews things.
177  */
179 {
180  //
181  // Create a new timestamp object and fill it in with
182  // the current time of day.
183  //
184 
186 
187 #if defined (WIN32)
188 
189  // gettimeofday does not exist on Windows
190  FILETIME currentTimeAsFileTime;
191  GetSystemTimeAsFileTime (&currentTimeAsFileTime);
192 
193  ULARGE_INTEGER currentTimeAsLargeInteger;
194  currentTimeAsLargeInteger.LowPart = currentTimeAsFileTime.dwLowDateTime;
195  currentTimeAsLargeInteger.HighPart = currentTimeAsFileTime.dwHighDateTime;
196  currentTimeAsLargeInteger.QuadPart -= offsetSecondsSince1970.QuadPart;
197 
198  timeStamp.time.tv_sec = static_cast<long> (currentTimeAsLargeInteger.QuadPart / 10000000);
199  timeStamp.time.tv_usec = static_cast<long> ((currentTimeAsLargeInteger.QuadPart - static_cast<int64_t>(timeStamp.time.tv_sec) * 10000000) / 10);
200 
201 #else
202 
203 #if defined (USE_MONOTONIC_CLOCK)
204  struct timespec ts;
205  ts.tv_sec = 0;
206  ts.tv_nsec = 0;
207 
208  if (0 != clock_gettime(CLOCK_MONOTONIC, &ts))
209  {
210  CRL_EXCEPTION("Failed to call clock_gettime().");
211  }
212 
213  TIMESPEC_TO_TIMEVAL(&timeStamp.time, &ts);
214 #else
215  gettimeofday(&timeStamp.time, 0);
216 #endif
217 
218 #endif
219 
220  return timeStamp;
221 }
222 
223 #endif // SENSORPOD_FIRMWARE
224 
225 /*
226  * Returns the seconds portion of the timestamp.
227  */
228 int32_t TimeStamp::getSeconds() const
229 {
230  return this->time.tv_sec;
231 }
232 
233 /*
234  * Returns the microseconds portion of the timestamp.
235  */
237 {
238  return this->time.tv_usec;
239 }
240 
242 {
243  return static_cast<int64_t>(this->time.tv_sec) * 1000000000 + static_cast<int64_t>(this->time.tv_usec) * 1000;
244 }
245 
247 {
248  struct timeval tmp_time;
249  tmp_time.tv_sec = 0;
250  tmp_time.tv_usec = 0;
251  timeradd(&this->time, &other.time, &tmp_time);
252  return tmp_time;
253 }
254 
256 {
257  struct timeval tmp_time;
258  tmp_time.tv_sec = 0;
259  tmp_time.tv_usec = 0;
260  timersub(&this->time, &other.time, &tmp_time);
261  return tmp_time;
262 }
263 
265 {
266  *this = *this + other;
267  return *this;
268 }
269 
271 {
272  *this = *this - other;
273  return *this;
274 }
275 
276 }}}} // namespaces
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:85
ros::Time * timeStamp(M &m)
TimeStamp & operator+=(TimeStamp const &other)
Definition: TimeStamp.cc:264
TimeStamp & operator-=(TimeStamp const &other)
Definition: TimeStamp.cc:270
Definition: channel.cc:58
TimeStamp operator+(TimeStamp const &other) const
Definition: TimeStamp.cc:246
void set(const struct timeval &value)
Definition: TimeStamp.cc:152
TimeStamp operator-(TimeStamp const &other) const
Definition: TimeStamp.cc:255


multisense_lib
Author(s):
autogenerated on Sat Jun 24 2023 03:01:21