2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-02-12 13:08:23 -07:00
|
|
|
#include <cinttypes>
|
2014-02-18 17:54:11 -07:00
|
|
|
#include <ctime>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <string>
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2010-01-21 12:55:01 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <mmsystem.h>
|
2010-07-21 22:15:11 -06:00
|
|
|
#include <sys/timeb.h>
|
2014-02-18 17:54:11 -07:00
|
|
|
#include <windows.h>
|
2010-11-19 20:24:51 -07:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2010-01-21 12:55:01 -07:00
|
|
|
|
2014-02-19 20:11:52 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Common/Timer.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2010-01-21 12:55:01 -07:00
|
|
|
namespace Common
|
|
|
|
{
|
2010-07-21 22:15:11 -06:00
|
|
|
u32 Timer::GetTimeMs()
|
|
|
|
{
|
2010-01-21 13:49:45 -07:00
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
return timeGetTime();
|
2015-01-28 12:28:01 -07:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
|
|
|
return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000));
|
2015-01-28 12:28:01 -07:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
return ((u32)(t.tv_sec * 1000 + t.tv_nsec / 1000000));
|
2008-12-07 22:30:24 -07:00
|
|
|
#endif
|
2010-07-21 22:15:11 -06:00
|
|
|
}
|
2009-02-15 23:18:18 -07:00
|
|
|
|
2014-11-19 11:57:12 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
double GetFreq()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
LARGE_INTEGER freq;
|
|
|
|
QueryPerformanceFrequency(&freq);
|
|
|
|
return 1000000.0 / double(freq.QuadPart);
|
2014-11-19 11:57:12 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
u64 Timer::GetTimeUs()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
LARGE_INTEGER time;
|
|
|
|
static double freq = GetFreq();
|
|
|
|
QueryPerformanceCounter(&time);
|
|
|
|
return u64(double(time.QuadPart) * freq);
|
2015-01-28 12:28:01 -07:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
|
|
|
return ((u64)(t.tv_sec * 1000000 + t.tv_usec));
|
2015-01-28 12:28:01 -07:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
return ((u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000));
|
2014-11-19 11:57:12 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
// --------------------------------------------
|
2009-02-15 23:18:18 -07:00
|
|
|
// Initiate, Start, Stop, and Update the time
|
2009-03-28 02:57:34 -06:00
|
|
|
// --------------------------------------------
|
2009-02-15 23:18:18 -07:00
|
|
|
|
|
|
|
// Set initial values for the class
|
2016-06-24 02:43:46 -06:00
|
|
|
Timer::Timer() : m_LastTime(0), m_StartTime(0), m_Running(false)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
Update();
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2009-02-15 23:18:18 -07:00
|
|
|
// Write the starting time
|
|
|
|
void Timer::Start()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
m_StartTime = GetTimeMs();
|
|
|
|
m_Running = true;
|
2009-02-15 23:18:18 -07:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2009-02-15 23:18:18 -07:00
|
|
|
// Stop the timer
|
|
|
|
void Timer::Stop()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// Write the final time
|
|
|
|
m_LastTime = GetTimeMs();
|
|
|
|
m_Running = false;
|
2009-02-15 23:18:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the last time variable
|
2009-02-22 05:43:25 -07:00
|
|
|
void Timer::Update()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
m_LastTime = GetTimeMs();
|
|
|
|
// TODO(ector) - QPF
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2009-02-15 23:18:18 -07:00
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
// -------------------------------------
|
2009-02-15 23:18:18 -07:00
|
|
|
// Get time difference and elapsed time
|
2009-03-28 02:57:34 -06:00
|
|
|
// -------------------------------------
|
2009-02-15 23:18:18 -07:00
|
|
|
|
|
|
|
// Get the number of milliseconds since the last Update()
|
2010-01-07 13:01:41 -07:00
|
|
|
u64 Timer::GetTimeDifference()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
return GetTimeMs() - m_LastTime;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 22:15:11 -06:00
|
|
|
// Add the time difference since the last Update() to the starting time.
|
|
|
|
// This is used to compensate for a paused game.
|
2009-02-15 23:18:18 -07:00
|
|
|
void Timer::AddTimeDifference()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
m_StartTime += GetTimeDifference();
|
2009-02-15 23:18:18 -07:00
|
|
|
}
|
2010-07-21 22:15:11 -06:00
|
|
|
|
2009-02-15 23:18:18 -07:00
|
|
|
// Get the time elapsed since the Start()
|
2009-02-22 05:43:25 -07:00
|
|
|
u64 Timer::GetTimeElapsed()
|
2009-02-15 23:18:18 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// If we have not started yet, return 1 (because then I don't
|
|
|
|
// have to change the FPS calculation in CoreRerecording.cpp .
|
|
|
|
if (m_StartTime == 0)
|
|
|
|
return 1;
|
2009-02-15 23:18:18 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// Return the final timer time if the timer is stopped
|
|
|
|
if (!m_Running)
|
|
|
|
return (m_LastTime - m_StartTime);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return (GetTimeMs() - m_StartTime);
|
2009-02-15 23:18:18 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 22:21:10 -06:00
|
|
|
// Get the formatted time elapsed since the Start()
|
2009-02-22 05:43:25 -07:00
|
|
|
std::string Timer::GetTimeElapsedFormatted() const
|
2009-02-15 23:18:18 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// If we have not started yet, return zero
|
|
|
|
if (m_StartTime == 0)
|
|
|
|
return "00:00:00:000";
|
|
|
|
|
|
|
|
// The number of milliseconds since the start.
|
|
|
|
// Use a different value if the timer is stopped.
|
|
|
|
u64 Milliseconds;
|
|
|
|
if (m_Running)
|
|
|
|
Milliseconds = GetTimeMs() - m_StartTime;
|
|
|
|
else
|
|
|
|
Milliseconds = m_LastTime - m_StartTime;
|
|
|
|
// Seconds
|
|
|
|
u32 Seconds = (u32)(Milliseconds / 1000);
|
|
|
|
// Minutes
|
|
|
|
u32 Minutes = Seconds / 60;
|
|
|
|
// Hours
|
|
|
|
u32 Hours = Minutes / 60;
|
|
|
|
|
|
|
|
std::string TmpStr = StringFromFormat("%02i:%02i:%02i:%03" PRIu64, Hours, Minutes % 60,
|
|
|
|
Seconds % 60, Milliseconds % 1000);
|
|
|
|
return TmpStr;
|
2009-02-15 23:18:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get current time
|
2008-12-07 22:30:24 -07:00
|
|
|
void Timer::IncreaseResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
timeBeginPeriod(1);
|
2008-12-07 22:30:24 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::RestoreResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
timeEndPeriod(1);
|
2008-12-07 22:30:24 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-15 23:18:18 -07:00
|
|
|
// Get the number of seconds since January 1 1970
|
2009-02-22 05:43:25 -07:00
|
|
|
u64 Timer::GetTimeSinceJan1970()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
time_t ltime;
|
|
|
|
time(<ime);
|
|
|
|
return ((u64)ltime);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2008-12-13 04:57:01 -07:00
|
|
|
|
2009-02-22 05:43:25 -07:00
|
|
|
u64 Timer::GetLocalTimeSinceJan1970()
|
2008-12-13 04:57:01 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
time_t sysTime, tzDiff, tzDST;
|
|
|
|
time(&sysTime);
|
2016-07-13 14:46:14 -06:00
|
|
|
tm* gmTime = localtime(&sysTime);
|
2010-08-26 13:24:47 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// Account for DST where needed
|
|
|
|
if (gmTime->tm_isdst == 1)
|
|
|
|
tzDST = 3600;
|
|
|
|
else
|
|
|
|
tzDST = 0;
|
2010-08-26 13:24:47 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// Lazy way to get local time in sec
|
|
|
|
gmTime = gmtime(&sysTime);
|
|
|
|
tzDiff = sysTime - mktime(gmTime);
|
2008-12-13 04:57:01 -07:00
|
|
|
|
2016-07-13 14:46:14 -06:00
|
|
|
return static_cast<u64>(sysTime + tzDiff + tzDST);
|
2008-12-13 04:57:01 -07:00
|
|
|
}
|
2009-01-14 23:48:15 -07:00
|
|
|
|
2010-07-21 22:15:11 -06:00
|
|
|
// Return the current time formatted as Minutes:Seconds:Milliseconds
|
|
|
|
// in the form 00:00:000.
|
2009-02-22 05:43:25 -07:00
|
|
|
std::string Timer::GetTimeFormatted()
|
2009-01-14 23:48:15 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
time_t sysTime;
|
|
|
|
time(&sysTime);
|
2014-03-29 04:05:44 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
struct tm* gmTime = localtime(&sysTime);
|
2009-03-21 17:00:25 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
char tmp[13];
|
|
|
|
strftime(tmp, 6, "%M:%S", gmTime);
|
2009-03-21 17:00:25 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// Now tack on the milliseconds
|
2010-07-21 22:15:11 -06:00
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
|
|
|
return StringFromFormat("%s:%03i", tmp, tp.millitm);
|
2015-01-28 12:28:01 -07:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
|
|
|
return StringFromFormat("%s:%03d", tmp, (int)(t.tv_usec / 1000));
|
2015-01-28 12:28:01 -07:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
return StringFromFormat("%s:%03d", tmp, (int)(t.tv_nsec / 1000000));
|
2010-07-21 22:15:11 -06:00
|
|
|
#endif
|
2009-01-14 23:48:15 -07:00
|
|
|
}
|
2009-02-15 23:18:18 -07:00
|
|
|
|
2009-08-25 00:34:58 -06:00
|
|
|
// Returns a timestamp with decimals for precise time comparisons
|
|
|
|
double Timer::GetDoubleTime()
|
|
|
|
{
|
2010-07-21 22:15:11 -06:00
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2015-01-28 12:28:01 -07:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
2015-01-28 12:28:01 -07:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
2010-07-21 22:15:11 -06:00
|
|
|
#endif
|
2016-06-24 02:43:46 -06:00
|
|
|
// Get continuous timestamp
|
|
|
|
u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970();
|
2009-08-25 00:34:58 -06:00
|
|
|
|
2016-06-24 02:43:46 -06: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.
|
|
|
|
TmpSeconds = TmpSeconds - DOUBLE_TIME_OFFSET;
|
2009-08-25 00:34:58 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// Make a smaller integer that fits in the double
|
|
|
|
u32 Seconds = (u32)TmpSeconds;
|
2010-07-21 22:15:11 -06:00
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
double ms = tp.millitm / 1000.0 / 1000.0;
|
2015-01-28 12:28:01 -07:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 02:43:46 -06:00
|
|
|
double ms = t.tv_usec / 1000000.0;
|
2015-01-28 12:28:01 -07:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
double ms = t.tv_nsec / 1000000000.0;
|
2010-07-21 22:15:11 -06:00
|
|
|
#endif
|
2016-06-24 02:43:46 -06:00
|
|
|
double TmpTime = Seconds + ms;
|
2010-07-21 22:15:11 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return TmpTime;
|
2009-08-25 00:34:58 -06:00
|
|
|
}
|
|
|
|
|
2015-07-11 17:49:12 -06:00
|
|
|
// Formats a timestamp from GetDoubleTime() into a date and time string
|
|
|
|
std::string Timer::GetDateTimeFormatted(double time)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// revert adjustments from GetDoubleTime() to get a normal Unix timestamp again
|
|
|
|
time_t seconds = (time_t)time + DOUBLE_TIME_OFFSET;
|
|
|
|
tm* localTime = localtime(&seconds);
|
2015-07-11 17:49:12 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
char tmp[32] = {};
|
|
|
|
strftime(tmp, sizeof(tmp), "%x %X", localTime);
|
|
|
|
return tmp;
|
2015-07-11 17:49:12 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
} // Namespace Common
|