将本地时间转换为日历值。
语法
time_t mktime( // See note in remarks section about linkage
struct tm *timeptr
);
__time32_t _mktime32(
struct tm *timeptr
);
__time64_t _mktime64(
struct tm *timeptr
);
参数
timeptr
有关指向时间结构的指针,请参阅 asctime。
返回值
_mktime32 返回编码为 time_t 类型的值的指定日历时间。 如果 timeptr 引用了 1970 年 1 月 1 日午夜之前的日期,或者如果无法表示日历时间,则 _mktime32 会返回转换为 time_t 类型的 -1。 使用 _mktime32 时,如果 timeptr 引用了协调世界时 (UTC) 2038 年 1 月 18 日 23:59:59 之后的日期,则它将返回转换为 time_t 类型的 –1。
如果 _mktime64 引用了 3000 年 12 月 31 日 23:59:59 (UTC) 之后的日期,则 __time64_t 将返回转换为 timeptr 类型的 -1。
注解
mktime、_mktime32 和 _mktime64 函数将由 timeptr 指向的提供的时间结构(可能不完整)转换为使用规范化值的完全定义的结构,然后将它转换为 time_t 日历时间值。 转换后的时间与由 time 函数返回的值具有相同的编码。 将忽略 tm_wday 结构的 tm_yday 和 timeptr 组件的原始值,并且不会将其他组件的原始值限制为其正常范围。
mktime 是一个等效于 _mktime64 的内联函数,除非定义 _USE_32BIT_TIME_T,否则在这种情况下,它等效于 _mktime32。
调整为 UTC 后,_mktime32 可处理从 1970 年 1 月 1 日午夜到 2038 年 1 月 18 日 23:59:59 (UTC) 之间的日期。
_mktime64 可处理从 1970 年 1 月 1 日午夜到 3000 年 12 月 31 日 23 时 59 分 59 秒之间的日期。 即使你指定的日期在范围内,此调整也可能会导致这些函数返回 -1(转换为 time_t、__time32_t 或 __time64_t)。 例如,如果你位于埃及开罗(比 UTC 早两个小时),则首先将从你在 timeptr 中指定的日期中减去两个小时,现在减完后可能会使你的日期超出范围。
这些函数可用于在 tm 结构中进行验证和填充。 如果成功,这些函数会将 tm_wday 和 tm_yday 的值设置为相应的值,并设置其他组件以表示指定的日历时间,但是同时使其值强制位于正常范围内。 除非确定 tm_mday 和 tm_mon,否则不会设置 tm_year 的最终值。 指定 tm 结构时间时,将 tm_isdst 字段设置为:
零 (0),指示标准时间有效。
大于 0 的值,指示夏令时有效。
小于零的值,使用 C 运行时库代码计算有效的是标准时间还是夏令时。
C 运行时库将从 TZ 环境变量中确定夏令时行为。 如果未设置 TZ,则使用 Win32 API 调用 GetTimeZoneInformation 从操作系统中获取夏令时信息。 如果此调用失败,则该库将假定使用了用于实现夏令时计算的美国规则。
tm_isdst 是必填字段。 如果未设置,则未定义其值,并且这些函数的返回值不可预知。 如果 timeptr 指向由之前对 tm、asctime 或 gmtime 的调用(或这些函数的变量)返回的 localtime 结构,则 tm_isdst 字段将包含正确的值。
gmtime 和 localtime(以及 _gmtime32、_gmtime64、_localtime32 和 _localtime64)函数对每个线程使用一个缓冲区以实现转换。 如果你为 mktime、_mktime32 或 _mktime64 提供此缓冲区,则会损坏之前的内容。
这些函数将验证其参数。 如果 timeptr 是空指针,则将调用无效的参数处理程序,如参数验证中所述。 如果允许执行继续,则这些函数将返回 -1 并将 errno 设置为 EINVAL。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
注释
使用 Windows SDK 版本 10.0.26100.6901 和 Visual Studio 2026 或更高版本时, mktime 不再 static inline (内部链接)。
inline 而是(外部链接)。
若要返回到以前的行为, #define _STATIC_INLINE_UCRT_FUNCTIONS=1 请先包括任何 CRT 标头。 默认情况下,_STATIC_INLINE_UCRT_FUNCTIONS 设置为 0。
此更改增加了与 C++ 标准的 UCRT 一致性,并提高了与C++模块的兼容性。
要求
| 例程 | 必需的标头 |
|---|---|
mktime |
<time.h> |
_mktime32 |
<time.h> |
_mktime64 |
<time.h> |
有关兼容性的详细信息,请参阅 兼容性。
库
C 运行时库的所有版本。
示例
// crt_mktime.c
/* The example takes a number of days
* as input and returns the time, the current
* date, and the specified number of days.
*/
#include <time.h>
#include <stdio.h>
int main( void )
{
struct tm when;
__time64_t now, result;
int days;
char buff[80];
time( &now );
_localtime64_s( &when, &now );
asctime_s( buff, sizeof(buff), &when );
printf( "Current time is %s\n", buff );
days = 20;
when.tm_mday = when.tm_mday + days;
if( (result = mktime( &when )) != (time_t)-1 ) {
asctime_s( buff, sizeof(buff), &when );
printf( "In %d days the time will be %s\n", days, buff );
} else
perror( "mktime failed" );
}
示例输出
Current time is Fri Apr 25 13:34:07 2003
In 20 days the time will be Thu May 15 13:34:07 2003
另请参阅
工时管理
%>
.- .
.- .
.- .
.- .