diff options
Diffstat (limited to 'src/common/timer.cpp')
| -rw-r--r-- | src/common/timer.cpp | 95 |
1 files changed, 23 insertions, 72 deletions
diff --git a/src/common/timer.cpp b/src/common/timer.cpp index f0c5b1a43..2dc15e434 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp | |||
| @@ -3,31 +3,16 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <ctime> | 5 | #include <ctime> |
| 6 | |||
| 7 | #include <fmt/format.h> | 6 | #include <fmt/format.h> |
| 8 | |||
| 9 | #ifdef _WIN32 | ||
| 10 | #include <windows.h> | ||
| 11 | // windows.h needs to be included before other windows headers | ||
| 12 | #include <mmsystem.h> | ||
| 13 | #include <sys/timeb.h> | ||
| 14 | #else | ||
| 15 | #include <sys/time.h> | ||
| 16 | #endif | ||
| 17 | #include "common/common_types.h" | 7 | #include "common/common_types.h" |
| 18 | #include "common/string_util.h" | 8 | #include "common/string_util.h" |
| 19 | #include "common/timer.h" | 9 | #include "common/timer.h" |
| 20 | 10 | ||
| 21 | namespace Common { | 11 | namespace Common { |
| 22 | 12 | ||
| 23 | u32 Timer::GetTimeMs() { | 13 | std::chrono::milliseconds Timer::GetTimeMs() { |
| 24 | #ifdef _WIN32 | 14 | return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 25 | return timeGetTime(); | 15 | std::chrono::system_clock::now().time_since_epoch()); |
| 26 | #else | ||
| 27 | struct timeval t; | ||
| 28 | (void)gettimeofday(&t, nullptr); | ||
| 29 | return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000)); | ||
| 30 | #endif | ||
| 31 | } | 16 | } |
| 32 | 17 | ||
| 33 | // -------------------------------------------- | 18 | // -------------------------------------------- |
| @@ -63,7 +48,7 @@ void Timer::Update() { | |||
| 63 | // ------------------------------------- | 48 | // ------------------------------------- |
| 64 | 49 | ||
| 65 | // Get the number of milliseconds since the last Update() | 50 | // Get the number of milliseconds since the last Update() |
| 66 | u64 Timer::GetTimeDifference() { | 51 | std::chrono::milliseconds Timer::GetTimeDifference() { |
| 67 | return GetTimeMs() - m_LastTime; | 52 | return GetTimeMs() - m_LastTime; |
| 68 | } | 53 | } |
| 69 | 54 | ||
| @@ -74,11 +59,11 @@ void Timer::AddTimeDifference() { | |||
| 74 | } | 59 | } |
| 75 | 60 | ||
| 76 | // Get the time elapsed since the Start() | 61 | // Get the time elapsed since the Start() |
| 77 | u64 Timer::GetTimeElapsed() { | 62 | std::chrono::milliseconds Timer::GetTimeElapsed() { |
| 78 | // If we have not started yet, return 1 (because then I don't | 63 | // If we have not started yet, return 1 (because then I don't |
| 79 | // have to change the FPS calculation in CoreRerecording.cpp . | 64 | // have to change the FPS calculation in CoreRerecording.cpp . |
| 80 | if (m_StartTime == 0) | 65 | if (m_StartTime.count() == 0) |
| 81 | return 1; | 66 | return std::chrono::milliseconds(1); |
| 82 | 67 | ||
| 83 | // Return the final timer time if the timer is stopped | 68 | // Return the final timer time if the timer is stopped |
| 84 | if (!m_Running) | 69 | if (!m_Running) |
| @@ -90,49 +75,34 @@ u64 Timer::GetTimeElapsed() { | |||
| 90 | // Get the formatted time elapsed since the Start() | 75 | // Get the formatted time elapsed since the Start() |
| 91 | std::string Timer::GetTimeElapsedFormatted() const { | 76 | std::string Timer::GetTimeElapsedFormatted() const { |
| 92 | // If we have not started yet, return zero | 77 | // If we have not started yet, return zero |
| 93 | if (m_StartTime == 0) | 78 | if (m_StartTime.count() == 0) |
| 94 | return "00:00:00:000"; | 79 | return "00:00:00:000"; |
| 95 | 80 | ||
| 96 | // The number of milliseconds since the start. | 81 | // The number of milliseconds since the start. |
| 97 | // Use a different value if the timer is stopped. | 82 | // Use a different value if the timer is stopped. |
| 98 | u64 Milliseconds; | 83 | std::chrono::milliseconds Milliseconds; |
| 99 | if (m_Running) | 84 | if (m_Running) |
| 100 | Milliseconds = GetTimeMs() - m_StartTime; | 85 | Milliseconds = GetTimeMs() - m_StartTime; |
| 101 | else | 86 | else |
| 102 | Milliseconds = m_LastTime - m_StartTime; | 87 | Milliseconds = m_LastTime - m_StartTime; |
| 103 | // Seconds | 88 | // Seconds |
| 104 | u32 Seconds = (u32)(Milliseconds / 1000); | 89 | std::chrono::seconds Seconds = std::chrono::duration_cast<std::chrono::seconds>(Milliseconds); |
| 105 | // Minutes | 90 | // Minutes |
| 106 | u32 Minutes = Seconds / 60; | 91 | std::chrono::minutes Minutes = std::chrono::duration_cast<std::chrono::minutes>(Milliseconds); |
| 107 | // Hours | 92 | // Hours |
| 108 | u32 Hours = Minutes / 60; | 93 | std::chrono::hours Hours = std::chrono::duration_cast<std::chrono::hours>(Milliseconds); |
| 109 | 94 | ||
| 110 | std::string TmpStr = fmt::format("{:02}:{:02}:{:02}:{:03}", Hours, Minutes % 60, Seconds % 60, | 95 | std::string TmpStr = fmt::format("{:02}:{:02}:{:02}:{:03}", Hours.count(), Minutes.count() % 60, |
| 111 | Milliseconds % 1000); | 96 | Seconds.count() % 60, Milliseconds.count() % 1000); |
| 112 | return TmpStr; | 97 | return TmpStr; |
| 113 | } | 98 | } |
| 114 | 99 | ||
| 115 | // Get current time | ||
| 116 | void Timer::IncreaseResolution() { | ||
| 117 | #ifdef _WIN32 | ||
| 118 | timeBeginPeriod(1); | ||
| 119 | #endif | ||
| 120 | } | ||
| 121 | |||
| 122 | void Timer::RestoreResolution() { | ||
| 123 | #ifdef _WIN32 | ||
| 124 | timeEndPeriod(1); | ||
| 125 | #endif | ||
| 126 | } | ||
| 127 | |||
| 128 | // Get the number of seconds since January 1 1970 | 100 | // Get the number of seconds since January 1 1970 |
| 129 | u64 Timer::GetTimeSinceJan1970() { | 101 | std::chrono::seconds Timer::GetTimeSinceJan1970() { |
| 130 | time_t ltime; | 102 | return std::chrono::duration_cast<std::chrono::seconds>(GetTimeMs()); |
| 131 | time(<ime); | ||
| 132 | return ((u64)ltime); | ||
| 133 | } | 103 | } |
| 134 | 104 | ||
| 135 | u64 Timer::GetLocalTimeSinceJan1970() { | 105 | std::chrono::seconds Timer::GetLocalTimeSinceJan1970() { |
| 136 | time_t sysTime, tzDiff, tzDST; | 106 | time_t sysTime, tzDiff, tzDST; |
| 137 | struct tm* gmTime; | 107 | struct tm* gmTime; |
| 138 | 108 | ||
| @@ -149,7 +119,7 @@ u64 Timer::GetLocalTimeSinceJan1970() { | |||
| 149 | gmTime = gmtime(&sysTime); | 119 | gmTime = gmtime(&sysTime); |
| 150 | tzDiff = sysTime - mktime(gmTime); | 120 | tzDiff = sysTime - mktime(gmTime); |
| 151 | 121 | ||
| 152 | return (u64)(sysTime + tzDiff + tzDST); | 122 | return std::chrono::seconds(sysTime + tzDiff + tzDST); |
| 153 | } | 123 | } |
| 154 | 124 | ||
| 155 | // Return the current time formatted as Minutes:Seconds:Milliseconds | 125 | // Return the current time formatted as Minutes:Seconds:Milliseconds |
| @@ -164,30 +134,16 @@ std::string Timer::GetTimeFormatted() { | |||
| 164 | 134 | ||
| 165 | strftime(tmp, 6, "%M:%S", gmTime); | 135 | strftime(tmp, 6, "%M:%S", gmTime); |
| 166 | 136 | ||
| 167 | // Now tack on the milliseconds | 137 | u64 milliseconds = static_cast<u64>(GetTimeMs().count()) % 1000; |
| 168 | #ifdef _WIN32 | 138 | return fmt::format("{}:{:03}", tmp, milliseconds); |
| 169 | struct timeb tp; | ||
| 170 | (void)::ftime(&tp); | ||
| 171 | return fmt::format("{}:{:03}", tmp, tp.millitm); | ||
| 172 | #else | ||
| 173 | struct timeval t; | ||
| 174 | (void)gettimeofday(&t, nullptr); | ||
| 175 | return fmt::format("{}:{:03}", tmp, static_cast<int>(t.tv_usec / 1000)); | ||
| 176 | #endif | ||
| 177 | } | 139 | } |
| 178 | 140 | ||
| 179 | // Returns a timestamp with decimals for precise time comparisons | 141 | // Returns a timestamp with decimals for precise time comparisons |
| 180 | // ---------------- | 142 | // ---------------- |
| 181 | double Timer::GetDoubleTime() { | 143 | double Timer::GetDoubleTime() { |
| 182 | #ifdef _WIN32 | ||
| 183 | struct timeb tp; | ||
| 184 | (void)::ftime(&tp); | ||
| 185 | #else | ||
| 186 | struct timeval t; | ||
| 187 | (void)gettimeofday(&t, nullptr); | ||
| 188 | #endif | ||
| 189 | // Get continuous timestamp | 144 | // Get continuous timestamp |
| 190 | u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970(); | 145 | u64 TmpSeconds = static_cast<u64>(Common::Timer::GetTimeSinceJan1970().count()); |
| 146 | double ms = static_cast<u64>(GetTimeMs().count()) % 1000; | ||
| 191 | 147 | ||
| 192 | // Remove a few years. We only really want enough seconds to make | 148 | // Remove a few years. We only really want enough seconds to make |
| 193 | // sure that we are detecting actual actions, perhaps 60 seconds is | 149 | // sure that we are detecting actual actions, perhaps 60 seconds is |
| @@ -196,12 +152,7 @@ double Timer::GetDoubleTime() { | |||
| 196 | TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60); | 152 | TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60); |
| 197 | 153 | ||
| 198 | // Make a smaller integer that fits in the double | 154 | // Make a smaller integer that fits in the double |
| 199 | u32 Seconds = (u32)TmpSeconds; | 155 | u32 Seconds = static_cast<u32>(TmpSeconds); |
| 200 | #ifdef _WIN32 | ||
| 201 | double ms = tp.millitm / 1000.0 / 1000.0; | ||
| 202 | #else | ||
| 203 | double ms = t.tv_usec / 1000000.0; | ||
| 204 | #endif | ||
| 205 | double TmpTime = Seconds + ms; | 156 | double TmpTime = Seconds + ms; |
| 206 | 157 | ||
| 207 | return TmpTime; | 158 | return TmpTime; |