9 static const char rcsid[] __attribute__((used)) =
"$Id: Time.cpp 1653 2016-02-28 19:54:59Z sella $";
17 using namespace sella::util;
18 using namespace std::chrono;
24 struct timeval
Time::htontv(const struct timeval &tv) throw () {
25 struct timeval ret_val;
27 ret_val.tv_sec = htonl(tv.tv_sec);
28 ret_val.tv_usec = htonl(tv.tv_usec);
33 struct timeval
Time::ntohtv(const struct timeval &tv) throw () {
34 struct timeval ret_val;
36 ret_val.tv_sec = ntohl(tv.tv_sec);
37 ret_val.tv_usec = ntohl(tv.tv_usec);
42 int Time::comparetv(
const struct timeval &a,
const struct timeval &b)
throw () {
43 if (a.tv_sec > b.tv_sec) {
45 }
else if (a.tv_sec < b.tv_sec) {
47 }
else if (a.tv_usec > b.tv_usec) {
49 }
else if (a.tv_usec < b.tv_usec) {
56 void Time::cleartv(
struct timeval &tv)
throw () {
60 struct timeval& Time::copytv(
struct timeval &dst,
const struct timeval &src)
throw () {
66 struct timeval& Time::subtracttv(
struct timeval &dst,
const struct timeval &src)
throw () {
67 if ((dst.tv_usec -= src.tv_usec) < 0) {
68 dst.tv_usec += 1000000LL;
71 dst.tv_sec -= src.tv_sec;
76 struct timeval& Time::addtv(
struct timeval &dst,
const struct timeval &src)
throw () {
77 if ((dst.tv_usec -= src.tv_usec) > 1000000LL) {
78 dst.tv_usec -= 1000000LL;
81 dst.tv_sec -= src.tv_sec;
86 struct timespec
Time::htonts(const struct timespec &ts) throw () {
87 struct timespec ret_spec;
89 ret_spec.tv_sec = htonl(ts.tv_sec);
90 ret_spec.tv_nsec = htonl(ts.tv_nsec);
95 struct timespec
Time::ntohts(const struct timespec &ts) throw () {
96 struct timespec ret_spec;
98 ret_spec.tv_sec = ntohl(ts.tv_sec);
99 ret_spec.tv_nsec = ntohl(ts.tv_nsec);
104 int Time::comparets(
const struct timespec &a,
const struct timespec &b)
throw () {
105 if (a.tv_sec > b.tv_sec) {
107 }
else if (a.tv_sec < b.tv_sec) {
109 }
else if (a.tv_nsec > b.tv_nsec) {
111 }
else if (a.tv_nsec < b.tv_nsec) {
118 void Time::clearts(
struct timespec &ts)
throw () {
122 struct timespec& Time::copyts(
struct timespec &dst,
const struct timespec &src)
throw () {
128 struct timespec& Time::subtractts(
struct timespec &dst,
const struct timespec &src)
throw () {
129 if ((dst.tv_nsec -= src.tv_nsec) < 0) {
130 dst.tv_nsec += 1000000000LL;
133 dst.tv_sec -= src.tv_sec;
138 struct timespec& Time::addts(
struct timespec &dst,
const struct timespec &src)
throw () {
139 if ((dst.tv_nsec -= src.tv_nsec) > 1000000000LL) {
140 dst.tv_nsec -= 1000000000LL;
143 dst.tv_sec -= src.tv_sec;
148 struct timespec
Time::clock_realtime() throw () {
151 if (::clock_gettime(CLOCK_REALTIME, &now)) {
158 struct timespec
Time::clock_realtime_coarse() throw () {
159 #ifdef CLOCK_REALTIME_COARSE
160 struct timespec now = { 0, 0 };
162 if (::clock_gettime(CLOCK_REALTIME_COARSE, &now)) {
168 return Time::clock_realtime();
172 struct timespec
Time::clock_monotonic() throw () {
173 struct timespec now = { 0, 0 };
175 if (::clock_gettime(CLOCK_MONOTONIC, &now)) {
182 struct timespec
Time::clock_monotonic_coarse() throw () {
183 #ifdef CLOCK_MONOTINIC_COARSE
184 struct timespec now = { 0, 0 };
186 if (::clock_gettime(CLOCK_MONOTONIC_COARSE, &now)) {
192 return Time::clock_monotonic();
196 struct timespec
Time::clock_monotonic_raw() throw () {
197 struct timespec now = { 0, 0 };
199 if (::clock_gettime(CLOCK_MONOTONIC_RAW, &now)) {
206 struct timespec
Time::clock_boottime() throw () {
207 #ifdef CLOCK_BOOTTIME
210 if (::clock_gettime(CLOCK_BOOTTIME, &now)) {
216 return Time::clock_monotonic();
220 struct timespec
Time::clock_process_cputime_id() throw () {
221 struct timespec now = { 0, 0 };
223 if (::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now)) {
230 struct timespec
Time::clock_thread_cputime_id() throw () {
231 struct timespec now = { 0, 0 };
233 if (::clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now)) {
240 uint64_t Time::tstosec(
const struct timespec &ts)
throw () {
241 return ((ts.tv_nsec / 1000000000LL) + ts.tv_sec);
244 uint64_t Time::tstomsec(
const struct timespec &ts)
throw () {
245 return ((ts.tv_nsec / 1000000LL) + (ts.tv_sec * 1000LL));
248 uint64_t Time::tstousec(
const struct timespec &ts)
throw () {
249 return (ts.tv_nsec / 1000LL + (ts.tv_sec * 1000000LL));
253 struct timespec
Time::usectots(uint64_t usec) throw () {
254 struct timespec ret_val;
256 ret_val.tv_sec = usec / 1000;
257 ret_val.tv_nsec = (usec % 1000) * 1000000;
262 struct timespec
Time::msectots(const uint64_t msec) throw () {
263 struct timespec ret_val;
265 ret_val.tv_sec = msec / 1000000;
266 ret_val.tv_nsec = ((msec % 1000000) * 1000000);
272 struct timespec
Time::sectots(uint64_t sec) throw () {
273 struct timespec ret_val;
275 ret_val.tv_sec = sec;
281 std::string Time::tmtostr(
const struct tm &tm,
const char* format)
throw () {
282 char buf[512] = { 0 };
284 strftime(buf,
sizeof(buf), format, &tm);
286 return std::string(buf);
289 std::string Time::tstostr(
const struct timespec &ts,
const char* format)
throw () {
290 return sectostr(tstosec(ts), format);
293 std::string Time::sectostr(uint64_t sec,
const char* format)
throw () {
294 time_t s = (sec == 0) ? time(NULL) : sec;
295 struct tm tm = *::localtime(&s);
297 return tmtostr(tm, format);
300 std::string Time::msectostr(uint64_t msec,
const char* format)
throw () {
301 return sectostr(msec / 1000LL, format);
304 std::string Time::usectostr(uint64_t usec,
const char* format)
throw () {
305 return sectostr(usec / 1000000LL, format);
308 struct timeval
Time::gettimeofday(struct timezone *tz) throw () {
311 ::gettimeofday(&now, tz);
316 uint64_t Time::gettimeofday_usec(
struct timezone *tz)
throw () {
319 ::gettimeofday(&now, tz);
321 return tvtousec(now);
324 uint64_t Time::gettimeofday_msec(
struct timezone *tz)
throw () {
327 ::gettimeofday(&now, tz);
329 return tvtomsec(now);
332 uint64_t Time::gettimeofday_sec(
struct timezone *tz)
throw () {
335 ::gettimeofday(&now, tz);
340 uint64_t Time::tvtousec(
const struct timeval &tv)
throw () {
341 return (tv.tv_usec + (tv.tv_sec * 1000000LL));
344 uint64_t Time::tvtomsec(
const struct timeval &tv)
throw () {
345 return ((tv.tv_usec / 1000LL) + (tv.tv_sec * 1000LL));
348 uint64_t Time::tvtosec(
const struct timeval &tv)
throw () {
349 return ((tv.tv_usec / 1000000LL) + tv.tv_sec);
352 struct timeval
Time::chronototv(const std::chrono::system_clock::duration &duration) throw () {
353 struct timeval ret_val;
354 microseconds usec = duration_cast<microseconds>(duration);
356 if (usec <= microseconds(0)) {
357 ret_val.tv_sec = ret_val.tv_usec = 0;
359 ret_val.tv_sec = usec.count() / 1000000LL;
360 ret_val.tv_usec = usec.count() % 1000000LL;
366 struct timeval
Time::chronototv(const std::chrono::system_clock::time_point &tp) throw () {
367 return chronototv(duration_cast<milliseconds>(tp.time_since_epoch()));
370 struct timeval
Time::sectotv(uint64_t sec) throw () {
371 struct timeval ret_val;
373 ret_val.tv_sec = sec;
379 struct timeval
Time::msectotv(uint64_t msec) throw () {
380 struct timeval ret_val;
382 ret_val.tv_sec = msec / 1000LL;
383 ret_val.tv_usec = ((msec % 1000LL) * 1000LL);
388 struct timeval
Time::usectotv(uint64_t usec) throw () {
389 struct timeval ret_val;
391 ret_val.tv_sec = usec / 1000000LL;
392 ret_val.tv_usec = usec - (ret_val.tv_sec * 1000000LL);
397 struct timeval* Time::add_usectotv(
struct timeval &tv, uint64_t usec)
throw () {
398 tv.tv_sec += (usec / 1000000LL);
399 tv.tv_usec += (usec % 1000000LL);
401 if (tv.tv_usec > 1000000LL) {
402 tv.tv_usec -= 1000000LL;
409 struct timeval* Time::add_msectotv(
struct timeval &tv, uint64_t msec)
throw () {
410 tv.tv_sec += (msec / 1000LL);
411 tv.tv_usec += ((msec % 1000LL) * 1000LL);
413 if (tv.tv_usec > 1000000LL) {
414 tv.tv_usec -= 1000000LL;
421 struct timeval* Time::add_sectotv(
struct timeval &tv, uint64_t sec)
throw () {
424 if (tv.tv_usec > 1000000LL) {
425 tv.tv_usec -= 1000000LL;
432 std::string Time::getRFC1123Date(
void) throw () {
433 static const char *days[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
434 static const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
443 strftime(buf,
sizeof(buf),
"---, %d --- %Y %H:%M:%S GMT", &tm);
444 memcpy(buf, days[tm.tm_wday], 3);
445 memcpy(buf + 8, months[tm.tm_mon], 3);
447 return std::string(buf);