difftime(), difftime64() — Compute time difference

Standards

Standards / Extensions C or C++ Dependencies

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
Language Environment®

both  

Format

Format of the difftime() function:
#include <time.h>

double difftime(time64_t time2, time_t time1);
Format of the difftime64() function:
#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
⁄* 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));                                             
}                                                                               
Output
 11 x 55 = ? Enter your answer below
 605
 You answered correctly in 20 seconds

Related information