16 #ifdef MRPT_OS_WINDOWS 21 #include <sys/utime.h> 28 #include <sys/select.h> 36 #include <sys/types.h> 45 return uint64_t(
t) * UINT64_C(10000000) + UINT64_C(116444736) * UINT64_C(1000000000);
50 return uint64_t(
t*10000000.0)+ UINT64_C(116444736)*UINT64_C(1000000000);
55 return double(
t - UINT64_C(116444736)*UINT64_C(1000000000)) / 10000000.0;
62 #if defined(MRPT_OS_APPLE) 64 # include <sys/timeb.h> 65 # include <sys/types.h> 73 #ifdef MRPT_OS_WINDOWS 75 GetSystemTimeAsFileTime(&
t);
77 #elif defined(MRPT_OS_APPLE) 86 gettimeofday(&tv, NULL);
87 tim.tv_sec = tv.tv_sec;
88 tim.tv_nsec = tv.tv_usec*1000;
93 clock_gettime(CLOCK_REALTIME, &tim);
104 double sec_frac = T - floor(T);
107 const time_t tt = time_t(T);
109 struct tm * parts = localTime ? localtime(&tt) : gmtime(&tt);
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;
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) 186 clock_gettime(CLOCK_REALTIME, &tim);
189 struct tm * timeinfo;
191 timeinfo = localtime( &tt );
229 double timeSeconds = (
t<0) ? (-
t) :
t;
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)) );
237 "%02u:%02u:%02u.%03u",
252 time_t auxTime = tmp / (
uint64_t)10000000;
253 unsigned int secFractions = (
unsigned int)( 1000000 * (tmp % 10000000) / 10000000.0 );
254 tm *ptm = gmtime( &auxTime );
260 "%u/%02u/%02u,%02u:%02u:%02u.%06u",
266 (
unsigned int)ptm->tm_sec,
279 time_t auxTime = tmp / (
uint64_t)10000000;
280 unsigned int secFractions = (
unsigned int)( 1000000 * (tmp % 10000000) / 10000000.0 );
281 tm *ptm = localtime( &auxTime );
283 if (!ptm)
return "(Malformed timestamp)";
286 "%u/%02u/%02u,%02u:%02u:%02u.%06u",
292 (
unsigned int)ptm->tm_sec,
304 #ifdef MRPT_OS_WINDOWS 306 FileTimeToSystemTime( (FILETIME*)&
t, &sysT );
307 return sysT.wHour * 3600.0 + sysT.wMinute * 60.0 + sysT.wSecond + sysT.wMilliseconds * 0.001;
310 tm *ptm = gmtime( &auxTime );
312 return ptm->tm_hour * 3600.0 + ptm->tm_min * 60.0 + ptm->tm_sec;
326 const time_t auxTime = tmp / (
uint64_t)10000000;
327 const tm *ptm = localtime( &auxTime );
329 unsigned int secFractions = (
unsigned int)( 1000000 * (tmp % 10000000) / 10000000.0 );
331 const unsigned int user_secondFractionDigits = secondFractionDigits;
332 while (secondFractionDigits++<6)
333 secFractions = secFractions / 10;
336 "%02u:%02u:%02u.%0*u",
339 (
unsigned int)ptm->tm_sec,
340 user_secondFractionDigits,
352 time_t auxTime = tmp / (
uint64_t)10000000;
353 unsigned int secFractions = (
unsigned int)( 1000000 * (tmp % 10000000) / 10000000.0 );
354 tm *ptm = gmtime( &auxTime );
356 return string(
"(Malformed timestamp)");
359 "%02u:%02u:%02u.%06u",
362 (
unsigned int)ptm->tm_sec,
374 time_t auxTime = tmp / (
uint64_t)10000000;
375 tm *ptm = gmtime( &auxTime );
377 return string(
"(Malformed timestamp)");
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);
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);
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
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...
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
mrpt::system::TTimeStamp BASE_IMPEXP getCurrentTime()
Returns the current (UTC) system time.
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...
#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)
double BASE_IMPEXP extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp t)
Returns the number of seconds ellapsed from midnight in the given timestamp.
std::string BASE_IMPEXP timeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC): HH:MM:SS.MMMMMM.
std::string BASE_IMPEXP formatTimeInterval(const double timeSeconds)
Returns a formated string with the given time difference (passed as the number of seconds)...
mrpt::system::TTimeStamp BASE_IMPEXP getCurrentLocalTime()
Returns the current (local) time.
std::string BASE_IMPEXP 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 BASE_IMPEXP intervalFormat(const double seconds)
This function implements time interval formatting: Given a time in seconds, it will return a string d...
std::string BASE_IMPEXP 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 BASE_IMPEXP 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 BASE_IMPEXP dateTimeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
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.
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) ...
mrpt::system::TTimeStamp BASE_IMPEXP secondsToTimestamp(const double nSeconds)
Transform a time interval (in seconds) into TTimeStamp (e.g.
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...
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.
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...
#define ASSERTMSG_(f, __ERROR_MSG)
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...