appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::chronos Namespace Reference

Chronos - God of Time. More...

Classes

class  Time
 Time class. More...
 

Functions

timespec now ()
 Get the current time, indentified by CLOCK_REALTIME, since the last Epoch. More...
 
int now (timespec &ts)
 Get the current time, indentified by CLOCK_REALTIME, since the last Epoch. More...
 
double toFp (const timespec &ts)
 Convert timespec to floating point number equivalent. More...
 
timespec toTs (const double &t)
 Convert floating point seconds to timespec equivalent. More...
 
bool isSet (const timespec &a)
 Check if timespec is set. More...
 
void clear (timespec &ts)
 Clear this object's time. More...
 
timespec add (const timespec &a, const timespec &b)
 Add two timespecs. More...
 
timespec operator+ (const timespec &a, const timespec &b)
 a + b More...
 
timespec sub (const timespec &a, const timespec &b)
 Subtract two timespecs. More...
 
timespec operator- (const timespec &a, const timespec &b)
 a - b More...
 
timespec normalize (const timespec &a)
 Normalize the timespec. More...
 
std::ostream & operator<< (std::ostream &os, const Time &obj)
 Time insertion operator. More...
 

Variables

const long MILLION = 1000000
 1,000,000
 
const long long BILLION = 1000000000
 1,000,000,000
 

Detailed Description

Chronos - God of Time.

Function Documentation

timespec rnr::chronos::add ( const timespec &  a,
const timespec &  b 
)

Add two timespecs.

a + b.

Parameters
aTimespec a.
bTimespec b.
Returns
Normalized timespec.

Definition at line 116 of file Time.cxx.

References normalize().

Referenced by operator+(), and rnr::chronos::Time::operator+=().

117  {
118  timespec ts;
119 
120  ts.tv_sec = a.tv_sec + b.tv_sec;
121  ts.tv_nsec = a.tv_nsec + b.tv_nsec;
122 
123  return rnr::chronos::normalize(ts);
124  }
timespec normalize(const timespec &a)
Normalize the timespec.
Definition: Time.cxx:136
void rnr::chronos::clear ( timespec &  ts)

Clear this object's time.

Parameters
tsTimespec.

Definition at line 110 of file Time.cxx.

Referenced by rnr::chronos::Time::clear(), rnr::chronos::Time::getResolution(), now(), and rnr::chronos::Time::Time().

111  {
112  ts.tv_sec = 0;
113  ts.tv_nsec = 0;
114  }
bool rnr::chronos::isSet ( const timespec &  a)

Check if timespec is set.

Parameters
tsTimespec.
Returns
Returns true or false.

Definition at line 105 of file Time.cxx.

Referenced by rnr::LogBook::Entry::empty(), and rnr::chronos::Time::isSet().

106  {
107  return (ts.tv_sec != 0) || (ts.tv_nsec != 0);
108  }
timespec rnr::chronos::normalize ( const timespec &  a)

Normalize the timespec.

Parameters
aTimespec.
Returns
Normalized timespec where tv_nsec has a value in the range 0 to 999,999,999.

Definition at line 136 of file Time.cxx.

References BILLION.

Referenced by add(), operator-(), and sub().

137  {
138  timespec ts = a;
139 
140  while( ts.tv_nsec >= BILLION )
141  {
142  ts.tv_sec += 1;
143  ts.tv_nsec -= BILLION;
144  }
145 
146  while( ts.tv_nsec < 0 )
147  {
148  ts.tv_sec -= 1;
149  ts.tv_nsec += BILLION;
150  }
151 
152  return ts;
153  }
const long long BILLION
1,000,000,000
Definition: Time.h:66
timespec rnr::chronos::now ( )

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

Returns
Timespec.

Definition at line 70 of file Time.cxx.

References clear().

Referenced by rnr::LogBook::Entry::Entry(), rnr::chronos::Time::markNow(), and rnr::chronos::Time::now().

71  {
72  timespec ts;
73 
74  if( now(ts) != OK )
75  {
76  clear(ts);
77  }
78  return ts;
79  }
void clear(timespec &ts)
Clear this object&#39;s time.
Definition: Time.cxx:110
timespec now()
Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:70
int rnr::chronos::now ( timespec &  ts)

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

Parameters
[out]tsTimespec.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 81 of file Time.cxx.

82  {
83  return clock_gettime(CLOCK_REALTIME, &ts) == 0? OK: RC_ERROR;
84  }
timespec rnr::chronos::operator+ ( const timespec &  a,
const timespec &  b 
)
inline

a + b

Parameters
aTimespec a.
bTimespec b.
Returns
Normalized timespec.

Definition at line 140 of file Time.h.

References add(), and sub().

141  {
142  return add(a, b);
143  }
timespec add(const timespec &a, const timespec &b)
Add two timespecs.
Definition: Time.cxx:116
timespec rnr::chronos::operator- ( const timespec &  a,
const timespec &  b 
)
inline

a - b

Parameters
aTimespec a.
bTimespec b.
Returns
Normalized timespec.

Definition at line 165 of file Time.h.

References normalize(), and sub().

166  {
167  return sub(a, b);
168  }
timespec sub(const timespec &a, const timespec &b)
Subtract two timespecs.
Definition: Time.cxx:126
std::ostream& rnr::chronos::operator<< ( std::ostream &  os,
const Time obj 
)

Time insertion operator.

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

Referenced by rnr::chronos::Time::operator>().

timespec rnr::chronos::sub ( const timespec &  a,
const timespec &  b 
)

Subtract two timespecs.

a - b

Parameters
aTimespec a.
bTimespec b.
Returns
Normalized timespec.

Definition at line 126 of file Time.cxx.

References normalize().

Referenced by operator+(), operator-(), and rnr::chronos::Time::operator-=().

127  {
128  timespec ts;
129 
130  ts.tv_sec = a.tv_sec - b.tv_sec;
131  ts.tv_nsec = a.tv_nsec - b.tv_nsec;
132 
133  return rnr::chronos::normalize(ts);
134  }
timespec normalize(const timespec &a)
Normalize the timespec.
Definition: Time.cxx:136
double rnr::chronos::toFp ( const timespec &  ts)

Convert timespec to floating point number equivalent.

Parameters
tsTimespec.
Returns
Seconds and fractions of a second.

Definition at line 86 of file Time.cxx.

References BILLION.

Referenced by rnr::chronos::Time::getResolution(), rnr::chronos::Time::markNow(), rnr::chronos::Time::now(), rnr::chronos::Time::operator+=(), rnr::chronos::Time::operator-=(), rnr::chronos::Time::operator=(), and rnr::chronos::Time::Time().

87  {
88  return (double)ts.tv_sec + (double)ts.tv_nsec/ (double)BILLION;
89  }
const long long BILLION
1,000,000,000
Definition: Time.h:66
timespec rnr::chronos::toTs ( const double &  t)

Convert floating point seconds to timespec equivalent.

Parameters
Secondsand fractions of a second.
Returns
ts Timespec.

Definition at line 91 of file Time.cxx.

References BILLION.

Referenced by rnr::chronos::Time::operator*=(), rnr::chronos::Time::operator+=(), rnr::chronos::Time::operator-=(), rnr::chronos::Time::operator=(), and rnr::chronos::Time::Time().

92  {
93  timespec ts;
94  double sec, nsec;
95 
96  sec = floor(t);
97  nsec = (t - sec) * (double)BILLION;
98 
99  ts.tv_sec = (long)sec;
100  ts.tv_nsec = (long)nsec;
101 
102  return ts;
103  }
const long long BILLION
1,000,000,000
Definition: Time.h:66