mktime、_mktime32、_mktime64mktime, _mktime32, _mktime64

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

语法Syntax

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

参数Parameters

timeptrtimeptr
有关指向时间结构的指针,请参阅 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.

备注Remarks

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. 将忽略 timeptr 结构的 tm_wdaytm_yday 组件的原始值,并且不会将其他组件的原始值限制为其正常范围。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,否则在这种情况下,它等效于 _mktime32mktime 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 日午夜到 2038 年 1 月 18 日 23:59:59 (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 日午夜到 3000 年 12 月 31 日 23 时 59 分 59 秒之间的日期。_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_mdaytm_mon,否则不会设置 tm_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 指向由之前对 asctimegmtimelocaltime 的调用(或这些函数的变量)返回的 tm 结构,则 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 并将 errno 设置为 EINVALIf execution is allowed to continue, the functions return -1 and set errno to EINVAL.

要求Requirements

例程Routine 必需的标头Required header
mktime <time.h><time.h>
_mktime32 <time.h><time.h>
_mktime64 <time.h><time.h>

有关其他兼容性信息,请参见“简介”中的 兼容性For additional compatibility information, see Compatibility in the Introduction.

Libraries

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

示例Example

// 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、_wasctime asctime, _wasctime
gmtime、_gmtime32、_gmtime64 gmtime, _gmtime32, _gmtime64
localtime、_localtime32、_localtime64 localtime, _localtime32, _localtime64
_mkgmtime、_mkgmtime32、_mkgmtime64 _mkgmtime, _mkgmtime32, _mkgmtime64
time、_time32、_time64time, _time32, _time64