Files
2026-02-11 18:54:58 +08:00

161 lines
3.3 KiB
C++

#include "dl_time.h"
#include <thread>
#include <fmt/format.h>
#include "io/dl_log.h"
namespace dl
{
Time* g_time = nullptr;
time_t Time::GetTimeStamp()
{
using std::chrono::system_clock;
system_clock::time_point today = system_clock::now();
return system_clock::to_time_t(today);
}
time_t Time::GetTimeStampMs()
{
using namespace std::chrono;
auto current = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(current.time_since_epoch());
return duration.count();
}
std::string Time::TimeStampDelta2String(time_t t)
{
time_t h = t / 3600;
time_t m = (t % 3600) / 60;
time_t s = (t % 3600) % 60;
return fmt::format("{:0>2}:{:0>2}:{:0>2}", (int)h, (int)m, (int)s);
}
std::string Time::TimeStamp2String(time_t t, Type type,
std::string_view year, std::string_view month, std::string_view day,
std::string_view hour, std::string_view minute, std::string_view second)
{
tm* tt = localtime(&t);
std::string ret;
if (type & ET_YEAR)
{
ret += fmt::format("{:0>4}", tt->tm_year + 1900);
ret += year;
}
if (type & ET_MONTH)
{
ret += fmt::format("{:0>2}", tt->tm_mon + 1);
ret += month;
}
if (type & ET_DAY)
{
ret += fmt::format("{:0>2}", tt->tm_mday + 0);
ret += day;
}
if (type & ET_HOUR)
{
ret += fmt::format("{:0>2}", tt->tm_hour + 0);
ret += hour;
}
if (type & ET_MINUTE)
{
ret += fmt::format("{:0>2}", tt->tm_min + 0);
ret += minute;
}
if (type & ET_SECOND)
{
ret += fmt::format("{:0>2}", tt->tm_sec + 0);
ret += second;
}
return ret;
}
void Time::Sleep(unsigned t)
{
std::this_thread::sleep_for(std::chrono::milliseconds(t));
}
void Time::_update()
{
auto current = std::chrono::high_resolution_clock::now();
_delta = std::chrono::duration<FloatTime, std::chrono::seconds::period>
(current - _last).count();
//防止过大
/*if (_delta > 1.0_ft)
{
log(fmt::format("帧间隔({})过大,将限制为1.0s。", _delta));
_delta = 1.0_ft;
}*/
//重置时间点
_last = current;
//fps计算
++_countFrame;
_countSec += _delta;
if (_countSec >= 1.0_ft)
{
_fps = _countFrame;
_countFrame = 0;
_countSec -= 1.0_ft;
}
}
void Time::_update_pre()
{
auto current = std::chrono::high_resolution_clock::now();
_frame._timeStamp = Time::GetTimeStamp();
_frame._timeStampMs = Time::GetTimeStampMs();
_frame._tkc = (unsigned)std::chrono::duration_cast<std::chrono::milliseconds>
(current - _start).count();
// 初始化是否算第一帧内,还是单独算一帧,有待考量
}
void Time::_cacl_expect()
{
if (_fpsExpect)
_deltaExpect = 1_ft / _fpsExpect;
else
_deltaExpect = 0;
_deltaExpectNS = (std::chrono::microseconds)(int)(_deltaExpect * 1000000);
}
#if DL_PLATFORM == DL_PLATFORM_WIN32
#include <windows.h>
#endif
void Time::_sleep()
{
using namespace std::chrono;
if (_fpsExpect == 0)
;
else
{
high_resolution_clock::time_point t_expect = _last + _deltaExpectNS;
nanoseconds st = t_expect - high_resolution_clock::now();
if (st > 1100us)
{
//#if DL_PLATFORM == DL_PLATFORM_WIN32
// // 静态链接运行库(MT)时,sleep_for链接不上(已解决,vs版本问题)
// DWORD s = (st - 1000us).count() / 1000000ll;
// Sleep(s);
//#else
std::this_thread::sleep_for(st - 1000us);
//#endif
}
while (t_expect - high_resolution_clock::now() > 50us)
{
}
}
}
}