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 #include <iostream> // for the << operator
40 
41 using namespace mrpt;
42 using namespace mrpt::system;
43 using namespace std;
44 
46 {
47  return time_tToTimestamp(static_cast<double>(t));
48 }
49 
51 {
52  return double(
53  t.time_since_epoch().count() -
54  UINT64_C(116444736) * UINT64_C(1000000000)) /
55  10000000.0;
56 }
57 
58 /*---------------------------------------------------------------
59  timestampToParts
60  ---------------------------------------------------------------*/
62 {
63  const double T = mrpt::system::timestampTotime_t(t);
64  double sec_frac = T - floor(T);
65  ASSERT_(sec_frac < 1.0);
66 
67  const time_t tt = time_t(T);
68 
69  struct tm* parts = localTime ? localtime(&tt) : gmtime(&tt);
70  ASSERTMSG_(parts, "Malformed timestamp");
71 
72  p.year = parts->tm_year + 1900;
73  p.month = parts->tm_mon + 1;
74  p.day = parts->tm_mday;
75  p.day_of_week = parts->tm_wday + 1;
76  p.daylight_saving = parts->tm_isdst;
77  p.hour = parts->tm_hour;
78  p.minute = parts->tm_min;
79  p.second = parts->tm_sec + sec_frac;
80 }
81 
82 /*---------------------------------------------------------------
83  buildTimestampFromParts
84  ---------------------------------------------------------------*/
86 {
87  struct tm parts;
88 
89  parts.tm_year = p.year - 1900;
90  parts.tm_mon = p.month - 1;
91  parts.tm_mday = p.day;
92  parts.tm_wday = p.day_of_week - 1;
93  parts.tm_isdst = p.daylight_saving;
94  parts.tm_hour = p.hour;
95  parts.tm_min = p.minute;
96  parts.tm_sec = int(p.second);
97 
98  double sec_frac = p.second - parts.tm_sec;
99 
100  time_t tt = mrpt::system::os::timegm(&parts); // Local time: mktime
101 
102  return mrpt::system::time_tToTimestamp(double(tt) + sec_frac);
103 }
104 
105 /*---------------------------------------------------------------
106  buildTimestampFromPartsLocalTime
107  ---------------------------------------------------------------*/
109 {
110  struct tm parts;
111 
112  parts.tm_year = p.year - 1900;
113  parts.tm_mon = p.month - 1;
114  parts.tm_mday = p.day;
115  parts.tm_wday = p.day_of_week - 1;
116  parts.tm_isdst = p.daylight_saving;
117  parts.tm_hour = p.hour;
118  parts.tm_min = p.minute;
119  parts.tm_sec = int(p.second);
120 
121  double sec_frac = p.second - parts.tm_sec;
122 
123  time_t tt = mktime(&parts);
124 
125  return mrpt::system::time_tToTimestamp(double(tt) + sec_frac);
126 }
127 
128 /*---------------------------------------------------------------
129  formatTimeInterval
130  ---------------------------------------------------------------*/
132 {
133  double timeSeconds = (t < 0) ? (-t) : t;
134 
135  unsigned int nHours = (unsigned int)timeSeconds / 3600;
136  unsigned int nMins = ((unsigned int)timeSeconds % 3600) / 60;
137  unsigned int nSecs = (unsigned int)timeSeconds % 60;
138  unsigned int milSecs =
139  (unsigned int)(1000 * (timeSeconds - floor(timeSeconds)));
140 
141  return format("%02u:%02u:%02u.%03u", nHours, nMins, nSecs, milSecs);
142 }
143 
144 /*---------------------------------------------------------------
145  Convert a timestamp into this textual form: YEAR/MONTH/DAY,HH:MM:SS.MMM
146  ---------------------------------------------------------------*/
148 {
149  if (t == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
150 
151  uint64_t tmp =
152  (t.time_since_epoch().count() - ((uint64_t)116444736 * 1000000000));
153  time_t auxTime = tmp / (uint64_t)10000000;
154  unsigned int secFractions =
155  (unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
156  tm* ptm = gmtime(&auxTime);
157 
158  if (!ptm) return std::string("(Malformed timestamp)");
159 
160  return format(
161  "%u/%02u/%02u,%02u:%02u:%02u.%06u", 1900 + ptm->tm_year,
162  ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min,
163  (unsigned int)ptm->tm_sec, secFractions);
164 }
165 
166 /*---------------------------------------------------------------
167  Convert a timestamp into this textual form (in local time):
168  YEAR/MONTH/DAY,HH:MM:SS.MMM
169  ---------------------------------------------------------------*/
171 {
172  if (t == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
173 
174  uint64_t tmp =
175  (t.time_since_epoch().count() - ((uint64_t)116444736 * 1000000000));
176  time_t auxTime = tmp / (uint64_t)10000000;
177  unsigned int secFractions =
178  (unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
179  tm* ptm = localtime(&auxTime);
180 
181  if (!ptm) return "(Malformed timestamp)";
182 
183  return format(
184  "%u/%02u/%02u,%02u:%02u:%02u.%06u", 1900 + ptm->tm_year,
185  ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min,
186  (unsigned int)ptm->tm_sec, secFractions);
187 }
188 
189 /*---------------------------------------------------------------
190  extractDayTimeFromTimestamp
191  ---------------------------------------------------------------*/
193  const mrpt::system::TTimeStamp tt)
194 {
195  MRPT_START
196  ASSERT_(tt != INVALID_TIMESTAMP);
197 
198  auto t = tt.time_since_epoch().count();
199 #ifdef _WIN32
200  SYSTEMTIME sysT;
201  FileTimeToSystemTime((FILETIME*)&t, &sysT);
202  return sysT.wHour * 3600.0 + sysT.wMinute * 60.0 + sysT.wSecond +
203  sysT.wMilliseconds * 0.001;
204 #else
205  time_t auxTime =
206  (t - ((uint64_t)116444736 * 1000000000)) / (uint64_t)10000000;
207  tm* ptm = gmtime(&auxTime);
208  ASSERTMSG_(ptm, "Malformed timestamp");
209  return ptm->tm_hour * 3600.0 + ptm->tm_min * 60.0 + ptm->tm_sec;
210 #endif
211  MRPT_END
212 }
213 
214 /*---------------------------------------------------------------
215  Convert a timestamp into this textual form: HH:MM:SS.MMM
216  ---------------------------------------------------------------*/
218  const mrpt::system::TTimeStamp tt, unsigned int secondFractionDigits)
219 {
220  if (tt == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
221  auto t = tt.time_since_epoch().count();
222 
223  uint64_t tmp = (t - ((uint64_t)116444736 * 1000000000));
224  const time_t auxTime = tmp / (uint64_t)10000000;
225  const tm* ptm = localtime(&auxTime);
226 
227  unsigned int secFractions =
228  (unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
229  // We start with 10^{-6} second units: reduce if requested by user:
230  const unsigned int user_secondFractionDigits = secondFractionDigits;
231  while (secondFractionDigits++ < 6) secFractions = secFractions / 10;
232 
233  return format(
234  "%02u:%02u:%02u.%0*u", ptm->tm_hour, ptm->tm_min,
235  (unsigned int)ptm->tm_sec, user_secondFractionDigits, secFractions);
236 }
237 
238 /*---------------------------------------------------------------
239  Convert a timestamp into this textual form: HH:MM:SS.MMM
240  ---------------------------------------------------------------*/
242 {
243  if (tt == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
244  auto t = tt.time_since_epoch().count();
245 
246  uint64_t tmp = (t - ((uint64_t)116444736 * 1000000000));
247  time_t auxTime = tmp / (uint64_t)10000000;
248  unsigned int secFractions =
249  (unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
250  tm* ptm = gmtime(&auxTime);
251  if (!ptm) return string("(Malformed timestamp)");
252 
253  return format(
254  "%02u:%02u:%02u.%06u", ptm->tm_hour, ptm->tm_min,
255  (unsigned int)ptm->tm_sec, secFractions);
256 }
257 
258 /*---------------------------------------------------------------
259  Convert a timestamp into this textual form: YEAR/MONTH/DAY
260  ---------------------------------------------------------------*/
262 {
263  if (tt == INVALID_TIMESTAMP) return string("INVALID_TIMESTAMP");
264  auto t = tt.time_since_epoch().count();
265 
266  uint64_t tmp = (t - ((uint64_t)116444736 * 1000000000));
267  time_t auxTime = tmp / (uint64_t)10000000;
268  tm* ptm = gmtime(&auxTime);
269  if (!ptm) return string("(Malformed timestamp)");
270 
271  return format(
272  "%u/%02u/%02u", 1900 + ptm->tm_year, ptm->tm_mon + 1, ptm->tm_mday);
273 }
274 
275 /** This function implements time interval formatting: Given a time in seconds,
276  * it will return a string describing the interval with the most appropriate
277  * unit.
278  * E.g.: 1.23 year, 3.50 days, 9.3 hours, 5.3 minutes, 3.34 sec, 178.1 ms, 87.1
279  * us.
280  */
282 {
283  if (seconds >= 365 * 24 * 3600)
284  return format("%.2f years", seconds / (365 * 24 * 3600));
285  else if (seconds >= 24 * 3600)
286  return format("%.2f days", seconds / (24 * 3600));
287  else if (seconds >= 3600)
288  return format("%.2f hours", seconds / 3600);
289  else if (seconds >= 60)
290  return format("%.2f minutes", seconds / 60);
291  else if (seconds >= 1)
292  return format("%.2f sec", seconds);
293  else if (seconds >= 1e-3)
294  return format("%.2f ms", seconds * 1e3);
295  else if (seconds >= 1e-6)
296  return format("%.2f us", seconds * 1e6);
297  else
298  return format("%.2f ns", seconds * 1e9);
299 }
300 
301 std::ostream& mrpt::system::operator<<(std::ostream& o, const TTimeStamp& t)
302 {
303  const uint64_t v = t.time_since_epoch().count();
304  o << v;
305  return o;
306 }
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:61
std::ostream & operator<<(std::ostream &o, const TTimeStamp &t)
Textual representation of a TTimeStamp as the plain number in time_since_epoch().count() ...
Definition: datetime.cpp:301
#define MRPT_START
Definition: exceptions.h:262
GLdouble GLdouble t
Definition: glext.h:3689
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
mrpt::system::TTimeStamp buildTimestampFromParts(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in UTC)
Definition: datetime.cpp:85
double extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp t)
Returns the number of seconds ellapsed from midnight in the given timestamp.
Definition: datetime.cpp:192
std::string timeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC): HH:MM:SS.MMMMMM.
Definition: datetime.cpp:241
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:131
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
std::string dateToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form: YEAR/MONTH/DAY.
Definition: datetime.cpp:261
The parts of a date/time (it&#39;s like the standard &#39;tm&#39; but with fractions of seconds).
Definition: datetime.h:49
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:281
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::system::TTimeStamp buildTimestampFromPartsLocalTime(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in local time)
Definition: datetime.cpp:108
unsigned __int64 uint64_t
Definition: rptypes.h:50
const GLdouble * v
Definition: glext.h:3678
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:147
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#define MRPT_END
Definition: exceptions.h:266
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:217
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:170
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.h:93
GLfloat GLfloat p
Definition: glext.h:6305
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:43
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:50



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020