TimeHelper.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * TimeHelper.h
  3. *
  4. * Created on: May 22, 2017
  5. * Author: guoxs
  6. */
  7. #ifndef _UTILS_TIME_HELPER_H_
  8. #define _UTILS_TIME_HELPER_H_
  9. #include <time.h>
  10. #include "manager/LanguageManager.h"
  11. /**
  12. * @brief 时间操作帮助类
  13. */
  14. class TimeHelper {
  15. public:
  16. /**
  17. * @brief 获取当前日期时间,单位为毫秒
  18. */
  19. static long long getCurrentTime();
  20. /**
  21. * @brief 获取当前日期
  22. */
  23. static struct tm * getDateTime();
  24. /**
  25. * @brief 设置日期
  26. */
  27. static bool setDateTime(struct tm *ptm);
  28. /**
  29. * @brief 设置日期
  30. * @param pDate 格式:2017-09-13 16:00:00
  31. */
  32. static bool setDateTime(const char *pDate);
  33. static time_t StringToDatetime(std::string str)
  34. {
  35. char *cha = (char*)str.data(); // 将string转换成char*。
  36. tm tm_; // 定义tm结构体。
  37. int year, month, day, hour, minute, second;// 定义时间的各个int临时变量。
  38. sscanf(cha, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);// 将string存储的日期时间,转换为int临时变量。
  39. tm_.tm_year = year - 1900; // 年,由于tm结构体存储的是从1900年开始的时间,所以tm_year为int临时变量减去1900。
  40. tm_.tm_mon = month - 1; // 月,由于tm结构体的月份存储范围为0-11,所以tm_mon为int临时变量减去1。
  41. tm_.tm_mday = day; // 日。
  42. tm_.tm_hour = hour - 8; // 时。
  43. tm_.tm_min = minute; // 分。
  44. tm_.tm_sec = second; // 秒。
  45. tm_.tm_isdst = 0; // 非夏令时。
  46. time_t t_ = mktime(&tm_); // 将tm结构体转换成time_t格式。
  47. return t_; // 返回值。
  48. }
  49. static std::string DatetimeToString(tm *t){
  50. //struct tm *t = TimeHelper::getDateTime();
  51. char timeStr[50];
  52. static const char *day[] = {
  53. LANGUAGEMANAGER->getValue("Sunday").c_str(),
  54. LANGUAGEMANAGER->getValue("Monday").c_str(),
  55. LANGUAGEMANAGER->getValue("Tuesday").c_str(),
  56. LANGUAGEMANAGER->getValue("Wednesday").c_str(),
  57. LANGUAGEMANAGER->getValue("Thursday").c_str(),
  58. LANGUAGEMANAGER->getValue("Friday").c_str(),
  59. LANGUAGEMANAGER->getValue("Saturday").c_str()};
  60. std::string formatStr = "%s %d"+LANGUAGEMANAGER->getValue("Year")+"%02d"+LANGUAGEMANAGER->getValue("Month")+"%02d"+LANGUAGEMANAGER->getValue("Day")+" %02d:%02d:%02d";
  61. sprintf(timeStr, formatStr.c_str(), day[t->tm_wday],1900 + t->tm_year, t->tm_mon + 1, t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
  62. std::string result = timeStr;
  63. return result;
  64. }
  65. static time_t getDateline() {
  66. // time_t timep;
  67. // struct tm *p;
  68. //
  69. // time(&timep);
  70. // p = gmtime(&timep);
  71. struct tm *tm_ = TimeHelper::getDateTime();
  72. // tm_->tm_year = tm_->tm_year - 1900; // 年,由于tm结构体存储的是从1900年开始的时间,所以tm_year为int临时变量减去1900。
  73. // tm_->tm_mon = tm_->tm_mon - 1; // 月,由于tm结构体的月份存储范围为0-11,所以tm_mon为int临时变量减去1。
  74. // tm_->tm_mday = tm_->tm_mday; // 日。
  75. tm_->tm_hour = tm_->tm_hour - 8; // 时。
  76. // tm_->tm_min = tm_->tm_min; // 分。
  77. // tm_->tm_sec = tm_->tm_sec; // 秒。
  78. // tm_->tm_isdst = 0; // 非夏令时。
  79. time_t t_ = mktime(tm_); // 将tm结构体转换成time_t格式。
  80. return t_; // 返回值。
  81. }
  82. static std::string getTimeStrOnTimeDifference(std::string str, int timeDifference, const char *format) {
  83. // 先把时间戳调整到对应时差
  84. time_t timet = stoi(str);
  85. struct tm *t = gmtime(&timet);
  86. char pDate[25];
  87. sprintf(pDate,"%d-%02d-%02d %02d:%02d:%02d",
  88. 1900 + t->tm_year, 1+ t->tm_mon, t->tm_mday,
  89. t->tm_hour + timeDifference,t->tm_min,t->tm_sec);
  90. tm tm_; // 定义tm结构体。
  91. int year, month, day, hour, minute, second;// 定义时间的各个int临时变量。
  92. sscanf(pDate, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);// 将string存储的日期时间,转换为int临时变量。
  93. tm_.tm_year = year - 1900; // 年,由于tm结构体存储的是从1900年开始的时间,所以tm_year为int临时变量减去1900。
  94. tm_.tm_mon = month - 1; // 月,由于tm结构体的月份存储范围为0-11,所以tm_mon为int临时变量减去1。
  95. tm_.tm_mday = day; // 日。
  96. tm_.tm_hour = hour; // 时。
  97. tm_.tm_min = minute; // 分。
  98. tm_.tm_sec = second; // 秒。
  99. tm_.tm_isdst = 0; // 非夏令时。
  100. time_t _timet = mktime(&tm_); // 将tm结构体转换成time_t格式。
  101. // 再转换成字符串
  102. struct tm *_t = gmtime(&_timet);
  103. char _pDate[25];
  104. sprintf(_pDate, format,
  105. 1900 + _t->tm_year, 1+ _t->tm_mon, _t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  106. std::string result = _pDate;
  107. return result;
  108. }
  109. };
  110. #endif /* _UTILS_TIME_HELPER_H_ */