2009-07-28 15:32:10 -06:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include <time.h>
|
2009-01-14 23:48:15 -07:00
|
|
|
#include <sys/timeb.h>
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
#include "Timer.h"
|
2009-02-15 23:18:18 -07:00
|
|
|
#include "StringUtil.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
u32 timeGetTime()
|
|
|
|
{
|
|
|
|
struct timeb t;
|
|
|
|
ftime(&t);
|
|
|
|
return((u32)(t.time * 1000 + t.millitm));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
2009-02-15 23:18:18 -07:00
|
|
|
|
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
|
2009-02-22 05:43:25 -07:00
|
|
|
Timer::Timer()
|
2009-02-15 23:18:18 -07:00
|
|
|
: m_LastTime(0), m_StartTime(0), m_Running(false)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
Update();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
QueryPerformanceFrequency((LARGE_INTEGER*)&m_frequency);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-15 23:18:18 -07:00
|
|
|
// Write the starting time
|
|
|
|
void Timer::Start()
|
|
|
|
{
|
|
|
|
m_StartTime = timeGetTime();
|
|
|
|
m_Running = true;
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2009-02-15 23:18:18 -07:00
|
|
|
// Stop the timer
|
|
|
|
void Timer::Stop()
|
|
|
|
{
|
|
|
|
// Write the final time
|
|
|
|
m_LastTime = timeGetTime();
|
|
|
|
m_Running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the last time variable
|
2009-02-22 05:43:25 -07:00
|
|
|
void Timer::Update()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
m_LastTime = timeGetTime();
|
|
|
|
//TODO(ector) - QPF
|
|
|
|
}
|
2009-02-15 23:18:18 -07:00
|
|
|
|
2008-12-07 22:30:24 -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()
|
2009-02-22 05:43:25 -07:00
|
|
|
s64 Timer::GetTimeDifference()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
return(timeGetTime() - m_LastTime);
|
|
|
|
}
|
|
|
|
|
2009-02-16 00:17:34 -07: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()
|
|
|
|
{
|
|
|
|
m_StartTime += GetTimeDifference();
|
|
|
|
}
|
2009-02-16 00:17:34 -07:00
|
|
|
// Wind back the starting time to a custom time
|
|
|
|
void Timer::WindBackStartingTime(u64 WindBack)
|
|
|
|
{
|
|
|
|
m_StartTime += WindBack;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
// Rrturn 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
|
|
|
|
2009-02-15 23:18:18 -07:00
|
|
|
return (timeGetTime() - m_StartTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the formattet 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
|
|
|
{
|
|
|
|
// If we have not started yet, return zero
|
2009-02-20 15:04:52 -07:00
|
|
|
if (m_StartTime == 0)
|
|
|
|
return "00:00:00:000";
|
2009-02-15 23:18:18 -07:00
|
|
|
|
|
|
|
// The number of milliseconds since the start, use a different value if the timer is stopped
|
2009-02-22 05:43:25 -07:00
|
|
|
u64 Milliseconds;
|
2009-02-20 15:04:52 -07:00
|
|
|
if (m_Running)
|
2009-02-15 23:18:18 -07:00
|
|
|
Milliseconds = timeGetTime() - m_StartTime;
|
|
|
|
else
|
|
|
|
Milliseconds = m_LastTime - m_StartTime;
|
|
|
|
// Seconds
|
2009-02-22 05:43:25 -07:00
|
|
|
u32 Seconds = (u32)(Milliseconds / 1000);
|
2009-02-15 23:18:18 -07:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current time
|
2008-12-07 22:30:24 -07:00
|
|
|
void Timer::IncreaseResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
timeBeginPeriod(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::RestoreResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
timeEndPeriod(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
void _time64(u64* t)
|
|
|
|
{
|
|
|
|
*t = 0; //TODO
|
|
|
|
}
|
|
|
|
#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
|
|
|
{
|
|
|
|
time_t ltime;
|
|
|
|
time(<ime);
|
|
|
|
return((u64)ltime);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
time_t sysTime, tzDiff;
|
|
|
|
struct tm * gmTime;
|
|
|
|
|
|
|
|
time(&sysTime);
|
|
|
|
// Lazy way to get local time in sec
|
|
|
|
gmTime = gmtime(&sysTime);
|
|
|
|
tzDiff = sysTime - mktime(gmTime);
|
|
|
|
|
|
|
|
return (u64)(sysTime + tzDiff);
|
|
|
|
}
|
2009-01-14 23:48:15 -07:00
|
|
|
|
2009-02-15 23:18:18 -07: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
|
|
|
{
|
2009-03-14 23:34:35 -06:00
|
|
|
time_t sysTime;
|
|
|
|
struct tm * gmTime;
|
|
|
|
char formattedTime[13];
|
2009-03-21 17:00:25 -06:00
|
|
|
char tmp[13];
|
2009-03-14 23:34:35 -06:00
|
|
|
|
|
|
|
time(&sysTime);
|
|
|
|
gmTime = localtime(&sysTime);
|
2009-03-21 17:00:25 -06:00
|
|
|
|
|
|
|
strftime(tmp, 6, "%M:%S", gmTime);
|
|
|
|
|
2009-03-14 23:34:35 -06:00
|
|
|
// Now tack on the milliseconds
|
2009-01-14 23:48:15 -07:00
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2009-03-21 17:00:25 -06:00
|
|
|
sprintf(formattedTime, "%s:%03i", tmp, tp.millitm);
|
2009-01-14 23:48:15 -07:00
|
|
|
|
2009-03-14 23:34:35 -06:00
|
|
|
return std::string(formattedTime);
|
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
|
2009-09-08 10:07:13 -06:00
|
|
|
// ----------------
|
2009-08-25 00:34:58 -06:00
|
|
|
double Timer::GetDoubleTime()
|
|
|
|
{
|
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
|
|
|
u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970(); // Get continous timestamp
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
//if (TmpSeconds < 0) return 0; // Check the the user's clock is working somewhat
|
|
|
|
|
|
|
|
u32 Seconds = (u32)TmpSeconds; // Make a smaller integer that fits in the double
|
|
|
|
double ms = tp.millitm / 1000.0;
|
|
|
|
double TmpTime = Seconds + ms;
|
|
|
|
return TmpTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
} // Namespace Common
|
2009-08-25 00:34:58 -06:00
|
|
|
|
|
|
|
|