time.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * time.cpp
  3. *
  4. * Created on: 2022年8月18日
  5. * Author: pengzc
  6. */
  7. #include "time.h"
  8. namespace base {
  9. DateTime::DateTime() {
  10. }
  11. DateTime::DateTime(time_t timestamp) {
  12. struct tm *t = localtime(&timestamp);
  13. year = t->tm_year + 1900;
  14. month = t->tm_mon + 1;
  15. day = t->tm_mday;
  16. hour = t->tm_hour;
  17. min = t->tm_min;
  18. second = t->tm_sec;
  19. week = t->tm_wday;
  20. }
  21. DateTime::DateTime(const std::string& datetime) {
  22. sscanf(datetime.c_str(), "%d-%d-%d %d:%d:%d",
  23. &year, &month, &day, &hour, &min, &second);
  24. }
  25. std::string DateTime::ToString() {
  26. char buf[64] = {0};
  27. snprintf(buf, int(sizeof(buf)),
  28. "%04d-%02d-%02d %02d:%02d:%02d",
  29. year, month, day, hour, min, second);
  30. return buf;
  31. }
  32. // 开机累计毫秒
  33. int64_t uptime() {
  34. struct timespec ts;
  35. clock_gettime(CLOCK_BOOTTIME, &ts);
  36. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  37. }
  38. void SetSystemDateTime(time_t timestamp) {
  39. TimeHelper::setDateTime(gmtime(&timestamp));
  40. }
  41. void SetSystemDateTime(const DateTime& dt) {
  42. struct tm t = *TimeHelper::getDateTime();
  43. t.tm_year = dt.year - 1900;
  44. t.tm_mon = (dt.month - 1) % 12; //[0,11]
  45. t.tm_mday = dt.day % 32; //[1,31]
  46. t.tm_hour = dt.hour % 24; //[0,23]
  47. t.tm_min = dt.min % 60; //[0,59]
  48. t.tm_sec = dt.second % 60; //[0,59]
  49. TimeHelper::setDateTime(&t);
  50. }
  51. class DateTime GetBuildDateTime() {
  52. std::string monthes[] = {
  53. "Jan",
  54. "Feb",
  55. "Mar",
  56. "Apr",
  57. "May",
  58. "Jun",
  59. "Jul",
  60. "Aug",
  61. "Sep",
  62. "Oct",
  63. "Nov",
  64. "Dec",
  65. };
  66. std::string dateStr = __DATE__;
  67. int year = atoi(dateStr.substr(dateStr.length() - 4).c_str());
  68. int month = 0;
  69. for (int i = 0; i < 12; i++) {
  70. if (dateStr.find(monthes[i]) != std::string::npos) {
  71. month = i + 1;
  72. break;
  73. }
  74. }
  75. std::string dayStr = dateStr.substr(4, 2);
  76. int day = atoi(dayStr.c_str());
  77. std::string timeStr = __TIME__;
  78. std::string hourStr = timeStr.substr(0, 2);
  79. int hour = atoi(hourStr.c_str());
  80. std::string minStr = timeStr.substr(3, 2);
  81. int min = atoi(minStr.c_str());
  82. std::string secondStr = timeStr.substr(6, 2);
  83. int second = atoi(secondStr.c_str());
  84. DateTime dt;
  85. dt.year = year;
  86. dt.month = month;
  87. dt.day = day;
  88. dt.hour = hour;
  89. dt.min = min;
  90. dt.second = second;
  91. return dt;
  92. }
  93. class DateTime GetDateTimeNow() {
  94. DateTime now;
  95. struct tm *t = TimeHelper::getDateTime();
  96. now.year = t->tm_year + 1900;
  97. now.month = t->tm_mon + 1;
  98. now.day = t->tm_mday;
  99. now.hour = t->tm_hour;
  100. now.min = t->tm_min;
  101. now.second = t->tm_sec;
  102. now.week = t->tm_wday;
  103. return now;
  104. }
  105. int64_t DateTime::Unix() const {
  106. struct tm t = *TimeHelper::getDateTime();
  107. t.tm_year = year - 1900;
  108. t.tm_mon = (month - 1) % 12; //[0,11]
  109. t.tm_mday = day % 32; //[1,31]
  110. t.tm_hour = hour % 24; //[0,23]
  111. t.tm_min = min % 60; //[0,59]
  112. t.tm_sec = second % 60; //[0,59]
  113. return mktime(&t);
  114. }
  115. DateTime DateTime::Add(int64_t seconds) {
  116. int64_t unix_time = Unix();
  117. unix_time += seconds;
  118. return DateTime(unix_time);
  119. }
  120. } /* namespace */