difftime
From cppreference.net
|
定义于头文件
<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函数(页码:338)
- C89/C90 标准 (ISO/IEC 9899:1990):
-
- 7.12.2.2 difftime 函数 (页码: 171)
参见
|
C++ 文档
关于
difftime
|