123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- /*
- * TimeHelper.h
- *
- * Created on: May 22, 2017
- * Author: guoxs
- */
- #ifndef _UTILS_TIME_HELPER_H_
- #define _UTILS_TIME_HELPER_H_
- #include <time.h>
- #include "manager/LanguageManager.h"
- /**
- * @brief 时间操作帮助类
- */
- class TimeHelper {
- public:
- /**
- * @brief 获取当前日期时间,单位为毫秒
- */
- static long long getCurrentTime();
- /**
- * @brief 获取当前日期
- */
- static struct tm * getDateTime();
- /**
- * @brief 设置日期
- */
- static bool setDateTime(struct tm *ptm);
- /**
- * @brief 设置日期
- * @param pDate 格式:2017-09-13 16:00:00
- */
- static bool setDateTime(const char *pDate);
- static time_t StringToDatetime(std::string str)
- {
- char *cha = (char*)str.data(); // 将string转换成char*。
- tm tm_; // 定义tm结构体。
- int year, month, day, hour, minute, second;// 定义时间的各个int临时变量。
- sscanf(cha, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);// 将string存储的日期时间,转换为int临时变量。
- tm_.tm_year = year - 1900; // 年,由于tm结构体存储的是从1900年开始的时间,所以tm_year为int临时变量减去1900。
- tm_.tm_mon = month - 1; // 月,由于tm结构体的月份存储范围为0-11,所以tm_mon为int临时变量减去1。
- tm_.tm_mday = day; // 日。
- tm_.tm_hour = hour - 8; // 时。
- tm_.tm_min = minute; // 分。
- tm_.tm_sec = second; // 秒。
- tm_.tm_isdst = 0; // 非夏令时。
- time_t t_ = mktime(&tm_); // 将tm结构体转换成time_t格式。
- return t_; // 返回值。
- }
- static std::string DatetimeToString(tm *t){
- //struct tm *t = TimeHelper::getDateTime();
- char timeStr[50];
- static const char *day[] = {
- LANGUAGEMANAGER->getValue("Sunday").c_str(),
- LANGUAGEMANAGER->getValue("Monday").c_str(),
- LANGUAGEMANAGER->getValue("Tuesday").c_str(),
- LANGUAGEMANAGER->getValue("Wednesday").c_str(),
- LANGUAGEMANAGER->getValue("Thursday").c_str(),
- LANGUAGEMANAGER->getValue("Friday").c_str(),
- LANGUAGEMANAGER->getValue("Saturday").c_str()};
- std::string formatStr = "%s %d"+LANGUAGEMANAGER->getValue("Year")+"%02d"+LANGUAGEMANAGER->getValue("Month")+"%02d"+LANGUAGEMANAGER->getValue("Day")+" %02d:%02d:%02d";
- 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);
- std::string result = timeStr;
- return result;
- }
- static time_t getDateline() {
- // time_t timep;
- // struct tm *p;
- //
- // time(&timep);
- // p = gmtime(&timep);
- struct tm *tm_ = TimeHelper::getDateTime();
- // tm_->tm_year = tm_->tm_year - 1900; // 年,由于tm结构体存储的是从1900年开始的时间,所以tm_year为int临时变量减去1900。
- // tm_->tm_mon = tm_->tm_mon - 1; // 月,由于tm结构体的月份存储范围为0-11,所以tm_mon为int临时变量减去1。
- // tm_->tm_mday = tm_->tm_mday; // 日。
- tm_->tm_hour = tm_->tm_hour - 8; // 时。
- // tm_->tm_min = tm_->tm_min; // 分。
- // tm_->tm_sec = tm_->tm_sec; // 秒。
- // tm_->tm_isdst = 0; // 非夏令时。
- time_t t_ = mktime(tm_); // 将tm结构体转换成time_t格式。
- return t_; // 返回值。
- }
- static std::string getTimeStrOnTimeDifference(std::string str, int timeDifference, const char *format) {
- // 先把时间戳调整到对应时差
- time_t timet = stoi(str);
- struct tm *t = gmtime(&timet);
- char pDate[25];
- sprintf(pDate,"%d-%02d-%02d %02d:%02d:%02d",
- 1900 + t->tm_year, 1+ t->tm_mon, t->tm_mday,
- t->tm_hour + timeDifference,t->tm_min,t->tm_sec);
- tm tm_; // 定义tm结构体。
- int year, month, day, hour, minute, second;// 定义时间的各个int临时变量。
- sscanf(pDate, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);// 将string存储的日期时间,转换为int临时变量。
- tm_.tm_year = year - 1900; // 年,由于tm结构体存储的是从1900年开始的时间,所以tm_year为int临时变量减去1900。
- tm_.tm_mon = month - 1; // 月,由于tm结构体的月份存储范围为0-11,所以tm_mon为int临时变量减去1。
- tm_.tm_mday = day; // 日。
- tm_.tm_hour = hour; // 时。
- tm_.tm_min = minute; // 分。
- tm_.tm_sec = second; // 秒。
- tm_.tm_isdst = 0; // 非夏令时。
- time_t _timet = mktime(&tm_); // 将tm结构体转换成time_t格式。
- // 再转换成字符串
- struct tm *_t = gmtime(&_timet);
- char _pDate[25];
- sprintf(_pDate, format,
- 1900 + _t->tm_year, 1+ _t->tm_mon, _t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
- std::string result = _pDate;
- return result;
- }
- };
- #endif /* _UTILS_TIME_HELPER_H_ */
|