difftime
From cppreference.com
| ヘッダー <time.h> で定義 |
||
2つのカレンダー時刻(time_t オブジェクト)の差(time_end - time_beg)を秒単位で計算します。time_end が time_beg より前の時点を参照する場合、結果は負になります。
目次 |
[編集] パラメータ
| time_beg, time_end | - | 比較する時刻 |
[編集] 戻り値
2つの時刻の差(秒単位)。
[編集] 注記
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)
|