16 #ifdef MRPT_OS_WINDOWS 21 #include <sys/utime.h> 28 #include <sys/select.h> 36 #include <sys/types.h> 46 UINT64_C(116444736) * UINT64_C(1000000000);
52 UINT64_C(116444736) * UINT64_C(1000000000);
57 return double(
t - UINT64_C(116444736) * UINT64_C(1000000000)) / 10000000.0;
64 #if defined(MRPT_OS_APPLE) 66 #include <sys/timeb.h> 67 #include <sys/types.h> 75 #ifdef MRPT_OS_WINDOWS 77 GetSystemTimeAsFileTime(&
t);
79 #elif defined(MRPT_OS_APPLE) 88 gettimeofday(&tv,
nullptr);
89 tim.tv_sec = tv.tv_sec;
90 tim.tv_nsec = tv.tv_usec * 1000;
95 clock_gettime(CLOCK_REALTIME, &tim);
106 double sec_frac = T - floor(T);
109 const time_t tt = time_t(T);
111 struct tm* parts = localTime ? localtime(&tt) : gmtime(&tt);
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;
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);
140 double sec_frac =
p.second - parts.tm_sec;
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);
163 double sec_frac =
p.second - parts.tm_sec;
165 time_t tt = mktime(&parts);
175 #ifdef MRPT_OS_WINDOWS 177 GetSystemTimeAsFileTime(&tt);
178 FileTimeToLocalFileTime(&tt, &
t);
181 #elif defined(MRPT_OS_APPLE) 187 clock_gettime(CLOCK_REALTIME, &tim);
192 timeinfo = localtime(&tt);
202 tim +
static_cast<int64_t>(num_seconds * 10000000.0));
233 double timeSeconds = (
t < 0) ? (-
t) :
t;
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)));
241 return format(
"%02u:%02u:%02u.%03u", nHours, nMins, nSecs, milSecs);
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);
257 if (!ptm)
return std::string(
"(Malformed timestamp)");
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);
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);
279 if (!ptm)
return "(Malformed timestamp)";
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);
296 #ifdef MRPT_OS_WINDOWS 298 FileTimeToSystemTime((FILETIME*)&
t, &sysT);
299 return sysT.wHour * 3600.0 + sysT.wMinute * 60.0 + sysT.wSecond +
300 sysT.wMilliseconds * 0.001;
304 tm* ptm = gmtime(&auxTime);
306 return ptm->tm_hour * 3600.0 + ptm->tm_min * 60.0 + ptm->tm_sec;
320 const time_t auxTime = tmp / (
uint64_t)10000000;
321 const tm* ptm = localtime(&auxTime);
323 unsigned int secFractions =
324 (
unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
326 const unsigned int user_secondFractionDigits = secondFractionDigits;
327 while (secondFractionDigits++ < 6) secFractions = secFractions / 10;
330 "%02u:%02u:%02u.%0*u", ptm->tm_hour, ptm->tm_min,
331 (
unsigned int)ptm->tm_sec, user_secondFractionDigits, secFractions);
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)");
349 "%02u:%02u:%02u.%06u", ptm->tm_hour, ptm->tm_min,
350 (
unsigned int)ptm->tm_sec, secFractions);
361 time_t auxTime = tmp / (
uint64_t)10000000;
362 tm* ptm = gmtime(&auxTime);
363 if (!ptm)
return string(
"(Malformed timestamp)");
366 "%u/%02u/%02u", 1900 + ptm->tm_year, ptm->tm_mon + 1, ptm->tm_mday);
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);
392 return format(
"%.2f ns", seconds * 1e9);
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
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...
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
time_t timegm(struct tm *tm)
An OS-independent version of timegm (which is not present in all compilers): converts a time structur...
#define THROW_EXCEPTION(msg)
mrpt::system::TTimeStamp buildTimestampFromParts(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in UTC)
double extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp t)
Returns the number of seconds ellapsed from midnight in the given timestamp.
std::string timeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC): HH:MM:SS.MMMMMM.
std::string formatTimeInterval(const double timeSeconds)
Returns a formated string with the given time difference (passed as the number of seconds)...
mrpt::system::TTimeStamp getCurrentLocalTime()
Returns the current (local) time.
std::string dateToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form: YEAR/MONTH/DAY.
The parts of a date/time (it's like the standard 'tm' but with fractions of seconds).
std::string intervalFormat(const double seconds)
This function implements time interval formatting: Given a time in seconds, it will return a string d...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
mrpt::system::TTimeStamp buildTimestampFromPartsLocalTime(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in local time)
unsigned __int64 uint64_t
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.
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.
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) ...
mrpt::system::TTimeStamp secondsToTimestamp(const double nSeconds)
Transform a time interval (in seconds) into TTimeStamp (e.g.
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...
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.
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...
#define ASSERTMSG_(f, __ERROR_MSG)
double timestampTotime_t(const mrpt::system::TTimeStamp t)
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...