123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #ifndef _FY_TIME_H_
- #define _FY_TIME_H_
- #include <time.h>
- #include <stdint.h>
- #include <string>
- #include "utils/TimeHelper.h"
- #include "utils/Log.h"
- namespace base {
- class time {
- public:
- static time now() {
- return time();
- }
- // 开机累计毫秒
- static int64_t uptime() {
- struct timespec ts;
- clock_gettime(CLOCK_BOOTTIME, &ts);
- return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
- }
- bool after(const time& t) {
- return true;
- }
- bool before(const time& t) {
- return true;
- }
- struct DateTime {
- int year;
- int mon;
- int day;
- int hour;
- int min;
- int sec;
- int week;
- };
- static struct DateTime GetDateTimeNow() {
- DateTime now;
- struct tm *t = TimeHelper::getDateTime();
- now.year = t->tm_year + 1900;
- now.mon = t->tm_mon + 1;
- now.day = t->tm_mday;
- now.hour = t->tm_hour;
- now.min = t->tm_min;
- now.sec = t->tm_sec;
- now.week = t->tm_wday;
- return now;
- }
- static struct DateTime GetBuildDateTime() {
- std::string monthes[] = {
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec",
- };
- std::string dateStr = __DATE__;
- int year = atoi(dateStr.substr(dateStr.length() - 4).c_str());
- int month = 0;
- for (int i = 0; i < 12; i++) {
- if (dateStr.find(monthes[i]) != std::string::npos) {
- month = i + 1;
- break;
- }
- }
- std::string dayStr = dateStr.substr(4, 2);
- int day = atoi(dayStr.c_str());
- std::string timeStr = __TIME__;
- std::string hourStr = timeStr.substr(0, 2);
- int hour = atoi(hourStr.c_str());
- std::string minStr = timeStr.substr(3, 2);
- int min = atoi(minStr.c_str());
- std::string secondStr = timeStr.substr(6, 2);
- int second = atoi(secondStr.c_str());
- DateTime dt;
- dt.year = year;
- dt.mon = month;
- dt.day = day;
- dt.hour = hour;
- dt.min = min;
- dt.sec = second;
- return dt;
- }
- static void SetSystemDateTime(time_t timestamp) {
- TimeHelper::setDateTime(gmtime(×tamp));
- }
- static void SetSystemDateTime(DateTime dt) {
- struct tm t = *TimeHelper::getDateTime();
- t.tm_year = dt.year - 1900;
- t.tm_mon = (dt.mon - 1) % 12; //[0,11]
- t.tm_mday = dt.day % 32; //[1,31]
- t.tm_hour = dt.hour % 24; //[0,23]
- t.tm_min = dt.min % 60; //[0,59]
- t.tm_sec = dt.sec % 60; //[0,59]
- TimeHelper::setDateTime(&t);
- }
- private:
- };
- } /* namespace base */
- #endif /* _FY_TIME_H_ */
|