timer.cpp 5.1 KB

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