The C difftime() function subtracts one time_t calendar value from another and returns the interval in seconds as a double. The order is time2 - time1.
Use it instead of assuming that time_t is a plain integer with one unit per second. A positive result means the second argument in time order, time2, is later. Include <time.h>.
Syntax
Start with this concrete difftime() call:
C
difftime(finish, start);
Call
What it does
difftime(finish, start);
Returns the number of seconds from start to finish.
Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.
Library declaration
C
double difftime(time_t time2, time_t time1);
Parameters
Parameter
Description
time2
Later (or comparison) calendar time.
time1
Earlier (or baseline) calendar time.
Return Value
Return Value
Description
Positive double
time2 is later than time1.
Zero
The calendar-time values represent the same instant.
Negative double
time2 is earlier than time1.
C Examples
Example 1 — Find an Interval
This example demonstrates difftime() with a small, complete program and checks the relevant result before using it.