Time::now() supports cross-platform
This commit is contained in:
parent
3fd3fbbbd8
commit
41671fa1f3
|
|
@ -23,6 +23,7 @@
|
||||||
#include <kiwano/core/Time.h>
|
#include <kiwano/core/Time.h>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
{
|
{
|
||||||
|
|
@ -69,19 +70,32 @@ const Duration Time::operator-(const Time& other) const
|
||||||
|
|
||||||
Time Time::Now() noexcept
|
Time Time::Now() noexcept
|
||||||
{
|
{
|
||||||
static LARGE_INTEGER freq = {};
|
#if defined(KGE_PLATFORM_WINDOWS)
|
||||||
if (freq.QuadPart == 0LL)
|
|
||||||
|
static double millisecs_per_count = {};
|
||||||
|
if (millisecs_per_count == 0)
|
||||||
{
|
{
|
||||||
|
LARGE_INTEGER freq = {};
|
||||||
// the Function will always succceed on systems that run Windows XP or later
|
// the Function will always succceed on systems that run Windows XP or later
|
||||||
QueryPerformanceFrequency(&freq);
|
QueryPerformanceFrequency(&freq);
|
||||||
|
millisecs_per_count = 1000.0 / static_cast<double>(freq.QuadPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
LARGE_INTEGER count;
|
LARGE_INTEGER count;
|
||||||
QueryPerformanceCounter(&count);
|
QueryPerformanceCounter(&count);
|
||||||
|
return Time{ static_cast<long>(count.QuadPart * millisecs_per_count) };
|
||||||
|
|
||||||
const long long whole = (count.QuadPart / freq.QuadPart) * 1000LL;
|
#else
|
||||||
const long long part = (count.QuadPart % freq.QuadPart) * 1000LL / freq.QuadPart;
|
|
||||||
return Time{ static_cast<long>(whole + part) };
|
using std::chrono::steady_clock;
|
||||||
|
using std::chrono::duration_cast;
|
||||||
|
using std::chrono::milliseconds;
|
||||||
|
|
||||||
|
const auto now = steady_clock::now();
|
||||||
|
const long long count = duration_cast<milliseconds>(now.time_since_epoch()).count();
|
||||||
|
return Time{ static_cast<long>(count) };
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue