#ifndef _TIME_UTC_ZONE_H_ #define _TIME_UTC_ZONE_H_ #include <stdio.h> #include <time.h> #include <sys/time.h> unsigned long GetTimeOfDay_second(int utc_flag); unsigned long GetTimeOfDay_ms(int utc_flag); unsigned long GetTimeOfDay_us(int utc_flag); #endif ==================================================================== #include "time_utc_zone.h" // 1、gettimeofday //================================================================================ /* #include <sys/time.h> struct timeval { time_t tv_sec; // seconds suseconds_t tv_usec; // microseconds }; struct timezone { int tz_minuteswest; // minutes west of Greenwich int tz_dsttime; // type of DST correction }; //gives the number of seconds and microseconds since the Epoch(utc) int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); */ unsigned long GetTimeOfDay_second(int utc_flag) { struct timeval tv; struct timezone tz; gettimeofday( &tv, NULL); // 返回utc时间 if(1 == utc_flag) { return tv.tv_sec + tv.tv_usec/1000000; } // 返回本地时间 else { return tv.tv_sec + tv.tv_usec/1000000 - tz.tz_minuteswest * 60; /// 本地时间:秒单位 } } unsigned long GetTimeOfDay_ms(int utc_flag) { struct timeval tv; struct timezone tz; gettimeofday( &tv, NULL); // 返回utc时间 if(1 == utc_flag) { return tv.tv_sec*1000 + tv.tv_usec/1000; } // 返回本地时间 else { return tv.tv_sec*1000 + tv.tv_usec/1000 - tz.tz_minuteswest*60*1000; } } unsigned long GetTimeOfDay_us(int utc_flag) { struct timeval tv; struct timezone tz; gettimeofday( &tv, NULL); // 返回utc时间 if(1 == utc_flag) { return tv.tv_sec*1000000 + tv.tv_usec; } // 返回本地时间 else { return tv.tv_sec*1000000 + tv.tv_usec - tz.tz_minuteswest*60*1000000; } } //================================================================================ //================================================================================ // 2、time_t time(time_t *t); /* 返回utc时间,单位秒 returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). */ //================================================================================ //================================================================================ // 3、localtime /* 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; } */ // struct tm *localtime(const time_t *timep); // timep为UTC时间,加时区,转换为本地时间的struct tm结构体 // struct tm *localtime_r(const time_t *timep, struct tm *result); // 这里有时区转换, 加时区,从UTC时间转为本地时间 void LocalTime(time_t *T,struct tm *s_tm1) { struct tm s_tm; tzset(); ///s_tm = localtime(T); /// 将UTC时间,转换成本地时间 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; } //================================================================================ //================================================================================ // 4、mktime /// mktime***有时区转换,减时区,将本地时间转换成utc时间 ///time_t mktime(struct tm * timeptr); void mkTime(struct tm *s_tm) { /// 将系统时间的结构体,转成UTC秒数 printf("TTT--mktime[%ld]\n",(long int)mktime(s_tm)); } //================================================================================ // 5、gmtime***无时区转换 //2016-04-18测试无时区转换,直接将时间秒数转换成结构体 ///struct tm *gmtime(const time_t *time); void gmTime(time_t *T) { struct tm *s_tm; s_tm = gmtime(T); /// 将UTC时间的秒数,转换成照样是UTC时间的年月日结构体 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); } //================================================================================ // 6、ctime***有时区转换,加时区,将utc时间转换成本地时间 ///char *ctime(const time_t *time); void cTime(time_t *time) { /// 将UTC时间的秒数,转换成本地时间的年月日字符串 printf("TTT-ctime-[%s]\n",ctime(time)); } //================================================================================ // 7、asctime***有时区转换,加时区,将utc时间转换成本地时间 // char *asctime(const struct tm *tm); // char *asctime_r(const struct tm *tm, char *buf); ///把timeptr指向的tm结构体中储存的时间转换为字符串字符串格式返回 void ascTime(struct tm *s_tm) { ///将本地时间的结构体,转换成本地时间的年月日字符串 printf("TTT--asctime--[%s]\n",asctime(s_tm)); } //================================================================================ /* char *asctime(const struct tm *tm); char *asctime_r(const struct tm *tm, char *buf); char *ctime(const time_t *timep); char *ctime_r(const time_t *timep, char *buf); struct tm *gmtime(const time_t *timep); struct tm *gmtime_r(const time_t *timep, struct tm *result); struct tm *localtime(const time_t *timep); struct tm *localtime_r(const time_t *timep, struct tm *result); time_t mktime(struct tm *tm); */ //================================================================================ /* 8、size_t strftime(char *s, size_t max, const char *format, const struct tm *tm); 函数功能:将时间格式化,或者说:格式化一个时间字符串。 头文件:time.h 函数原型:我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下: */ ///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))); //输出当前日期、时间和时区 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() { } #if 0 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; } #endif |
|