21 #include <sys/utime.h>
28 #include <sys/select.h>
36 #include <sys/types.h>
47 UINT64_C(116444736) * UINT64_C(1000000000);
53 UINT64_C(116444736) * UINT64_C(1000000000);
58 return double(
t - UINT64_C(116444736) * UINT64_C(1000000000)) / 10000000.0;
65 #if defined(__APPLE__)
67 #include <sys/timeb.h>
68 #include <sys/types.h>
78 GetSystemTimeAsFileTime(&
t);
80 #elif defined(__APPLE__)
89 gettimeofday(&tv,
nullptr);
90 tim.tv_sec = tv.tv_sec;
91 tim.tv_nsec = tv.tv_usec * 1000;
96 clock_gettime(CLOCK_REALTIME, &tim);
107 double sec_frac = T - floor(T);
110 const time_t tt = time_t(T);
112 struct tm* parts = localTime ? localtime(&tt) : gmtime(&tt);
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;
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);
141 double sec_frac =
p.second - parts.tm_sec;
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);
164 double sec_frac =
p.second - parts.tm_sec;
166 time_t tt = mktime(&parts);
178 GetSystemTimeAsFileTime(&tt);
179 FileTimeToLocalFileTime(&tt, &
t);
182 #elif defined(__APPLE__)
188 clock_gettime(CLOCK_REALTIME, &tim);
193 timeinfo = localtime(&tt);
203 tim +
static_cast<int64_t>(num_seconds * 10000000.0));
234 double timeSeconds = (
t < 0) ? (-
t) :
t;
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)));
242 return format(
"%02u:%02u:%02u.%03u", nHours, nMins, nSecs, milSecs);
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);
258 if (!ptm)
return std::string(
"(Malformed timestamp)");
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);
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);
280 if (!ptm)
return "(Malformed timestamp)";
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);
299 FileTimeToSystemTime((FILETIME*)&
t, &sysT);
300 return sysT.wHour * 3600.0 + sysT.wMinute * 60.0 + sysT.wSecond +
301 sysT.wMilliseconds * 0.001;
305 tm* ptm = gmtime(&auxTime);
307 return ptm->tm_hour * 3600.0 + ptm->tm_min * 60.0 + ptm->tm_sec;
321 const time_t auxTime = tmp / (
uint64_t)10000000;
322 const tm* ptm = localtime(&auxTime);
324 unsigned int secFractions =
325 (
unsigned int)(1000000 * (tmp % 10000000) / 10000000.0);
327 const unsigned int user_secondFractionDigits = secondFractionDigits;
328 while (secondFractionDigits++ < 6) secFractions = secFractions / 10;
331 "%02u:%02u:%02u.%0*u", ptm->tm_hour, ptm->tm_min,
332 (
unsigned int)ptm->tm_sec, user_secondFractionDigits, secFractions);
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)");
350 "%02u:%02u:%02u.%06u", ptm->tm_hour, ptm->tm_min,
351 (
unsigned int)ptm->tm_sec, secFractions);
362 time_t auxTime = tmp / (
uint64_t)10000000;
363 tm* ptm = gmtime(&auxTime);
364 if (!ptm)
return string(
"(Malformed timestamp)");
367 "%u/%02u/%02u", 1900 + ptm->tm_year, ptm->tm_mon + 1, ptm->tm_mday);
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);
393 return format(
"%.2f ns", seconds * 1e9);