time.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef _FY_TIME_H_
  2. #define _FY_TIME_H_
  3. #include <time.h>
  4. #include <stdint.h>
  5. #include <string>
  6. #include "utils/TimeHelper.h"
  7. #include "utils/Log.h"
  8. namespace base {
  9. class time {
  10. public:
  11. static time now() {
  12. return time();
  13. }
  14. // 开机累计毫秒
  15. static int64_t uptime() {
  16. struct timespec ts;
  17. clock_gettime(CLOCK_BOOTTIME, &ts);
  18. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  19. }
  20. bool after(const time& t) {
  21. return true;
  22. }
  23. bool before(const time& t) {
  24. return true;
  25. }
  26. struct DateTime {
  27. int year;
  28. int mon;
  29. int day;
  30. int hour;
  31. int min;
  32. int sec;
  33. int week;
  34. };
  35. static struct DateTime GetDateTimeNow() {
  36. DateTime now;
  37. struct tm *t = TimeHelper::getDateTime();
  38. now.year = t->tm_year + 1900;
  39. now.mon = t->tm_mon + 1;
  40. now.day = t->tm_mday;
  41. now.hour = t->tm_hour;
  42. now.min = t->tm_min;
  43. now.sec = t->tm_sec;
  44. now.week = t->tm_wday;
  45. return now;
  46. }
  47. static struct DateTime GetBuildDateTime() {
  48. std::string monthes[] = {
  49. "Jan",
  50. "Feb",
  51. "Mar",
  52. "Apr",
  53. "May",
  54. "Jun",
  55. "Jul",
  56. "Aug",
  57. "Sep",
  58. "Oct",
  59. "Nov",
  60. "Dec",
  61. };
  62. std::string dateStr = __DATE__;
  63. int year = atoi(dateStr.substr(dateStr.length() - 4).c_str());
  64. int month = 0;
  65. for (int i = 0; i < 12; i++) {
  66. if (dateStr.find(monthes[i]) != std::string::npos) {
  67. month = i + 1;
  68. break;
  69. }
  70. }
  71. std::string dayStr = dateStr.substr(4, 2);
  72. int day = atoi(dayStr.c_str());
  73. std::string timeStr = __TIME__;
  74. std::string hourStr = timeStr.substr(0, 2);
  75. int hour = atoi(hourStr.c_str());
  76. std::string minStr = timeStr.substr(3, 2);
  77. int min = atoi(minStr.c_str());
  78. std::string secondStr = timeStr.substr(6, 2);
  79. int second = atoi(secondStr.c_str());
  80. DateTime dt;
  81. dt.year = year;
  82. dt.mon = month;
  83. dt.day = day;
  84. dt.hour = hour;
  85. dt.min = min;
  86. dt.sec = second;
  87. return dt;
  88. }
  89. static void SetSystemDateTime(time_t timestamp) {
  90. TimeHelper::setDateTime(gmtime(&timestamp));
  91. }
  92. static void SetSystemDateTime(DateTime dt) {
  93. struct tm t = *TimeHelper::getDateTime();
  94. t.tm_year = dt.year - 1900;
  95. t.tm_mon = (dt.mon - 1) % 12; //[0,11]
  96. t.tm_mday = dt.day % 32; //[1,31]
  97. t.tm_hour = dt.hour % 24; //[0,23]
  98. t.tm_min = dt.min % 60; //[0,59]
  99. t.tm_sec = dt.sec % 60; //[0,59]
  100. TimeHelper::setDateTime(&t);
  101. }
  102. private:
  103. };
  104. } /* namespace base */
  105. #endif /* _FY_TIME_H_ */