function
<ctime>

difftime

double difftime (time_t end, time_t beginning);
Return difference between two times
Calculates the difference in seconds between beginning and end.

Parameters

end
Higher bound of the time interval whose length is calculated.
beginning
Lower bound of the time interval whose length is calculated.
If this describes a time point later than end, the result is negative.
time_t is an alias of a fundamental arithmetic type capable of representing times as returned by function time.

Return Value

The result of (end-beginning) in seconds as a floating-point value of type double.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* difftime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */

int main ()
{
  time_t now;
  struct tm newyear;
  double seconds;

  time(&now);  /* get current time; same as: now = time(NULL)  */

  newyear = *localtime(&now);

  newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;
  newyear.tm_mon = 0;  newyear.tm_mday = 1;

  seconds = difftime(now,mktime(&newyear));

  printf ("%.f seconds since new year in the current timezone.\n", seconds);

  return 0;
}

Output:
3777291 seconds since new year in the current timezone.


Data races

Concurrently calling this function is safe, causing no data races.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

See also