这个时间类主要是用来获取系统毫秒时间、时区、当地时间,以及对传入的时间的转换,适用于linux和windows。
TimeUtility.h
#pragma once
#include
#pragma once
#include
namespace COMMON
{
namespace UTILITY
{
class TimeUtility
{
public:
TimeUtility();
~TimeUtility();
static unsigned long GetTickCount2();
static time_t ConvertTimerToTime_t(std::string dateTime);
static std::string ConvertTime_tToTimeString(time_t time);
static std::string formatA(const char* fmt, ...);
static int GetTimezone();
static time_t StringUTCToTime_t(const std::string& strUTCTime);
static std::string StringUTCToLocal(std::string UTCTime);
static std::string StringLocalToUTC(std::string localTime);
static time_t StringLocalToTime_t(const std::string& strLocalTime);
static std::string StringTimeAddSecond(const std::string& dateTime, const char ops, time_t second);
};
}
}
TimeUtility.cpp
#include "TimeUtility.h"
#ifdef WIN32
#include
#include
#include
#else
#include
#endif
#include
#include
#include
#include
#include
#include
#include
namespace COMMON
{
namespace UTILITY
{
TimeUtility::TimeUtility()
{
}
TimeUtility::~TimeUtility()
{
}
unsigned long TimeUtility::GetTickCount2() //获取当前系统毫秒时间
{
#ifdef WIN32
return GetTickCount();
#else
struct timeval tv;
if (gettimeofday(&tv, nullptr) != 0)
return 0;
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
#endif
}
std::string TimeUtility::ConvertTime_tToTimeString(time_t time)
{
#ifdef WIN32
SYSTEMTIME sysTime;
FILETIME ft;
LONGLONG ll = Int32x32To64(time, 10000000) + 116444736000000000;
ft.dwLowDateTime = (DWORD)ll;
ft.dwHighDateTime = (DWORD)(ll >> 32);
FileTimeToSystemTime(&ft, &sysTime);
std::ostringstream os;
os.fill('0');
os.width(4);
os << sysTime.wYear << "-";
os.width(2);
os << sysTime.wMonth << "-";
os.width(2);
os << sysTime.wDay << "T";
os.width(2);
os << sysTime.wHour << ":";
os.width(2);
os << sysTime.wMinute << ":";
os.width(2);
os << sysTime.wSecond;
return os.str();
#else
struct tm readableTime;
gmtime_r(&time, &readableTime);
return formatA("%04d-%02d-%02dT%02d:%02d:%02d",
readableTime.tm_year + 1900, readableTime.tm_mon + 1, readableTime.tm_mday,
readableTime.tm_hour, readableTime.tm_min, readableTime.tm_sec);
#endif
}
time_t TimeUtility::ConvertTimerToTime_t(std::string dateTime)
{
time_t result = 0;
std::replace(dateTime.begin(), dateTime.end(), '-', ' ');
std::replace(dateTime.begin(), dateTime.end(), 'T', ' ');
std::replace(dateTime.begin(), dateTime.end(), ':', ' ');
std::istringstream is(dateTime);
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int seconds = 0;
is >> year >> month >> day >> hour >> minute >> seconds;
tm t = { 0 };
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = day;
t.tm_hour = hour;
t.tm_min = minute;
t.tm_sec = seconds;
#if defined(_WIN32) || defined(_WIN64)
result = _mkgmtime64(&t) * 1000;
#elif defined(__linux__)
result = timegm(&t) * 1000;
#endif
return result;
}
std::string TimeUtility::formatA(const char* fmt, ...)
{
std::string strResult;
const int maxLen = 1024;
if (fmt)
{
char *temp = new(std::nothrow) char[maxLen]();
va_list argList;
va_start(argList, fmt);
#if defined(_WIN32) || defined(_WIN64)
if(vsnprintf_s(temp, maxLen, maxLen, fmt, argList) > 0)
#elif defined(__linux__)
if(vsnprintf(temp, maxLen, fmt, argList) > 0)
#endif
strResult = temp;
delete [] temp;
va_end(argList);
}
return strResult;
}
int TimeUtility::GetTimezone()
{
static int zone = -13;
struct tm *pt;
time_t now, tmp;
if (zone == -13)
{
/* 返回UTC时骸间 */
now = time(NULL);
/* 解析UTC时骸间 */
pt = gmtime(&now);
if (pt)
{
/* 构造本时区的UTC时间 */
tmp = mktime(pt);
zone = (now - tmp) / 3600;
}
else
zone = 8;
}
return zone;
}
time_t TimeUtility::StringUTCToTime_t(const std::string& strUTCTime)
{
struct tm tTp = { 0 };
int nYear, nMon, nDay, nHour, nMin, nSec;
sscanf(strUTCTime.c_str(), "%d-%d-%dT%d:%d:%d", &nYear, &nMon, &nDay, &nHour, &nMin, &nSec);
tTp.tm_year = nYear - 1900;
tTp.tm_mon = nMon - 1;
tTp.tm_mday = nDay;
tTp.tm_hour = nHour + GetTimezone();
tTp.tm_min = nMin;
tTp.tm_sec = nSec;
return mktime(&tTp);
}
time_t TimeUtility::StringLocalToTime_t(const std::string& strLocalTime)
{
struct tm tTp = { 0 };
int nYear, nMon, nDay, nHour, nMin, nSec;
sscanf(strLocalTime.c_str(), "%d-%d-%dT%d:%d:%d", &nYear, &nMon, &nDay, &nHour, &nMin, &nSec);
tTp.tm_year = nYear - 1900;
tTp.tm_mon = nMon - 1;
tTp.tm_mday = nDay;
tTp.tm_hour = nHour;// +GetTimezone();
tTp.tm_min = nMin;
tTp.tm_sec = nSec;
return mktime(&tTp);
}
std::string TimeUtility::StringUTCToLocal(std::string UTCTime)
{
time_t bg = StringUTCToTime_t(UTCTime);
struct tm* pt = localtime(&bg);
std::string strLocalTime = formatA("%04d-%02d-%02dT%02d:%02d:%02d", pt->tm_year + 1900, pt->tm_mon + 1, pt->tm_mday,
pt->tm_hour, pt->tm_min, pt->tm_sec);
return strLocalTime;
}
std::string TimeUtility::StringLocalToUTC(std::string localTime)
{
time_t bg = StringLocalToTime_t(localTime);
struct tm* pt = gmtime(&bg);
std::string strUTCTime = formatA("%04d-%02d-%02dT%02d:%02d:%02d", pt->tm_year + 1900, pt->tm_mon + 1, pt->tm_mday,
pt->tm_hour, pt->tm_min, pt->tm_sec);
return strUTCTime;
}
std::string TimeUtility::StringTimeAddSecond(const std::string& dateTime, const char ops, time_t second)
{
time_t sec = StringUTCToTime_t(dateTime);
if (ops == '+')
sec += second;
else if (ops == '-')
sec -= second;
return ConvertTime_tToTimeString(sec);
}
}
}