Main MRPT website > C++ reference for MRPT 1.5.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) + UINT64_C(116444736) * UINT64_C(1000000000);
46 }
47 
49 {
50  return uint64_t(t*10000000.0)+ UINT64_C(116444736)*UINT64_C(1000000000);
51 }
52 
54 {
55  return double(t - UINT64_C(116444736)*UINT64_C(1000000000)) / 10000000.0;
56 }
57 
58 /* Jerome Monceaux 2011/03/08: bilock@gmail.com
59  * comment this include because it is not find
60  * under snow leopard
61  */
62 #if defined(MRPT_OS_APPLE)
63 //# include <CFBase.h> // for CFAbsoluteTimeGetCurrent
64 # include <sys/timeb.h>
65 # include <sys/types.h>
66 #endif
67 
68 /*---------------------------------------------------------------
69  Returns the current system time.
70  ---------------------------------------------------------------*/
72 {
73 #ifdef MRPT_OS_WINDOWS
74  FILETIME t;
75  GetSystemTimeAsFileTime(&t);
76  return (((uint64_t)t.dwHighDateTime) << 32) | ((uint64_t)t.dwLowDateTime);
77 #elif defined(MRPT_OS_APPLE)
78 
79  /* Jerome Monceaux 2011/03/08: bilock@gmail.com
80  * comment the next line because it does not compile
81  * under snow osx and an exception was thrown systematically
82  */
83  struct timeval tv;
84  timespec tim;
85 
86  gettimeofday(&tv, NULL);
87  tim.tv_sec = tv.tv_sec;
88  tim.tv_nsec = tv.tv_usec*1000;
89 
90  return time_tToTimestamp( tim.tv_sec ) + tim.tv_nsec/100;
91 #else
92  timespec tim;
93  clock_gettime(CLOCK_REALTIME, &tim);
94  return time_tToTimestamp( tim.tv_sec ) + tim.tv_nsec/100;
95 #endif
96 }
97 
98 /*---------------------------------------------------------------
99  timestampToParts
100  ---------------------------------------------------------------*/
102 {
103  const double T = mrpt::system::timestampTotime_t(t);
104  double sec_frac = T - floor(T);
105  ASSERT_(sec_frac<1.0);
106 
107  const time_t tt = time_t(T);
108 
109  struct tm * parts = localTime ? localtime(&tt) : gmtime(&tt);
110  ASSERTMSG_(parts, "Malformed timestamp");
111 
112  p.year = parts->tm_year + 1900;
113  p.month = parts->tm_mon + 1;
114  p.day = parts->tm_mday;
115  p.day_of_week = parts->tm_wday + 1;
116  p.daylight_saving = parts->tm_isdst;
117  p.hour = parts->tm_hour;
118  p.minute = parts->tm_min;
119  p.second = parts->tm_sec + sec_frac;
120 }
121 
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: http://www.wand.net.nz/~smr26/wordpress/2009/01/19/monotonic-time-in-mac-os-x/
183  THROW_EXCEPTION("to do")
184 #else
185  timespec tim;
186  clock_gettime(CLOCK_REALTIME, &tim);
187 
188  time_t tt;
189  struct tm * timeinfo;
190  time(&tt);
191  timeinfo = localtime( &tt );
192 
193  return time_tToTimestamp( mktime(timeinfo) ) + tim.tv_nsec/100;
194 #endif
195 }
196 
198 {
199  return static_cast<mrpt::system::TTimeStamp>(tim + static_cast<int64_t>(num_seconds*10000000.0));
200 }
201 
202 /*---------------------------------------------------------------
203  timeDifference
204  ---------------------------------------------------------------*/
206 {
207  MRPT_START
210 
211  return (int64_t(t2)- int64_t(t1))/10000000.0;
212 
213  MRPT_END
214 }
215 
216 /*---------------------------------------------------------------
217  secondsToTimestamp
218  ---------------------------------------------------------------*/
220 {
221  return (mrpt::system::TTimeStamp)(nSeconds*10000000.0);
222 }
223 
224 /*---------------------------------------------------------------
225  formatTimeInterval
226  ---------------------------------------------------------------*/
227 string mrpt::system::formatTimeInterval( const double t )
228 {
229  double timeSeconds = (t<0) ? (-t) : t;
230 
231  unsigned int nHours = (unsigned int)timeSeconds / 3600;
232  unsigned int nMins = ((unsigned int)timeSeconds % 3600) / 60 ;
233  unsigned int nSecs = (unsigned int)timeSeconds % 60;
234  unsigned int milSecs= (unsigned int) ( 1000*(timeSeconds - floor(timeSeconds)) );
235 
236  return format(
237  "%02u:%02u:%02u.%03u",
238  nHours,
239  nMins,
240  nSecs,
241  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 = (unsigned int)( 1000000 * (tmp % 10000000) / 10000000.0 );
254  tm *ptm = gmtime( &auxTime );
255 
256  if (!ptm)
257  return std::string("(Malformed timestamp)");
258 
259  return format(
260  "%u/%02u/%02u,%02u:%02u:%02u.%06u",
261  1900+ptm->tm_year,
262  ptm->tm_mon+1,
263  ptm->tm_mday,
264  ptm->tm_hour,
265  ptm->tm_min,
266  (unsigned int)ptm->tm_sec,
267  secFractions );
268 }
269 
270 /*---------------------------------------------------------------
271  Convert a timestamp into this textual form (in local time):
272  YEAR/MONTH/DAY,HH:MM:SS.MMM
273  ---------------------------------------------------------------*/
275 {
276  if (t==INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
277 
278  uint64_t tmp = (t - ((uint64_t)116444736*1000000000));
279  time_t auxTime = tmp / (uint64_t)10000000;
280  unsigned int secFractions = (unsigned int)( 1000000 * (tmp % 10000000) / 10000000.0 );
281  tm *ptm = localtime( &auxTime );
282 
283  if (!ptm) return "(Malformed timestamp)";
284 
285  return format(
286  "%u/%02u/%02u,%02u:%02u:%02u.%06u",
287  1900+ptm->tm_year,
288  ptm->tm_mon+1,
289  ptm->tm_mday,
290  ptm->tm_hour,
291  ptm->tm_min,
292  (unsigned int)ptm->tm_sec,
293  secFractions );
294 }
295 
296 /*---------------------------------------------------------------
297  extractDayTimeFromTimestamp
298  ---------------------------------------------------------------*/
300 {
301  MRPT_START
303 
304 #ifdef MRPT_OS_WINDOWS
305  SYSTEMTIME sysT;
306  FileTimeToSystemTime( (FILETIME*)&t, &sysT );
307  return sysT.wHour * 3600.0 + sysT.wMinute * 60.0 + sysT.wSecond + sysT.wMilliseconds * 0.001;
308 #else
309  time_t auxTime = (t - ((uint64_t)116444736*1000000000)) / (uint64_t)10000000;
310  tm *ptm = gmtime( &auxTime );
311  ASSERTMSG_(ptm, "Malformed timestamp");
312  return ptm->tm_hour * 3600.0 + ptm->tm_min * 60.0 + ptm->tm_sec;
313 #endif
314  MRPT_END
315 }
316 
317 
318 /*---------------------------------------------------------------
319  Convert a timestamp into this textual form: HH:MM:SS.MMM
320  ---------------------------------------------------------------*/
321 string mrpt::system::timeLocalToString(const mrpt::system::TTimeStamp t, unsigned int secondFractionDigits)
322 {
323  if (t==INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
324 
325  uint64_t tmp = (t - ((uint64_t)116444736*1000000000));
326  const time_t auxTime = tmp / (uint64_t)10000000;
327  const tm *ptm = localtime( &auxTime );
328 
329  unsigned int secFractions = (unsigned int)( 1000000 * (tmp % 10000000) / 10000000.0 );
330  // We start with 10^{-6} second units: reduce if requested by user:
331  const unsigned int user_secondFractionDigits = secondFractionDigits;
332  while (secondFractionDigits++<6)
333  secFractions = secFractions / 10;
334 
335  return format(
336  "%02u:%02u:%02u.%0*u",
337  ptm->tm_hour,
338  ptm->tm_min,
339  (unsigned int)ptm->tm_sec,
340  user_secondFractionDigits,
341  secFractions );
342 }
343 
344 /*---------------------------------------------------------------
345  Convert a timestamp into this textual form: HH:MM:SS.MMM
346  ---------------------------------------------------------------*/
348 {
349  if (t==INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
350 
351  uint64_t tmp = (t - ((uint64_t)116444736*1000000000));
352  time_t auxTime = tmp / (uint64_t)10000000;
353  unsigned int secFractions = (unsigned int)( 1000000 * (tmp % 10000000) / 10000000.0 );
354  tm *ptm = gmtime( &auxTime );
355  if (!ptm)
356  return string("(Malformed timestamp)");
357 
358  return format(
359  "%02u:%02u:%02u.%06u",
360  ptm->tm_hour,
361  ptm->tm_min,
362  (unsigned int)ptm->tm_sec,
363  secFractions );
364 }
365 
366 /*---------------------------------------------------------------
367  Convert a timestamp into this textual form: YEAR/MONTH/DAY
368  ---------------------------------------------------------------*/
370 {
371  if (t==INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
372 
373  uint64_t tmp = (t - ((uint64_t)116444736*1000000000));
374  time_t auxTime = tmp / (uint64_t)10000000;
375  tm *ptm = gmtime( &auxTime );
376  if (!ptm)
377  return string("(Malformed timestamp)");
378 
379  return format(
380  "%u/%02u/%02u",
381  1900+ptm->tm_year,
382  ptm->tm_mon+1,
383  ptm->tm_mday
384  );
385 }
386 
387 /** This function implements time interval formatting: Given a time in seconds, it will return a string describing the interval with the most appropriate unit.
388  * E.g.: 1.23 year, 3.50 days, 9.3 hours, 5.3 minutes, 3.34 sec, 178.1 ms, 87.1 us.
389  */
391 mrpt::system::intervalFormat(const double seconds)
392 {
393  if (seconds>=365*24*3600)
394  return format("%.2f years",seconds/(365*24*3600) );
395  else if (seconds>=24*3600)
396  return format("%.2f days",seconds/(24*3600));
397  else if (seconds>=3600)
398  return format("%.2f hours",seconds/3600);
399  else if (seconds>=60)
400  return format("%.2f minutes",seconds/60);
401  else if (seconds>=1)
402  return format("%.2f sec",seconds);
403  else if (seconds>=1e-3)
404  return format("%.2f ms",seconds*1e3);
405  else if (seconds>=1e-6)
406  return format("%.2f us",seconds*1e6);
407  else return format("%.2f ns",seconds*1e9);
408 }
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:30
void BASE_IMPEXP 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:101
GLdouble GLdouble t
Definition: glext.h:3610
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
mrpt::system::TTimeStamp BASE_IMPEXP getCurrentTime()
Returns the current (UTC) system time.
Definition: datetime.cpp:71
time_t BASE_IMPEXP timegm(struct tm *tm)
An OS-independent version of timegm (which is not present in all compilers): converts a time structur...
Definition: os.cpp:116
#define THROW_EXCEPTION(msg)
mrpt::system::TTimeStamp BASE_IMPEXP buildTimestampFromParts(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in UTC)
Definition: datetime.cpp:127
double BASE_IMPEXP extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp t)
Returns the number of seconds ellapsed from midnight in the given timestamp.
Definition: datetime.cpp:299
std::string BASE_IMPEXP timeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC): HH:MM:SS.MMMMMM.
Definition: datetime.cpp:347
STL namespace.
std::string BASE_IMPEXP formatTimeInterval(const double timeSeconds)
Returns a formated string with the given time difference (passed as the number of seconds)...
Definition: datetime.cpp:227
mrpt::system::TTimeStamp BASE_IMPEXP getCurrentLocalTime()
Returns the current (local) time.
Definition: datetime.cpp:173
std::string BASE_IMPEXP dateToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form: YEAR/MONTH/DAY.
Definition: datetime.cpp:369
#define MRPT_END
__int64 int64_t
Definition: rptypes.h:51
The parts of a date/time (it&#39;s like the standard &#39;tm&#39; but with fractions of seconds).
Definition: datetime.h:35
std::string BASE_IMPEXP intervalFormat(const double seconds)
This function implements time interval formatting: Given a time in seconds, it will return a string d...
Definition: datetime.cpp:391
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
GLsizei const GLchar ** string
Definition: glext.h:3919
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:17
mrpt::system::TTimeStamp BASE_IMPEXP 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:52
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string BASE_IMPEXP 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 BASE_IMPEXP 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:321
#define ASSERT_(f)
mrpt::system::TTimeStamp BASE_IMPEXP 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:197
mrpt::system::TTimeStamp BASE_IMPEXP secondsToTimestamp(const double nSeconds)
Transform a time interval (in seconds) into TTimeStamp (e.g.
Definition: datetime.cpp:219
double BASE_IMPEXP 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:205
std::string BASE_IMPEXP 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:274
mrpt::system::TTimeStamp BASE_IMPEXP 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:48
#define ASSERTMSG_(f, __ERROR_MSG)
GLfloat GLfloat p
Definition: glext.h:5587
double BASE_IMPEXP 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:53



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020