4

I'm trying to write a simple function in C that would calculate the difference between two moments in nanoseconds. To do this, I thought of using the function gettimeofday, which updates the given struct timeval's fields.

As the man page said, the struct timeval's fields are:

time_t      tv_sec;     /* seconds */  
suseconds_t tv_usec;    /* microseconds */  

My question is as follows:

Is the tv_usec field is the WHOLE TIME passed since the EPOCH in microseconds, or is it just the remain of the time in microseconds?

For example, if the time passed is 100 seconds and 25 microseconds, will the tv_usec field have the value '25' or the value '100000025'?

Thanks a lot.

Zach
  • 537
  • 4
  • 9
  • 19
  • FWIW, `gettimeofday` only provides microsecond resolution, and is obsolete in POSIX.1-2008, so you should use `clock_gettime` instead. – user611775 Feb 20 '11 at 23:52

1 Answers1

4

It's the remainder.

This is the rest of the elapsed time (a fraction of a second), represented as the number of microseconds. It is always less than one million.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • 1
    Just FYI: a signed 64 bit number (`long long` must be at least this long) could represent almost 300,000 years from the epoch, measured in microseconds. – caf Feb 21 '11 at 01:10
  • A 32 bit integer can only represent a little more than 71 minutes as microseconds. – JustinDanielson Sep 17 '13 at 23:32