TimeHelper.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 std::string getTimeStrOnTimeDifference(std::string str, int timeDifference) {
  66. // 先把时间戳调整到对应时差
  67. time_t timet = stoi(str);
  68. struct tm *t = gmtime(&timet);
  69. char pDate[25];
  70. sprintf(pDate,"%d-%02d-%02d %02d:%02d:%02d",
  71. 1900 + t->tm_year, 1+ t->tm_mon, t->tm_mday,
  72. t->tm_hour + timeDifference,t->tm_min,t->tm_sec);
  73. tm tm_; // 定义tm结构体。
  74. int year, month, day, hour, minute, second;// 定义时间的各个int临时变量。
  75. sscanf(pDate, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);// 将string存储的日期时间,转换为int临时变量。
  76. tm_.tm_year = year - 1900; // 年,由于tm结构体存储的是从1900年开始的时间,所以tm_year为int临时变量减去1900。
  77. tm_.tm_mon = month - 1; // 月,由于tm结构体的月份存储范围为0-11,所以tm_mon为int临时变量减去1。
  78. tm_.tm_mday = day; // 日。
  79. tm_.tm_hour = hour; // 时。
  80. tm_.tm_min = minute; // 分。
  81. tm_.tm_sec = second; // 秒。
  82. tm_.tm_isdst = 0; // 非夏令时。
  83. time_t _timet = mktime(&tm_); // 将tm结构体转换成time_t格式。
  84. // 再转换成字符串
  85. struct tm *_t = gmtime(&_timet);
  86. char _pDate[25];
  87. sprintf(_pDate,"%d-%02d-%02d",
  88. 1900 + _t->tm_year, 1+ _t->tm_mon, _t->tm_mday);
  89. std::string result = _pDate;
  90. return result;
  91. }
  92. };
  93. #endif /* _UTILS_TIME_HELPER_H_ */