Main MRPT website > C++ reference for MRPT 1.9.9
datetime.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 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/utils_defs.h>
13 #include <mrpt/system/datetime.h>
14 #include <mrpt/system/os.h>
15 
16 #ifdef MRPT_OS_WINDOWS
17 #include <conio.h>
18 #include <windows.h>
19 #include <process.h>
20 #include <tlhelp32.h>
21 #include <sys/utime.h>
22 #include <io.h>
23 #include <direct.h>
24 #else
25 #include <pthread.h>
26 #include <termios.h>
27 #include <unistd.h>
28 #include <sys/select.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31 #include <utime.h>
32 #include <errno.h>
33 #endif
34 
35 #include <time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 
39 using namespace mrpt;
40 using namespace mrpt::system;
41 using namespace std;
42 
44 {
45  return uint64_t(t) * UINT64_C(10000000) +
46  UINT64_C(116444736) * UINT64_C(1000000000);
47 }
48 
50 {
51  return uint64_t(t * 10000000.0) +
52  UINT64_C(116444736) * UINT64_C(1000000000);
53 }
54 
56 {
57  return double(t - UINT64_C(116444736) * UINT64_C(1000000000)) / 10000000.0;
58 }
59 
60 /* Jerome Monceaux 2011/03/08: bilock@gmail.com
61  * comment this include because it is not find
62  * under snow leopard
63  */
64 #if defined(MRPT_OS_APPLE)
65 //# include <CFBase.h> // for CFAbsoluteTimeGetCurrent
66 #include <sys/timeb.h>
67 #include <sys/types.h>
68 #endif
69 
70 /*---------------------------------------------------------------
71  Returns the current system time.
72  ---------------------------------------------------------------*/
74 {
75 #ifdef MRPT_OS_WINDOWS
76  FILETIME t;
77  GetSystemTimeAsFileTime(&t);
78  return (((uint64_t)t.dwHighDateTime) << 32) | ((uint64_t)t.dwLowDateTime);
79 #elif defined(MRPT_OS_APPLE)
80 
81  /* Jerome Monceaux 2011/03/08: bilock@gmail.com
82  * comment the next line because it does not compile
83  * under snow osx and an exception was thrown systematically
84  */
85  struct timeval tv;
86  timespec tim;
87 
88  gettimeofday(&tv, nullptr);
89  tim.tv_sec = tv.tv_sec;
90  tim.tv_nsec = tv.tv_usec * 1000;
91 
92  return time_tToTimestamp(tim.tv_sec) + tim.tv_nsec / 100;
93 #else
94  timespec tim;
95  clock_gettime(CLOCK_REALTIME, &tim);
96  return time_tToTimestamp(tim.tv_sec) + tim.tv_nsec / 100;
97 #endif
98 }
99 
100 /*---------------------------------------------------------------
101  timestampToParts
102  ---------------------------------------------------------------*/
104 {
105  const double T = mrpt::system::timestampTotime_t(t);
106  double sec_frac = T - floor(T);
107  ASSERT_(sec_frac < 1.0);
108 
109  const time_t tt = time_t(T);
110 
111  struct tm* parts = localTime ? localtime(&tt) : gmtime(&tt);
112  ASSERTMSG_(parts, "Malformed timestamp");
113 
114  p.year = parts->tm_year + 1900;
115  p.month = parts->tm_mon + 1;
116  p.day = parts->tm_mday;
117  p.day_of_week = parts->tm_wday + 1;
118  p.daylight_saving = parts->tm_isdst;
119  p.hour = parts->tm_hour;
120  p.minute = parts->tm_min;
121  p.second = parts->tm_sec + sec_frac;
122 }
123 
124 /*---------------------------------------------------------------
125  buildTimestampFromParts
126  ---------------------------------------------------------------*/
128 {
129  struct tm parts;
130 
131  parts.tm_year = p.year - 1900;
132  parts.tm_mon = p.month - 1;
133  parts.tm_mday = p.day;
134  parts.tm_wday = p.day_of_week - 1;
135  parts.tm_isdst = p.daylight_saving;
136  parts.tm_hour = p.hour;
137  parts.tm_min = p.minute;
138  parts.tm_sec = int(p.second);
139 
140  double sec_frac = p.second - parts.tm_sec;
141 
142  time_t tt = mrpt::system::os::timegm(&parts); // Local time: mktime
143 
144  return mrpt::system::time_tToTimestamp(double(tt) + sec_frac);
145 }
146 
147 /*---------------------------------------------------------------
148  buildTimestampFromPartsLocalTime
149  ---------------------------------------------------------------*/
151 {
152  struct tm parts;
153 
154  parts.tm_year = p.year - 1900;
155  parts.tm_mon = p.month - 1;
156  parts.tm_mday = p.day;
157  parts.tm_wday = p.day_of_week - 1;
158  parts.tm_isdst = p.daylight_saving;
159  parts.tm_hour = p.hour;
160  parts.tm_min = p.minute;
161  parts.tm_sec = int(p.second);
162 
163  double sec_frac = p.second - parts.tm_sec;
164 
165  time_t tt = mktime(&parts);
166 
167  return mrpt::system::time_tToTimestamp(double(tt) + sec_frac);
168 }
169 
170 /*---------------------------------------------------------------
171  Returns the current local time.
172  ---------------------------------------------------------------*/
174 {
175 #ifdef MRPT_OS_WINDOWS
176  FILETIME tt, t;
177  GetSystemTimeAsFileTime(&tt);
178  FileTimeToLocalFileTime(&tt, &t);
179 
180  return (((uint64_t)t.dwHighDateTime) << 32) | ((uint64_t)t.dwLowDateTime);
181 #elif defined(MRPT_OS_APPLE)
182  // See:
183  // http://www.wand.net.nz/~smr26/wordpress/2009/01/19/monotonic-time-in-mac-os-x/
184  THROW_EXCEPTION("to do")
185 #else
186  timespec tim;
187  clock_gettime(CLOCK_REALTIME, &tim);
188 
189  time_t tt;
190  struct tm* timeinfo;
191  time(&tt);
192  timeinfo = localtime(&tt);
193 
194  return time_tToTimestamp(mktime(timeinfo)) + tim.tv_nsec / 100;
195 #endif
196 }
197 
199  const mrpt::system::TTimeStamp tim, const double num_seconds)
200 {
201  return static_cast<mrpt::system::TTimeStamp>(
202  tim + static_cast<int64_t>(num_seconds * 10000000.0));
203 }
204 
205 /*---------------------------------------------------------------
206  timeDifference
207  ---------------------------------------------------------------*/
210 {
211  MRPT_START
214 
215  return (int64_t(t2) - int64_t(t1)) / 10000000.0;
216 
217  MRPT_END
218 }
219 
220 /*---------------------------------------------------------------
221  secondsToTimestamp
222  ---------------------------------------------------------------*/
224 {
225  return (mrpt::system::TTimeStamp)(nSeconds * 10000000.0);
226 }
227 
228 /*---------------------------------------------------------------
229  formatTimeInterval
230  ---------------------------------------------------------------*/
232 {
233  double timeSeconds = (t < 0) ? (-t) : t;
234 
235  unsigned int nHours = (unsigned int)timeSeconds / 3600;
236  unsigned int nMins = ((unsigned int)timeSeconds % 3600) / 60;
237  unsigned int nSecs = (unsigned int)timeSeconds % 60;
238  unsigned int milSecs =
239  (unsigned int)(1000 * (timeSeconds - floor(timeSeconds)));
240 
241  return format("%02u:%02u:%02u.%03u", nHours, nMins, nSecs, milSecs);
242 }
243 
244 /*---------------------------------------------------------------
245  Convert a timestamp into this textual form: YEAR/MONTH/DAY,HH:MM:SS.MMM
246  ---------------------------------------------------------------*/
248 {
249  if (t == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
250 
251  uint64_t tmp = (t - ((uint64_t)116444736 * 1000000000));
252  time_t auxTime = tmp / (uint64_t)10000000;
253  unsigned int secFractions =
254  (unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
255  tm* ptm = gmtime(&auxTime);
256 
257  if (!ptm) return std::string("(Malformed timestamp)");
258 
259  return format(
260  "%u/%02u/%02u,%02u:%02u:%02u.%06u", 1900 + ptm->tm_year,
261  ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min,
262  (unsigned int)ptm->tm_sec, secFractions);
263 }
264 
265 /*---------------------------------------------------------------
266  Convert a timestamp into this textual form (in local time):
267  YEAR/MONTH/DAY,HH:MM:SS.MMM
268  ---------------------------------------------------------------*/
270 {
271  if (t == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
272 
273  uint64_t tmp = (t - ((uint64_t)116444736 * 1000000000));
274  time_t auxTime = tmp / (uint64_t)10000000;
275  unsigned int secFractions =
276  (unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
277  tm* ptm = localtime(&auxTime);
278 
279  if (!ptm) return "(Malformed timestamp)";
280 
281  return format(
282  "%u/%02u/%02u,%02u:%02u:%02u.%06u", 1900 + ptm->tm_year,
283  ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min,
284  (unsigned int)ptm->tm_sec, secFractions);
285 }
286 
287 /*---------------------------------------------------------------
288  extractDayTimeFromTimestamp
289  ---------------------------------------------------------------*/
292 {
293  MRPT_START
295 
296 #ifdef MRPT_OS_WINDOWS
297  SYSTEMTIME sysT;
298  FileTimeToSystemTime((FILETIME*)&t, &sysT);
299  return sysT.wHour * 3600.0 + sysT.wMinute * 60.0 + sysT.wSecond +
300  sysT.wMilliseconds * 0.001;
301 #else
302  time_t auxTime =
303  (t - ((uint64_t)116444736 * 1000000000)) / (uint64_t)10000000;
304  tm* ptm = gmtime(&auxTime);
305  ASSERTMSG_(ptm, "Malformed timestamp");
306  return ptm->tm_hour * 3600.0 + ptm->tm_min * 60.0 + ptm->tm_sec;
307 #endif
308  MRPT_END
309 }
310 
311 /*---------------------------------------------------------------
312  Convert a timestamp into this textual form: HH:MM:SS.MMM
313  ---------------------------------------------------------------*/
315  const mrpt::system::TTimeStamp t, unsigned int secondFractionDigits)
316 {
317  if (t == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
318 
319  uint64_t tmp = (t - ((uint64_t)116444736 * 1000000000));
320  const time_t auxTime = tmp / (uint64_t)10000000;
321  const tm* ptm = localtime(&auxTime);
322 
323  unsigned int secFractions =
324  (unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
325  // We start with 10^{-6} second units: reduce if requested by user:
326  const unsigned int user_secondFractionDigits = secondFractionDigits;
327  while (secondFractionDigits++ < 6) secFractions = secFractions / 10;
328 
329  return format(
330  "%02u:%02u:%02u.%0*u", ptm->tm_hour, ptm->tm_min,
331  (unsigned int)ptm->tm_sec, user_secondFractionDigits, secFractions);
332 }
333 
334 /*---------------------------------------------------------------
335  Convert a timestamp into this textual form: HH:MM:SS.MMM
336  ---------------------------------------------------------------*/
338 {
339  if (t == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
340 
341  uint64_t tmp = (t - ((uint64_t)116444736 * 1000000000));
342  time_t auxTime = tmp / (uint64_t)10000000;
343  unsigned int secFractions =
344  (unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
345  tm* ptm = gmtime(&auxTime);
346  if (!ptm) return string("(Malformed timestamp)");
347 
348  return format(
349  "%02u:%02u:%02u.%06u", ptm->tm_hour, ptm->tm_min,
350  (unsigned int)ptm->tm_sec, secFractions);
351 }
352 
353 /*---------------------------------------------------------------
354  Convert a timestamp into this textual form: YEAR/MONTH/DAY
355  ---------------------------------------------------------------*/
357 {
358  if (t == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
359 
360  uint64_t tmp = (t - ((uint64_t)116444736 * 1000000000));
361  time_t auxTime = tmp / (uint64_t)10000000;
362  tm* ptm = gmtime(&auxTime);
363  if (!ptm) return string("(Malformed timestamp)");
364 
365  return format(
366  "%u/%02u/%02u", 1900 + ptm->tm_year, ptm->tm_mon + 1, ptm->tm_mday);
367 }
368 
369 /** This function implements time interval formatting: Given a time in seconds,
370  * it will return a string describing the interval with the most appropriate
371  * unit.
372  * E.g.: 1.23 year, 3.50 days, 9.3 hours, 5.3 minutes, 3.34 sec, 178.1 ms, 87.1
373  * us.
374  */
376 {
377  if (seconds >= 365 * 24 * 3600)
378  return format("%.2f years", seconds / (365 * 24 * 3600));
379  else if (seconds >= 24 * 3600)
380  return format("%.2f days", seconds / (24 * 3600));
381  else if (seconds >= 3600)
382  return format("%.2f hours", seconds / 3600);
383  else if (seconds >= 60)
384  return format("%.2f minutes", seconds / 60);
385  else if (seconds >= 1)
386  return format("%.2f sec", seconds);
387  else if (seconds >= 1e-3)
388  return format("%.2f ms", seconds * 1e3);
389  else if (seconds >= 1e-6)
390  return format("%.2f us", seconds * 1e6);
391  else
392  return format("%.2f ns", seconds * 1e9);
393 }
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:32
void timestampToParts(TTimeStamp t, TTimeParts &p, bool localTime=false)
Gets the individual parts of a date/time (days, hours, minutes, seconds) - UTC time or local time...
Definition: datetime.cpp:103
GLdouble GLdouble t
Definition: glext.h:3689
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
Definition: datetime.cpp:73
time_t timegm(struct tm *tm)
An OS-independent version of timegm (which is not present in all compilers): converts a time structur...
Definition: os.cpp:109
#define THROW_EXCEPTION(msg)
mrpt::system::TTimeStamp buildTimestampFromParts(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in UTC)
Definition: datetime.cpp:127
double extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp t)
Returns the number of seconds ellapsed from midnight in the given timestamp.
Definition: datetime.cpp:290
std::string timeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC): HH:MM:SS.MMMMMM.
Definition: datetime.cpp:337
STL namespace.
std::string formatTimeInterval(const double timeSeconds)
Returns a formated string with the given time difference (passed as the number of seconds)...
Definition: datetime.cpp:231
mrpt::system::TTimeStamp getCurrentLocalTime()
Returns the current (local) time.
Definition: datetime.cpp:173
std::string dateToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form: YEAR/MONTH/DAY.
Definition: datetime.cpp:356
#define MRPT_END
__int64 int64_t
Definition: rptypes.h:49
The parts of a date/time (it&#39;s like the standard &#39;tm&#39; but with fractions of seconds).
Definition: datetime.h:38
std::string intervalFormat(const double seconds)
This function implements time interval formatting: Given a time in seconds, it will return a string d...
Definition: datetime.cpp:375
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLsizei const GLchar ** string
Definition: glext.h:4101
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:16
mrpt::system::TTimeStamp buildTimestampFromPartsLocalTime(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in local time)
Definition: datetime.cpp:150
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string dateTimeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
Definition: datetime.cpp:247
std::string timeLocalToString(const mrpt::system::TTimeStamp t, unsigned int secondFractionDigits=6)
Convert a timestamp into this textual form (in local time): HH:MM:SS.MMMMMM.
Definition: datetime.cpp:314
#define ASSERT_(f)
mrpt::system::TTimeStamp timestampAdd(const mrpt::system::TTimeStamp tim, const double num_seconds)
Shifts a timestamp the given amount of seconds (>0: forwards in time, <0: backwards) ...
Definition: datetime.cpp:198
mrpt::system::TTimeStamp secondsToTimestamp(const double nSeconds)
Transform a time interval (in seconds) into TTimeStamp (e.g.
Definition: datetime.cpp:223
double timeDifference(const mrpt::system::TTimeStamp t_first, const mrpt::system::TTimeStamp t_later)
Returns the time difference from t1 to t2 (positive if t2 is posterior to t1), in seconds...
Definition: datetime.cpp:208
std::string dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
Definition: datetime.cpp:269
mrpt::system::TTimeStamp time_tToTimestamp(const double t)
Transform from standard "time_t" (actually a double number, it can contain fractions of seconds) to T...
Definition: datetime.cpp:49
#define ASSERTMSG_(f, __ERROR_MSG)
GLfloat GLfloat p
Definition: glext.h:6305
double timestampTotime_t(const mrpt::system::TTimeStamp t)
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...
Definition: datetime.cpp:55



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