timer.cpp 5.1 KB

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