timer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <ctime>
  5. #include <fmt/format.h>
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. // windows.h needs to be included before other windows headers
  9. #include <mmsystem.h>
  10. #include <sys/timeb.h>
  11. #else
  12. #include <sys/time.h>
  13. #endif
  14. #include "common/common_types.h"
  15. #include "common/string_util.h"
  16. #include "common/timer.h"
  17. namespace Common {
  18. u32 Timer::GetTimeMs() {
  19. #ifdef _WIN32
  20. return timeGetTime();
  21. #else
  22. struct timeval t;
  23. (void)gettimeofday(&t, nullptr);
  24. return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000));
  25. #endif
  26. }
  27. // --------------------------------------------
  28. // Initiate, Start, Stop, and Update the time
  29. // --------------------------------------------
  30. // Set initial values for the class
  31. Timer::Timer() : m_LastTime(0), m_StartTime(0), m_Running(false) {
  32. Update();
  33. }
  34. // Write the starting time
  35. void Timer::Start() {
  36. m_StartTime = GetTimeMs();
  37. m_Running = true;
  38. }
  39. // Stop the timer
  40. void Timer::Stop() {
  41. // Write the final time
  42. m_LastTime = GetTimeMs();
  43. m_Running = false;
  44. }
  45. // Update the last time variable
  46. void Timer::Update() {
  47. m_LastTime = GetTimeMs();
  48. // TODO(ector) - QPF
  49. }
  50. // -------------------------------------
  51. // Get time difference and elapsed time
  52. // -------------------------------------
  53. // Get the number of milliseconds since the last Update()
  54. u64 Timer::GetTimeDifference() {
  55. return GetTimeMs() - m_LastTime;
  56. }
  57. // Add the time difference since the last Update() to the starting time.
  58. // This is used to compensate for a paused game.
  59. void Timer::AddTimeDifference() {
  60. m_StartTime += GetTimeDifference();
  61. }
  62. // Get the time elapsed since the Start()
  63. u64 Timer::GetTimeElapsed() {
  64. // If we have not started yet, return 1 (because then I don't
  65. // have to change the FPS calculation in CoreRerecording.cpp .
  66. if (m_StartTime == 0)
  67. return 1;
  68. // Return the final timer time if the timer is stopped
  69. if (!m_Running)
  70. return (m_LastTime - m_StartTime);
  71. return (GetTimeMs() - m_StartTime);
  72. }
  73. // Get the formatted time elapsed since the Start()
  74. std::string Timer::GetTimeElapsedFormatted() const {
  75. // If we have not started yet, return zero
  76. if (m_StartTime == 0)
  77. return "00:00:00:000";
  78. // The number of milliseconds since the start.
  79. // Use a different value if the timer is stopped.
  80. u64 Milliseconds;
  81. if (m_Running)
  82. Milliseconds = GetTimeMs() - m_StartTime;
  83. else
  84. Milliseconds = m_LastTime - m_StartTime;
  85. // Seconds
  86. u32 Seconds = (u32)(Milliseconds / 1000);
  87. // Minutes
  88. u32 Minutes = Seconds / 60;
  89. // Hours
  90. u32 Hours = Minutes / 60;
  91. std::string TmpStr = fmt::format("{:02}:{:02}:{:02}:{:03}", Hours, Minutes % 60, Seconds % 60,
  92. Milliseconds % 1000);
  93. return TmpStr;
  94. }
  95. // Get current time
  96. void Timer::IncreaseResolution() {
  97. #ifdef _WIN32
  98. timeBeginPeriod(1);
  99. #endif
  100. }
  101. void Timer::RestoreResolution() {
  102. #ifdef _WIN32
  103. timeEndPeriod(1);
  104. #endif
  105. }
  106. // Get the number of seconds since January 1 1970
  107. u64 Timer::GetTimeSinceJan1970() {
  108. time_t ltime;
  109. time(&ltime);
  110. return ((u64)ltime);
  111. }
  112. u64 Timer::GetLocalTimeSinceJan1970() {
  113. time_t sysTime, tzDiff, tzDST;
  114. struct tm* gmTime;
  115. time(&sysTime);
  116. // Account for DST where needed
  117. gmTime = localtime(&sysTime);
  118. if (gmTime->tm_isdst == 1)
  119. tzDST = 3600;
  120. else
  121. tzDST = 0;
  122. // Lazy way to get local time in sec
  123. gmTime = gmtime(&sysTime);
  124. tzDiff = sysTime - mktime(gmTime);
  125. return (u64)(sysTime + tzDiff + tzDST);
  126. }
  127. // Return the current time formatted as Minutes:Seconds:Milliseconds
  128. // in the form 00:00:000.
  129. std::string Timer::GetTimeFormatted() {
  130. time_t sysTime;
  131. struct tm* gmTime;
  132. char tmp[13];
  133. time(&sysTime);
  134. gmTime = localtime(&sysTime);
  135. strftime(tmp, 6, "%M:%S", gmTime);
  136. // Now tack on the milliseconds
  137. #ifdef _WIN32
  138. struct timeb tp;
  139. (void)::ftime(&tp);
  140. return fmt::format("{}:{:03}", tmp, tp.millitm);
  141. #else
  142. struct timeval t;
  143. (void)gettimeofday(&t, nullptr);
  144. return fmt::format("{}:{:03}", tmp, static_cast<int>(t.tv_usec / 1000));
  145. #endif
  146. }
  147. // Returns a timestamp with decimals for precise time comparisons
  148. // ----------------
  149. double Timer::GetDoubleTime() {
  150. #ifdef _WIN32
  151. struct timeb tp;
  152. (void)::ftime(&tp);
  153. #else
  154. struct timeval t;
  155. (void)gettimeofday(&t, nullptr);
  156. #endif
  157. // Get continuous timestamp
  158. u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970();
  159. // Remove a few years. We only really want enough seconds to make
  160. // sure that we are detecting actual actions, perhaps 60 seconds is
  161. // enough really, but I leave a year of seconds anyway, in case the
  162. // user's clock is incorrect or something like that.
  163. TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60);
  164. // Make a smaller integer that fits in the double
  165. u32 Seconds = (u32)TmpSeconds;
  166. #ifdef _WIN32
  167. double ms = tp.millitm / 1000.0 / 1000.0;
  168. #else
  169. double ms = t.tv_usec / 1000000.0;
  170. #endif
  171. double TmpTime = Seconds + ms;
  172. return TmpTime;
  173. }
  174. } // Namespace Common