Skip to content
Snippets Groups Projects
Commit 3fc4a19c authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Changes to CTime by Marti Morta.

parent 88adef59
No related branches found
No related tags found
No related merge requests found
...@@ -189,6 +189,13 @@ int main(void) ...@@ -189,6 +189,13 @@ int main(void)
{ {
cout << e.what() << endl; 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"; cout << "\n Example: average (t1,t2)\n\n";
ta = ( t1 + t2 ) / 2; ta = ( t1 + t2 ) / 2;
...@@ -232,6 +239,12 @@ int main(void) ...@@ -232,6 +239,12 @@ int main(void)
<< " " << t1 << endl << " " << t1 << endl
<< " " << t10 << endl; << " " << t10 << endl;
t1.setFormat(ctf_dtfile);
t10.setFormat(ctf_dtfile);
cout << " Format ctf_dtfile: " << endl
<< " " << t1 << endl
<< " " << t10 << endl;
t1.setFormat(ctf_ms); t1.setFormat(ctf_ms);
t10.setFormat(ctf_ms); t10.setFormat(ctf_ms);
cout << " Format ctf_ms: " << endl cout << " Format ctf_ms: " << endl
...@@ -266,14 +279,13 @@ int main(void) ...@@ -266,14 +279,13 @@ int main(void)
sleep(1); sleep(1);
t1.set(); t1.set();
CTime tzero = t1.getRef(); CTime tzero = t1.getRef();
cout << " Time ref: " << tzero << endl; cout << " Time ref: " << tzero << endl;
cout << " t1: " << t1 << endl; cout << " t1 (ref.): " << t1 << endl;
cout << " t2: " << t2 << endl << endl; cout << " t2 (raw): " << t2 << endl << endl;
cout << " Set useRef(true) for t2 and set its time" << endl << endl; cout << " Set useRef(true) for t2 and set its time" << endl << endl;
t2.useRef(true);
t2.set(); t2.set();
t2.useRef(true);
cout << " t2: " << t2 << " Is ref used?: " << t2.isRefUsed() << endl << endl; cout << " t2: " << t2 << " Is ref used?: " << t2.isRefUsed() << endl << endl;
cout << " t2: " << t2.getTimeInSeconds() << endl;
cout << "\n Example: END\n\n"; cout << "\n Example: END\n\n";
......
...@@ -17,11 +17,12 @@ ...@@ -17,11 +17,12 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "ctime.h" #include "ctime.h"
using namespace std;
// static members // static members
CTime CTime::zero; CTime CTime::zero;
// //X
// CONSTRUCTOR / DESTRUCTOR // CONSTRUCTOR / DESTRUCTOR
// //
// CONSTRUCTOR // CONSTRUCTOR
...@@ -156,22 +157,14 @@ void CTime::resetRef() ...@@ -156,22 +157,14 @@ void CTime::resetRef()
void CTime::useRef(bool use) void CTime::useRef(bool use)
{ {
this->use_zero = use; if(*this >= this->zero)
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 = diff(*this,this->zero);
this->nsec += 1000000000 - this->zero.nanoseconds(); }
} else { else{
this->sec -= this->zero.seconds(); *this = this->zero;
this->nsec -= this->zero.nanoseconds(); }
}*/ this->use_zero = use;
} }
bool CTime::isRefUsed() bool CTime::isRefUsed()
...@@ -179,38 +172,37 @@ bool CTime::isRefUsed() ...@@ -179,38 +172,37 @@ bool CTime::isRefUsed()
return this->use_zero; return this->use_zero;
} }
CTime CTime::diff(CTime &t0, CTime &t1) CTime CTime::diff(CTime &t1, CTime &t0)
{ {
CTime tmp; long long s,n;
tmp = t0;
if ( (long)t0.nsec - (long)t1.sec < 0 ) 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; s -= 1;
tmp.nsec += 1000000000 - t1.nsec; n += 1000000000;
} else {
tmp.sec -= t1.sec;
tmp.nsec -= t1.nsec;
} }
return tmp;
}
// OPERATIONS if (s < 0)
CTime CTime::operator - (CTime &t) throw CTimeException(_HERE_,"negative time");
{
CTime tmp; CTime tmp;
tmp.sec=this->sec; tmp.sec=s;
tmp.nsec=this->nsec; tmp.nsec=n;
// if ( (long)this->nsec - (long)t.nsec < 0 ) return tmp;
// { }
// 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); // 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) CTime CTime::operator + (CTime &t)
...@@ -218,7 +210,7 @@ CTime CTime::operator + (CTime &t) ...@@ -218,7 +210,7 @@ CTime CTime::operator + (CTime &t)
CTime tmp; CTime tmp;
unsigned long tn; unsigned long tn;
if ( (double)this->sec + (double)t.sec < ULONG_MAX ) if ( this->sec + t.sec < ULONG_MAX )
{ {
tn = this->nsec + t.nsec; tn = this->nsec + t.nsec;
tmp.sec = this->sec + t.sec; tmp.sec = this->sec + t.sec;
...@@ -310,6 +302,7 @@ std::string CTime::getString() ...@@ -310,6 +302,7 @@ std::string CTime::getString()
{ {
default: default:
case ctf_secnano: case ctf_secnano:
//sec nsec
//o << t.own_time.tv_sec << " " << t.own_time.tv_nsec; //o << t.own_time.tv_sec << " " << t.own_time.tv_nsec;
if(this->nsec<10) extra_zero = "00000000"; if(this->nsec<10) extra_zero = "00000000";
else if(this->nsec<100) extra_zero = "0000000"; else if(this->nsec<100) extra_zero = "0000000";
...@@ -322,17 +315,26 @@ std::string CTime::getString() ...@@ -322,17 +315,26 @@ std::string CTime::getString()
output << this->sec << " " << extra_zero << this->nsec; output << this->sec << " " << extra_zero << this->nsec;
break; break;
case ctf_datetime: case ctf_datetime:
//YYYY-MM-DD,HH:MM:SS
timeinfo = localtime ( (time_t *)&this->sec ); timeinfo = localtime ( (time_t *)&this->sec );
strftime (outstr,23,"%F,%T",timeinfo); strftime (outstr,23,"%F,%T",timeinfo);
output << outstr; output << outstr;
break; 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: case ctf_ms:
// sec.0ms
ms = round( this->nsec/1000000 ); ms = round( this->nsec/1000000 );
if(ms<100 && ms>0) extra_zero = "0"; if(ms<100 && ms>0) extra_zero = "0";
if(ms<10 && ms>0) extra_zero = "00"; if(ms<10 && ms>0) extra_zero = "00";
output << this->sec << "." << extra_zero << ms ; output << this->sec << "." << extra_zero << ms ;
break; break;
case ctf_us: case ctf_us:
// sec.0000us
us = round( this->nsec/1000 ); us = round( this->nsec/1000 );
if(us<10) extra_zero = "00000"; if(us<10) extra_zero = "00000";
else if(us<100) extra_zero = "0000"; else if(us<100) extra_zero = "0000";
...@@ -370,8 +372,8 @@ timespec CTime::msToTimespec( double time_ms ) ...@@ -370,8 +372,8 @@ timespec CTime::msToTimespec( double time_ms )
timespec temp; timespec temp;
double seconds = (double)time_ms/1000.0; double seconds = (double)time_ms/1000.0;
temp.tv_sec = (long)floor(seconds); temp.tv_sec = (long long)floor(seconds);
temp.tv_nsec = (long)floor((seconds-temp.tv_sec)*1000000000); temp.tv_nsec = (long long)floor((seconds-temp.tv_sec)*1000000000);
return temp; return temp;
} }
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
* *
* * miliseconds * * 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 /** \class CTime
* *
...@@ -310,7 +310,7 @@ class CTime ...@@ -310,7 +310,7 @@ class CTime
*/ */
void resetRef(); void resetRef();
CTime diff(CTime &t0, CTime &t1); CTime diff(CTime &t1, CTime &t0);
///@} ///@}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment