C庫函數(shù) time_t mktime(struct tm *timeptr) 指向的結(jié)構(gòu)轉(zhuǎn)換成一個(gè)time_t值,根據(jù)本地時(shí)區(qū)。
以下是mktime() 函數(shù)的聲明。
time_t mktime(struct tm *timeptr)
timeptr -- 這是一個(gè) time_t 值代表一個(gè)日歷時(shí)間分解成其組成部分的指針。下面是詳細(xì)的timeptr結(jié)構(gòu)
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 */ };
這個(gè)函數(shù)返回日歷時(shí)間作為參數(shù)傳遞一個(gè)time_t值對(duì)應(yīng)的。錯(cuò)誤時(shí),返回-1值。
下面的例子演示了如何使用mktime() 函數(shù)。
#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); }
讓我們編譯和運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:
Wed Jul 4 00:00:01 2001