mktime() – C语言库函数
C库函数 time_t mktime(struct tm *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 ");}else{ strftime(buffer,sizeof(buffer),"%c",&info );print(buffer);}return(0);}
让我们编译和运行上面的程序,这将产生以下结果:
Wed Jul 4 00:00:01 2001