time(), time_t, localtime(), localtime_r()的理解和用法

time_t的定义:

[objc]  view plain  copy
  1. typedef __darwin_time_t time_t;   
[objc]  view plain  copy
  1. typedef long    __darwin_time_t;    /* time() */  
从定义可以看出,time_t实际上的数据类型是long。


time()

[objc]  view plain  copy
  1. <pre name="code" class="objc">time_t time ( time_ttime_t * ptr_time );  
 
参数和返回值都是time_t object。 

如果参数为空,则返回当前时间距1970年1月1日00:00点 UTC的秒数;

[html]  view plain  copy
  1. <pre name="code" class="objc">    long secondsSince1970 = time(NULL);  
  2.     printf("It has been %ld seconds since 1970\n", secondsSince1970);  
 

[html]  view plain  copy
  1. It has been 1413202571 seconds since 1970  

如果参数不为空,此时返回值和参数都为当前时间距1970年1月1日00:00点 UTC的秒数。

[html]  view plain  copy
  1. time_t t;  
  2. time_t sec = 1000000000;  
  3. t = time(&sec);  
  4. printf("sec = %ld, t = %ld\n", sec, t);  
 

sec = 1413202571, t = 1413202571

localtime()

[html]  view plain  copy
  1. struct tm * localtime ( const time_t * ptr_time );  
 
tm是一个包含了年月日时分秒的结构,定义如下: 

[html]  view plain  copy
  1. struct tm {  
  2.     int tm_sec; //seconds after the minute[0-60]  
  3.     int tm_min; //minutes after the hour[0-59]  
  4.     int tm_hour; //hours since midnight[0-23]  
  5.     int tm_mday; //day of the month[0-31]  
  6.     int tm_mon; //months since January[0-11]  
  7.     int tm_year; //years since 1900  
  8.     int tm_wday; //days since Sunday[0-6]  
  9.     int tm_yday; //days since January  
  10.     int tm_isdst; //Daylight Savings Time flag  
  11.     int tm_gmtoff; //offset from CUT in seconds  
  12.     char *tm_zone; //timezone abbreviation  
  13. };  

 
一般搭配time()函数使用,可以得到当前时间的年月日时分秒等。该函数所得时间为当前时区。 

 

[html]  view plain  copy
  1. long secondsSince1970 = time(NULL);  
  2. printf("It has been %ld seconds since 1970\n", secondsSince1970);  
  3.   
  4. struct tm now;  
  5. localtime_r(&secondsSince1970, &now);  
  6. printf("Current time is %d-%d-%d %d:%d:%d\n", now.tm_year + 1900, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);  
 

It has been 1413203631 seconds since 1970

Current time is 2014-10-13 20:33:51

localtime_r()

[html]  view plain  copy
  1. struct tm *localtime_r(const time_t *restrict timer,  
  2.        struct tm *restrict result);  
 
由于localtime()是不重入的函数,因此是thread not safe的。如果多次调用这个函数,下一次返回的参数会把上一次返回的参数覆盖掉。。。就是说 
[html]  view plain  copy
  1. struct tm* ptm = localtime(&tNow);    
  2. struct tm* ptmEnd = localtime(&tEnd);   
 
这里的ptm == ptmEnd 

还有同样碉堡功能的时间函数还有asctime()ctime()gmtime()
所以有了localtime_r()的存在,localtime()的可重入版本。当要多次使用的时候,最好用localtime_r()


参考文献:

http://www.codingunit.com/c-reference-time-h-structure-tm

http://pubs.opengroup.org/onlinepubs/009695399/functions/localtime.html

http://blog.csdn.net/markman101/article/details/7861415

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值