GrabBag/VrUtils/Src/VrDateUtils.cpp

73 lines
2.2 KiB
C++
Raw Normal View History

2025-07-23 01:35:14 +08:00
#include "VrDateUtils.h"
/**
*
*/
std::string CVrDateUtils::GetNowTime()
{
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
#ifdef _WIN32
struct tm ptminfo;
localtime_s(&ptminfo, &tt);
#else
struct tm ptminfo = *(localtime(&tt));
#endif
//printf("current: %02d-%02d-%02d %02d:%02d:%02d\n",
// ptminfo.tm_year + 1900, ptminfo.tm_mon + 1, ptminfo.tm_mday,
// ptminfo.tm_hour, ptminfo.tm_min, ptminfo.tm_sec);
char date[60] = { 0 };
#ifdef _WIN32
sprintf_s(date, "%02d%02d%02d%02d%02d%02d", ptminfo.tm_year + 1900, \
ptminfo.tm_mon + 1, ptminfo.tm_mday, ptminfo.tm_hour, ptminfo.tm_min, ptminfo.tm_sec);
#else
sprintf(date, "%02d%02d%02d%02d%02d%02d", ptminfo.tm_year + 1900, \
ptminfo.tm_mon + 1, ptminfo.tm_mday, ptminfo.tm_hour, ptminfo.tm_min, ptminfo.tm_sec);
#endif
return std::string(date);
}
2025-10-08 21:45:37 +08:00
/**
* :-- ::
*/
std::string CVrDateUtils::GetStrNowTime(bool bYear)
{
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
#ifdef _WIN32
struct tm ptminfo;
localtime_s(&ptminfo, &tt);
#else
struct tm ptminfo = *(localtime(&tt));
#endif
char date[60] = { 0 };
#ifdef _WIN32
if (bYear)
{
sprintf_s(date, "%04d-%02d-%02d %02d:%02d:%02d", ptminfo.tm_year + 1900, \
ptminfo.tm_mon + 1, ptminfo.tm_mday, ptminfo.tm_hour, ptminfo.tm_min, ptminfo.tm_sec);
}
else
{
sprintf_s(date, "%02d-%02d %02d:%02d:%02d", ptminfo.tm_mon + 1, ptminfo.tm_mday, ptminfo.tm_hour, ptminfo.tm_min, ptminfo.tm_sec);
}
#else
if (bYear)
{
sprintf(date, "%04d-%02d-%02d %02d:%02d:%02d", ptminfo.tm_year + 1900, \
ptminfo.tm_mon + 1, ptminfo.tm_mday, ptminfo.tm_hour, ptminfo.tm_min, ptminfo.tm_sec);
}
else
{
sprintf(date, "%02d-%02d %02d:%02d:%02d", ptminfo.tm_mon + 1, ptminfo.tm_mday, ptminfo.tm_hour, ptminfo.tm_min, ptminfo.tm_sec);
}
#endif
return std::string(date);
}
/**
*
*/
unsigned long long CVrDateUtils::GetTimestamp()
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}