當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 difftime用法及代碼示例

C語言time頭文件(time.h)中difftime函數的用法及代碼示例。

用法:

double difftime (time_t end, time_t beginning);
兩次之間的收益差
計算兩者之間的差值(以秒為單位)beginningend

參數

end
計算長度的時間間隔的上限。
beginning
計算其時間長度的時間間隔的下限。
如果這描述的時間晚於end,結果為負數。
time_t是基本麵的別名算術類型能夠代表函數返回的時間time

返回值

的結果(end-beginning)以秒為單位的浮點值類型double

示例

/* 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標準庫大神的英文原創作品 C difftime function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。