time_t tm timeval 和 时间字符串的转换

转自:http://blog.csdn.net/ncepubdtb/article/details/38899505,做了一点补充。

  1. 1、常用的时间存储方式  
  2.   
  3. 1)time_t类型,这本质上是一个长整数,表示从1970-01-01 00:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。  
  4.   
  5. 2)tm结构,这本质上是一个结构体,里面包含了各时间字段  
  6.   
  7. struct tm {  
  8.         int tm_sec;     /* seconds after the minute - [0,59] */  
  9.         int tm_min;     /* minutes after the hour - [0,59] */  
  10.         int tm_hour;    /* hours since midnight - [0,23] */  
  11.         int tm_mday;    /* day of the month - [1,31] */  
  12.         int tm_mon;     /* months since January - [0,11] */  
  13.         int tm_year;    /* years since 1900 */  
  14.         int tm_wday;    /* days since Sunday - [0,6] */  
  15.         int tm_yday;    /* days since January 1 - [0,365] */  
  16.         int tm_isdst;   /* daylight savings time flag */  
  17.         };  
  18.   
  19. 其中tm_year表示从1900年到目前计时时间间隔多少年,如果是手动设置值的话,tm_isdst通常取值-1。  

  20. 3)struct timeval结构体在time.h中的定义为
  21. struct timeval {
              time_t       tv_sec;     /* seconds */
              suseconds_t   tv_usec; /* microseconds */
     };

  22. 2、常用的时间函数  
  23.   
  24. time_t time(time_t *t); //取得从1970年1月1日至今的秒数  
  25. char *asctime(const struct tm *tm); //将结构中的信息转换为真实世界的时间,以字符串的形式显示  
  26. char *ctime(const time_t *timep); //将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样  
  27. struct tm *gmtime(const time_t *timep); //将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针   
  28. struct tm *localtime(const time_t *timep); //和gmtime类似,但是它是经过时区转换的时间。  
  29. time_t mktime(struct tm *tm); //将struct tm 结构的时间转换为从1970年至今的秒数  
  30. int gettimeofday(struct timeval *tv, struct timezone *tz); //返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用  
  31. double difftime(time_t time1, time_t time2); //返回两个时间相差的秒数  
  32.   
  33.   
  34. 3、时间与字符串的转换  
  35.   
  36. 需要包含的头文件如下  
  37.   
  38. #include <iostream>  
  39. #include <time.h>  
  40. #include <stdlib.h>  
  41. #include <string.h>  
  42.   
  43. 1)unix/windows下时间转字符串参考代码  
  44.   
  45. time_t t;  //秒时间  
  46. tm* local; //本地时间   
  47. tm* gmt;   //格林威治时间  
  48. char buf[128]= {0};  
  49.   
  50. t = time(NULL); //或者time(&t);//获取目前秒时间  
  51. local = localtime(&t); //转为本地时间  
  52. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local);  
  53. std::cout << buf << std::endl;  
  54.   
  55. gmt = gmtime(&t);//转为格林威治时间  
  56. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt);  
  57. std::cout << buf << std::endl;  
  58.   
  59.   
  60.   
  61. 2)unix字符串转时间参考代码  
  62.   
  63.   
  64. tm tm_;  
  65. time_t t_;  
  66. char buf[128]= {0};  
  67.   
  68. strcpy(buf, "2012-01-01 14:00:00");  
  69. strptime(buf, "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间  
  70. tm_.tm_isdst = -1;  
  71. t_  = mktime(&tm_); //将tm时间转换为秒时间  
  72. t_ += 3600;  //秒数加3600  
  73.   
  74. tm_ = *localtime(&t_);//输出时间  
  75. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_);  
  76. std::cout << buf << std::endl;  
  77.   
  78.   
  79.   
  80.  3)由于windows下没有strptime函数,所以可以使用scanf来格式化  
  81.   
  82.   
  83. time_t StringToDatetime(char *str)  
  84. {  
  85.     tm tm_;  
  86.     int year, month, day, hour, minute,second;  
  87.     sscanf(str,"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);  
  88.     tm_.tm_year  = year-1900;  
  89.     tm_.tm_mon   = month-1;  
  90.     tm_.tm_mday  = day;  
  91.     tm_.tm_hour  = hour;  
  92.     tm_.tm_min   = minute;  
  93.     tm_.tm_sec   = second;  
  94.     tm_.tm_isdst = 0;  
  95.   
  96.     time_t t_ = mktime(&tm_); //已经减了8个时区  
  97.     return t_; //秒时间  
  98. }  

  99. 4)timeval获取时间示例:
  100. struct timeval start_time, over_time, consume_time;
    gettimeofday(&over_time, NULL);//get the current time
    start_time = over_time;
    	do something.....
    gettimeofday(&over_time, NULL);
    timeval_subtract(&consume_time, &start_time, &over_time);//计算时间差
     
  101. /** 
          * 计算两个时间的间隔,得到时间差 
          * @param struct timeval* resule 返回计算出来的时间 
          * @param struct timeval* x 需要计算的前一个时间 
          * @param struct timeval* y 需要计算的后一个时间 
          * return -1 failure ,0 success 
      **/ 
    timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y) 
      { 
            if ( x->tv_sec>y->tv_sec ) 
                      return -1; 
        
            if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) ) 
                      return -1; 
        
            result->tv_sec = ( y->tv_sec-x->tv_sec ); 
            result->tv_usec = ( y->tv_usec-x->tv_usec ); 
        
            if (result->tv_usec<0) 
            { 
                      result->tv_sec--; 
                      result->tv_usec+=1000000; 
            } 
        
            return 0; 
      }
  102. 4、关于localtime与localtime_r的区别
  103. struct tm *localtime(const time_t *timep);
  104. 这个函数在返回的时候,返回的是一个指针,实际的内存是localtime内部通过static申请的静态内存,所以通过localtime调用后的返回值不及时使用的话,很有可能被其他线程localtime调用所覆盖掉
    struct tm *localtime_r(const time_t *timep, struct tm *result);
  105. localtime_r则是由调用者在第二个参数传入一个struct tm result指针,该函数会把结果填充到这个传入的指针所指内存里面;成功的返回值指针也就是struct tm result。
    多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的。
    其他的时间函数,如asctime,asctime_r;ctime,ctime_r;gmtime,gmtime_r都是类似的,所以,<strong>时间函数的 _r 版本都是线程安全的。</strong>
    </span>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值