appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::chronos::Time Class Reference

Time class. More...

#include <Time.h>

Public Member Functions

 Time ()
 Default constructor.
 
 Time (const timespec &ts)
 Initialization constructor. More...
 
 Time (const double &t)
 Initialization constructor. More...
 
 Time (const Time &src)
 Copy constructor. More...
 
 ~Time ()
 Destructor.
 
double t ()
 Get this object's value as a floating point number of seconds and fractions of a second. More...
 
timespec ts ()
 Get this object's value in timespec format. More...
 
bool isSet ()
 Check if this object's time is set. More...
 
void clear ()
 Clear this object's time.
 
double getResolution ()
 Get the CLOCK_REALTIME resolution. More...
 
double now ()
 Get the current time, indentified by CLOCK_REALTIME, since the last Epoch. More...
 
double markNow ()
 Mark the current time, indentified by CLOCK_REALTIME, since the last Epoch. More...
 
Timeoperator= (const Time &b)
 this = b More...
 
Timeoperator= (const timespec &b)
 this = b More...
 
Timeoperator= (const double &b)
 this = b More...
 
Timeoperator+= (const Time &b)
 this += b More...
 
Timeoperator+= (const timespec &b)
 this += b More...
 
Timeoperator+= (const double &b)
 this += b More...
 
Timeoperator-= (const Time &b)
 this -= b More...
 
Timeoperator-= (const timespec &b)
 this -= b More...
 
Timeoperator-= (const double &b)
 this -= b More...
 
Timeoperator*= (const double &b)
 this *= b More...
 
bool operator== (const Time &b)
 this == b More...
 
bool operator== (const timespec &b)
 this == b More...
 
bool operator== (const double &b)
 this == b More...
 
bool operator< (const Time &b)
 this < b More...
 
bool operator< (const timespec &b)
 this < b More...
 
bool operator< (const double &b)
 this < b More...
 
bool operator> (const Time &b)
 this > b More...
 
bool operator> (const timespec &b)
 this > b More...
 
bool operator> (const double &b)
 this > b More...
 
bool operator<= (const Time &b)
 this <= b More...
 
bool operator<= (const timespec &b)
 this <= b More...
 
bool operator<= (const double &b)
 this <= b More...
 
bool operator>= (const Time &b)
 this >= b More...
 
bool operator>= (const timespec &b)
 this >= b More...
 
bool operator>= (const double &b)
 this >= b More...
 
std::string calendarTime (const int resSec=0) const
 

Protected Attributes

timespec m_tsTime
 time in timespec format
 
double m_fpTime
 time in floating point format
 

Friends

Time operator+ (Time a, const Time &b)
 c = a + b More...
 
Time operator+ (Time a, const timespec &b)
 c = a + b More...
 
Time operator+ (Time a, const double &b)
 c = a + b More...
 
Time operator- (Time a, const Time &b)
 c = a - b More...
 
Time operator- (Time a, const timespec &b)
 c = a - b More...
 
Time operator- (Time a, const double &b)
 c = a - b More...
 
Time operator* (Time a, const double &b)
 c = a * b More...
 
std::ostream & operator<< (std::ostream &os, const Time &obj)
 Time insertion operator. More...
 

Detailed Description

Time class.

The class uses timespec as the underlining interface to system calls. Some calls require the lower resolution timeval interface. This class will autobmaitcally down/up samples between the two interfaces

Definition at line 192 of file Time.h.

Constructor & Destructor Documentation

Time::Time ( const timespec &  ts)

Initialization constructor.

Parameters
tsTime in timespec format.

Definition at line 167 of file Time.cxx.

References rnr::chronos::toFp().

168 {
169  m_tsTime = ts;
171 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
timespec ts()
Get this object&#39;s value in timespec format.
Definition: Time.h:244
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
Time::Time ( const double &  t)

Initialization constructor.

Parameters
tSeconds.

Definition at line 173 of file Time.cxx.

References rnr::chronos::toTs().

174 {
175  m_fpTime = t;
177 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
double t()
Get this object&#39;s value as a floating point number of seconds and fractions of a second.
Definition: Time.h:234
timespec toTs(const double &t)
Convert floating point seconds to timespec equivalent.
Definition: Time.cxx:91
Time::Time ( const Time src)

Copy constructor.

Parameters
srcSource object.

Definition at line 179 of file Time.cxx.

References m_fpTime, and m_tsTime.

180 {
181  m_tsTime = src.m_tsTime;
182  m_fpTime = src.m_fpTime;
183 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618

Member Function Documentation

double Time::getResolution ( )

Get the CLOCK_REALTIME resolution.

Returns
Returns a floating point number of the resolution in seconds and fractions of a second.

Definition at line 196 of file Time.cxx.

References rnr::chronos::clear(), and rnr::chronos::toFp().

Referenced by isSet().

197 {
198  timespec res;
199 
200  if( clock_getres(CLOCK_REALTIME, &res) < 0 )
201  {
202  chronos::clear(res);
203  }
204 
205  return chronos::toFp(res);
206 }
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
void clear(timespec &ts)
Clear this object&#39;s time.
Definition: Time.cxx:110
bool rnr::chronos::Time::isSet ( )
inline

Check if this object's time is set.

Returns
Returns true or false.

Definition at line 254 of file Time.h.

References clear(), getResolution(), rnr::chronos::isSet(), m_tsTime, markNow(), now(), operator*=(), operator+=(), operator-=(), and operator=().

255  {
256  return chronos::isSet(m_tsTime);
257  }
bool isSet(const timespec &a)
Check if timespec is set.
Definition: Time.cxx:105
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double Time::markNow ( )

Mark the current time, indentified by CLOCK_REALTIME, since the last Epoch.

The internal time data are automatically updated.

Returns
Returns a floating point number of the current time in seconds and fractions of a second.

Definition at line 215 of file Time.cxx.

References rnr::chronos::now(), and rnr::chronos::toFp().

Referenced by isSet(), rnr::Thread::schedBlock(), and rnr::Thread::thread().

216 {
219  return m_fpTime;
220 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
timespec now()
Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:70
double Time::now ( )

Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.

No internal time data are touched.

Returns
Returns a floating point number of the current time in seconds and fractions of a second.

Definition at line 208 of file Time.cxx.

References rnr::chronos::now(), and rnr::chronos::toFp().

Referenced by execFeedAnimal(), execSleep(), execWalkAnimal(), isSet(), and updateAnimals().

209 {
210  timespec ts = chronos::now();
211 
212  return chronos::toFp(ts);
213 }
timespec ts()
Get this object&#39;s value in timespec format.
Definition: Time.h:244
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
timespec now()
Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:70
Time & Time::operator*= ( const double &  b)

this *= b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 290 of file Time.cxx.

References rnr::chronos::toTs().

Referenced by isSet().

291 {
292  m_fpTime *= b;
294  return *this;
295 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
timespec toTs(const double &t)
Convert floating point seconds to timespec equivalent.
Definition: Time.cxx:91
Time & Time::operator+= ( const Time b)

this += b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 248 of file Time.cxx.

References rnr::chronos::add(), m_tsTime, and rnr::chronos::toFp().

Referenced by isSet().

249 {
252  return *this;
253 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
timespec add(const timespec &a, const timespec &b)
Add two timespecs.
Definition: Time.cxx:116
Time & Time::operator+= ( const timespec &  b)

this += b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 255 of file Time.cxx.

References rnr::chronos::add(), and rnr::chronos::toFp().

256 {
259  return *this;
260 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
timespec add(const timespec &a, const timespec &b)
Add two timespecs.
Definition: Time.cxx:116
Time & Time::operator+= ( const double &  b)

this += b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 262 of file Time.cxx.

References rnr::chronos::toTs().

263 {
264  m_fpTime += b;
266  return *this;
267 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
timespec toTs(const double &t)
Convert floating point seconds to timespec equivalent.
Definition: Time.cxx:91
Time & Time::operator-= ( const Time b)

this -= b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 269 of file Time.cxx.

References m_tsTime, rnr::chronos::sub(), and rnr::chronos::toFp().

Referenced by isSet().

270 {
273  return *this;
274 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
timespec sub(const timespec &a, const timespec &b)
Subtract two timespecs.
Definition: Time.cxx:126
Time & Time::operator-= ( const timespec &  b)

this -= b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 276 of file Time.cxx.

References rnr::chronos::sub(), and rnr::chronos::toFp().

277 {
280  return *this;
281 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
timespec sub(const timespec &a, const timespec &b)
Subtract two timespecs.
Definition: Time.cxx:126
Time & Time::operator-= ( const double &  b)

this -= b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 283 of file Time.cxx.

References rnr::chronos::toTs().

284 {
285  m_fpTime -= b;
287  return *this;
288 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
timespec toTs(const double &t)
Convert floating point seconds to timespec equivalent.
Definition: Time.cxx:91
bool rnr::chronos::Time::operator< ( const Time b)
inline

this < b

Parameters
bRvalue object.
Returns
Returns true if this object is an earlier time than b. Otherwise returns false.

Definition at line 495 of file Time.h.

References m_tsTime.

Referenced by operator<=().

496  {
497  return operator<(b.m_tsTime);
498  }
bool operator<(const Time &b)
this < b
Definition: Time.h:495
bool Time::operator< ( const timespec &  b)

this < b

Parameters
bRvalue object.
Returns
Returns true if this object is an earlier time than b. Otherwise returns false.

Definition at line 307 of file Time.cxx.

308 {
309  if( m_tsTime.tv_sec < b.tv_sec )
310  {
311  return true;
312  }
313  else if( (m_tsTime.tv_sec == b.tv_sec) && (m_tsTime.tv_nsec < b.tv_nsec) )
314  {
315  return true;
316  }
317  else
318  {
319  return false;
320  }
321 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
bool rnr::chronos::Time::operator< ( const double &  b)
inline

this < b

Parameters
bRvalue object.
Returns
Returns true if this object is an earlier time than b. Otherwise returns false.

Definition at line 504 of file Time.h.

References m_fpTime.

505  {
506  return m_fpTime < b;
507  }
double m_fpTime
time in floating point format
Definition: Time.h:618
bool rnr::chronos::Time::operator<= ( const Time b)
inline

this <= b

Parameters
bRvalue object.
Returns
Returns true if this object is an earlier or equal time to b. Otherwise returns false.

Definition at line 549 of file Time.h.

References m_tsTime, operator<(), and operator==().

550  {
551  return operator<(b.m_tsTime) || operator==(b.m_tsTime);
552  }
bool operator<(const Time &b)
this < b
Definition: Time.h:495
bool operator==(const Time &b)
this == b
Definition: Time.h:464
bool rnr::chronos::Time::operator<= ( const timespec &  b)
inline

this <= b

Parameters
bRvalue object.
Returns
Returns true if this object is an earlier or equal time to b. Otherwise returns false.

Definition at line 555 of file Time.h.

References operator<(), and operator==().

556  {
557  return operator<(b) || operator==(b);
558  }
bool operator<(const Time &b)
this < b
Definition: Time.h:495
bool operator==(const Time &b)
this == b
Definition: Time.h:464
bool rnr::chronos::Time::operator<= ( const double &  b)
inline

this <= b

Parameters
bRvalue object.
Returns
Returns true if this object is an earlier or equal time to b. Otherwise returns false.

Definition at line 561 of file Time.h.

References m_fpTime.

562  {
563  return m_fpTime <= b;
564  }
double m_fpTime
time in floating point format
Definition: Time.h:618
Time & Time::operator= ( const Time b)

this = b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 227 of file Time.cxx.

References m_fpTime, and m_tsTime.

Referenced by isSet().

228 {
229  m_tsTime = b.m_tsTime;
230  m_fpTime = b.m_fpTime;
231  return *this;
232 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
Time & Time::operator= ( const timespec &  b)

this = b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 234 of file Time.cxx.

References rnr::chronos::toFp().

235 {
236  m_tsTime = b;
238  return *this;
239 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
Time & Time::operator= ( const double &  b)

this = b

Parameters
bRvalue object.
Returns
Reference to this.

Definition at line 241 of file Time.cxx.

References rnr::chronos::toTs().

242 {
243  m_fpTime = b;
245  return *this;
246 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
timespec toTs(const double &t)
Convert floating point seconds to timespec equivalent.
Definition: Time.cxx:91
bool rnr::chronos::Time::operator== ( const Time b)
inline

this == b

Parameters
bRvalue object.
Returns
Returns true if this object is the same time as b. Otherwise returns false.

Definition at line 464 of file Time.h.

References m_tsTime.

Referenced by operator<=(), and operator>=().

465  {
466  return operator==(b.m_tsTime);
467  }
bool operator==(const Time &b)
this == b
Definition: Time.h:464
bool rnr::chronos::Time::operator== ( const timespec &  b)
inline

this == b

Parameters
bRvalue object.
Returns
Returns true if this object is the same time as b. Otherwise returns false.

Definition at line 470 of file Time.h.

References m_tsTime.

471  {
472  return (m_tsTime.tv_sec == b.tv_sec) &&
473  (m_tsTime.tv_nsec == b.tv_nsec);
474  }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
bool rnr::chronos::Time::operator== ( const double &  b)
inline

this == b

Parameters
bRvalue object.
Returns
Returns true if this object is the same time as b. Otherwise returns false.

Definition at line 477 of file Time.h.

References m_fpTime.

478  {
479  return m_fpTime == b;
480  }
double m_fpTime
time in floating point format
Definition: Time.h:618
bool rnr::chronos::Time::operator> ( const Time b)
inline

this > b

Parameters
bRvalue object.
Returns
Returns true if this object is an later time than b. Otherwise returns false.

Definition at line 522 of file Time.h.

References m_tsTime.

Referenced by operator>=().

523  {
524  return operator>(b.m_tsTime);
525  }
bool operator>(const Time &b)
this > b
Definition: Time.h:522
bool Time::operator> ( const timespec &  b)

this > b

Parameters
bRvalue object.
Returns
Returns true if this object is an later time than b. Otherwise returns false.

Definition at line 323 of file Time.cxx.

References m_tsTime, rnr::chronos::operator<<(), and rnr::str::rtrim().

324 {
325  if( m_tsTime.tv_sec > b.tv_sec )
326  {
327  return true;
328  }
329  else if( (m_tsTime.tv_sec == b.tv_sec) && (m_tsTime.tv_nsec > b.tv_nsec) )
330  {
331  return true;
332  }
333  else
334  {
335  return false;
336  }
337 }
timespec m_tsTime
time in timespec format
Definition: Time.h:617
bool rnr::chronos::Time::operator> ( const double &  b)
inline

this > b

Parameters
bRvalue object.
Returns
Returns true if this object is an later time than b. Otherwise returns false.

Definition at line 531 of file Time.h.

References m_fpTime.

532  {
533  return m_fpTime > b;
534  }
double m_fpTime
time in floating point format
Definition: Time.h:618
bool rnr::chronos::Time::operator>= ( const Time b)
inline

this >= b

Parameters
bRvalue object.
Returns
Returns true if this object is an later or equal time to b. Otherwise returns false.

Definition at line 579 of file Time.h.

References m_tsTime, operator==(), and operator>().

580  {
581  return operator>(b.m_tsTime) || operator==(b.m_tsTime);
582  }
bool operator>(const Time &b)
this > b
Definition: Time.h:522
bool operator==(const Time &b)
this == b
Definition: Time.h:464
bool rnr::chronos::Time::operator>= ( const timespec &  b)
inline

this >= b

Parameters
bRvalue object.
Returns
Returns true if this object is an later or equal time to b. Otherwise returns false.

Definition at line 585 of file Time.h.

References operator==(), and operator>().

586  {
587  return operator>(b) || operator==(b);
588  }
bool operator>(const Time &b)
this > b
Definition: Time.h:522
bool operator==(const Time &b)
this == b
Definition: Time.h:464
bool rnr::chronos::Time::operator>= ( const double &  b)
inline

this >= b

Parameters
bRvalue object.
Returns
Returns true if this object is an later or equal time to b. Otherwise returns false.

Definition at line 591 of file Time.h.

References m_fpTime, and operator<<.

592  {
593  return m_fpTime >= b;
594  }
double m_fpTime
time in floating point format
Definition: Time.h:618
double rnr::chronos::Time::t ( )
inline

Get this object's value as a floating point number of seconds and fractions of a second.

Returns
Double.

Definition at line 234 of file Time.h.

References m_fpTime.

235  {
236  return m_fpTime;
237  }
double m_fpTime
time in floating point format
Definition: Time.h:618
timespec rnr::chronos::Time::ts ( )
inline

Get this object's value in timespec format.

Returns
timespec.

Definition at line 244 of file Time.h.

References m_tsTime.

Referenced by rnr::Thread::exec(), and rnr::Thread::schedBlock().

245  {
246  return m_tsTime;
247  }
timespec m_tsTime
time in timespec format
Definition: Time.h:617

Friends And Related Function Documentation

Time operator* ( Time  a,
const double &  b 
)
friend

c = a * b

Parameters
aTime object.
bRvalue object.
Returns
Time

Definition at line 441 of file Time.h.

442  {
443  return a *= b;
444  }
Time operator+ ( Time  a,
const Time b 
)
friend

c = a + b

Parameters
aTime object.
bRvalue object.
Returns
Time

Definition at line 387 of file Time.h.

388  {
389  return a += b;
390  }
Time operator+ ( Time  a,
const timespec &  b 
)
friend

c = a + b

Parameters
aTime object.
bRvalue object.
Returns
Time

Definition at line 393 of file Time.h.

394  {
395  return a += b;
396  }
Time operator+ ( Time  a,
const double &  b 
)
friend

c = a + b

Parameters
aTime object.
bRvalue object.
Returns
Time

Definition at line 399 of file Time.h.

400  {
401  return a += b;
402  }
Time operator- ( Time  a,
const Time b 
)
friend

c = a - b

Parameters
aTime object.
bRvalue object.
Returns
Time

Definition at line 416 of file Time.h.

417  {
418  return a -= b;
419  }
Time operator- ( Time  a,
const timespec &  b 
)
friend

c = a - b

Parameters
aTime object.
bRvalue object.
Returns
Time

Definition at line 422 of file Time.h.

423  {
424  return a -= b;
425  }
Time operator- ( Time  a,
const double &  b 
)
friend

c = a - b

Parameters
aTime object.
bRvalue object.
Returns
Time

Definition at line 428 of file Time.h.

429  {
430  return a -= b;
431  }
std::ostream& operator<< ( std::ostream &  os,
const Time obj 
)
friend

Time insertion operator.

Parameters
osOutput stream.
objObject to insert.
Returns
Reference to output stream.

Referenced by operator>=().


The documentation for this class was generated from the following files: