Main MRPT website > C++ reference for MRPT 1.9.9
xsens_time.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 // Note, this function requires compiler option "-lrt" to be set when compiling
10 // with gcc
11 
12 #include "xsens_time.h"
13 #include <sys/timeb.h>
14 
15 #ifdef _WIN32
16 #include <windows.h>
17 #else
18 #include <unistd.h>
19 #include <sys/time.h>
20 #endif
21 
22 #include <chrono>
23 #include <thread>
24 
25 namespace xsens
26 {
27 //////////////////////////////////////////////////////////////////////////////////////////
28 //////////////////////////////////// Other functions
29 ///////////////////////////////////////
30 //////////////////////////////////////////////////////////////////////////////////////////
31 
32 //////////////////////////////////////////////////////////////////////////////////////////
33 // A platform-independent clock.
34 uint32_t getTimeOfDay(tm* date_, time_t* secs_)
35 {
36 #ifdef _WIN32
37  _timeb tp; //__timeb32 tp;
38 
39  _ftime(&tp); //_ftime32_s(&tp);
40 
41  if (date_ != nullptr)
42  {
43  time_t tin = tp.time; //__time32_t tin = tp.time;
44  *date_ = *localtime(&tin); // _localtime32_s(date_,&tin);
45  }
46  if (secs_ != nullptr) secs_[0] = tp.time;
47 
48  // 86400 = 24*60*60 = secs in a day, this gives us the seconds since
49  // midnight
50  return (1000 * ((uint32_t)tp.time % XSENS_SEC_PER_DAY)) + tp.millitm;
51 
52 /* Jerome Monceaux : 2011/03/08
53  * Add a special case for apple
54  * because librt is not available
55  * so clock_gettime as well
56  */
57 #else
58 #ifdef __APPLE__
59  struct timeval tv;
60  timespec tp;
61 
62  gettimeofday(&tv, nullptr);
63  tp.tv_sec = tv.tv_sec;
64  tp.tv_nsec = tv.tv_usec * 1000;
65 #else
66  timespec tp;
67  clock_gettime(CLOCK_REALTIME, &tp); // compile with -lrt
68 #endif
69 
70  if (date_ != nullptr) localtime_r(&tp.tv_sec, date_);
71 
72  if (secs_ != nullptr) secs_[0] = tp.tv_sec;
73 
74  // 86400 = 24*60*60 = secs in a day, this gives us the seconds since
75  // midnight
76  return (1000 * (tp.tv_sec % XSENS_SEC_PER_DAY)) + (tp.tv_nsec / 1000000);
77 #endif
78 }
79 
80 //////////////////////////////////////////////////////////////////////////////////////////
81 // A platform-independent sleep routine.
82 void msleep(uint32_t ms)
83 {
84 #ifdef _WIN32
85  Sleep(ms);
86 #else
87  clock_t end = clock() + (CLOCKS_PER_SEC / 1000) * ms;
88  clock_t diff;
89 
90  while ((diff = end - clock()) > 0)
91  {
92  diff = (1000 * diff) / CLOCKS_PER_SEC;
93  if (diff > 1000)
94  std::this_thread::sleep_for(std::chrono::milliseconds(diff / 1000));
95  else
96  usleep(diff * 1000);
97  }
98 #endif
99 }
100 
102 {
103  TimeStamp ms;
104  time_t s;
105  ms = (TimeStamp)getTimeOfDay(nullptr, &s);
106  ms = (ms % 1000) + (((TimeStamp)s) * 1000);
107 
108  return ms;
109 }
110 
111 } // end of xsens namespace
#define XSENS_SEC_PER_DAY
The number of seconds in a normal day.
Definition: xsens_time.h:19
TimeStamp timeStampNow(void)
Definition: xsens_time.cpp:101
GLdouble s
Definition: glext.h:3676
void msleep(uint32_t ms)
A platform-independent sleep routine.
Definition: xsens_time.cpp:82
GLuint GLuint end
Definition: glext.h:3528
uint64_t TimeStamp
A real-time timestamp (ms)
Definition: xsens_time.h:24
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:95
unsigned __int32 uint32_t
Definition: rptypes.h:47
uint32_t getTimeOfDay(tm *date_, time_t *secs_)
A platform-independent clock.
Definition: xsens_time.cpp:34



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019