1. 程式人生 > >time_t tm timeval 和 時間字串的轉換

time_t tm timeval 和 時間字串的轉換

轉自:http://blog.csdn.net/ncepubdtb/article/details/38899505,做了一點補充。

  1. 1、常用的時間儲存方式  
  2. 1)time_t型別,這本質上是一個長整數,表示從1970-01-01 00:00:00到目前計時時間的秒數,如果需要更精確一點的,可以使用timeval精確到毫秒。  
  3. 2)tm結構,這本質上是一個結構體,裡面包含了各時間欄位  
  4. structtm {  
  5.         int tm_sec;     /* seconds after the minute - [0,59] */
  6.         int tm_min;     /* minutes after the hour - [0,59] */
  7.         int tm_hour;    /* hours since midnight - [0,23] */
  8.         int tm_mday;    /* day of the month - [1,31] */
  9.         int tm_mon;     /* months since January - [0,11] */
  10.         int tm_year;    /* years since 1900 */
  11.         int tm_wday;    /* days since Sunday - [0,6] */
  12.         int tm_yday;    /* days since January 1 - [0,365] */
  13.         int tm_isdst;   /* daylight savings time flag */
  14.         };  
  15. 其中tm_year表示從1900年到目前計時時間間隔多少年,如果是手動設定值的話,tm_isdst通常取值-1。  
  16. 3)struct timeval結構體在time.h中的定義為
  17. [cpp] view plain copy  print?
    1. struct timeval {  
    2.           time_t       tv_sec;     /* seconds */
    3.           suseconds_t   tv_usec; /* microseconds */
    4.  };  
  18. 2、常用的時間函式  
  19. time_t time(time_t *t); //取得從1970年1月1日至今的秒數
  20. char *asctime(conststructtm *tm); //將結構中的資訊轉換為真實世界的時間,以字串的形式顯示
  21. char *ctime(consttime_t *timep); //將timep轉換為真是世界的時間,以字串顯示,它和asctime不同就在於傳入的引數形式不一樣
  22. structtm *gmtime(consttime_t *timep); //將time_t表示的時間轉換為沒有經過時區轉換的UTC時間,是一個struct tm結構指標 
  23. structtm *localtime(consttime_t *timep); //和gmtime類似,但是它是經過時區轉換的時間。
  24. time_t mktime(structtm *tm); //將struct tm 結構的時間轉換為從1970年至今的秒數
  25. int gettimeofday(struct timeval *tv, struct timezone *tz); //返回當前距離1970年的秒數和微妙數,後面的tz是時區,一般不用
  26. double difftime(time_t time1, time_t time2); //返回兩個時間相差的秒數
  27. 3、時間與字串的轉換  
  28. 需要包含的標頭檔案如下  
  29. #include <iostream>
  30. #include <time.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. 1)unix/windows下時間轉字串參考程式碼  
  34. time_t t;  //秒時間
  35. tm* local; //本地時間 
  36. tm* gmt;   //格林威治時間
  37. char buf[128]= {0};  
  38. t = time(NULL); //或者time(&t);//獲取目前秒時間
  39. local = localtime(&t); //轉為本地時間
  40. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local);  
  41. std::cout << buf << std::endl;  
  42. gmt = gmtime(&t);//轉為格林威治時間
  43. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt);  
  44. std::cout << buf << std::endl;  
  45. 2)unix字串轉時間參考程式碼  
  46. tm tm_;  
  47. time_t t_;  
  48. char buf[128]= {0};  
  49. strcpy(buf, "2012-01-01 14:00:00");  
  50. strptime(buf, "%Y-%m-%d %H:%M:%S", &tm_); //將字串轉換為tm時間
  51. tm_.tm_isdst = -1;  
  52. t_  = mktime(&tm_); //將tm時間轉換為秒時間
  53. t_ += 3600;  //秒數加3600
  54. tm_ = *localtime(&t_);//輸出時間
  55. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_);  
  56. std::cout << buf << std::endl;  
  57.  3)由於windows下沒有strptime函式,所以可以使用scanf來格式化  
  58. time_t StringToDatetime(char *str)  
  59. {  
  60.     tm tm_;  
  61.     int year, month, day, hour, minute,second;  
  62.     sscanf(str,"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);  
  63.     tm_.tm_year  = year-1900;  
  64.     tm_.tm_mon   = month-1;  
  65.     tm_.tm_mday  = day;  
  66.     tm_.tm_hour  = hour;  
  67.     tm_.tm_min   = minute;  
  68.     tm_.tm_sec   = second;  
  69.     tm_.tm_isdst = 0;  
  70.     time_t t_ = mktime(&tm_); //已經減了8個時區
  71.     return t_; //秒時間
  72. }  
  73. 4)timeval獲取時間示例:
  74. [cpp] view plain copy  print?
    1. struct timeval start_time, over_time, consume_time;  
    2. gettimeofday(&over_time, NULL);//get the current time
    3. start_time = over_time;  
    4.     do something.....  
    5. gettimeofday(&over_time, NULL);  
    6. timeval_subtract(&consume_time, &start_time, &over_time);//計算時間差
  75. [cpp] view plain copy  print?
    1. /**  
    2.       * 計算兩個時間的間隔,得到時間差  
    3.       * @param struct timeval* resule 返回計算出來的時間  
    4.       * @param struct timeval* x 需要計算的前一個時間  
    5.       * @param struct timeval* y 需要計算的後一個時間  
    6.       * return -1 failure ,0 success  
    7.   **/
    8. timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y)   
    9.   {   
    10.         if ( x->tv_sec>y->tv_sec )   
    11.                   return -1;   
    12.         if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) )   
    13.                   return -1;   
    14.         result->tv_sec = ( y->tv_sec-x->tv_sec );   
    15.         result->tv_usec = ( y->tv_usec-x->tv_usec );   
    16.         if (result->tv_usec<0)   
    17.         {   
    18.                   result->tv_sec--;   
    19.                   result->tv_usec+=1000000;   
    20.         }   
    21.         return 0;   
    22.   }  
  76. [cpp] view plain copy  print?
    1. 4、關於localtime與localtime_r的區別  
  77. [cpp] view plain copy  print?
    1. structtm *localtime(consttime_t *timep);  
  78. [cpp] view plain copy  print?
    1. 這個函式在返回的時候,返回的是一個指標,實際的記憶體是localtime內部通過static申請的靜態記憶體,所以通過localtime呼叫後的返回值不及時使用的話,很有可能被其他執行緒localtime呼叫所覆蓋掉  
    2. structtm *localtime_r(consttime_t *timep, structtm *result);  
  79. [cpp] view plain copy  print?
    1. localtim