From e36ca33cc64b020bd7d0961ae2186e9794de0e76 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 29 Jan 2018 14:02:18 +0100 Subject: [PATCH] implement basic operations over class Time This adds members and overloads for * addition, subtraction / difference, multiplication by time_t, * comparison, less-than / greater-than, equality, * trivial formatting (needed in boost unittest) operations involving *class Time* objects. --- src/timefunc.cpp | 102 ++++++++++-- src/timefunc.hxx | 286 ++++++++++++++++++++++++++++++--- test/test_timefunc.cpp | 426 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 780 insertions(+), 34 deletions(-) diff --git a/src/timefunc.cpp b/src/timefunc.cpp index cab9469..30df7a6 100644 --- a/src/timefunc.cpp +++ b/src/timefunc.cpp @@ -779,7 +779,7 @@ namespace clock { static inline clockid_t clockid_of_flags (const enum type::id id, - const enum type::variant var) + const enum type::variant var) NOEXCEPT { clockid_t cid = CLOCK_MONOTONIC_COARSE; @@ -834,11 +834,10 @@ namespace clock { 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) + Time::Time (const enum type::id id, + const enum type::variant var) NOEXCEPT : value (zero_time) , id (id) , variant (var) @@ -846,19 +845,41 @@ namespace clock { { } int64_t - Time::as_nanosec (void) - { return int64_t (this->value.tv_sec) * NANO + this->value.tv_nsec; } + 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) /* likely to overflow */ + Time::as_nanosec_L (void) const NOEXCEPT /* likely to overflow */ { return static_cast(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) + Time::unset (void) NOEXCEPT { this->value = zero_time; } bool - Time::set (void) + Time::set (void) NOEXCEPT { struct timespec now; @@ -877,10 +898,43 @@ namespace clock { 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