Files
dl_old/dl/system/dl_time.cpp
T
2023-12-23 16:39:54 +08:00

106 lines
2.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);
}
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, StringFormat sf /*= StringFormat::SF_HH_MM_SS*/)
{
tm* tt = localtime(&t);
switch (sf)
{
case StringFormat::YYYY_MM_DD:
return fmt::format("{:0>4}-{:0>2}-{:0>2}",
tt->tm_year + 1900, tt->tm_mon + 1, tt->tm_mday);
case StringFormat::YYYY_MM_DD_HH_MM_SS:
return fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}",
tt->tm_year + 1900, tt->tm_mon + 1, tt->tm_mday,
tt->tm_hour, tt->tm_min, tt->tm_sec);
case StringFormat::YYYY_MM_DD_HH_MM:
return fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}",
tt->tm_year + 1900, tt->tm_mon + 1, tt->tm_mday,
tt->tm_hour, tt->tm_min);
case StringFormat::HH_MM_SS:
default:
return fmt::format("{:0>2}:{:0>2}:{:0>2}", tt->tm_hour, tt->tm_min, tt->tm_sec);
}
}
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::_cacl_expect()
{
if (_fpsExpect)
_deltaExpect = 1_ft / _fpsExpect;
else
_deltaExpect = 0;
_deltaExpectNS = (std::chrono::microseconds)(int)(_deltaExpect * 1000000);
}
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)
std::this_thread::sleep_for(st - 1000us);
while (t_expect - high_resolution_clock::now() > 50us)
{
}
}
}
}