C++ ctime difftime() 函式
示例
測量兩個時間戳之間的時間差
struct tm date;
time_t now;
time_t before;
// Create a timestamp for right now
time(&now);
// Create a timestamp for 5 hours ago
date = *localtime(&now);
date.tm_hour -= 5;
before = mktime(&date);
// Calculate the difference between the two timestamps in seconds
cout << difftime(now, before);
自己動手試一試 »
定義和用法
difftime()
函式返回兩個時間戳之間的秒數。
如果開始時間戳大於結束時間戳,則結果為負數。
difftime()
函式定義在 <ctime>
標頭檔案中。
語法
difftime(time_t end, time_t start);
資料型別 time_t
表示一個數字。
引數值
引數 | 描述 |
---|---|
end | 必需。要測量的時間間隔結束的時間戳。 |
start | 必需。要測量的時間間隔開始的時間戳。 |
技術詳情
返回 | 一個 double 值,表示兩個時間戳之間的秒數。 |
---|