mktime
|
定义于头文件
<time.h>
|
||
将表示为 struct tm 结构的本地日历时间重新规范化,并同时将其转换为自纪元起算的时间,以 time_t 对象形式表示。 arg - > tm_wday 和 arg - > tm_yday 字段将被忽略。 arg 中的数值不会进行越界检查。
当
arg
-
>
tm_isdst
为负值时,会导致
mktime
尝试判断指定时间是否处于夏令时期间。
若转换到
time_t
成功,则
arg
对象将被修改。
arg
的所有字段都会更新至其有效取值范围内。
arg
-
>
tm_wday
和
arg
-
>
tm_yday
会基于其他字段的可用信息重新计算。
目录 |
参数
| arg | - | 指向 tm 对象的指针,指定要转换的本地日历时间 |
返回值
成功时返回自纪元起的时间作为 time_t 对象,若 arg 无法表示为 time_t 对象则返回 - 1 (此种情况下 POSIX 还要求将 EOVERFLOW 存储至 errno )。
注释
如果
struct
tm
对象是通过 POSIX
strptime
或等效函数获取的,则
tm_isdst
的值是不确定的,在调用
mktime
前需要显式设置该值。
示例
#define _POSIX_C_SOURCE 200112L // for setenv on gcc #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { setenv("TZ", "/usr/share/zoneinfo/America/New_York", 1); // POSIX-specific struct tm tm = *localtime(&(time_t){time(NULL)}); printf("Today is %s", asctime(&tm)); printf("(DST is %s)\n", tm.tm_isdst ? "in effect" : "not in effect"); tm.tm_mon -= 100; // tm_mon is now outside its normal range mktime(&tm); // tm_isdst is not set to -1; today's DST status is used printf("100 months ago was %s", asctime(&tm)); printf("(DST was %s)\n", tm.tm_isdst ? "in effect" : "not in effect"); }
可能的输出:
Today is Fri Apr 22 11:53:36 2016 (DST is in effect) 100 months ago was Sat Dec 22 10:53:36 2007 (DST was not in effect)
参考文献
- C23 标准 (ISO/IEC 9899:2024):
-
- 7.27.2.3 mktime 函数 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018):
-
- 7.27.2.3 mktime 函数 (p: 285-286)
- C11 标准 (ISO/IEC 9899:2011):
-
- 7.27.2.3 mktime 函数 (p: 390-391)
- C99标准(ISO/IEC 9899:1999):
-
- 7.23.2.3 mktime函数(页码:340-341)
- C89/C90 标准 (ISO/IEC 9899:1990):
-
- 4.12.2.3 mktime 函数
参阅
|
(C23)
(C11)
|
将纪元时间转换为本地时间表示的日历时间
(函数) |
|
C++ 文档
关于
mktime
|
|