From 53831eeae4bae4343a33fd0b12fb05af48c9f1dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Morta=20Garriga?= <mmorta@iri.upc.edu> Date: Thu, 27 Oct 2011 17:19:47 +0000 Subject: [PATCH] Added ref time function --- src/time/ctime.cpp | 58 +++++++++++++++++++++++++++++++++++++--------- src/time/ctime.h | 2 ++ 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/time/ctime.cpp b/src/time/ctime.cpp index 29efe06..6923fa0 100644 --- a/src/time/ctime.cpp +++ b/src/time/ctime.cpp @@ -128,8 +128,12 @@ void CTime::set(double milliseconds) this->nsec = time_temp.tv_nsec; if(this->use_zero && milliseconds<0.0) { - this->sec -= this->zero.seconds(); - this->nsec -= this->zero.nanoseconds(); + CTime tmp; + tmp.sec = this->sec; + tmp.nsec = this->nsec; + tmp = diff(tmp,this->zero); + this->sec = tmp.sec; + this->nsec = tmp.nsec; } } @@ -153,6 +157,21 @@ void CTime::resetRef() void CTime::useRef(bool use) { this->use_zero = use; + CTime tmp; + tmp.sec = this->sec; + tmp.nsec = this->nsec; + tmp = diff(tmp,this->zero); + this->sec = tmp.sec; + this->nsec = tmp.nsec; + /* + if ( (long)this->nsec - (long)this->zero.seconds() < 0 ) + { + this->sec -= this->zero.seconds() - 1; + this->nsec += 1000000000 - this->zero.nanoseconds(); + } else { + this->sec -= this->zero.seconds(); + this->nsec -= this->zero.nanoseconds(); + }*/ } bool CTime::isRefUsed() @@ -160,23 +179,40 @@ bool CTime::isRefUsed() return this->use_zero; } -// OPERATIONS -CTime CTime::operator - (CTime &t) +CTime CTime::diff(CTime &t0, CTime &t1) { CTime tmp; - - if ( (long)this->nsec - (long)t.nsec < 0 ) + tmp = t0; + if ( (long)t0.nsec - (long)t1.sec < 0 ) { - tmp.sec = this->sec - t.sec - 1; - tmp.nsec = 1000000000 + this->nsec - t.nsec; + tmp.sec -= t1.sec - 1; + tmp.nsec += 1000000000 - t1.nsec; } else { - tmp.sec = this->sec - t.sec; - tmp.nsec = this->nsec - t.nsec; + tmp.sec -= t1.sec; + tmp.nsec -= t1.nsec; } - return tmp; } +// OPERATIONS +CTime CTime::operator - (CTime &t) +{ + CTime tmp; + tmp.sec=this->sec; + tmp.nsec=this->nsec; + +// if ( (long)this->nsec - (long)t.nsec < 0 ) +// { +// tmp.sec = this->sec - t.sec - 1; +// tmp.nsec = 1000000000 + this->nsec - t.nsec; +// } else { +// tmp.sec = this->sec - t.sec; +// tmp.nsec = this->nsec - t.nsec; +// } + + return diff(tmp,t); +} + CTime CTime::operator + (CTime &t) { CTime tmp; diff --git a/src/time/ctime.h b/src/time/ctime.h index d2d13a2..0d90882 100644 --- a/src/time/ctime.h +++ b/src/time/ctime.h @@ -310,6 +310,8 @@ class CTime */ void resetRef(); + CTime diff(CTime &t0, CTime &t1); + ///@} /// @name Operators -- GitLab