diff --git a/Source/Core/Common/Timer.cpp b/Source/Core/Common/Timer.cpp index 9ac3645841..ef910f6c27 100644 --- a/Source/Core/Common/Timer.cpp +++ b/Source/Core/Common/Timer.cpp @@ -4,23 +4,16 @@ #include "Common/Timer.h" #include -#include #ifdef _WIN32 -#include - #include -#include -#include +#include +#include #else #include #endif -#include -#include - #include "Common/CommonTypes.h" -#include "Common/StringUtil.h" namespace Common { @@ -106,38 +99,6 @@ u64 Timer::GetLocalTimeSinceJan1970() return static_cast(sysTime + tzDiff + tzDST); } -double Timer::GetSystemTimeAsDouble() -{ - // FYI: std::chrono::system_clock epoch is not required to be 1970 until c++20. - // We will however assume time_t IS unix time. - using Clock = std::chrono::system_clock; - - // TODO: Use this on switch to c++20: - // const auto since_epoch = Clock::now().time_since_epoch(); - const auto unix_epoch = Clock::from_time_t({}); - const auto since_epoch = Clock::now() - unix_epoch; - - const auto since_double_time_epoch = since_epoch - std::chrono::seconds(DOUBLE_TIME_OFFSET); - return std::chrono::duration_cast>(since_double_time_epoch).count(); -} - -std::string Timer::SystemTimeAsDoubleToString(double time) -{ - // revert adjustments from GetSystemTimeAsDouble() to get a normal Unix timestamp again - time_t seconds = (time_t)time + DOUBLE_TIME_OFFSET; - tm* localTime = localtime(&seconds); - -#ifdef _WIN32 - wchar_t tmp[32] = {}; - wcsftime(tmp, std::size(tmp), L"%x %X", localTime); - return WStringToUTF8(tmp); -#else - char tmp[32] = {}; - strftime(tmp, sizeof(tmp), "%x %X", localTime); - return tmp; -#endif -} - void Timer::IncreaseResolution() { #ifdef _WIN32 diff --git a/Source/Core/Common/Timer.h b/Source/Core/Common/Timer.h index 160f3a23c9..0d357aff2b 100644 --- a/Source/Core/Common/Timer.h +++ b/Source/Core/Common/Timer.h @@ -3,7 +3,6 @@ #pragma once -#include #include "Common/CommonTypes.h" namespace Common @@ -24,19 +23,9 @@ public: static u64 GetLocalTimeSinceJan1970(); - // Returns biased system timestamp as double - // It is very unlikely you want to use this in new code; ideally we can remove it completely. - static double GetSystemTimeAsDouble(); - // Formats a timestamp from GetSystemTimeAsDouble() into a date and time string - static std::string SystemTimeAsDoubleToString(double time); - static void IncreaseResolution(); static void RestoreResolution(); - // Arbitrarily chosen value (38 years) that is subtracted in GetSystemTimeAsDouble() - // to increase sub-second precision of the resulting double timestamp - static constexpr int DOUBLE_TIME_OFFSET = (38 * 365 * 24 * 60 * 60); - private: u64 m_start_ms{0}; u64 m_end_ms{0}; diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index 76a3adc6ad..ddc0f9dfc2 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -270,6 +270,42 @@ static int GetEmptySlot(std::map m) return -1; } +// Arbitrarily chosen value (38 years) that is subtracted in GetSystemTimeAsDouble() +// to increase sub-second precision of the resulting double timestamp +static constexpr int DOUBLE_TIME_OFFSET = (38 * 365 * 24 * 60 * 60); + +static double GetSystemTimeAsDouble() +{ + // FYI: std::chrono::system_clock epoch is not required to be 1970 until c++20. + // We will however assume time_t IS unix time. + using Clock = std::chrono::system_clock; + + // TODO: Use this on switch to c++20: + // const auto since_epoch = Clock::now().time_since_epoch(); + const auto unix_epoch = Clock::from_time_t({}); + const auto since_epoch = Clock::now() - unix_epoch; + + const auto since_double_time_epoch = since_epoch - std::chrono::seconds(DOUBLE_TIME_OFFSET); + return std::chrono::duration_cast>(since_double_time_epoch).count(); +} + +static std::string SystemTimeAsDoubleToString(double time) +{ + // revert adjustments from GetSystemTimeAsDouble() to get a normal Unix timestamp again + time_t seconds = (time_t)time + DOUBLE_TIME_OFFSET; + tm* localTime = localtime(&seconds); + +#ifdef _WIN32 + wchar_t tmp[32] = {}; + wcsftime(tmp, std::size(tmp), L"%x %X", localTime); + return WStringToUTF8(tmp); +#else + char tmp[32] = {}; + strftime(tmp, sizeof(tmp), "%x %X", localTime); + return tmp; +#endif +} + static std::string MakeStateFilename(int number); // read state timestamps @@ -284,7 +320,7 @@ static std::map GetSavedStates() { if (ReadHeader(filename, header)) { - double d = Common::Timer::GetSystemTimeAsDouble() - header.time; + double d = GetSystemTimeAsDouble() - header.time; // increase time until unique value is obtained while (m.find(d) != m.end()) @@ -359,7 +395,7 @@ static void CompressAndDumpState(CompressAndDumpState_args save_args) StateHeader header{}; SConfig::GetInstance().GetGameID().copy(header.gameID, std::size(header.gameID)); header.size = s_use_compression ? (u32)buffer_size : 0; - header.time = Common::Timer::GetSystemTimeAsDouble(); + header.time = GetSystemTimeAsDouble(); f.WriteArray(&header, 1); @@ -471,7 +507,7 @@ std::string GetInfoStringOfSlot(int slot, bool translate) if (!ReadHeader(filename, header)) return translate ? Common::GetStringT("Unknown") : "Unknown"; - return Common::Timer::SystemTimeAsDoubleToString(header.time); + return SystemTimeAsDoubleToString(header.time); } u64 GetUnixTimeOfSlot(int slot) @@ -481,8 +517,7 @@ u64 GetUnixTimeOfSlot(int slot) return 0; constexpr u64 MS_PER_SEC = 1000; - return static_cast(header.time * MS_PER_SEC) + - (Common::Timer::DOUBLE_TIME_OFFSET * MS_PER_SEC); + return static_cast(header.time * MS_PER_SEC) + (DOUBLE_TIME_OFFSET * MS_PER_SEC); } static void LoadFileStateData(const std::string& filename, std::vector& ret_data)