difftime
來自 cppreference.com
定義於標頭檔案 <time.h> |
||
計算兩個日曆時間(time_t 物件)之間的差值,以秒為單位(time_end - time_beg)。如果 time_end
表示的時間點在 time_beg
之前,則結果為負數。
目錄 |
[編輯] 引數
time_beg, time_end | - | 要比較的時間 |
[編輯] 返回值
兩個時間之間的差值,以秒為單位。
[編輯] 注意
在 POSIX 系統上,time_t 以秒為單位進行度量,difftime
等同於算術減法,但 C 和 C++ 允許 time_t 使用分數單位。
[編輯] 示例
以下程式計算自本月開始以來已經過去的秒數。
執行此程式碼
#include <stdio.h> #include <time.h> int main(void) { time_t now = time(0); struct tm beg = *localtime(&now); // set beg to the beginning of the month beg.tm_hour = 0, beg.tm_min = 0, beg.tm_sec = 0, beg.tm_mday = 1; double seconds = difftime(now, mktime(&beg)); printf("%.f seconds have passed since the beginning of the month.\n", seconds); return 0; }
輸出
1937968 seconds have passed since the beginning of the month.
[編輯] 參考
- C17 標準 (ISO/IEC 9899:2018)
- 7.27.2.2 difftime 函式 (p: 285)
- C11 標準 (ISO/IEC 9899:2011)
- 7.27.2.2 difftime 函式 (p: 390)
- C99 標準 (ISO/IEC 9899:1999)
- 7.23.2.2 difftime 函式 (p: 338)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 7.12.2.2 difftime 函式 (p: 171)
[編輯] 另請參閱
C++ 文件 用於 difftime
|