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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST