Simbody  3.6
Timespec/Nanosecond/Second Conversions

These inline functions provide a fast and convenient way for doing arithmetic with the ugly Posix timespec struct. More...

Functions

long long SimTK::timespecToNs (const timespec &ts)
 Convert a time stored in a timespec struct to the equivalent number of nanoseconds (as a signed quantity). More...
 
void SimTK::nsToTimespec (const long long &ns, timespec &ts)
 Given a signed number of nanoseconds, convert that into seconds and leftover nanoseconds in a timespec struct. More...
 
double SimTK::nsToSec (const long long &ns)
 Given a count of nanosecond ticks as a signed 64 bit integer, return the same time interval as a double precision floating point number of seconds. More...
 
long long SimTK::secToNs (const double &s)
 Given a signed time interval as a double precision floating point number of seconds, return the same time interval as a count of nanosecond ticks in a signed 64 bit integer. More...
 

Detailed Description

These inline functions provide a fast and convenient way for doing arithmetic with the ugly Posix timespec struct.

Use them to convert the timespec to a long long integer number of nanoseconds, do arithmetic in that form, and then convert back. Negative times are handled correctly (they come up as the result of subtraction and comparisons).

We usually prefer to deal with times as a double precision floating point number of seconds and functions are provided for converting between nanoseconds and seconds in this format.

Cautions:
  • For long intervals the precision of time in seconds will necessarily be less than the precision of the nanosecond count, since IEEE double precision has a 53 bit mantissa, while 63 bits are available for the count.
  • A signed long long integer containing a count of nanosecond ticks is limited to time intervals of about +/- 292 years, which is substantially less than can be contained in a timespec, but is plenty for interval timing.

Function Documentation

◆ timespecToNs()

long long SimTK::timespecToNs ( const timespec &  ts)
inline

Convert a time stored in a timespec struct to the equivalent number of nanoseconds (as a signed quantity).

See also
nsToTimespec()

◆ nsToTimespec()

void SimTK::nsToTimespec ( const long long &  ns,
timespec &  ts 
)
inline

Given a signed number of nanoseconds, convert that into seconds and leftover nanoseconds in a timespec struct.

See also
timespecToNs()

◆ nsToSec()

double SimTK::nsToSec ( const long long &  ns)
inline

Given a count of nanosecond ticks as a signed 64 bit integer, return the same time interval as a double precision floating point number of seconds.

See Timespec/Nanosecond/Second Conversions for cautions.

See also
secToNs()

◆ secToNs()

long long SimTK::secToNs ( const double &  s)
inline

Given a signed time interval as a double precision floating point number of seconds, return the same time interval as a count of nanosecond ticks in a signed 64 bit integer.

See Timespec/Nanosecond/Second Conversions for cautions.

See also
nsToSec()