123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * time.cpp
- *
- * Created on: 2022年8月18日
- * Author: pengzc
- */
- #include "time.h"
- namespace base {
- DateTime::DateTime() {
- }
- DateTime::DateTime(time_t timestamp) {
- struct tm *t = localtime(×tamp);
- year = t->tm_year + 1900;
- month = t->tm_mon + 1;
- day = t->tm_mday;
- hour = t->tm_hour;
- min = t->tm_min;
- second = t->tm_sec;
- week = t->tm_wday;
- }
- DateTime::DateTime(const std::string& datetime) {
- sscanf(datetime.c_str(), "%d-%d-%d %d:%d:%d",
- &year, &month, &day, &hour, &min, &second);
- }
- std::string DateTime::ToString() {
- char buf[64] = {0};
- snprintf(buf, int(sizeof(buf)),
- "%04d-%02d-%02d %02d:%02d:%02d",
- year, month, day, hour, min, second);
- return buf;
- }
- // 开机累计毫秒
- int64_t uptime() {
- struct timespec ts;
- clock_gettime(CLOCK_BOOTTIME, &ts);
- return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
- }
- void SetSystemDateTime(time_t timestamp) {
- TimeHelper::setDateTime(gmtime(×tamp));
- }
- void SetSystemDateTime(const DateTime& dt) {
- struct tm t = *TimeHelper::getDateTime();
- t.tm_year = dt.year - 1900;
- t.tm_mon = (dt.month - 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.second % 60; //[0,59]
- TimeHelper::setDateTime(&t);
- }
- class 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.month = month;
- dt.day = day;
- dt.hour = hour;
- dt.min = min;
- dt.second = second;
- return dt;
- }
- class DateTime GetDateTimeNow() {
- DateTime now;
- struct tm *t = TimeHelper::getDateTime();
- now.year = t->tm_year + 1900;
- now.month = t->tm_mon + 1;
- now.day = t->tm_mday;
- now.hour = t->tm_hour;
- now.min = t->tm_min;
- now.second = t->tm_sec;
- now.week = t->tm_wday;
- return now;
- }
- int64_t DateTime::Unix() const {
- struct tm t = *TimeHelper::getDateTime();
- t.tm_year = year - 1900;
- t.tm_mon = (month - 1) % 12; //[0,11]
- t.tm_mday = day % 32; //[1,31]
- t.tm_hour = hour % 24; //[0,23]
- t.tm_min = min % 60; //[0,59]
- t.tm_sec = second % 60; //[0,59]
- return mktime(&t);
- }
- DateTime DateTime::Add(int64_t seconds) {
- int64_t unix_time = Unix();
- unix_time += seconds;
- return DateTime(unix_time);
- }
- } /* namespace */
|