#include <unistd.h>
#include <string.h>
#include <sys/timeb.h>
-#include <sys/syscall.h>
#include <timefunc.hxx>
#include <i18n.h>
return string(buf);
}
+string format_date(time_t seconds)
+{
+ char buf[50];
+ memset (buf, 0, 50);
+ struct tm ta;
+ if (localtime_r((time_t *)&seconds, &ta) == NULL)
+ memset (&ta, 0, sizeof(struct tm));
+
+ strftime (buf, 49, "%d.%m.%Y", &ta);
+ return string(buf);
+}
+
void seconds_to_hour_minute(int seconds, int *hour, int *minute)
{
if (hour != NULL) {
bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds)
{
struct timespec tp[1];
- int res= ::syscall(__NR_clock_gettime, CLOCK_MONOTONIC, tp);
+ int res= clock_gettime (CLOCK_MONOTONIC, tp);
if (0 == res)
{
seconds= tp->tv_sec;
bool realtime_clock_gettime(long int& seconds, long int& nano_seconds)
{
struct timespec tp[1];
- int res= ::syscall(__NR_clock_gettime, CLOCK_REALTIME, tp);
+ int res= clock_gettime(CLOCK_REALTIME, tp);
if (0 == res)
{
seconds= tp->tv_sec;
} // 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) NOEXCEPT
+ {
+ 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 };
+
+ } /* [namespace] */
+
+ Time::Time (const enum type::id id,
+ const enum type::variant var) NOEXCEPT
+ : value (zero_time)
+ , id (id)
+ , variant (var)
+ , err (0)
+ { }
+
+ int64_t
+ Time::as_nanosec (void) const NOEXCEPT
+ {
+ return int64_t (this->value.tv_sec) * TIME_CONST_FACTOR_NANO
+ + this->value.tv_nsec;
+ }
+
+ long
+ Time::as_nanosec_L (void) const NOEXCEPT /* likely to overflow */
+ { return static_cast<long>(this->as_nanosec ()); }
+
+ Time &
+ Time::operator= (Time t2) NOEXCEPT
+ {
+ this->swap (t2);
+
+ return *this;
+ }
+
+ Time &
+ Time::operator= (struct timespec ts) NOEXCEPT
+ {
+ std::swap (this->value, ts);
+ this->id = clock::type::mono;
+ this->variant = clock::type::dflt;
+ this->err = 0;
+
+ return *this;
+ }
+
+ void
+ Time::unset (void) NOEXCEPT
+ { this->value = zero_time; }
+
+ bool
+ Time::set (void) NOEXCEPT
+ {
+ 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;
+ }
+
+ Time &
+ Time::add (const time_t sec, const long nsec) NOEXCEPT
+ {
+ this->value.tv_sec += sec;
+ this->value.tv_nsec += nsec;
+
+ this->carry_nsec ();
+
+ return *this;
+ }
+
+ Time &
+ Time::subtract (const time_t sec, const long nsec) NOEXCEPT
+ {
+ this->value.tv_sec -= sec;
+ this->value.tv_nsec -= nsec;
+
+ this->carry_nsec ();
+
+ return *this;
+ }
+
+ Time &
+ Time::scale (const time_t factor) NOEXCEPT
+ {
+ this->value.tv_sec *= factor;
+ this->value.tv_nsec *= factor;
+
+ this->carry_nsec ();
+
+ return *this;
+ }
+
+ boost::optional<Time>
+ now (const enum type::id id, const enum type::variant var) NOEXCEPT
+ {
+ Time ret (id, var);
+
+ if (!ret.set ()) {
+ return boost::none;
+ }
+
+ return ret;
+ }
+
+ Time
+ zero (const enum type::id id, const enum type::variant var) NOEXCEPT
+ { return Time (id, var); }
+
+ int
+ compare (const Time &t1, const Time &t2) NOEXCEPT
+ {
+ if (t1.value.tv_sec < t2.value.tv_sec) {
+ return -1;
+ }
+
+ if (t1.value.tv_sec > t2.value.tv_sec) {
+ return 1;
+ }
+
+ if (t1.value.tv_nsec < t2.value.tv_nsec) {
+ return -1;
+ }
+
+ if (t1.value.tv_nsec > t2.value.tv_nsec) {
+ return 1;
+ }
+
+ return 0;
+ }
+
+} /* [namespace clock] */
+
+} /* [namespace I2n] */
+