[转]C++ ctime库

1. 类型
clock_t: 是个long型,用来记录一段时间内的时钟计时单元数,即CPU的运行单元时间。
size_t: 标准C库中定义的,应为unsigned int,在64位系统中为long unsigned int。
time_t: 从1970年1月1日0时0分0秒到该时间点所经过的秒数。
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代表一月) - 取值区间为[0,11] */
  int tm_year;      /* 年份,其值等于实际年份减去1900 比如今年是2014,那么你得到tm_year的值是114*/
  int tm_wday;      /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
  int tm_yday;      /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
  int tm_isdst;     /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
2. 时间的操作
clock: 返回时钟计时单元数,自从这个程序开始运行。
time: 返回当前的time_t。
difftime: 计算time_t两个之间的时间差。

3. 转换
mktime: 转换tm structure成time_t
asctime: 转换tm structure成字符串
ctime: 转换time_t成字符串
gmtime(const time_t *time): 转换time_t成tm as UTC time 返回的是struct tm*
localtime(const time_t *time): 转换time_t成tm as local time 返回的是struct tm*
strftime: 格式时间成字符串
转换成字符串的几个函数:asctime, ctime, strftime
4. 宏
CLOCKS_PER_SEC: 它用来表示一秒钟会有多少个时钟计时单元。

    1. // 测量事件的持续时间   
    2. void test_clock_t()  
    3. {  
    4.     long i = 100000000L;  
    5.     clock_t start, finish;  
    6.     double duration;  
    7.     start = clock();  
    8.   
    9.     /* 测量一个事件持续的时间 */  
    10.     while(i--) {};  
    11.   
    12.     finish = clock();  
    13.     duration = (double)(finish - start) / CLOCKS_PER_SEC;  
    14.     printf("Time to do 100000000 empty loops is %f seconds ", duration);  
    15. }  
    16.   
    17. void test_time_t()  
    18. {  
    19.     time_t t = time(NULL);  
    20.     printf("The Calendar Time now is %d ", t);  
    21. }  
    22.   
    23. void test_difftime()  
    24. {  
    25.     time_t start,end;  
    26.     start = time(NULL);  
    27.     system("pause");  
    28.     end = time(NULL);  
    29.     printf("The pause used %5.4f seconds. ", difftime(end, start));  
    30. }   
      1. // 下面都是一些转换函数的应用   
      2. // mktime: tm --> time_c   
      3. void test_mktime()  
      4. {  
      5.     struct tm t;  
      6.     time_t t_of_day;  
      7.     t.tm_year = 1997 - 1900;  
      8.     t.tm_mon = 6;  
      9.     t.tm_mday = 1;  
      10.     t.tm_hour = 0;  
      11.     t.tm_min = 0;  
      12.     t.tm_sec = 1;  
      13.     t.tm_wday = 4; /* Day of the week */  
      14.     t.tm_yday = 0; /* Does not show in asctime */  
      15.     t.tm_isdst = 0;  
      16.   
      17.     t_of_day = mktime(&t);  
      18.     printf(ctime(&t_of_day));  
      19. }  
      20.   
      21. // localtime: time_c --> tm   
      22. void test_localtime()  
      23. {  
      24.     time_t rawtime;  
      25.     struct tm* timeinfo;  
      26.   
      27.     time(&rawtime);  
      28.     timeinfo = localtime(&rawtime);  
      29.     printf("Current local time and date: %s", asctime(timeinfo));  
      30. }  
      31.   
      32. // gmtime: time_c --> tm   
      33. void test_gmtime()  
      34. {  
      35.     time_t rawtime;  
      36.     struct tm* timeinfo;  
      37.   
      38.     time(&rawtime);  
      39.     timeinfo = gmtime(&rawtime);  
      40.     printf("UTC time and date: %s", asctime(timeinfo));  
      41. }  
      42.   
      43. // ctime: time_t --> string   
      44. void test_ctime()  
      45. {  
      46.     time_t t = time(NULL);  
      47.     std::string str = ctime(&t);  
      48.     std::cout << str << std::endl;  
      49. }

 链接地址:http://blog.csdn.net/huang_xw/article/details/8192663