C庫函數(shù) struct tm *gmtime(const time_t *timer) 使用定時(shí)器所指向的數(shù)值,以填補(bǔ)atm結(jié)構(gòu)的值,表示對(duì)應(yīng)的時(shí)間,以協(xié)調(diào)通用時(shí)間(UTC)或GMT時(shí)區(qū)表示。
以下是gmtime() 函數(shù)的聲明。
struct tm *gmtime(const time_t *timer)
timeptr -- 這是一個(gè)time_t值表示日歷時(shí)間的指針。
這個(gè)函數(shù)返回指向tm結(jié)構(gòu)和下面的時(shí)間信息填寫。timeptr結(jié)構(gòu)的細(xì)節(jié):
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 */ };
下面的例子顯示gmtime() 函數(shù)的用法。
#include <stdio.h> #include <time.h> #define BST (+1) #define CCT (+8) int main () { time_t rawtime; struct tm *info; time(&rawtime); /* Get GMT time */ info = gmtime(&rawtime ); printf("Current world clock: "); printf("London : %2d:%02d ", (info->tm_hour+BST)%24, info->tm_min); printf("China : %2d:%02d ", (info->tm_hour+CCT)%24, info->tm_min); return(0); }
讓我們編譯和運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:
Current world clock: London : 14:10 China : 21:10