mktime、_mktime32、_mktime64mktime, _mktime32, _mktime64

将本地时间转换为日历值。Convert the local time to a calendar value.


time_t mktime(
   struct tm *timeptr
__time32_t _mktime32(
   struct tm *timeptr
__time64_t _mktime64(
   struct tm *timeptr


有关指向时间结构的指针,请参阅 asctimePointer to time structure; see asctime.

返回值Return Value

_mktime32返回编码为类型的值的指定的日历时间time_t_mktime32 returns the specified calendar time encoded as a value of type time_t. 如果timeptr引用了 1970 年 1 月 1 日午夜前的日期或者如果无法表示日历时间, _mktime32返回-1 强制转换为类型time_tIf timeptr references a date before midnight, January 1, 1970, or if the calendar time cannot be represented, _mktime32 returns -1 cast to type time_t. 使用时 _mktime32 ; 如果timeptr 23:59:59 2038 年 1 月 18 日协调世界时 (UTC) 之后的日期引用它将返回-1 强制转换为类型time_tWhen using _mktime32 and if timeptr references a date after 23:59:59 January 18, 2038, Coordinated Universal Time (UTC), it will return -1 cast to type time_t.

_mktime64将返回-1 强制转换为类型 __time64_t如果timeptr引用 23:59:59,3000 年 12 月 31 日,UTC 之后的日期。_mktime64 will return -1 cast to type __time64_t if timeptr references a date after 23:59:59, December 31, 3000, UTC.


Mktime_mktime32_mktime64函数将转换的提供的时间结构 (可能不完整) 指向timeptr到完全定义的结构与标准化值,然后将它转换time_t日历时间值。The mktime, _mktime32 and _mktime64 functions convert the supplied time structure (possibly incomplete) pointed to by timeptr into a fully defined structure with normalized values and then converts it to a time_t calendar time value. 转换后的时间与由 time 函数返回的值具有相同的编码。The converted time has the same encoding as the values returned by the time function. 原始值tm_wdaytm_yday组成部分timeptr结构将被忽略,并且其他组件的原始值不会限制为其正常范围。The original values of the tm_wday and tm_yday components of the timeptr structure are ignored, and the original values of the other components are not restricted to their normal ranges.

mktime是内联函数,它等效于 _mktime64,除非 _USE_32BIT_TIME_T定义,在这种情况下,它相当于 _mktime32.mktime is an inline function that is equivalent to _mktime64, unless _USE_32BIT_TIME_T is defined, in which case it is equivalent to _mktime32.

调整为 UTC 后, _mktime32句柄日期从 1970 年 1 月 1 日午夜到 23:59:59 2038 年 1 月 18 日,UTC。After an adjustment to UTC, _mktime32 handles dates from midnight, January 1, 1970, to 23:59:59 January 18, 2038, UTC. _mktime64句柄日期从午夜,自 1970 年 1 月 1 日 23:59:59 到 3000 年 12 月 31 日。_mktime64 handles dates from midnight, January 1, 1970 to 23:59:59, December 31, 3000. 这种调整可能会导致这些函数返回-1 (强制转换为time_t__time32_t__time64_t) 即使您指定的日期范围内。This adjustment may cause these functions to return -1 (cast to time_t, __time32_t or __time64_t) even though the date you specify is within range. 例如,如果你位于埃及开罗(比 UTC 早两个小时),则首先将从你在 timeptr 中指定的日期中减去两个小时;此时这可能会使你的日期超出范围。For example, if you are in Cairo, Egypt, which is two hours ahead of UTC, two hours will first be subtracted from the date you specify in timeptr; this may now put your date out of range.

这些函数可用于在 tm 结构中进行验证和填充。These functions may be used to validate and fill in a tm structure. 如果成功,这些函数将设置的值tm_wdaytm_yday根据和设置其他组件以表示指定的日历时间,但其值强制到正常范围。If successful, these functions set the values of tm_wday and tm_yday as appropriate and set the other components to represent the specified calendar time, but with their values forced to the normal ranges. 最终值tm_mday之前未设置tm_montm_year确定。The final value of tm_mday is not set until tm_mon and tm_year are determined. 指定时tm结构时,请设置tm_isdst字段:When specifying a tm structure time, set the tm_isdst field to:

  • 零 (0),指示标准时间有效。Zero (0) to indicate that standard time is in effect.

  • 大于 0 的值,指示夏令时有效。A value greater than 0 to indicate that daylight saving time is in effect.

  • 小于零的值,使用 C 运行时库代码计算有效的是标准时间还是夏令时。A value less than zero to have the C run-time library code compute whether standard time or daylight saving time is in effect.

C 运行时库将从 TZ 环境变量中确定夏令时行为。The C run-time library will determine the daylight savings time behavior from the TZ environment variable. 如果TZ未设置,Win32 API 调用GetTimeZoneInformation用于从操作系统中获取夏令时信息。If TZ is not set, the Win32 API call GetTimeZoneInformation is used to get the daylight savings time information from the operating system. 如果此操作失败,则该库将假设使用用于实现夏令时计算的美国规则。If this fails, the library assumes the United States' rules for implementing the calculation of daylight saving time are used. tm_isdst是必填的字段。tm_isdst is a required field. 如果未设置,则未定义其值,并且这些函数的返回值不可预知。If not set, its value is undefined and the return value from these functions is unpredictable. 如果timeptr指向tm返回通过以前调用结构asctimegmtime,或localtime(或这些函数的变量), tm_isdst字段包含正确的值。If timeptr points to a tm structure returned by a previous call to asctime, gmtime, or localtime (or variants of these functions), the tm_isdst field contains the correct value.

请注意, gmtimelocaltime (并 _gmtime32_gmtime64_localtime32,和 _localtime64) 使用每个线程一个缓冲区用于转换。Note that gmtime and localtime (and _gmtime32, _gmtime64, _localtime32, and _localtime64) use a single buffer per thread for the conversion. 如果提供此缓冲区mktime_mktime32_mktime64,以前的内容会被销毁。If you supply this buffer to mktime, _mktime32 or _mktime64, the previous contents are destroyed.

这些函数将验证其参数。These functions validate their parameter. 如果 timeptr 是 null 指针,则调用无效参数处理程序,如参数验证中所述。If timeptr is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,函数将返回-1 并设置errnoEINVALIf execution is allowed to continue, the functions return -1 and set errno to EINVAL.


例程所返回的值Routine 必需的标头Required header
mktimemktime <time.h><time.h>
_mktime32_mktime32 <time.h><time.h>
_mktime64_mktime64 <time.h><time.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


C 运行时库的所有版本。All versions of the C run-time libraries.


// 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" );

示例输出Sample Output

Current time is Fri Apr 25 13:34:07 2003

In 20 days the time will be Thu May 15 13:34:07 2003

请参阅See also

时间管理Time Management
asctime、_wasctimeasctime, _wasctime
gmtime、_gmtime32、_gmtime64gmtime, _gmtime32, _gmtime64
localtime、_localtime32、_localtime64localtime, _localtime32, _localtime64
_mkgmtime、_mkgmtime32、_mkgmtime64_mkgmtime, _mkgmtime32, _mkgmtime64
time、_time32、_time64time, _time32, _time64