| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-12 21:08:23 +01:00
										 |  |  | #include <cinttypes>
 | 
					
						
							| 
									
										
										
										
											2014-02-19 01:54:11 +01:00
										 |  |  | #include <ctime>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 19:55:01 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | #include <mmsystem.h>
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | #include <sys/timeb.h>
 | 
					
						
							| 
									
										
										
										
											2014-02-19 01:54:11 +01:00
										 |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2010-11-20 03:24:51 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-01-21 19:55:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/StringUtil.h"
 | 
					
						
							|  |  |  | #include "Common/Timer.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 19:55:01 +00:00
										 |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | u32 Timer::GetTimeMs() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-21 20:49:45 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	return timeGetTime(); | 
					
						
							| 
									
										
										
										
											2010-11-22 02:42:53 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-11-20 03:24:51 +00:00
										 |  |  | 	struct timeval t; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	(void)gettimeofday(&t, nullptr); | 
					
						
							| 
									
										
										
										
											2010-11-20 03:24:51 +00:00
										 |  |  | 	return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000)); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // --------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | // Initiate, Start, Stop, and Update the time
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // --------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Set initial values for the class
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | Timer::Timer() | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	: m_LastTime(0), m_StartTime(0), m_Running(false) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | // Write the starting time
 | 
					
						
							|  |  |  | void Timer::Start() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-21 20:49:45 +00:00
										 |  |  | 	m_StartTime = GetTimeMs(); | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	m_Running = true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | // Stop the timer
 | 
					
						
							|  |  |  | void Timer::Stop() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Write the final time
 | 
					
						
							| 
									
										
										
										
											2010-01-21 20:49:45 +00:00
										 |  |  | 	m_LastTime = GetTimeMs(); | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	m_Running = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Update the last time variable
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | void Timer::Update() | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-21 20:49:45 +00:00
										 |  |  | 	m_LastTime = GetTimeMs(); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	//TODO(ector) - QPF
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // -------------------------------------
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | // Get time difference and elapsed time
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // -------------------------------------
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Get the number of milliseconds since the last Update()
 | 
					
						
							| 
									
										
										
										
											2010-01-07 20:01:41 +00:00
										 |  |  | u64 Timer::GetTimeDifference() | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-21 20:49:45 +00:00
										 |  |  | 	return GetTimeMs() - m_LastTime; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | // Add the time difference since the last Update() to the starting time.
 | 
					
						
							|  |  |  | // This is used to compensate for a paused game.
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | void Timer::AddTimeDifference() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_StartTime += GetTimeDifference(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | // Get the time elapsed since the Start()
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | u64 Timer::GetTimeElapsed() | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | 	// If we have not started yet, return 1 (because then I don't
 | 
					
						
							|  |  |  | 	// have to change the FPS calculation in CoreRerecording.cpp .
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	if (m_StartTime == 0) return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:21:10 +00:00
										 |  |  | 	// Return the final timer time if the timer is stopped
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	if (!m_Running) return (m_LastTime - m_StartTime); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 20:49:45 +00:00
										 |  |  | 	return (GetTimeMs() - m_StartTime); | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:21:10 +00:00
										 |  |  | // Get the formatted time elapsed since the Start()
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | std::string Timer::GetTimeElapsedFormatted() const | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	// If we have not started yet, return zero
 | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | 	if (m_StartTime == 0) | 
					
						
							|  |  |  | 		return "00:00:00:000"; | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | 	// The number of milliseconds since the start.
 | 
					
						
							|  |  |  | 	// Use a different value if the timer is stopped.
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | 	u64 Milliseconds; | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | 	if (m_Running) | 
					
						
							| 
									
										
										
										
											2010-01-21 21:27:52 +00:00
										 |  |  | 		Milliseconds = GetTimeMs() - m_StartTime; | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		Milliseconds = m_LastTime - m_StartTime; | 
					
						
							|  |  |  | 	// Seconds
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | 	u32 Seconds = (u32)(Milliseconds / 1000); | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	// Minutes
 | 
					
						
							|  |  |  | 	u32 Minutes = Seconds / 60; | 
					
						
							|  |  |  | 	// Hours
 | 
					
						
							|  |  |  | 	u32 Hours = Minutes / 60; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-12 21:08:23 +01:00
										 |  |  | 	std::string TmpStr = StringFromFormat("%02i:%02i:%02i:%03" PRIu64, | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | 		Hours, Minutes % 60, Seconds % 60, Milliseconds % 1000); | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	return TmpStr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get current time
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | void Timer::IncreaseResolution() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	timeBeginPeriod(1); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Timer::RestoreResolution() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	timeEndPeriod(1); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | // Get the number of seconds since January 1 1970
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | u64 Timer::GetTimeSinceJan1970() | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	time_t ltime; | 
					
						
							|  |  |  | 	time(<ime); | 
					
						
							|  |  |  | 	return((u64)ltime); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-13 11:57:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | u64 Timer::GetLocalTimeSinceJan1970() | 
					
						
							| 
									
										
										
										
											2008-12-13 11:57:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-08-26 19:24:47 +00:00
										 |  |  | 	time_t sysTime, tzDiff, tzDST; | 
					
						
							| 
									
										
										
										
											2008-12-13 11:57:01 +00:00
										 |  |  | 	struct tm * gmTime; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time(&sysTime); | 
					
						
							| 
									
										
										
										
											2010-08-26 19:24:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Account for DST where needed
 | 
					
						
							|  |  |  | 	gmTime = localtime(&sysTime); | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	if (gmTime->tm_isdst == 1) | 
					
						
							| 
									
										
										
										
											2010-08-26 19:24:47 +00:00
										 |  |  | 		tzDST = 3600; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		tzDST = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-13 11:57:01 +00:00
										 |  |  | 	// Lazy way to get local time in sec
 | 
					
						
							| 
									
										
										
										
											2014-02-16 23:51:41 -05:00
										 |  |  | 	gmTime = gmtime(&sysTime); | 
					
						
							| 
									
										
										
										
											2008-12-13 11:57:01 +00:00
										 |  |  | 	tzDiff = sysTime - mktime(gmTime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-26 19:24:47 +00:00
										 |  |  | 	return (u64)(sysTime + tzDiff + tzDST); | 
					
						
							| 
									
										
										
										
											2008-12-13 11:57:01 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-01-15 06:48:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | // Return the current time formatted as Minutes:Seconds:Milliseconds
 | 
					
						
							|  |  |  | // in the form 00:00:000.
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | std::string Timer::GetTimeFormatted() | 
					
						
							| 
									
										
										
										
											2009-01-15 06:48:15 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-03-15 05:34:35 +00:00
										 |  |  | 	time_t sysTime; | 
					
						
							|  |  |  | 	time(&sysTime); | 
					
						
							| 
									
										
										
										
											2014-03-29 11:05:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 14:23:34 +13:00
										 |  |  | 	struct tm * gmTime = localtime(&sysTime); | 
					
						
							| 
									
										
										
										
											2009-03-21 23:00:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 14:23:34 +13:00
										 |  |  | 	char tmp[13]; | 
					
						
							| 
									
										
										
										
											2009-03-21 23:00:25 +00:00
										 |  |  | 	strftime(tmp, 6, "%M:%S", gmTime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-15 05:34:35 +00:00
										 |  |  | 	// Now tack on the milliseconds
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	struct timeb tp; | 
					
						
							|  |  |  | 	(void)::ftime(&tp); | 
					
						
							| 
									
										
										
										
											2014-02-08 14:23:34 +13:00
										 |  |  | 	return StringFromFormat("%s:%03i", tmp, tp.millitm); | 
					
						
							| 
									
										
										
										
											2010-11-22 02:42:53 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-11-20 03:24:51 +00:00
										 |  |  | 	struct timeval t; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	(void)gettimeofday(&t, nullptr); | 
					
						
							| 
									
										
										
										
											2014-02-08 14:23:34 +13:00
										 |  |  | 	return StringFromFormat("%s:%03d", tmp, (int)(t.tv_usec / 1000)); | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-01-15 06:48:15 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 06:34:58 +00:00
										 |  |  | // Returns a timestamp with decimals for precise time comparisons
 | 
					
						
							| 
									
										
										
										
											2009-09-08 16:07:13 +00:00
										 |  |  | // ----------------
 | 
					
						
							| 
									
										
										
										
											2009-08-25 06:34:58 +00:00
										 |  |  | double Timer::GetDoubleTime() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	struct timeb tp; | 
					
						
							|  |  |  | 	(void)::ftime(&tp); | 
					
						
							| 
									
										
										
										
											2010-11-22 02:42:53 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-11-20 03:24:51 +00:00
										 |  |  | 	struct timeval t; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	(void)gettimeofday(&t, nullptr); | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:21:10 +00:00
										 |  |  | 	// Get continuous timestamp
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | 	u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970(); | 
					
						
							| 
									
										
										
										
											2009-08-25 06:34:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | 	// Remove a few years. We only really want enough seconds to make
 | 
					
						
							|  |  |  | 	// sure that we are detecting actual actions, perhaps 60 seconds is
 | 
					
						
							|  |  |  | 	// enough really, but I leave a year of seconds anyway, in case the
 | 
					
						
							|  |  |  | 	// user's clock is incorrect or something like that.
 | 
					
						
							| 
									
										
										
										
											2009-08-25 06:34:58 +00:00
										 |  |  | 	TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | 	// Make a smaller integer that fits in the double
 | 
					
						
							|  |  |  | 	u32 Seconds = (u32)TmpSeconds; | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2010-07-22 04:21:10 +00:00
										 |  |  | 	double ms = tp.millitm / 1000.0 / 1000.0; | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-11-22 02:42:53 +00:00
										 |  |  | 	double ms = t.tv_usec / 1000000.0; | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-08-25 06:34:58 +00:00
										 |  |  | 	double TmpTime = Seconds + ms; | 
					
						
							| 
									
										
										
										
											2010-07-22 04:15:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 06:34:58 +00:00
										 |  |  | 	return TmpTime; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | } // Namespace Common
 |