views:

216

answers:

1

struct timeval represents and instant in time with two members, tv_sec (seconds) and tv_usec (microseconds). In this representation, tv_usec is not by itself an absolute time it is a sub second offset off of tv_sec.

struct timespec works the same way except that instead of microseconds it's offset (tv_nsec) is stored in nanosecond units.

The question is: Is there a standard way to convert between these two?

+2  A: 

Looking at this doc, I would think multiplying tv_usec by 1000 is sufficient to get tv_nsec.

More important, I suspect is the source of the different structures: they could be filled by different clocks.

kdgregory
Since tv_nsec is a sub second offset, what if the multiplication by 1000 results in a value greater than 1 billion... Wouldn't you then need to add 1 to tv_sec and then set tv_nsec to the amount over 1 billion?
dicroce
No because `tv_usec`, being an offset in microseconds, is guaranteed to be smaller than 1 million, so multiplying it by 1000 gives a value less than 1 billion.
jk