Main MRPT website > C++ reference for MRPT 1.5.7
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 with gcc
10 
11 #include "xsens_time.h"
12 #include <sys/timeb.h>
13 
14 #ifdef _WIN32
15 # include <windows.h>
16 #else
17 # include <unistd.h>
18 # include <sys/time.h>
19 #endif
20 
21 namespace xsens {
22 
23 //////////////////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////// Other functions ////////////////////////////////////
25 //////////////////////////////////////////////////////////////////////////////////////////
26 
27 //////////////////////////////////////////////////////////////////////////////////////////
28 // A platform-independent clock.
29 uint32_t getTimeOfDay(tm* date_, time_t* secs_)
30 {
31 #ifdef _WIN32
32  _timeb tp; //__timeb32 tp;
33 
34  _ftime(&tp); //_ftime32_s(&tp);
35 
36  if (date_ != NULL)
37  {
38  time_t tin = tp.time; //__time32_t tin = tp.time;
39  *date_ = *localtime(&tin); // _localtime32_s(date_,&tin);
40  }
41  if (secs_ != NULL)
42  secs_[0] = tp.time;
43 
44  // 86400 = 24*60*60 = secs in a day, this gives us the seconds since midnight
45  return (1000 * ((uint32_t) tp.time % XSENS_SEC_PER_DAY)) + tp.millitm;
46 
47 /* Jerome Monceaux : 2011/03/08
48  * Add a special case for apple
49  * because librt is not available
50  * so clock_gettime as well
51  */
52 #else
53 # ifdef __APPLE__
54  struct timeval tv;
55  timespec tp;
56 
57  gettimeofday(&tv, NULL);
58  tp.tv_sec = tv.tv_sec;
59  tp.tv_nsec = tv.tv_usec*1000;
60 # else
61  timespec tp;
62  clock_gettime(CLOCK_REALTIME, &tp); // compile with -lrt
63 # endif
64 
65  if (date_ != NULL)
66  localtime_r(&tp.tv_sec,date_);
67 
68  if (secs_ != NULL)
69  secs_[0] = tp.tv_sec;
70 
71  // 86400 = 24*60*60 = secs in a day, this gives us the seconds since midnight
72  return (1000 * (tp.tv_sec % XSENS_SEC_PER_DAY)) + (tp.tv_nsec/1000000);
73 #endif
74 }
75 
76 //////////////////////////////////////////////////////////////////////////////////////////
77 // A platform-independent sleep routine.
78 void msleep(uint32_t ms)
79 {
80 #ifdef _WIN32
81  Sleep(ms);
82 #else
83  clock_t end = clock() + (CLOCKS_PER_SEC/1000) * ms;
84  clock_t diff;
85 
86  while ((diff = end - clock()) > 0)
87  {
88  diff = (1000 * diff) / CLOCKS_PER_SEC;
89  if (diff > 1000)
90  sleep(diff / 1000);
91  else
92  usleep(diff * 1000);
93  }
94 #endif
95 }
96 
98 {
99  TimeStamp ms;
100  time_t s;
101  ms = (TimeStamp) getTimeOfDay(NULL,&s);
102  ms = (ms % 1000) + (((TimeStamp)s)*1000);
103 
104  return ms;
105 }
106 
107 } // 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:97
GLdouble s
Definition: glext.h:3602
uint32_t getTimeOfDay(tm *date_, time_t *secs_)
A platform-independent clock.
Definition: xsens_time.cpp:29
void msleep(uint32_t ms)
A platform-independent sleep routine.
Definition: xsens_time.cpp:78
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
GLuint GLuint end
Definition: glext.h:3512
uint64_t TimeStamp
A real-time timestamp (ms)
Definition: xsens_time.h:24
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:92
unsigned __int32 uint32_t
Definition: rptypes.h:49



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019