#include <stdio.h> #include <time.h> #include <sys/time.h> /* time_t time(time_t t) 返回UTC時(shí)間 */ time_t GetTimerSecond(void) { struct timeval tv; struct timezone tz; gettimeofday( &tv, &tz); return tv.tv_sec - tz.tz_minuteswest * 60; /// 本地時(shí)間:秒單位 //return tv.tv_sec; /// UTC時(shí)間:秒單位 return tv.tv_sec * 1000 - tz.tz_minuteswest * 60 * 1000 + tv.tv_usec / 1000; } ///struct tm *localtime(const time_t *clock); ///localtime_r(&UTCTime, &tm_time); //這里有時(shí)區(qū)轉(zhuǎn)換, 從UTC時(shí)間轉(zhuǎn)為本地時(shí)間 /* struct tm st { st.tm_year = sysTime.year - 1900; st.tm_mon = sysTime.month- 1; st.tm_mday = sysTime.day; st.tm_hour = sysTime.hour; st.tm_min = sysTime.minute; st.tm_sec = sysTime.second; st.tm_isdst = 0; } */ /// ***localtime和localtime_r有時(shí)區(qū)轉(zhuǎn)換,,UTC加時(shí)區(qū) void LocalTime(time_t *T,struct tm *s_tm1) { struct tm s_tm; tzset(); ///s_tm = localtime(T); /// 將UTC時(shí)間,轉(zhuǎn)換成本地時(shí)間 localtime_r(T,&s_tm); /// 功能與localtime一樣,,但是可重入,,前者不可。 #if 1 fprintf(stdout,"TTT-localtime-[%d]-[%d]-[%d]:[%d]:[%d]:[%d]\n", s_tm.tm_year+1900, s_tm.tm_mon+1, s_tm.tm_mday, s_tm.tm_hour, s_tm.tm_min, s_tm.tm_sec); #endif s_tm1->tm_year = s_tm.tm_year; s_tm1->tm_mon = s_tm.tm_mon; s_tm1->tm_mday = s_tm.tm_mday; s_tm1->tm_hour = s_tm.tm_hour; s_tm1->tm_min = s_tm.tm_min; s_tm1->tm_sec = s_tm.tm_sec; } /// mktime***有時(shí)區(qū)轉(zhuǎn)換,,本地時(shí)間減時(shí)區(qū) ///time_t mktime(struct tm * timeptr); void mkTime(struct tm *s_tm) { /// 將系統(tǒng)時(shí)間的結(jié)構(gòu)體,,轉(zhuǎn)成UTC秒數(shù) printf("TTT--mktime[%ld]\n",(long int)mktime(s_tm)); } /// gmtime***無時(shí)區(qū)轉(zhuǎn)換 //2016-04-18測試無時(shí)區(qū)轉(zhuǎn)換 ///struct tm *gmtime(const time_t *time); void gmTime(time_t *T) { struct tm *s_tm; s_tm = gmtime(T); /// 將UTC時(shí)間的秒數(shù),轉(zhuǎn)換成照樣是UTC時(shí)間的年月日結(jié)構(gòu)體 fprintf(stdout,"TTT-gmtime-[%d]-[%d]-[%d]:[%d]:[%d]:[%d]\n", s_tm->tm_year+1900, s_tm->tm_mon+1, s_tm->tm_mday, s_tm->tm_hour, s_tm->tm_min, s_tm->tm_sec); } /// ctime***有時(shí)區(qū)轉(zhuǎn)換,,UTC加時(shí)區(qū) ///char *ctime(const time_t *time); void cTime(time_t *time) { /// 將UTC時(shí)間的秒數(shù),,轉(zhuǎn)換成本地時(shí)間的年月日字符串 printf("TTT-ctime-[%s]\n",ctime(time)); } /// asctime***無時(shí)區(qū)轉(zhuǎn)換 ///asctime ///把timeptr指向的tm結(jié)構(gòu)體中儲存的時(shí)間轉(zhuǎn)換為字符串字符串格式返回 void ascTime(struct tm *s_tm) { ///將本地時(shí)間的結(jié)構(gòu)體,,轉(zhuǎn)換成本地時(shí)間的年月日字符串 printf("TTT--asctime--[%s]\n",asctime(s_tm)); } *trftime 函數(shù)功能:將時(shí)間格式化,或者說:格式化一個時(shí)間字符串,。 頭文件:time.h 函數(shù)原型:我們可以使用strftime()函數(shù)將時(shí)間格式化為我們想要的格式,。它的原型如下: */ ///size_tstrftime(char*strDest,size_tmaxsize,constchar*format,conststructtm*timeptr); /* echo(strftime("%b %d %Y %X", mktime(20,0,0,12,31,98))); echo(gmstrftime("%b %d %Y %X", mktime(20,0,0,12,31,98))); //輸出當(dāng)前日期、時(shí)間和時(shí)區(qū) echo(gmstrftime("It is %a on %b %d, %Y, %X time zone: %Z",time())); ?>輸出: Dec 31 1998 20:00:00 Dec 31 1998 19:00:00 It is Wed on Jan 25, 2006, 11:32:10 time zone: W. Europe Standard Time */ void trfTime() { } int main() { time_t Time; struct tm s_tm; fprintf(stdout,"TTT--NOW time[%ld]--getTimeSecond[%ld]\n",(long int)time(NULL),(long int)GetTimerSecond()); fprintf(stdout,"TTT--timeZone8[%ld]\n",(long int)GetTimerSecond() - (long int)time(NULL)); Time = time(NULL); LocalTime(&Time,&s_tm); mkTime(&s_tm); Time = time(NULL); gmTime(&Time); cTime(&Time); ascTime(&s_tm); return 0; }
|
|