Next: Processor And CPU Time, Previous: Time Basics, Up: Date and Time
21.2 Elapsed Time
One way to represent an elapsed time is with a simple arithmetic data type, as with the following function to compute the elapsed time between two calendar times. This function is declared in time.h.
The
difftime
function returns the number of seconds of elapsed time between calendar time time1 and calendar time time0, as a value of typedouble
. The difference ignores leap seconds unless leap second support is enabled.In the GNU system, you can simply subtract
time_t
values. But on other systems, thetime_t
data type might use some other encoding where subtraction doesn’t work directly.
The GNU C library provides two data types specifically for representing an elapsed time. They are used by various GNU C library functions, and you can use them for your own purposes too. They’re exactly the same except that one has a resolution in microseconds, and the other, newer one, is in nanoseconds.
The
struct timeval
structure represents an elapsed time. It is declared in sys/time.h and has the following members:
long int tv_sec
- This represents the number of whole seconds of elapsed time.
long int tv_usec
- 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.
The
struct timespec
structure represents an elapsed time. It is declared in time.h and has the following members:
long int tv_sec
- This represents the number of whole seconds of elapsed time.
long int tv_nsec
- This is the rest of the elapsed time (a fraction of a second), represented as the number of nanoseconds. It is always less than one billion.
It is often necessary to subtract two values of type struct timeval
or struct timespec
. Here is the best way to do this. It works even on some peculiar operating systems where the tv_sec
member has an unsigned type.
/* <span class="roman"><font face="Times New Roman">Subtract the `struct timeval' values X and Y,</font></span> <span class="roman"><font face="Times New Roman">storing the result in RESULT.</font></span> <span class="roman"><font face="Times New Roman">Return 1 if the difference is negative, otherwise 0.</font></span> */ int timeval_subtract (result, x, y) struct timeval *result, *x, *y; { /* <span class="roman"><font face="Times New Roman">Perform the carry for the later subtraction by updating </font></span><var>y</var><span class="roman"><font face="Times New Roman">.</font></span> */ if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (x->tv_usec - y->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } /* <span class="roman"><font face="Times New Roman">Compute the time remaining to wait.</font></span> <code>tv_usec</code><span class="roman"><font face="Times New Roman"> is certainly positive.</font></span> */ result->tv_sec = x->tv_sec - y->tv_sec; result->tv_usec = x->tv_usec - y->tv_usec; /* <span class="roman"><font face="Times New Roman">Return 1 if result is negative.</font></span> */ return x->tv_sec < y->tv_sec; }
Common functions that use struct timeval
are gettimeofday
and settimeofday
.
There are no GNU C library functions specifically oriented toward dealing with elapsed times, but the calendar time, processor time, and alarm and sleeping functions have a lot to do with them.