timer.cpp 5.0 KB

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