函式
<ctime>

difftime

double difftime (time_t end, time_t beginning);
返回兩個時間之間的差值
計算 beginningend 之間的秒數差。

引數

end
所計算的時間間隔的上限。
beginning
所計算的時間間隔的下限。
如果它描述的時間點晚於 end,則結果為負數。
time_t 是一個基礎算術型別的別名,能夠表示由函式 time 返回的時間。

返回值

(end-beginning) 的結果,以 double 浮點型別的秒數表示。

示例

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;
}

輸出
3777291 seconds since new year in the current timezone.


資料競爭

併發呼叫此函式是安全的,不會導致資料競爭。

異常 (C++)

無異常保證:此函式從不丟擲異常。

另見