C語言time頭文件(time.h)中difftime函數的用法及代碼示例。
用法:
double difftime (time_t end, time_t beginning);
兩次之間的收益差
參數
- end
- 計算長度的時間間隔的上限。
- beginning
- 計算其時間長度的時間間隔的下限。
如果這描述的時間晚於end,結果為負數。
返回值
的結果(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語言 clock用法及代碼示例
- C語言 mktime用法及代碼示例
- C語言 time用法及代碼示例
- C語言 asctime用法及代碼示例
- C語言 ctime用法及代碼示例
- C語言 gmtime用法及代碼示例
- C語言 localtime用法及代碼示例
- C語言 strftime用法及代碼示例
注:本文由純淨天空篩選整理自C標準庫大神的英文原創作品 C difftime function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。