util: Use std::chrono for time getters

This commit is contained in:
MarcoFalke 2020-02-21 13:12:40 -05:00 committed by fanquake
parent 3c2e16be22
commit 9266f7497f
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1

View file

@ -90,6 +90,14 @@ template std::chrono::seconds GetTime();
template std::chrono::milliseconds GetTime(); template std::chrono::milliseconds GetTime();
template std::chrono::microseconds GetTime(); template std::chrono::microseconds GetTime();
template <typename T>
static T GetSystemTime()
{
const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
assert(now.count() > 0);
return now;
}
void SetMockTime(int64_t nMockTimeIn) void SetMockTime(int64_t nMockTimeIn)
{ {
Assert(nMockTimeIn >= 0); Assert(nMockTimeIn >= 0);
@ -103,23 +111,17 @@ int64_t GetMockTime()
int64_t GetTimeMillis() int64_t GetTimeMillis()
{ {
int64_t now = (boost::posix_time::microsec_clock::universal_time() - return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
assert(now > 0);
return now;
} }
int64_t GetTimeMicros() int64_t GetTimeMicros()
{ {
int64_t now = (boost::posix_time::microsec_clock::universal_time() - return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
assert(now > 0);
return now;
} }
int64_t GetSystemTimeInSeconds() int64_t GetSystemTimeInSeconds()
{ {
return GetTimeMicros()/1000000; return int64_t{GetSystemTime<std::chrono::seconds>().count()};
} }
std::string FormatISO8601DateTime(int64_t nTime) { std::string FormatISO8601DateTime(int64_t nTime) {