Debian Codegraph @

3rdparty/xspublic/xstypes/xstimestamp.h

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSTIMESTAMP_H
34 #define XSTIMESTAMP_H
35
36 #include "xstypesconfig.h"
37 #include "pstdint.h"
38
39 #ifdef __cplusplus
40 #ifndef XSENS_NO_STL
41 #include "xsstring.h"
42 #endif
43 #include "xstimeinfo.h"
44 extern "C" {
45 #endif
46 #ifndef __cplusplus
47 #define XSTIMESTAMP_INITIALIZER { 0 }
48 #endif
49
50 struct XsTimeStamp;
51 struct XsTimeInfo;
52 struct XsString;
53
54 XSTYPES_DLL_API void XsTimeStamp_setMilliSecondTime(struct XsTimeStamp* thisPtr, int64_t t);
55 XSTYPES_DLL_API double XsTimeStamp_timeOfDay(const struct XsTimeStamp* thisPtr);
56 XSTYPES_DLL_API int64_t XsTimeStamp_secondTime(const struct XsTimeStamp* thisPtr);
57 XSTYPES_DLL_API int32_t XsTimeStamp_milliSecondPart(const struct XsTimeStamp* thisPtr);
58 XSTYPES_DLL_API int32_t XsTimeStamp_secondPart(const struct XsTimeStamp* thisPtr);
59 XSTYPES_DLL_API int32_t XsTimeStamp_minutePart(const struct XsTimeStamp* thisPtr);
60 XSTYPES_DLL_API int32_t XsTimeStamp_hourPart(const struct XsTimeStamp* thisPtr);
61 XSTYPES_DLL_API int64_t XsTimeStamp_now(struct XsTimeStamp* thisPtr);
62 XSTYPES_DLL_API int64_t XsTimeStamp_maxValue(void);
63
64 XSTYPES_DLL_API int64_t XsTimeStamp_fromTimeInfo(struct XsTimeStamp* thisPtr, const struct XsTimeInfo* info);
65 XSTYPES_DLL_API void XsTimeStamp_toTimeInfo(struct XsTimeStamp const* thisPtr, struct XsTimeInfo* info);
66 #ifndef XSENS_NO_STL
67 XSTYPES_DLL_API void XsTimeStamp_toString(struct XsTimeStamp const* thisPtr, struct XsString* result);
68 #endif
69 XSTYPES_DLL_API void XsTimeStamp_utcToLocalTime(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local);
70 XSTYPES_DLL_API void XsTimeStamp_localToUtcTime(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc);
71 XSTYPES_DLL_API void XsTimeStamp_utcToLocalTime2(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local, const struct XsTimeInfo* info);
72 XSTYPES_DLL_API void XsTimeStamp_localToUtcTime2(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc, const struct XsTimeInfo* info);
73 XSTYPES_DLL_API void XsTimeStamp_utcToLocalTime_ms(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local, int64_t utcOffset);
74 XSTYPES_DLL_API void XsTimeStamp_localToUtcTime_ms(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc, int64_t utcOffset);
75
76 #ifdef __cplusplus
77 } // extern "C"
78 #endif
79
80 /*! \struct XsTimeStamp
81 \brief Class for managing timestamps in a unified way.
82 */
83 struct XsTimeStamp {
84 #ifdef __cplusplus
85 /*! \brief Construct a timestamp with \a t as the time in milliseconds. */
86 inline XsTimeStamp(int64_t t = 0) : m_msTime(t) {}
87
88 /*! \brief Construct a timestamp with \a t as the time in milliseconds. */
89 inline XsTimeStamp(int t) : m_msTime(t) {}
90
91 /*! \brief Construct a timestamp with \a t as the time in seconds. */
92 inline explicit XsTimeStamp(double t) : m_msTime((int64_t)(t*1000.0)) {}
93
94 /*! \brief Construct a copy of \a other. */
95 inline XsTimeStamp(const XsTimeStamp& other) : m_msTime(other.m_msTime) {}
96
97 /*! \brief Construct from \a utc */
98 inline XsTimeStamp(const XsTimeInfo& info)
99 {
100 XsTimeStamp_fromTimeInfo(this, &info);
101 }
102
103 /*! \brief Convert this timestamp to UTC time */
104 inline XsTimeInfo toTimeInfo() const
105 {
106 XsTimeInfo info;
107 XsTimeStamp_toTimeInfo(this, &info);
108 return info;
109 }
110
111 /*! \brief Convert this timestamp to a XsTimeInfo object \a info */
112 inline void toTimeInfo(XsTimeInfo& info) const
113 {
114 XsTimeStamp_toTimeInfo(this, &info);
115 }
116
117 #ifndef XSENS_NO_STL
118 /*! \brief Convert this timestamp to a string in format YYYY/MM/DD hh:mm:ss.nnn */
119 inline XsString toString() const
120 {
121 XsString result;
122 XsTimeStamp_toString(this, &result);
123 return result;
124 }
125 #endif
126
127 /*! \brief Assign the contents of the \a other timestamp to this timestamp. */
128 inline XsTimeStamp& operator = (const XsTimeStamp& other)
129 {
130 m_msTime = other.m_msTime;
131 return *this;
132 }
133
134 /*! \brief Get the stored time as milliseconds. */
135 inline int64_t msTime(void) const
136 { return m_msTime; }
137
138 /*! \brief Set the stored time to \a t milliseconds. */
139 inline void setMsTime(int64_t t)
140 { m_msTime = t; }
141
142 /*! \brief Get the time of day component of the stored timestamp in seconds as a double precision value.
143 */
144 inline double timeOfDay() const
145 { return XsTimeStamp_timeOfDay(this); }
146
147 /*! \brief Get the time of day component of the stored timestamp in milliseconds
148 */
149 inline int64_t msTimeOfDay() const
150 { return m_msTime % (24*60*60*1000); }
151
152 /*! \brief Return the time as seconds */
153 inline double secTime() const
154 { return ((double)m_msTime)*0.001; }
155
156 /*! \brief Set the time as seconds */
157 inline void setSecTime(double t)
158 { m_msTime = (int64_t) (t*1000.0); }
159
160 /*! \brief Get the sum of the current and the given \a other timestamp. \param other The value to add to this \returns The added timestamp values */
161 inline XsTimeStamp operator + (const XsTimeStamp& other) const
162 { return XsTimeStamp(m_msTime + other.m_msTime); }
163
164 /*! \brief Get the current minus the given \a other timestamp. \param other The value to subtract from this \returns The subtracted timestamp values */
165 inline XsTimeStamp operator - (const XsTimeStamp& other) const
166 { return XsTimeStamp(m_msTime - other.m_msTime); }
167
168 /*! \brief Get the result of adding the given \a other timestamp to the current timestamp. */
169 inline XsTimeStamp& operator += (const XsTimeStamp& other)
170 { m_msTime += other.m_msTime; return *this; }
171
172 /*! \brief Get the result of subtracting the given \a other timestamp from the current timestamp. */
173 inline XsTimeStamp& operator -= (const XsTimeStamp& d)
174 { m_msTime -= d.m_msTime; return *this; }
175
176 /*! \brief Test if the given \a other timestamp is smaller than the current timestamp. */
177 inline bool operator < (const XsTimeStamp& other) const
178 { return m_msTime < other.m_msTime; }
179
180 /*! \brief Test if the given \a other timestamp is smaller than or equal to the current timestamp. */
181 inline bool operator <= (const XsTimeStamp& other) const
182 { return m_msTime <= other.m_msTime; }
183
184 /*! \brief Test if the given \a other timestamp is equal to the current timestamp. */
185 inline bool operator == (const XsTimeStamp& other) const
186 { return m_msTime == other.m_msTime; }
187
188 /*! \brief Test if the given \a other timestamp is larger than the current timestamp. */
189 inline bool operator > (const XsTimeStamp& other) const
190 { return m_msTime > other.m_msTime; }
191
192 /*! \brief Test if the given \a other timestamp is larger than or equal to the current timestamp. */
193 inline bool operator >= (const XsTimeStamp& other) const
194 { return m_msTime >= other.m_msTime; }
195
196 /*! \brief Test if the given \a other timestamp is not equal to the current timestamp. */
197 inline bool operator != (const XsTimeStamp& other) const
198 { return m_msTime != other.m_msTime; }
199
200 /*! \brief Test if the given \a other is smaller than the current timestamp. */
201 inline bool operator < (int other) const
202 { return m_msTime < other; }
203
204 /*! \brief Test if the given \a other is smaller than or equal to the current timestamp. */
205 inline bool operator <= (int other) const
206 { return m_msTime <= other; }
207
208 /*! \brief Test if the given \a other is equal to the current timestamp. */
209 inline bool operator == (int other) const
210 { return m_msTime == other; }
211
212 /*! \brief Test if the given \a other is larger than the current timestamp. */
213 inline bool operator > (int other) const
214 { return m_msTime > other; }
215
216 /*! \brief Test if the given \a other is larger than or equal to the current timestamp. */
217 inline bool operator >= (int other) const
218 { return m_msTime >= other; }
219
220 /*! \brief Test if the given \a other is not equal to the current timestamp. */
221 inline bool operator != (int other) const
222 { return m_msTime != other; }
223
224 /*! \brief Returns the number of seconds elapsed since the epoch as stored in the XsTimeStamp */
225 inline int64_t secondTime() const
226 { return m_msTime/1000; }
227
228 /*! \brief Returns the millisecond part of the time (in the range 0-999) */
229 inline int32_t milliSecondPart() const
230 { return (int32_t) (m_msTime % 1000); }
231
232 /*! \brief Returns the seconds part of the time (in the range 0-59) */
233 inline int32_t secondPart() const
234 { return (int32_t) ((m_msTime/(1000))%60); }
235
236 /*! \brief Returns the minutes part of the time (in the range 0-59) */
237 inline int32_t minutePart() const
238 { return (int32_t) ((m_msTime/(60*1000))%60); }
239
240 /*! \brief Returns the hours part of the time (in the range 0-23) */
241 inline int32_t hourPart() const
242 { return (int32_t) ((m_msTime/(60*60*1000))%24); }
243
244 /*! \brief Returns the current time in ms since the epoch (Jan 1st 1970) */
245 inline static XsTimeStamp now()
246 {
247 XsTimeStamp tmp;
248 XsTimeStamp_now(&tmp);
249 return tmp;
250 }
251
252 /*! \brief Returns the current time in ms since the epoch (Jan 1st 1970) */
253 inline static int64_t nowMs()
254 {
255 XsTimeStamp tmp;
256 XsTimeStamp_now(&tmp);
257 return tmp.msTime();
258 }
259
260 /*! \brief Returns the maximum possible value of an %XsTimeStamp */
261 inline static XsTimeStamp maxValue()
262 {
263 return XsTimeStamp(int64_t(9223372036854775807LL)); //INT64_MAX
264 }
265
266 /*! \brief Increment the timestamp by one ms, prefix */
267 XsTimeStamp& operator++()
268 { ++m_msTime; return *this; }
269
270 /*! \brief Increment the timestamp by one ms, postfix */
271 XsTimeStamp operator++(int)
272 { return XsTimeStamp(m_msTime++); }
273
274 /*! \brief Decrement the timestamp by one ms, prefix */
275 XsTimeStamp& operator--()
276 { --m_msTime; return *this; }
277
278 /*! \brief Decrement the timestamp by one ms, postfix */
279 XsTimeStamp operator--(int)
280 { return XsTimeStamp(m_msTime--); }
281
282 /*! \brief Convert the supplied time from (assumed) UTC to local time, using the system's local time zone knowledge
283 \returns The converted time
284 \note Make sure to call XsTime_initializeTime() before calling this function as well as when changing time zones.
285 */
286 XsTimeStamp utcToLocalTime() const
287 {
288 XsTimeStamp local;
289 XsTimeStamp_utcToLocalTime(this, &local);
290 return local;
291 }
292
293 /*! \brief Convert the supplied time from (assumed) local time to UTC, using the system's local time zone knowledge
294 \returns The converted time
295 \note Make sure to call XsTime_initializeTime() before calling this function as well as when changing time zones.
296 */
297 XsTimeStamp localToUtcTime() const
298 {
299 XsTimeStamp utc;
300 XsTimeStamp_localToUtcTime(this, &utc);
301 return utc;
302 }
303
304 /*! \brief Convert the supplied time from (assumed) UTC to local time, using the offset in \a info
305 \param info The XsTimeInfo object containing the time offset to apply
306 \returns The converted time
307 */
308 XsTimeStamp utcToLocalTime(XsTimeInfo const& info) const
309 {
310 XsTimeStamp local;
311 XsTimeStamp_utcToLocalTime2(this, &local, &info);
312 return local;
313 }
314
315 /*! \brief Convert the supplied time from (assumed) local time to UTC, using the offset in \a info
316 \param info The XsTimeInfo object containing the time offset to apply
317 \returns The converted time
318 */
319 XsTimeStamp localToUtcTime(XsTimeInfo const& info) const
320 {
321 XsTimeStamp utc;
322 XsTimeStamp_localToUtcTime2(this, &utc, &info);
323 return utc;
324 }
325
326 /*! \brief Convert the supplied time from (assumed) UTC to local time, using the offset in \a info
327 \param utcOffset The time offset to apply
328 \returns The converted time
329 */
330 XsTimeStamp utcToLocalTime(int64_t utcOffset) const
331 {
332 XsTimeStamp local;
333 XsTimeStamp_utcToLocalTime_ms(this, &local, utcOffset);
334 return local;
335 }
336
337 /*! \brief Convert the supplied time from (assumed) local time to UTC, using the offset in \a info
338 \param utcOffset The time offset to apply
339 \returns The converted time
340 */
341 XsTimeStamp localToUtcTime(int64_t utcOffset) const
342 {
343 XsTimeStamp utc;
344 XsTimeStamp_localToUtcTime_ms(this, &utc, utcOffset);
345 return utc;
346 }
347 private:
348 #endif
349
350 int64_t m_msTime; //!< The timestamp value
351 };
352
353 typedef struct XsTimeStamp XsTimeStamp;
354
355 #endif