[关闭]
@perkyoung 2015-11-28T14:51:58.000000Z 字数 3617 阅读 2066

时间函数

UNIX_API


今天学习了一些linux关于时间的函数,还有几个关于文件的时间函数,比如utimeslutimes,futimes等,不在今天讨论的范围之内。

关于时间的函数,可以进行如下分类

  1. char * ctime(time_t*);
  2. char* ctime_r(time_t*, char* buf);
  3. char* asctime(struct tm*);
  4. char* asctime(struct tm*, char* buf);
  1. struct tm * = gmtime(time_t*);
  2. struct tm* = gmtime_r(time_t*, struct tm* result);
  3. struct tm* = localtime(time_t*);
  4. struct tm* = localtime_r(time_t*, struct tm* result);
  1. time_t mktime(struct tm *);
  1. int gettimeofday(struct timeval * __restrict, void * __restrict);

技巧性总结

综上所述,平时常用的就是

下面是示例代码

  1. #include <iostream>
  2. #include <time.h>
  3. #include <sys/time.h>
  4. using namespace std;
  5. /*
  6. * ctime(),
  7. */
  8. int main()
  9. {
  10. //ctime(const time_t* ),传入一个时间戳,返回一个时间的字符串.但是这个是分时区的,如果传入的
  11. //是0,那结果就是 Thu Jan 1 08:00:00 1970
  12. time_t a = 100;
  13. char * p = ctime(&a);
  14. cout << "ctime:" << endl;
  15. cout << "time is " << p << " a is " << a << endl;
  16. //char *
  17. //ctime_r(const time_t *clock, char *buf);提供和上面同样的功能,但是结果可以存储在用户提供的buf中
  18. //因为,返回的是一个志向static内存区域的指针,这个区域可能会被其他线程修改,这个buf起码要26个字符.
  19. char buf[26] = {0};
  20. char * p1 = ctime_r(&a, buf);
  21. cout << "time is " << buf << endl << endl;
  22. //gmtime(const time_t* ),传入时间戳,返回的是一个tm的结构,年份是距离1900年的时间
  23. //如果传入0的话,year就是70.
  24. struct tm* ret = gmtime(&a);
  25. cout << "gmtime: " << endl;
  26. cout << "year is " << ret->tm_year << endl;
  27. cout << "tm_sec is " << ret->tm_sec << endl;
  28. cout << "tm_wday is " << ret->tm_wday << endl;
  29. //同样,也有多线程的版本,gmtime(time_t *, tm* );
  30. struct tm result;
  31. gmtime_r(&a, &result);
  32. cout << "year is " << result.tm_year << endl;
  33. cout << "hour is " << result.tm_hour << endl;
  34. cout << "min is " << result.tm_min << endl;
  35. cout << "sec is " << result.tm_sec << endl<< endl;
  36. //localtime,目测localtime和gmtime用法是一样一样的.但是和gmtime不同的是:区分时区,gmtime返回的是0点,localtime返回的是8点.
  37. //所以一般情况下,我们使用的是localtime/localtime_r.
  38. ret = localtime(&a);
  39. cout << "localtime:" << endl;
  40. cout << "year is " << ret->tm_year << endl;
  41. cout << "tm_sec is " << ret->tm_sec << endl;
  42. cout << "tm_wday is " << ret->tm_wday << endl;
  43. //同样有多线程的版本
  44. ret = localtime_r(&a, &result);
  45. cout << "year is " << result.tm_year << endl;
  46. cout << "hour is " << result.tm_hour << endl;
  47. cout << "min is " << result.tm_min << endl;
  48. cout << "sec is " << result.tm_sec << endl<<endl;
  49. //asctime是传入的是tm的结构,根据tm结构中的数据,最终返回一个字符串,
  50. //同时提供多线程安全的版本,多传入一个buf,会写到这个buf中.
  51. result.tm_sec = 55;
  52. char * tmp = asctime(&result);
  53. cout << "asctime:" << endl;
  54. cout << "ret is " << tmp;
  55. cout << "year is " << result.tm_year << endl;
  56. cout << "hour is " << result.tm_hour << endl;
  57. cout << "min is " << result.tm_min << endl;
  58. cout << "sec is " << result.tm_sec << endl;
  59. char asctime_buf[26];
  60. result.tm_year = 80;
  61. tmp = asctime_r(&result, asctime_buf);
  62. cout << "ret is " << asctime_buf;
  63. cout << "year is " << result.tm_year << endl;
  64. cout << "hour is " << result.tm_hour << endl;
  65. cout << "min is " << result.tm_min << endl;
  66. cout << "sec is " << result.tm_sec << endl<<endl;
  67. //difftime顾名思义就是时间差,传入的是time_t类型的数据.
  68. time_t start = 1222;
  69. time_t end = 1111;
  70. double diffret = difftime(start,end);
  71. cout << "difftime:" << endl;
  72. cout << "diff is " << diffret << endl;
  73. //mktime顾名思义就是生成time_t 时间戳,这是穿进去的数字是根据自己当前的时区的.
  74. struct tm mktime_tm;
  75. mktime_tm.tm_year = 101;
  76. mktime_tm.tm_hour = 3;
  77. time_t mktime_ret = mktime(&mktime_tm);
  78. cout << "ret is " << mktime_ret << endl;
  79. struct tm* aaa = gmtime(&mktime_ret);
  80. cout << "hour is " << aaa->tm_hour << endl;
  81. aaa = localtime(&mktime_ret);
  82. cout << "hour is " << aaa->tm_hour << endl;
  83. //最特别的一个,gettimeofday,传递的参数都不同.timeval类型的参数
  84. //#include <sys/time.h>
  85. timeval timeofday_a;
  86. struct timezone timeofzone_a;
  87. gettimeofday(&timeofday_a, &timeofzone_a);
  88. cout << "timeofday is " << timeofday_a.tv_sec << endl;
  89. cout << "time zone is " << timeofzone_a.tz_dsttime << endl;
  90. cout << "time zone is " << timeofzone_a.tz_minuteswest << endl;
  91. //相同功能的函数就是time(time_t*);
  92. return 0;
  93. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注