长治网站建设费用,专业定制网站建设团队,几个有效网址谢谢,广西网站开发软件关于gmtime、gmtime_r、localtime、localtime_r 测试环境#xff1a;vmware 7 Redhat5.5#xff0c;系统时间使用UTC#xff0c;时区为上海。 1、函数功能介绍 使用man gmtime或man localtime都可以的得到这几个函数的介绍。原型如下#xff1a; struct tm *gmtime(const … 关于gmtime、gmtime_r、localtime、localtime_r 测试环境vmware 7 Redhat5.5系统时间使用UTC时区为上海。 1、函数功能介绍 使用man gmtime或man localtime都可以的得到这几个函数的介绍。原型如下 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); man手册中对它们的解释如下 The gmtime() function converts the calendar time timep to broken-down time representation, expressed in Coordinated Universal Time (UTC). It may return NULL when the year does not fit into an integer. The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions. The gmtime_r() function does the same, but stores the data in a user-supplied struct. The localtime() function converts the calendar time timep to broken-time representation, expressed relative to the users specified time zone. The function acts as if it called tzset(3) and sets the external variables tzname with information about the current time zone, timezone with the difference between Coordinated Universal Time (UTC) and local standard time in seconds, and daylight to a non-zero value if daylight savings time rules apply during some part of the year. The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions. The localtime_r() function does the same, but stores the data in a user-supplied struct. It need not set tzname。 翻译如下 gmtime() 函数将日历时间timep转换为用UTC时间表示的时间。它可能返回NULL比如年份不能放到一个整数中。返回值指向一个静态分配的结构该结构可能会被接下来的任何日期和时间函数调用覆盖。gmtime_r()函数功能与此相同但是它可以将数据存储到用户提供的结构体中。 localtime() 函数将日历时间timep转换为用户指定的时区的时间。这个函数的行为好像是它调用了tzset(3) 并且将外部变量tzname设置为当前时区的信息将timezone设为UTC和本地标准时间的差值并且如果在一年的部分时间使用日光节约规则时将daylight设置为非空值。返回值指向一个静态分配的结构该结构可能会被接下来的任何日期和时间函数调用覆盖。localtime_r()函数功能与此相同但是它可以将数据存储到用户提供的结构体中。它不需要设置tzname。 2、功能测试 程序一 #include stdio.h #include time.h int main() { time_t cur_timetime(NULL); if( cur_time 0 ) { perror(time); return -1; } struct tm utc_tm;; if( NULL gmtime_r( cur_time, utc_tm ) ) { perror(gmtime ); return -1; } struct tm local_tm; if( NULL localtime_r( cur_time, local_tm ) ) { perror(localtime ); return -1; } printf(UTC %s, asctime(utc_tm) ); printf(LOC %s, asctime(local_tm) ); printf(LOC %s, ctime(cur_time) ); return 0; } 程序输出 UTC Thu Oct 27 09:16:10 2011 LOC Thu Oct 27 17:16:10 2011 LOC Thu Oct 27 17:16:10 2011 由于系统时间使用了UTC可以看到“本地时间 UTC时间 8”输出正确。 程序二 #include stdio.h #include time.h int main() { time_t cur_timetime(NULL); if( cur_time 0 ) { perror(time); return -1; } struct tm *utc_tm gmtime( cur_time ); if( NULL utc_tm ) { perror(gmtime ); return -1; } printf(UTC %s, asctime(utc_tm) ); struct tm *local_tm localtime( cur_time ); if( NULL local_tm ) { perror(localtime ); return -1; } printf(LOC %s, asctime(local_tm) ); printf(LOC %s, ctime(cur_time) ); return 0; } 程序输出 UTC Thu Oct 27 09:20:45 2011 LOC Thu Oct 27 17:20:45 2011 LOC Thu Oct 27 17:20:45 2011 同样是正确的。 程序三 #include stdio.h #include time.h int main() { time_t cur_timetime(NULL); if( cur_time 0 ) { perror(time); return -1; } struct tm *utc_tm gmtime( cur_time ); if( NULL utc_tm ) { perror(gmtime ); return -1; } struct tm *local_tm localtime( cur_time ); if( NULL local_tm ) { perror(localtime ); return -1; } printf(UTC %s, asctime(utc_tm) ); printf(LOC %s, asctime(local_tm) ); printf(LOC %s, ctime(cur_time) ); return 0; } 程序输出 UTC Thu Oct 27 17:21:59 2011 LOC Thu Oct 27 17:21:59 2011 LOC Thu Oct 27 17:21:59 2011 这程序输出有错UTC时间和本地时间相同了这应该就是由于man文档中描述的“可能会被接下来的任何日期和时间函数调用覆盖”造成的。为验证这个设想使用程序四 程序四 #include stdio.h #include time.h int main() { time_t cur_timetime(NULL); if( cur_time 0 ) { perror(time); return -1; } struct tm *local_tm localtime( cur_time ); if( NULL local_tm ) { perror(localtime ); return -1; } struct tm *utc_tm gmtime( cur_time ); if( NULL utc_tm ) { perror(gmtime ); return -1; } printf(UTC %s, asctime(utc_tm) ); printf(LOC %s, asctime(local_tm) ); printf(LOC %s, ctime(cur_time) ); return 0; } 程序输出 UTC Thu Oct 27 09:24:23 2011 LOC Thu Oct 27 09:24:23 2011 LOC Thu Oct 27 17:24:23 2011 验证了该设想。 3、总结 使用gmtime和localtime后要立即处理结果否则返回的指针指向的内容可能会被覆盖一个好的方法是使用gmtime_r和localtime_r由于使用了用户分配的内存这两个函数是不会出错的。 Linux时间函数 分类 Linux C编程2012-04-28 22:48 22366人阅读 评论(6) 收藏 举报 linuxstructnulltimezonetimer 系统环境ubuntu10.04 简介 本文旨在为了解Linux各种时间类型与时间函数提供技术文档。 1、Linux下常用时间类型 Linux下常用时间类型有四种time_t、struct tm、struct timeval、struct timespec 1.1 time_t时间类型 time_t类型在time.h中定义
#ifndef __TIME_T #define __TIME_T typedef long time_t; #endif 可见time_t实际是一个长整型。其值表示为从UTC(coordinated universal time)时间1970年1月1日00时00分00秒(也称为Linux系统的Epoch时间)到当前时刻的秒数。由于time_t类型长度的限制它所表示的时间不能晚于2038年1月19日03时14分07秒(UTC)。为了能够表示更久远的时间可用64位或更长的整形数来保存日历时间这里不作详述。 使用time()函数获取当前时间的time_t值使用ctime()函数将time_t转为当地时间字符串。 备注UTC时间有时也称为GMT时间其实UTC和GMT两者几乎是同一概念。它们都是指格林尼治标准时间只不过UTC的称呼更为正式一点。两者区别在于前者是天文上的概念而后者是基于一个原子钟。 1.2 struct tm时间类型 tm结构在time.h中定义
#ifndef _TM_DEFINED struct tm{ int tm_sec; /*秒 - 取值区间为[0, 59]*/ int tm_min; /*分 - 取值区间为[0, 59]*/ int tm_hour; /*时 - 取值区间为[0, 23]*/ int tm_mday; /*日 - 取值区间为[1, 31]*/ int tm_mon; /*月份 - 取值区间为[0, 11]*/ int tm_year; /*年份 - 其值为1900年至今年数*/ int tm_wday; /*星期 - 取值区间[0, 6]0代表星期天1代表星期1以此类推*/ int tm_yday; /*从每年的1月1日开始的天数-取值区间为[0, 365]0代表1月1日*/ int tm_isdst; /*夏令时标识符使用夏令时tm_isdst为正不使用夏令时tm_isdst为0不了解情况时tm_isdst为负*/ }; #define _TM_DEFINED #endif ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。 使用gmtime( )和localtime( )可将time_t时间类型转换为tm结构体 使用mktime( )将tm结构体转换为time_t时间类型 使用asctime( )将struct tm转换为字符串形式。 1.3 struct timeval时间类型 timeval结构体在time.h中定义
Struct tmieval{ time_t tv_sec; /*秒s*/ suseconds_t tv_usec; /*微秒us*/ }; 设置时间函数settimeofday( )与获取时间函数gettimeofday( )均使用该事件类型作为传参。 1.4 struct timespec时间类型 timespec结构体在time.h定义 struct timespec{ time_t tv_sec; /*秒s*/ long tv_nsec; /*纳秒ns*/ }; 2、Linux下常用时间函数 Linux下常用时间函数有time( )、ctime( )、gmtime( )、localtime( )、mktime( )、asctime( )、difftime( )、gettimeofday( )、settimeofday( ) 2.1 time( )函数 头文件#include time.h 函数定义time_t time(time_t *timer) 功能描述该函数返回从1970年1月1日00时00分00秒至今所经过的秒数。如果time_t *timer非空指针函数也会将返回值存到timer指针指向的内存。 返回值成功则返回秒数失败则返回((time_t)-1)值错误原因存于errno中。 例 time_t seconds; seconds time((time_t *)NULL); 2.2 ctime( )函数 头文件#include time.h 函数定义char *ctime(const time_t *timep); 功能描述ctime( )将参数timep指向的time_t时间信息转换成实际所使用的时间日期表示方法并以字符串形式返回。字符串格式为Wed Jun 20 21:00:00 2012\n。 例
time_t timep; tmep time(NULL); printf(%s\n, ctime(timep)); 2.3 gmtime( )函数 头文件#include time.h 函数定义struct tm *gmtime(const time_t *timep) 功能描述gmtime( )将参数timep指向的time_t时间信息转换成以tm结构体表示的GMT时间信息并以struct tm*指针返回。 GMTGMT是中央时区,北京在东8区,相差8个小时所以北京时间GMT时间8小时。 例
int main(void) { char *wday[] {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; time_t timep; struct tm *p_tm; timep time(NULL); p_tm gmtime(timep); /*获取GMT时间*/ printf(%d-%d-%d , (p_tm-tm_year1900), (p_tm-mon1), p_tm-tm_mday); printf(%s %d:%d:%d\n, wday[p_tm-tm_wday], p_tm-tm_hour, p_tm-tm_min, p_tm-tm_sec); } 2.4 localtime( )函数 头文件#include time.h 函数定义struct tm *localtime(const time_t *timep); 功能描述localtime( )将参数timep指向的time_t时间信息转换成以tm结构体表示的本地时区时间(如北京时间 GMT小时)。 例
int main(void) { char *wday[] {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; time_t timep; struct tm *p_tm; timep time(NULL); p_tm localtime(timep); /*获取本地时区时间*/ printf(%d-%d-%d , (p_tm-tm_year1900), (p_tm-mon1), p_tm-tm_mday); printf(%s %d:%d:%d\n, wday[p_tm-tm_wday], p_tm-tm_hour, p_tm-tm_min, p_tm-tm_sec); return 0; } 2.5 mktime( )函数 头文件#include time.h 函数定义time_t mktime(struct tm *p_tm); 功能描述mktime( )将参数p_tm指向的tm结构体数据转换成从1970年1月1日00时00分00秒至今的GMT时间经过的秒数。 例
int main(void) { time_t timep: struct tm *p_tm; timep time(NULL); pintf(time( ):%d\n, timep); p_tm local(timep); timep mktime(p_tm); printf(time( )-localtime( )-mktime( ):%d\n, timep); return 0; } 2.6 asctime( )函数 头文件#include time.h 函数定义char *asctime(const struct tm *p_tm); 功能描述asctime( )将参数p_tm指向的tm结构体数据转换成实际使用的时间日期表示方法并以字符串形式返回(与ctime函数相同)。字符串格式为Wed Jun 20 21:00:00 2012\n。 例
int main(void) { time_t timep; timep time(NULL); printf(%s\n, asctime(gmtime(timep))); return 0; } 2.7 difftime( )函数 头文件#include time.h 函数定义double difftime(time_t timep1, time_t timep2); 功能描述difftime( )比较参数timep1和timep2时间是否相同并返回之间相差秒数。 例
int main(void) { time_t timep1, timep2; timep1 time(NULL); sleep(2); timep2 time(NULL); printf(the difference is %f seconds\n, difftime(timep1, timep2)); return 0; } 2.8 gettimeofday( )函数 头文件#include sys/time.h #include unistd.h 函数定义int gettimeofday(struct timeval *tv, struct timezone *tz); 功能描述gettimeofday( )把目前的时间信息存入tv指向的结构体当地时区信息则放到tz指向的结构体。 struct timezone原型
struct timezone{ int tz_minuteswest; /*miniutes west of Greenwich*/ int tz_dsttime; /*type of DST correction*/ }; 例
struct timeval tv; struct timeval tz; gettimeofday(tv, tz); 附 使用time函数族获取时间并输出指定格式字符串例子strftime( )函数
int main(void) { char strtime[20] {0}; time_t timep; struct tm *p_tm; timep time(NULL); p_tm localtime(timep); strftime(strtime, sizeof(strtime), %Y-%m-%d %H:%M:%S, p_tm); return 0; } 2.9 settimeofday( )函数 头文件#include sys/time.h #include unistd.h 函数定义int settimeofday(const struct timeval *tv, const struct timezone *gz); 功能描述settimeofday( )把当前时间设成由tv指向的结构体数据。当前地区信息则设成tz指向的结构体数据。 例 int main(void) { char t_string[] 2012-04-28 22:30:00; struct tm time_tm; struct timeval time_tv; time_t timep; int ret 0; sscanf(t_string, %d-%d-%d %d:%d:%d, time_tm.tm_year, time_tm.tm_mon, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); time_tm.tm_year - 1900; time_tm.tm_mon - 1; time_tm.tm_wday 0; time_tm.tm_yday 0; time_tm.tm_isdst 0; timep mktime(time_tm); time_tv.tv_sec timep; time_tv.tv_usec 0; ret settimeofday(time_tv, NULL); if(ret ! 0) { fprintf(stderr, settimeofday failed\n); return -1; } return 0; }