Debian Codegraph @

3rdparty/xspublic/xstypes/xstimestamp.c

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 #include "xstimestamp.h"
34 #include "xstime.h"
35 #include "xstimeinfo.h"
36 #ifndef XSENS_NO_STL
37 #include "xsstring.h"
38 #endif
39 #include <time.h>
40 #include <string.h>
41 #include <stdio.h>
42
43 /*! \class XsTimeStamp
44 \brief This class contains method to set, retrieve and compare timestamps
45 */
46
47 /*! \addtogroup cinterface C Interface
48 @{
49 */
50
51 /*! \relates XsTimeStamp \brief Set the stored time to \a miliseconds. */
52 void XsTimeStamp_setMilliSecondTime(XsTimeStamp* thisPtr, int64_t t)
53 {
54 thisPtr->m_msTime = t;
55 }
56
57 /*! \relates XsTimeStamp \brief Get the time of day component of the stored timestamp in seconds as a double precision value. */
58 double XsTimeStamp_timeOfDay(const XsTimeStamp* thisPtr)
59 {
60 return (double)(thisPtr->m_msTime % (24*60*60*1000))*0.001;
61 }
62
63 /*! \relates XsTimeStamp \brief Returns the number of seconds elapsed since the epoch as stored in the XsTimeStamp
64 */
65 int64_t XsTimeStamp_secondTime(const XsTimeStamp* thisPtr)
66 {
67 return thisPtr->m_msTime/1000;
68 }
69
70 /*! \relates XsTimeStamp \brief Returns the millisecond part of the time (in the range 0-999)
71 */
72 int32_t XsTimeStamp_milliSecondPart(const XsTimeStamp* thisPtr)
73 {
74 return (int32_t) (thisPtr->m_msTime % 1000);
75 }
76
77 /*! \relates XsTimeStamp \brief Returns the seconds part of the time (in the range 0-59)
78 */
79 int32_t XsTimeStamp_secondPart(const XsTimeStamp* thisPtr)
80 {
81 return (int32_t) ((thisPtr->m_msTime/(1000))%60);
82 }
83
84 /*! \relates XsTimeStamp \brief Returns the minutes part of the time (in the range 0-59)
85 */
86 int32_t XsTimeStamp_minutePart(const XsTimeStamp* thisPtr)
87 {
88 return (int32_t) ((thisPtr->m_msTime/(60*1000))%60);
89 }
90
91 /*! \relates XsTimeStamp \brief Returns the hours part of the time (in the range 0-23)
92 */
93 int32_t XsTimeStamp_hourPart(const XsTimeStamp* thisPtr)
94 {
95 return (int32_t) ((thisPtr->m_msTime/(60*60*1000))%24);
96 }
97
98 /*! \relates XsTimeStamp
99 \brief Returns the current time in ms since the epoch (Jan 1st 1970)
100 \see XsTime_timeStampNow
101 \param dest The object to write the time to, may be 0 in which case only the return value is generated.
102 \returns The current time in ms since the epoch (Jan 1st 1970)
103 */
104 int64_t XsTimeStamp_now(XsTimeStamp* dest)
105 {
106 return (int64_t) XsTime_timeStampNow(dest);
107 }
108
109 /*! \relates XsTimeStamp
110 \brief Returns the maximum value of an %XsTimeStamp
111 */
112 int64_t XsTimeStamp_maxValue(void)
113 {
114 return 9223372036854775807LL; //INT64_MAX;
115 }
116
117 /*! \relates XsTimeStamp
118 \brief Creates a (UTC) XsTimeStamp from an XsTimeInfo object
119 \param info The time info object to convert
120 \returns The converted UTC time in ms since the epoch (Jan 1st 1970)
121 */
122 int64_t XsTimeStamp_fromTimeInfo(struct XsTimeStamp* thisPtr, const struct XsTimeInfo* info)
123 {
124 int64_t rv = -1;
125 int64_t epoch = 0;
126 if (info && info->m_valid)
127 {
128 struct tm utctm;
129 utctm.tm_year = info->m_year - 1900;
130 utctm.tm_mon = info->m_month - 1;
131 utctm.tm_mday = info->m_day;
132
133 utctm.tm_hour = info->m_hour;
134 utctm.tm_min = info->m_minute;
135 utctm.tm_sec = info->m_second;
136
137 // unused
138 utctm.tm_wday = 0;
139 utctm.tm_yday = 0;
140 utctm.tm_isdst = 0;
141
142 #ifdef _WIN32
143 epoch = (int64_t)_mkgmtime(&utctm);
144 #else
145 epoch = (int64_t)timegm(&utctm);
146 #endif
147
148 rv = (epoch * 1000LL) + (info->m_nano / 1000000) + ((int64_t) info->m_utcOffset * 60000);
149 }
150 if (thisPtr)
151 thisPtr->m_msTime = rv;
152 return rv;
153 }
154
155 /*! \relates XsTimeStamp
156 \brief Converts the timestamp into an XsTimeInfo object
157 \param info The XsTimeInfo time object to write the conversion result to
158 */
159 void XsTimeStamp_toTimeInfo(struct XsTimeStamp const* thisPtr, struct XsTimeInfo* info)
160 {
161 struct tm tmUtc;
162 #ifdef _WIN32
163 __time64_t t;
164 t = (__time64_t)(thisPtr->m_msTime / 1000);
165 if (_gmtime64_s(&tmUtc, &t))
166 {
167 //in case of an error the result is an invalid XsTimeInfo
168 info->m_valid = 0;
169 return;
170 }
171 #elif (defined(__arm__) && defined(__ARMCC_VERSION))
172 #warning Function is not thread-safe for this platform/toolchain
173 time_t t;
174 t = (time_t)(thisPtr->m_msTime / 1000);
175 struct tm* tmUtcPtr = gmtime(&t);
176 if (tmUtcPtr == 0)
177 {
178 info->m_valid = 0;
179 return;
180 }
181 memcpy(&tmUtc, tmUtcPtr, sizeof(tmUtc));
182 #else
183 time_t t;
184 t = (time_t)(thisPtr->m_msTime / 1000);
185 if (gmtime_r(&t, &tmUtc) == 0)
186 {
187 //in case of an error the result is an invalid XsTimeInfo
188 info->m_valid = 0;
189 return;
190 }
191 #endif
192
193 info->m_day = (uint8_t) tmUtc.tm_mday;
194 info->m_hour = (uint8_t) tmUtc.tm_hour;
195 info->m_minute = (uint8_t) tmUtc.tm_min;
196 info->m_month = (uint8_t) tmUtc.tm_mon + 1;
197 info->m_nano = (uint32_t)((thisPtr->m_msTime % 1000) * 1e6);
198 info->m_second = (uint8_t) tmUtc.tm_sec;
199 info->m_year = (uint16_t) (tmUtc.tm_year + 1900);
200 info->m_valid = 1;
201 info->m_utcOffset = 0;
202 }
203
204 #ifndef XSENS_NO_STL
205 /*! \relates XsTimeStamp
206 \brief Converts the timestamp into an XsString object in format YYYY/MM/DD hh:mm:ss.nnn
207 \param result The XsString object to write the conversion result to
208 */
209 void XsTimeStamp_toString(struct XsTimeStamp const* thisPtr, struct XsString* result)
210 {
211 XsTimeInfo info;
212 char buffer[32];
213 XsTimeStamp_toTimeInfo(thisPtr, &info);
214
215 if (info.m_valid)
216 {
217 sprintf(buffer, "%04d/%02d/%02d %02d:%02d:%02d.%03d"
218 , (int) info.m_year
219 , (int) info.m_month
220 , (int) info.m_day
221 , (int) info.m_hour
222 , (int) info.m_minute
223 , (int) info.m_second
224 , (int) (thisPtr->m_msTime % 1000));
225 XsString_assign(result, 23, buffer);
226 }
227 else
228 XsString_assign(result, 23, "0000/00/00 00:00:00.000");
229 }
230 #endif
231
232 /*! \cond XS_INTERNAL */
233 extern int64_t XsTime_utcToLocalValue; //!< Internal storage for UTC to local time correction (ms)
234 extern int64_t XsTime_localToUtcValue; //!< Internal storage for local time to UTC correction (ms)
235 /*! \endcond */
236
237 /*! \brief Convert the supplied time from (assumed) UTC to local time, using the system's local time zone knowledge
238 \param local The returned, converted time
239 \note Make sure to call XsTime_initializeTime() before calling this function as well as when changing time zones.
240 */
241 void XsTimeStamp_utcToLocalTime(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local)
242 {
243 local->m_msTime = thisPtr->m_msTime + (thisPtr->m_msTime ? XsTime_utcToLocalValue : 0LL);
244 }
245
246 /*! \brief Convert the supplied time from (assumed) local time to UTC, using the system's local time zone knowledge
247 \param utc The returned, converted time
248 \note Make sure to call XsTime_initializeTime() before calling this function as well as when changing time zones.
249 */
250 void XsTimeStamp_localToUtcTime(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc)
251 {
252 utc->m_msTime = thisPtr->m_msTime + (thisPtr->m_msTime ? XsTime_localToUtcValue : 0LL);
253 }
254
255 /*! \brief Convert the supplied time from (assumed) UTC to local time, using the offset in \a info
256 \param local The returned, converted time
257 \param info The XsTimeInfo object containing the time offset to apply
258 */
259 void XsTimeStamp_utcToLocalTime2(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local, const struct XsTimeInfo* info)
260 {
261 local->m_msTime = thisPtr->m_msTime - (thisPtr->m_msTime ? (int64_t) info->m_utcOffset * 60000 : 0LL);
262 }
263
264 /*! \brief Convert the supplied time from (assumed) local time to UTC, using the offset in \a info
265 \param utc The returned, converted time
266 \param info The XsTimeInfo object containing the time offset to apply
267 */
268 void XsTimeStamp_localToUtcTime2(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc, const struct XsTimeInfo* info)
269 {
270 utc->m_msTime = thisPtr->m_msTime + (thisPtr->m_msTime ? (int64_t) info->m_utcOffset * 60000 : 0LL);
271 }
272
273 /*! \brief Convert the supplied time from (assumed) UTC to local time, using the offset in \a utcOffset
274 \param local The returned, converted time
275 \param utcOffset The time offset to apply as defined in XsTimeInfo::m_utcOffset but in ms instead of minutes
276 */
277 void XsTimeStamp_utcToLocalTime_ms(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local, int64_t utcOffset)
278 {
279 local->m_msTime = thisPtr->m_msTime - (thisPtr->m_msTime ? utcOffset: 0LL);
280 }
281
282 /*! \brief Convert the supplied time from (assumed) local time to UTC, using the offset in \a utcOffset
283 \param utc The returned, converted time
284 \param utcOffset The time offset to apply as defined in XsTimeInfo::m_utcOffset but in ms instead of minutes
285 */
286 void XsTimeStamp_localToUtcTime_ms(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc, int64_t utcOffset)
287 {
288 utc->m_msTime = thisPtr->m_msTime + (thisPtr->m_msTime ? utcOffset : 0LL);
289 }
290
291 /*! @} */