C语言 <time.h> mktime 函数
-
描述
C库函数time_t mktime(struct tm *timeptr)转换结构指向timeptr根据当地时区到time_t的值。 -
声明
以下是mktime函数的声明。time_t mktime(struct tm *timeptr)
参数- timeptr-这是指向代表日历时间的time_t值的指针,细分为其各个组成部分。下面是timeptr结构的详细信息
struct tm { int tm_sec; /* seconds, range 0 to 59 */ int tm_min; /* minutes, range 0 to 59 */ int tm_hour; /* hours, range 0 to 23 */ int tm_mday; /* day of the month, range 1 to 31 */ int tm_mon; /* month, range 0 to 11 */ int tm_year; /* The number of years since 1900 */ int tm_wday; /* day of the week, range 0 to 6 */ int tm_yday; /* day in the year, range 0 to 365 */ int tm_isdst; /* daylight saving time */ };
-
返回值
此函数返回与作为参数传递的日历时间相对应的time_t值。错误时,返回-1值。示例以下示例显示mktime函数的用法-
尝试一下#include <stdio.h> #include <time.h> int main () { int ret; struct tm info; char buffer[80]; info.tm_year = 2001 - 1900; info.tm_mon = 7 - 1; info.tm_mday = 4; info.tm_hour = 0; info.tm_min = 0; info.tm_sec = 1; info.tm_isdst = -1; ret = mktime(&info); if( ret == -1 ) { printf("Error: unable to make time using mktime\n"); } else { strftime(buffer, sizeof(buffer), "%c", &info ); printf(buffer); } return(0); }
让我们编译并运行上面的程序,它将产生以下结果。Wed Jul 4 00:00:01 2001