2013-09-05 04:17:46 +04:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <mmsystem.h>
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-09 04:15:08 +04:00
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/timer.h"
|
|
|
|
#include "common/string_util.h"
|
2013-09-05 04:17:46 +04:00
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
|
|
|
|
u32 Timer::GetTimeMs()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-04-02 02:20:08 +04:00
|
|
|
return timeGetTime();
|
2013-09-05 04:17:46 +04:00
|
|
|
#else
|
2014-04-02 02:20:08 +04:00
|
|
|
struct timeval t;
|
2014-12-03 21:57:57 +03:00
|
|
|
(void)gettimeofday(&t, nullptr);
|
2014-04-02 02:20:08 +04:00
|
|
|
return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000));
|
2013-09-05 04:17:46 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------
|
|
|
|
// Initiate, Start, Stop, and Update the time
|
|
|
|
// --------------------------------------------
|
|
|
|
|
|
|
|
// Set initial values for the class
|
|
|
|
Timer::Timer()
|
2014-04-02 02:20:08 +04:00
|
|
|
: m_LastTime(0), m_StartTime(0), m_Running(false)
|
2013-09-05 04:17:46 +04:00
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
Update();
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the starting time
|
|
|
|
void Timer::Start()
|
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
m_StartTime = GetTimeMs();
|
|
|
|
m_Running = true;
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the timer
|
|
|
|
void Timer::Stop()
|
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
// Write the final time
|
|
|
|
m_LastTime = GetTimeMs();
|
|
|
|
m_Running = false;
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the last time variable
|
|
|
|
void Timer::Update()
|
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
m_LastTime = GetTimeMs();
|
|
|
|
//TODO(ector) - QPF
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Get time difference and elapsed time
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
// Get the number of milliseconds since the last Update()
|
|
|
|
u64 Timer::GetTimeDifference()
|
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
return GetTimeMs() - m_LastTime;
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the time difference since the last Update() to the starting time.
|
|
|
|
// This is used to compensate for a paused game.
|
|
|
|
void Timer::AddTimeDifference()
|
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
m_StartTime += GetTimeDifference();
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the time elapsed since the Start()
|
|
|
|
u64 Timer::GetTimeElapsed()
|
|
|
|
{
|
2014-04-02 02:20:08 +04: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;
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
// Return the final timer time if the timer is stopped
|
|
|
|
if (!m_Running) return (m_LastTime - m_StartTime);
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
return (GetTimeMs() - m_StartTime);
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the formatted time elapsed since the Start()
|
|
|
|
std::string Timer::GetTimeElapsedFormatted() const
|
|
|
|
{
|
2014-04-02 02:20:08 +04: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:%03i",
|
|
|
|
Hours, Minutes % 60, Seconds % 60, Milliseconds % 1000);
|
|
|
|
return TmpStr;
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get current time
|
|
|
|
void Timer::IncreaseResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-04-02 02:20:08 +04:00
|
|
|
timeBeginPeriod(1);
|
2013-09-05 04:17:46 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::RestoreResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-04-02 02:20:08 +04:00
|
|
|
timeEndPeriod(1);
|
2013-09-05 04:17:46 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of seconds since January 1 1970
|
|
|
|
u64 Timer::GetTimeSinceJan1970()
|
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
time_t ltime;
|
|
|
|
time(<ime);
|
|
|
|
return((u64)ltime);
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 Timer::GetLocalTimeSinceJan1970()
|
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
time_t sysTime, tzDiff, tzDST;
|
|
|
|
struct tm * gmTime;
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
time(&sysTime);
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
// Account for DST where needed
|
|
|
|
gmTime = localtime(&sysTime);
|
|
|
|
if(gmTime->tm_isdst == 1)
|
|
|
|
tzDST = 3600;
|
|
|
|
else
|
|
|
|
tzDST = 0;
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
// Lazy way to get local time in sec
|
|
|
|
gmTime = gmtime(&sysTime);
|
|
|
|
tzDiff = sysTime - mktime(gmTime);
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
return (u64)(sysTime + tzDiff + tzDST);
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the current time formatted as Minutes:Seconds:Milliseconds
|
|
|
|
// in the form 00:00:000.
|
|
|
|
std::string Timer::GetTimeFormatted()
|
|
|
|
{
|
2014-04-02 02:20:08 +04:00
|
|
|
time_t sysTime;
|
|
|
|
struct tm * gmTime;
|
|
|
|
char tmp[13];
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
time(&sysTime);
|
|
|
|
gmTime = localtime(&sysTime);
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
strftime(tmp, 6, "%M:%S", gmTime);
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
// Now tack on the milliseconds
|
2013-09-05 04:17:46 +04:00
|
|
|
#ifdef _WIN32
|
2014-04-02 02:20:08 +04:00
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2014-10-24 08:17:01 +04:00
|
|
|
return StringFromFormat("%s:%03i", tmp, tp.millitm);
|
2013-09-05 04:17:46 +04:00
|
|
|
#else
|
2014-04-02 02:20:08 +04:00
|
|
|
struct timeval t;
|
2014-12-03 21:57:57 +03:00
|
|
|
(void)gettimeofday(&t, nullptr);
|
2014-10-24 08:17:01 +04:00
|
|
|
return StringFromFormat("%s:%03d", tmp, (int)(t.tv_usec / 1000));
|
2013-09-05 04:17:46 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a timestamp with decimals for precise time comparisons
|
|
|
|
// ----------------
|
|
|
|
double Timer::GetDoubleTime()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-04-02 02:20:08 +04:00
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2013-09-05 04:17:46 +04:00
|
|
|
#else
|
2014-04-02 02:20:08 +04:00
|
|
|
struct timeval t;
|
2014-12-03 21:57:57 +03:00
|
|
|
(void)gettimeofday(&t, nullptr);
|
2013-09-05 04:17:46 +04:00
|
|
|
#endif
|
2014-04-02 02:20:08 +04:00
|
|
|
// Get continuous timestamp
|
|
|
|
u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970();
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04: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 - (38 * 365 * 24 * 60 * 60);
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
// Make a smaller integer that fits in the double
|
|
|
|
u32 Seconds = (u32)TmpSeconds;
|
2013-09-05 04:17:46 +04:00
|
|
|
#ifdef _WIN32
|
2014-04-02 02:20:08 +04:00
|
|
|
double ms = tp.millitm / 1000.0 / 1000.0;
|
2013-09-05 04:17:46 +04:00
|
|
|
#else
|
2014-04-02 02:20:08 +04:00
|
|
|
double ms = t.tv_usec / 1000000.0;
|
2013-09-05 04:17:46 +04:00
|
|
|
#endif
|
2014-04-02 02:20:08 +04:00
|
|
|
double TmpTime = Seconds + ms;
|
2013-09-05 04:17:46 +04:00
|
|
|
|
2014-04-02 02:20:08 +04:00
|
|
|
return TmpTime;
|
2013-09-05 04:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace Common
|