op25/op25/gr-op25_repeater/lib/ezpwd/timeofday

74 lines
1.7 KiB
Plaintext

#ifndef _EZPWD_TIMEOFDAY
#define _EZPWD_TIMEOFDAY
#include <sys/time.h>
//
// ezpwd::timeofday -- Return current time.
// ezpwd::epoch -- The UNIX epoch.
// ezpwd::seconds -- convert timeval to a real-valued seconds
// <timeval> < <timeval>-- less-than comparison on timevals
// <timeval> - <timeval>-- difference on timevals
//
namespace ezpwd {
inline
timeval timeofday()
{
timeval tv;
::gettimeofday( &tv, NULL );
return tv;
}
timeval epoch()
{
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
return tv;
}
inline
double seconds( const timeval &rhs )
{
return rhs.tv_usec / 1000000.0 + rhs.tv_sec;
}
} // namespace ezpwd
inline
bool operator<(
const timeval &lhs,
const timeval &rhs )
{
return ( lhs.tv_sec < rhs.tv_sec
|| (( lhs.tv_sec == rhs.tv_sec )
&& ( lhs.tv_usec < rhs.tv_usec )));
}
inline
timeval operator-(
const timeval &lhs,
timeval rhs ) // copy; adjusted...
{
timeval result;
if ( lhs < rhs ) {
result = ezpwd::epoch();
} else {
// See http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html
if ( lhs.tv_usec < rhs.tv_usec ) {
int sec = ( rhs.tv_usec - lhs.tv_usec ) / 1000000 + 1;
rhs.tv_usec -= sec * 1000000;
rhs.tv_sec += sec;
}
if ( lhs.tv_usec - rhs.tv_usec > 1000000 ) {
int sec = ( lhs.tv_usec - rhs.tv_usec ) / 1000000;
rhs.tv_usec += sec * 1000000;
rhs.tv_sec -= sec;
}
result.tv_sec = lhs.tv_sec - rhs.tv_sec;
result.tv_usec = lhs.tv_usec - rhs.tv_usec;
}
return result;
}
#endif // _EZPWD_TIMEOFDAY