xstimestamp.c
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 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 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #include "xstimestamp.h"
66 #include "xstime.h"
67 #include "xstimeinfo.h"
68 #ifndef XSENS_NO_STL
69  #include "xsstring.h"
70 #endif
71 #include <time.h>
72 #include <string.h>
73 #include <stdio.h>
74 
85 {
86  thisPtr->m_msTime = t;
87 }
88 
90 double XsTimeStamp_timeOfDay(const XsTimeStamp* thisPtr)
91 {
92  return (double)(thisPtr->m_msTime % (24 * 60 * 60 * 1000)) * 0.001;
93 }
94 
97 int64_t XsTimeStamp_secondTime(const XsTimeStamp* thisPtr)
98 {
99  return thisPtr->m_msTime / 1000;
100 }
101 
105 {
106  return (int32_t)(thisPtr->m_msTime % 1000);
107 }
108 
112 {
113  return (int32_t)((thisPtr->m_msTime / (1000)) % 60);
114 }
115 
119 {
120  return (int32_t)((thisPtr->m_msTime / (60 * 1000)) % 60);
121 }
122 
126 {
127  return (int32_t)((thisPtr->m_msTime / (60 * 60 * 1000)) % 24);
128 }
129 
137 {
138  return (int64_t) XsTime_timeStampNow(dest);
139 }
140 
144 int64_t XsTimeStamp_maxValue(void)
145 {
146  return 9223372036854775807LL; //INT64_MAX;
147 }
148 
154 int64_t XsTimeStamp_fromTimeInfo(struct XsTimeStamp* thisPtr, const struct XsTimeInfo* info)
155 {
156  int64_t rv = -1;
157  int64_t epoch = 0;
158  if (info && info->m_valid)
159  {
160  struct tm utctm;
161  utctm.tm_year = info->m_year - 1900;
162  utctm.tm_mon = info->m_month - 1;
163  utctm.tm_mday = info->m_day;
164 
165  utctm.tm_hour = info->m_hour;
166  utctm.tm_min = info->m_minute;
167  utctm.tm_sec = info->m_second;
168 
169  // unused
170  utctm.tm_wday = 0;
171  utctm.tm_yday = 0;
172  utctm.tm_isdst = 0;
173 
174 #ifdef _WIN32
175  epoch = (int64_t)_mkgmtime(&utctm);
176 #else
177  epoch = (int64_t)timegm(&utctm);
178 #endif
179 
180  rv = (epoch * 1000LL) + (info->m_nano / 1000000) + ((int64_t) info->m_utcOffset * 60000);
181  }
182  if (thisPtr)
183  thisPtr->m_msTime = rv;
184  return rv;
185 }
186 
191 void XsTimeStamp_toTimeInfo(struct XsTimeStamp const* thisPtr, struct XsTimeInfo* info)
192 {
193  struct tm tmUtc;
194 #ifdef _WIN32
195  __time64_t t;
196  t = (__time64_t)(thisPtr->m_msTime / 1000);
197  if (_gmtime64_s(&tmUtc, &t))
198  {
199  //in case of an error the result is an invalid XsTimeInfo
200  info->m_valid = 0;
201  return;
202  }
203 #elif (defined(__arm__) && defined(__ARMCC_VERSION))
204 #warning Function is not thread-safe for this platform/toolchain
205  time_t t;
206  t = (time_t)(thisPtr->m_msTime / 1000);
207  struct tm* tmUtcPtr = gmtime(&t);
208  if (tmUtcPtr == 0)
209  {
210  info->m_valid = 0;
211  return;
212  }
213  memcpy(&tmUtc, tmUtcPtr, sizeof(tmUtc));
214 #else
215  time_t t;
216  t = (time_t)(thisPtr->m_msTime / 1000);
217  if (gmtime_r(&t, &tmUtc) == 0)
218  {
219  //in case of an error the result is an invalid XsTimeInfo
220  info->m_valid = 0;
221  return;
222  }
223 #endif
224 
225  info->m_day = (uint8_t) tmUtc.tm_mday;
226  info->m_hour = (uint8_t) tmUtc.tm_hour;
227  info->m_minute = (uint8_t) tmUtc.tm_min;
228  info->m_month = (uint8_t) tmUtc.tm_mon + 1;
229  info->m_nano = (uint32_t)((thisPtr->m_msTime % 1000) * 1e6);
230  info->m_second = (uint8_t) tmUtc.tm_sec;
231  info->m_year = (uint16_t)(tmUtc.tm_year + 1900);
232  info->m_valid = 1;
233  info->m_utcOffset = 0;
234 }
235 
236 #ifndef XSENS_NO_STL
237 
241 void XsTimeStamp_toString(struct XsTimeStamp const* thisPtr, struct XsString* result)
242 {
243  XsTimeInfo info;
244  char buffer[32];
245  XsTimeStamp_toTimeInfo(thisPtr, &info);
246 
247  if (info.m_valid)
248  {
249  sprintf(buffer, "%04d/%02d/%02d %02d:%02d:%02d.%03d"
250  , (int) info.m_year
251  , (int) info.m_month
252  , (int) info.m_day
253  , (int) info.m_hour
254  , (int) info.m_minute
255  , (int) info.m_second
256  , (int)(thisPtr->m_msTime % 1000));
257  XsString_assign(result, 23, buffer);
258  }
259  else
260  XsString_assign(result, 23, "0000/00/00 00:00:00.000");
261 }
262 #endif
263 
265 extern int64_t XsTime_utcToLocalValue;
266 extern int64_t XsTime_localToUtcValue;
267 
273 void XsTimeStamp_utcToLocalTime(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local)
274 {
275  local->m_msTime = thisPtr->m_msTime + (thisPtr->m_msTime ? XsTime_utcToLocalValue : 0LL);
276 }
277 
282 void XsTimeStamp_localToUtcTime(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc)
283 {
284  utc->m_msTime = thisPtr->m_msTime + (thisPtr->m_msTime ? XsTime_localToUtcValue : 0LL);
285 }
286 
291 void XsTimeStamp_utcToLocalTime2(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local, const struct XsTimeInfo* info)
292 {
293  local->m_msTime = thisPtr->m_msTime - (thisPtr->m_msTime ? (int64_t) info->m_utcOffset * 60000 : 0LL);
294 }
295 
300 void XsTimeStamp_localToUtcTime2(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc, const struct XsTimeInfo* info)
301 {
302  utc->m_msTime = thisPtr->m_msTime + (thisPtr->m_msTime ? (int64_t) info->m_utcOffset * 60000 : 0LL);
303 }
304 
309 void XsTimeStamp_utcToLocalTime_ms(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* local, int64_t utcOffset)
310 {
311  local->m_msTime = thisPtr->m_msTime - (thisPtr->m_msTime ? utcOffset : 0LL);
312 }
313 
318 void XsTimeStamp_localToUtcTime_ms(struct XsTimeStamp const* thisPtr, struct XsTimeStamp* utc, int64_t utcOffset)
319 {
320  utc->m_msTime = thisPtr->m_msTime + (thisPtr->m_msTime ? utcOffset : 0LL);
321 }
322 
XsTimeStamp_utcToLocalTime_ms
void XsTimeStamp_utcToLocalTime_ms(struct XsTimeStamp const *thisPtr, struct XsTimeStamp *local, int64_t utcOffset)
Convert the supplied time from (assumed) UTC to local time, using the offset in utcOffset.
Definition: xstimestamp.c:309
XsTimeStamp::XsTimeStamp_now
int64_t XsTimeStamp_now(XsTimeStamp *dest)
Returns the current time in ms since the epoch (Jan 1st 1970)
Definition: xstimestamp.c:136
xsstring.h
XsTimeStamp_localToUtcTime2
void XsTimeStamp_localToUtcTime2(struct XsTimeStamp const *thisPtr, struct XsTimeStamp *utc, const struct XsTimeInfo *info)
Convert the supplied time from (assumed) local time to UTC, using the offset in info.
Definition: xstimestamp.c:300
XsTimeInfo::m_hour
uint8_t m_hour
The hour (if time is valid)
Definition: xstimeinfo.h:93
XsTimeInfo::m_utcOffset
int16_t m_utcOffset
Offset to UTC time in minutes. This value can be added to the stored time to get UTC time.
Definition: xstimeinfo.h:97
time.h
XsTimeStamp::XsTimeStamp_timeOfDay
double XsTimeStamp_timeOfDay(const XsTimeStamp *thisPtr)
Get the time of day component of the stored timestamp in seconds as a double precision value.
Definition: xstimestamp.c:90
xstimestamp.h
XsTimeStamp_localToUtcTime_ms
void XsTimeStamp_localToUtcTime_ms(struct XsTimeStamp const *thisPtr, struct XsTimeStamp *utc, int64_t utcOffset)
Convert the supplied time from (assumed) local time to UTC, using the offset in utcOffset.
Definition: xstimestamp.c:318
XsTimeStamp::XsTimeStamp_hourPart
int32_t XsTimeStamp_hourPart(const XsTimeStamp *thisPtr)
Returns the hours part of the time (in the range 0-23)
Definition: xstimestamp.c:125
XsTimeStamp::XsTimeStamp_minutePart
int32_t XsTimeStamp_minutePart(const XsTimeStamp *thisPtr)
Returns the minutes part of the time (in the range 0-59)
Definition: xstimestamp.c:118
XsTimeStamp::XsTimeStamp_maxValue
int64_t XsTimeStamp_maxValue(void)
Returns the maximum value of an XsTimeStamp.
Definition: xstimestamp.c:144
XsTimeInfo::m_valid
uint8_t m_valid
Validity indicator.
Definition: xstimeinfo.h:96
XsTimeStamp::XsTimeStamp_secondPart
int32_t XsTimeStamp_secondPart(const XsTimeStamp *thisPtr)
Returns the seconds part of the time (in the range 0-59)
Definition: xstimestamp.c:111
XsTimeInfo::m_second
uint8_t m_second
The second (if time is valid)
Definition: xstimeinfo.h:95
XsTimeInfo::m_day
uint8_t m_day
The day of the month (if date is valid)
Definition: xstimeinfo.h:92
XsTimeStamp_utcToLocalTime
void XsTimeStamp_utcToLocalTime(struct XsTimeStamp const *thisPtr, struct XsTimeStamp *local)
Convert the supplied time from (assumed) UTC to local time, using the system's local time zone knowle...
Definition: xstimestamp.c:273
uint32_t
unsigned int uint32_t
Definition: pstdint.h:485
XsTimeStamp::XsTimeStamp_milliSecondPart
int32_t XsTimeStamp_milliSecondPart(const XsTimeStamp *thisPtr)
Returns the millisecond part of the time (in the range 0-999)
Definition: xstimestamp.c:104
XsTimeStamp::XsTimeStamp_toString
void XsTimeStamp_toString(struct XsTimeStamp const *thisPtr, struct XsString *result)
Converts the timestamp into an XsString object in format YYYY/MM/DD hh:mm:ss.nnn.
Definition: xstimestamp.c:241
XsTimeInfo::m_nano
uint32_t m_nano
Nanosecond part of the time.
Definition: xstimeinfo.h:89
XsTimeStamp_utcToLocalTime2
void XsTimeStamp_utcToLocalTime2(struct XsTimeStamp const *thisPtr, struct XsTimeStamp *local, const struct XsTimeInfo *info)
Convert the supplied time from (assumed) UTC to local time, using the offset in info.
Definition: xstimestamp.c:291
xstimeinfo.h
XsTimeStamp::XsTimeStamp_setMilliSecondTime
void XsTimeStamp_setMilliSecondTime(XsTimeStamp *thisPtr, int64_t t)
Set the stored time to miliseconds.
Definition: xstimestamp.c:84
XsTimeStamp::XsTimeStamp_secondTime
int64_t XsTimeStamp_secondTime(const XsTimeStamp *thisPtr)
Returns the number of seconds elapsed since the epoch as stored in the XsTimeStamp.
Definition: xstimestamp.c:97
XsTimeInfo::m_month
uint8_t m_month
The month (if date is valid)
Definition: xstimeinfo.h:91
XsTimeStamp::XsTimeStamp_fromTimeInfo
int64_t XsTimeStamp_fromTimeInfo(struct XsTimeStamp *thisPtr, const struct XsTimeInfo *info)
Creates a (UTC) XsTimeStamp from an XsTimeInfo object.
Definition: xstimestamp.c:154
XsTimeInfo::m_year
uint16_t m_year
The year (if date is valid)
Definition: xstimeinfo.h:90
XsTimeInfo::m_minute
uint8_t m_minute
The minute (if time is valid)
Definition: xstimeinfo.h:94
XsTimeStamp_localToUtcTime
void XsTimeStamp_localToUtcTime(struct XsTimeStamp const *thisPtr, struct XsTimeStamp *utc)
Convert the supplied time from (assumed) local time to UTC, using the system's local time zone knowle...
Definition: xstimestamp.c:282
XsTime_timeStampNow
int64_t XsTime_timeStampNow(XsTimeStamp *now)
Returns the current time in ms since the epoch (Jan 1st 1970)
Definition: xstime.c:401
XsTimeStamp::m_msTime
int64_t m_msTime
The timestamp value.
Definition: xstimestamp.h:455
xstime.h
XsTimeStamp::XsTimeStamp_toTimeInfo
void XsTimeStamp_toTimeInfo(struct XsTimeStamp const *thisPtr, struct XsTimeInfo *info)
Converts the timestamp into an XsTimeInfo object.
Definition: xstimestamp.c:191
int32_t
signed int int32_t
Definition: pstdint.h:515
XsString_assign
void XsString_assign(XsString *thisPtr, XsSize count, const char *src)
Reinitializes the XsArray with space for count items and copies them from src.
Definition: xsstring.c:142
XsString
A 0-terminated managed string of characters.
XsTimeStamp
This class contains method to set, retrieve and compare timestamps.
Definition: xstimestamp.h:115
XsTimeInfo
A structure for storing Time values.
Definition: xstimeinfo.h:87


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20