difftime(), difftime64() — Compute time difference
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
Format
#include <time.h>
double difftime(time64_t time2, time_t time1);
#define _LARGE_TIME_API
#include <time.h>
double difftime64(time64_t time2, time64_t time1);
General description
Computes the difference in seconds between time2 and time1, which are calendar times returned by time().
The difftime() function returns the difference between two calendar times as a double. The return value is hexadecimal floating-point or IEEE Binary Floating-Point format depending on the floating-point mode of the thread invoking difftime(). The difftime() function uses __isBFP() to determine which floating-point format (hexadecimal floating-point or IEEE Binary Floating-Point) to return on the invoking thread.
The function difftime64() will behave exactly like difftime() except it will support calendar times beyond 03:14:07 UTC on January 19, 2038 with a limit of 23:59:59 UTC on December 31, 9999.
Returned value
Returns the elapsed time in seconds from time1 to time2 as a double.
Example
/* CELEBD04
This example shows a timing application using &diff..
The example calculates how long, on average, it takes a
user to input some data to the program.
*/
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t start, finish;
int i, n, num;
int answer;
printf("11 x 55 = ? Enter your answer below\n");
time(&start);
scanf("%d",&answer);
time(&finish);
printf("You answered %s in %.0f seconds.\n",
answer == 605 ? "correctly" : "incorrectly",
difftime(finish,start));
}
11 x 55 = ? Enter your answer below
605
You answered correctly in 20 seconds
Related information
- time.h
- asctime(), asctime64() — Convert time to character string
- asctime_r(), asctime64_r() — Convert date and time to a character string
- ctime(), ctime64() — Convert time to character string
- ctime_r(), ctime64_r() — Convert time value to date and time character string
- gmtime(), gmtime64() — Convert time to broken-down UTC time
- gmtime_r(), gmtime64_r() — Convert a time value to broken-down UTC time
- __isBFP() — Determine application floating-point format
- localtime(), localtime64() — Convert time and correct for local time
- localtime_r(), localtime64_r() — Convert time value to broken-down local time
- mktime(), mktime64() — Convert local time
- strftime() — Convert to formatted time
- time(),time64() — Determine current UTC time