32 this->use_zero =
false;
34 this->setFormat(ctf_us);
66 return (
double)this->sec + (double)this->nsec/1000000000;
72 return ( (
double) ( this->sec * 1000 + this->nsec / 1000000 ));
77 return ( (
double) ( this->sec * 1000000 + this->nsec / 1000 ));
86 t.tv_sec = (long) this->sec;
87 t.tv_nsec = (long) this->nsec;
96 t.tv_sec = (long) this->sec;
97 t.tv_usec = (long) (this->nsec/1000);
104 return (time_t)this->sec;
115 #if defined __APPLE__
117 gettimeofday(&tv, NULL);
118 time_temp.tv_sec = tv.tv_sec;
119 time_temp.tv_nsec = tv.tv_usec*1000;
121 clock_gettime(CLOCK_REALTIME, &time_temp );
126 time_temp = this->msToTimespec(milliseconds);
128 this->sec = time_temp.tv_sec;
129 this->nsec = time_temp.tv_nsec;
130 if(this->use_zero && milliseconds<0.0)
134 tmp.nsec = this->nsec;
135 tmp = diff(tmp,this->zero);
137 this->nsec = tmp.nsec;
144 this->use_zero =
true;
155 this->use_zero =
false;
160 if(*
this >= this->zero)
162 *
this = diff(*
this,this->zero);
167 this->use_zero = use;
172 return this->use_zero;
179 s = (
long long)t1.sec - (
long long)t0.sec;
180 n = (
long long)t1.nsec - (
long long)t0.nsec;
213 if ( this->sec + t.sec < ULONG_MAX )
215 tn = this->nsec + t.nsec;
216 tmp.sec = this->sec + t.sec;
218 if( tn / 1000000000 > 0)
221 tmp.nsec = (this->nsec + t.nsec) % 1000000000;
234 t = (double) this->sec + (((
double) this->nsec) / 1000000000) ;
238 tmp.sec = (
unsigned long) s;
239 tmp.nsec = (
unsigned long)( ns * 1000000000 );
246 return( (this->sec == t.sec) && (this->nsec == t.nsec) );
251 return( (this->sec != t.sec) || (this->nsec != t.nsec) );
256 if(this->sec == t.sec)
257 return((this->nsec >= t.nsec));
259 return((this->sec > t.sec));
264 if(this->sec == t.sec)
265 return((this->nsec <= t.nsec));
267 return((this->sec < t.sec));
272 if(this->sec == t.sec)
273 return((this->nsec > t.nsec));
275 return((this->sec > t.sec));
280 if(this->sec == t.sec)
281 return((this->nsec < t.nsec));
283 return((this->sec < t.sec));
286 std::ostream& operator << (std::ostream &o,
CTime &t)
297 struct tm * timeinfo;
298 std::string extra_zero;
299 std::stringstream output;
301 switch(this->print_format)
307 if(this->nsec<10) extra_zero =
"00000000";
308 else if(this->nsec<100) extra_zero =
"0000000";
309 else if(this->nsec<1000) extra_zero =
"000000";
310 else if(this->nsec<10000) extra_zero =
"00000";
311 else if(this->nsec<100000) extra_zero =
"0000";
312 else if(this->nsec<1000000) extra_zero =
"000";
313 else if(this->nsec<10000000) extra_zero =
"00";
314 else if(this->nsec<100000000) extra_zero =
"0";
315 output << this->sec <<
" " << extra_zero << this->nsec;
319 timeinfo = localtime ( (time_t *)&this->sec );
320 strftime (outstr,23,
"%F,%T",timeinfo);
325 timeinfo = localtime ( (time_t *)&this->sec );
326 strftime (outstr,23,
"%F-%H%M%S-",timeinfo);
327 output << outstr << (this->nsec/1000000);
331 ms = round( this->nsec/1000000 );
332 if(ms<100 && ms>0) extra_zero =
"0";
333 if(ms<10 && ms>0) extra_zero =
"00";
334 output << this->sec <<
"." << extra_zero << ms ;
338 us = round( this->nsec/1000 );
339 if(us<10) extra_zero =
"00000";
340 else if(us<100) extra_zero =
"0000";
341 else if(us<1000) extra_zero =
"000";
342 else if(us<10000) extra_zero =
"00";
343 else if(us<100000) extra_zero =
"0";
344 output << this->sec <<
"." << extra_zero << us ;
353 this->print_format = format;
358 return this->print_format;
366 return( time.tv_sec*1000.0 + (time.tv_nsec/1000000.0) );
374 double seconds = (double)time_ms/1000.0;
375 temp.tv_sec = (
long long)floor(seconds);
376 temp.tv_nsec = (
long long)floor((seconds-temp.tv_sec)*1000000000);
double getTimeInMilliseconds(void)
Get time in milliseconds.
unsigned long nanoseconds(void)
Get Nanoseconds.
bool operator<=(CTime &t)
Comparison <= operator.
CTime operator+(CTime &t)
Calculates time addition.
static double timespecToMs(timespec t)
conversion from timespec to milliseconds
long getTimeInMicroseconds(void)
Get time in microseconds.
void useRef(bool use)
sets if use or not the reference internal time
bool operator!=(CTime &t)
Comparison operator.
void setRef()
Sets the internal reference time.
CTime operator-(CTime &t)
Calculates time difference.
timeval getTimeInTimeval(void)
Get time in a timeval strcuture.
unsigned long seconds(void)
Get Seconds.
bool operator>(CTime &t)
Comparison > operator.
bool operator==(CTime &t)
Comparison operator.
std::string getString(void)
Gives a formatted string of this time.
time_t getTimeInTime_t(void)
Get time in time_t.
Implementation of a time class and operations.
ctimeformat getFormat()
Gets output string format.
static timespec msToTimespec(double ms)
conversion from milliseconds to timespec
timespec getTimeInTimespec(void)
Get time in a timespec strcuture.
double getTimeInSeconds(void)
Get time in seconds.
void setFormat(ctimeformat format)
Sets output string format.
void set(double milliseconds=-1.0)
Sets the internal time.
bool operator<(CTime &t)
Comparison < operator.
bool isRefUsed()
Get if is used or not the reference internal time.
CTime operator/(int div)
Calculates division by integer.
bool operator>=(CTime &t)
Comparison >= operator.
CTime getRef()
Get the reference internal time.
void resetRef()
Resets the reference time to the epoch.