From: Philipp Gesang Date: Mon, 29 Jan 2018 10:06:13 +0000 (+0100) Subject: implement a basic time/clock datastructure X-Git-Tag: v2.11~1^2~17 X-Git-Url: http://developer.intra2net.com/git/?p=libi2ncommon;a=commitdiff_plain;h=8b5814e27eff0a680bd9228e9c2cede306158549 implement a basic time/clock datastructure Add a class ``Time'' to the timefunc part of the library. The goal is to provide a generalist handle around struct timespec to supersede the current ones that had been built on now deprecated APIs (e. g. ``timeb.h''). --- diff --git a/src/timefunc.cpp b/src/timefunc.cpp index e8b1917..cab9469 100644 --- a/src/timefunc.cpp +++ b/src/timefunc.cpp @@ -771,3 +771,125 @@ bool realtime_clock_gettime(long int& seconds, long int& nano_seconds) } // eo realtime_clock_gettime(long int&,long int&) +namespace I2n { + +namespace clock { + + namespace { + + static inline clockid_t + clockid_of_flags (const enum type::id id, + const enum type::variant var) + { + clockid_t cid = CLOCK_MONOTONIC_COARSE; + + switch (id) { + + default: + case type::mono: { + switch (var) { + default: { + break; + } + case type::raw: { + cid = CLOCK_MONOTONIC_RAW; + break; + } + case type::exact: { + cid = CLOCK_MONOTONIC; + break; + } + } + break; + } + + case type::real: { + if (var == type::exact) { + cid = CLOCK_REALTIME; + } else { + cid = CLOCK_REALTIME_COARSE; + } + break; + } + + case type::boot: { + if (var & type::exact) { + cid = CLOCK_BOOTTIME; + } + break; + } + + case type::cpu: { + if (var == type::thread) { + cid = CLOCK_THREAD_CPUTIME_ID; + } else { + cid = CLOCK_PROCESS_CPUTIME_ID; + } + break; + } + } /* [switch id] */ + + return cid; + } + + static const struct timespec zero_time = { 0, 0 }; + +# define NANO (1000L * 1000 * 1000) + + } /* [namespace] */ + + Time::Time (const enum type::id id, const enum type::variant var) + : value (zero_time) + , id (id) + , variant (var) + , err (0) + { } + + int64_t + Time::as_nanosec (void) + { return int64_t (this->value.tv_sec) * NANO + this->value.tv_nsec; } + + long + Time::as_nanosec_L (void) /* likely to overflow */ + { return static_cast(this->as_nanosec ()); } + + void + Time::unset (void) + { this->value = zero_time; } + + bool + Time::set (void) + { + struct timespec now; + + errno = 0; + if (clock_gettime (clockid_of_flags (this->id, this->variant), &now) + == -1) + { + this->err = errno; + this->unset (); + + return false; + } + this->err = 0; + this->value = now; + + return true; + } + + boost::optional