From 3fc4a19cd70eb1487823c4daf362b971754370f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergi=20Hern=C3=A0ndez=20Juan?= <shernand@iri.upc.edu>
Date: Wed, 30 Oct 2013 11:47:59 +0000
Subject: [PATCH] Changes to CTime by Marti Morta.

---
 src/examples/test_time.cpp | 22 +++++++---
 src/time/ctime.cpp         | 90 +++++++++++++++++++-------------------
 src/time/ctime.h           |  4 +-
 3 files changed, 65 insertions(+), 51 deletions(-)

diff --git a/src/examples/test_time.cpp b/src/examples/test_time.cpp
index 7ebea9e..c238bbf 100644
--- a/src/examples/test_time.cpp
+++ b/src/examples/test_time.cpp
@@ -189,6 +189,13 @@ int main(void)
     {
       cout << e.what() << endl;
     }
+  cout << "\n  Example: - Exception (negative)\n\n";
+    try{
+      ts = t1 - t2;
+    }catch(CException &e)
+    {
+      cout << e.what() << endl;
+    }
 
   cout << "\n  Example: average (t1,t2)\n\n";
     ta = ( t1 + t2 ) / 2;
@@ -232,6 +239,12 @@ int main(void)
         << "     "          << t1       << endl
         << "     "          << t10      << endl;
 
+    t1.setFormat(ctf_dtfile);
+    t10.setFormat(ctf_dtfile);
+    cout << "    Format ctf_dtfile: "   << endl
+        << "     "          << t1       << endl
+        << "     "          << t10      << endl;
+
     t1.setFormat(ctf_ms);
     t10.setFormat(ctf_ms);
     cout << "    Format ctf_ms: "       << endl
@@ -266,14 +279,13 @@ int main(void)
     sleep(1);
     t1.set();
     CTime tzero = t1.getRef();
-    cout << "    Time ref: " << tzero << endl;
-    cout << "    t1:       " << t1 << endl;
-    cout << "    t2:       " << t2 << endl << endl;
+    cout << "    Time ref:  " << tzero << endl;
+    cout << "    t1 (ref.): " << t1 << endl;
+    cout << "    t2 (raw):  " << t2 << endl << endl;
     cout << "    Set useRef(true) for t2 and set its time" << endl << endl;
-    t2.useRef(true);
     t2.set();
+    t2.useRef(true);
     cout << "    t2:       " << t2 << "  Is ref used?: " << t2.isRefUsed() << endl << endl;
-    cout << "    t2:       " << t2.getTimeInSeconds() << endl;
 
   cout << "\n  Example: END\n\n";
 
diff --git a/src/time/ctime.cpp b/src/time/ctime.cpp
index 6923fa0..06fb17f 100644
--- a/src/time/ctime.cpp
+++ b/src/time/ctime.cpp
@@ -17,11 +17,12 @@
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "ctime.h"
+using namespace std;
 
 // static members
 CTime CTime::zero;
 
-//
+//X
 // CONSTRUCTOR / DESTRUCTOR
 //
 // CONSTRUCTOR
@@ -156,22 +157,14 @@ 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 )
+  if(*this >= this->zero)
   {
-    this->sec  -= this->zero.seconds() - 1;
-    this->nsec += 1000000000 - this->zero.nanoseconds();
-  } else {
-    this->sec  -= this->zero.seconds();
-    this->nsec -= this->zero.nanoseconds();
-  }*/
+    *this = diff(*this,this->zero);
+  }
+  else{
+    *this = this->zero;
+  }
+  this->use_zero = use;
 }
 
 bool CTime::isRefUsed()
@@ -179,38 +172,37 @@ bool CTime::isRefUsed()
   return this->use_zero;
 }
 
-CTime CTime::diff(CTime &t0, CTime &t1)
+CTime CTime::diff(CTime &t1, CTime &t0)
 {
-  CTime tmp;
-  tmp = t0;
-  if ( (long)t0.nsec - (long)t1.sec < 0 )
+  long long s,n;
+
+  s = (long long)t1.sec - (long long)t0.sec;
+  n = (long long)t1.nsec - (long long)t0.nsec;
+
+  if ( n < 0 )
   {
-    tmp.sec  -= t1.sec - 1;
-    tmp.nsec += 1000000000 - t1.nsec;
-  } else {
-    tmp.sec  -= t1.sec;
-    tmp.nsec -= t1.nsec;
+    s -= 1;
+    n += 1000000000;
   }
-  return tmp;
-}
 
-// OPERATIONS
-CTime CTime::operator - (CTime &t)
-{
+  if (s < 0)
+    throw CTimeException(_HERE_,"negative time");
+
   CTime tmp;
-  tmp.sec=this->sec;
-  tmp.nsec=this->nsec;
+  tmp.sec=s;
+  tmp.nsec=n;
 
-//   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 tmp;
+}
 
-  return diff(tmp,t);
+// OPERATIONS (t1-t0)
+CTime CTime::operator - (CTime &t0)
+{
+  CTime t1;
+  t1.sec=this->sec;
+  t1.nsec=this->nsec;
+  
+  return diff(t1,t0);
 }
 
 CTime CTime::operator + (CTime &t)
@@ -218,7 +210,7 @@ CTime CTime::operator + (CTime &t)
   CTime tmp;
   unsigned long tn;
 
-  if ( (double)this->sec + (double)t.sec < ULONG_MAX )
+  if ( this->sec + t.sec < ULONG_MAX )
   {
     tn    = this->nsec + t.nsec;
     tmp.sec = this->sec  + t.sec;
@@ -310,6 +302,7 @@ std::string CTime::getString()
   {
     default:
     case ctf_secnano:
+      //sec nsec
       //o << t.own_time.tv_sec << " " << t.own_time.tv_nsec;
       if(this->nsec<10)             extra_zero = "00000000";
       else if(this->nsec<100)       extra_zero = "0000000";
@@ -322,17 +315,26 @@ std::string CTime::getString()
       output << this->sec << " " << extra_zero << this->nsec;
     break;
     case ctf_datetime:
+      //YYYY-MM-DD,HH:MM:SS
       timeinfo = localtime ( (time_t *)&this->sec );
       strftime (outstr,23,"%F,%T",timeinfo);
       output << outstr;
     break;
+    case ctf_dtfile:
+      //YYYY-MM-DD-HHMMSS-MS
+      timeinfo = localtime ( (time_t *)&this->sec );
+      strftime (outstr,23,"%F-%H%M%S-",timeinfo);
+      output << outstr << (this->nsec/1000000);
+    break;
     case ctf_ms:
+      // sec.0ms
       ms = round( this->nsec/1000000 );
       if(ms<100 && ms>0) extra_zero = "0";
       if(ms<10  && ms>0)  extra_zero = "00";
       output << this->sec << "." << extra_zero << ms ;
     break;
     case ctf_us:
+      // sec.0000us
       us = round( this->nsec/1000 );
       if(us<10)             extra_zero = "00000";
       else if(us<100)       extra_zero = "0000";
@@ -370,8 +372,8 @@ timespec CTime::msToTimespec( double time_ms )
   timespec temp;
 
   double seconds = (double)time_ms/1000.0;
-  temp.tv_sec  = (long)floor(seconds);
-  temp.tv_nsec = (long)floor((seconds-temp.tv_sec)*1000000000);
+  temp.tv_sec  = (long long)floor(seconds);
+  temp.tv_nsec = (long long)floor((seconds-temp.tv_sec)*1000000000);
 
   return temp;
 }
diff --git a/src/time/ctime.h b/src/time/ctime.h
index 0d90882..cd16027 100644
--- a/src/time/ctime.h
+++ b/src/time/ctime.h
@@ -42,7 +42,7 @@
  *
  * * miliseconds
  */
-enum ctimeformat { ctf_secnano, ctf_datetime, ctf_ms, ctf_us };
+enum ctimeformat { ctf_secnano, ctf_datetime, ctf_dtfile, ctf_ms, ctf_us };
 
 /** \class CTime
   *
@@ -310,7 +310,7 @@ class CTime
     */
     void resetRef();
 
-    CTime diff(CTime &t0, CTime &t1);
+    CTime diff(CTime &t1, CTime &t0);
 
   ///@}
 
-- 
GitLab