2014-12-17 05:38:14 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
2013-09-05 00:17:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-29 22:37:15 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <fmt/format.h>
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/common_types.h"
|
2014-04-09 00:15:08 +00:00
|
|
|
#include "common/string_util.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/timer.h"
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
namespace Common {
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::milliseconds Timer::GetTimeMs() {
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch());
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------
|
|
|
|
// Initiate, Start, Stop, and Update the time
|
|
|
|
// --------------------------------------------
|
|
|
|
|
|
|
|
// Set initial values for the class
|
2016-09-18 00:38:01 +00:00
|
|
|
Timer::Timer() : m_LastTime(0), m_StartTime(0), m_Running(false) {
|
2014-04-01 22:20:08 +00:00
|
|
|
Update();
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the starting time
|
2016-09-18 00:38:01 +00:00
|
|
|
void Timer::Start() {
|
2014-04-01 22:20:08 +00:00
|
|
|
m_StartTime = GetTimeMs();
|
|
|
|
m_Running = true;
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the timer
|
2016-09-18 00:38:01 +00:00
|
|
|
void Timer::Stop() {
|
2014-04-01 22:20:08 +00:00
|
|
|
// Write the final time
|
|
|
|
m_LastTime = GetTimeMs();
|
|
|
|
m_Running = false;
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the last time variable
|
2016-09-18 00:38:01 +00:00
|
|
|
void Timer::Update() {
|
2014-04-01 22:20:08 +00:00
|
|
|
m_LastTime = GetTimeMs();
|
2016-09-18 00:38:01 +00:00
|
|
|
// TODO(ector) - QPF
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Get time difference and elapsed time
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
// Get the number of milliseconds since the last Update()
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::milliseconds Timer::GetTimeDifference() {
|
2014-04-01 22:20:08 +00:00
|
|
|
return GetTimeMs() - m_LastTime;
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the time difference since the last Update() to the starting time.
|
|
|
|
// This is used to compensate for a paused game.
|
2016-09-18 00:38:01 +00:00
|
|
|
void Timer::AddTimeDifference() {
|
2014-04-01 22:20:08 +00:00
|
|
|
m_StartTime += GetTimeDifference();
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the time elapsed since the Start()
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::milliseconds Timer::GetTimeElapsed() {
|
2014-04-01 22:20:08 +00:00
|
|
|
// If we have not started yet, return 1 (because then I don't
|
|
|
|
// have to change the FPS calculation in CoreRerecording.cpp .
|
2018-07-21 09:09:15 +00:00
|
|
|
if (m_StartTime.count() == 0)
|
|
|
|
return std::chrono::milliseconds(1);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
// Return the final timer time if the timer is stopped
|
2016-09-18 00:38:01 +00:00
|
|
|
if (!m_Running)
|
|
|
|
return (m_LastTime - m_StartTime);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
return (GetTimeMs() - m_StartTime);
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the formatted time elapsed since the Start()
|
2016-09-18 00:38:01 +00:00
|
|
|
std::string Timer::GetTimeElapsedFormatted() const {
|
2014-04-01 22:20:08 +00:00
|
|
|
// If we have not started yet, return zero
|
2018-07-21 09:09:15 +00:00
|
|
|
if (m_StartTime.count() == 0)
|
2014-04-01 22:20:08 +00:00
|
|
|
return "00:00:00:000";
|
|
|
|
|
|
|
|
// The number of milliseconds since the start.
|
|
|
|
// Use a different value if the timer is stopped.
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::milliseconds Milliseconds;
|
2014-04-01 22:20:08 +00:00
|
|
|
if (m_Running)
|
|
|
|
Milliseconds = GetTimeMs() - m_StartTime;
|
|
|
|
else
|
|
|
|
Milliseconds = m_LastTime - m_StartTime;
|
|
|
|
// Seconds
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::seconds Seconds = std::chrono::duration_cast<std::chrono::seconds>(Milliseconds);
|
2014-04-01 22:20:08 +00:00
|
|
|
// Minutes
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::minutes Minutes = std::chrono::duration_cast<std::chrono::minutes>(Milliseconds);
|
2014-04-01 22:20:08 +00:00
|
|
|
// Hours
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::hours Hours = std::chrono::duration_cast<std::chrono::hours>(Milliseconds);
|
2014-04-01 22:20:08 +00:00
|
|
|
|
2018-07-21 09:09:15 +00:00
|
|
|
std::string TmpStr = fmt::format("{:02}:{:02}:{:02}:{:03}", Hours.count(), Minutes.count() % 60,
|
|
|
|
Seconds.count() % 60, Milliseconds.count() % 1000);
|
2014-04-01 22:20:08 +00:00
|
|
|
return TmpStr;
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of seconds since January 1 1970
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::seconds Timer::GetTimeSinceJan1970() {
|
|
|
|
return std::chrono::duration_cast<std::chrono::seconds>(GetTimeMs());
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 09:09:15 +00:00
|
|
|
std::chrono::seconds Timer::GetLocalTimeSinceJan1970() {
|
2014-04-01 22:20:08 +00:00
|
|
|
time_t sysTime, tzDiff, tzDST;
|
2016-09-18 00:38:01 +00:00
|
|
|
struct tm* gmTime;
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
time(&sysTime);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
// Account for DST where needed
|
|
|
|
gmTime = localtime(&sysTime);
|
2016-09-18 00:38:01 +00:00
|
|
|
if (gmTime->tm_isdst == 1)
|
2014-04-01 22:20:08 +00:00
|
|
|
tzDST = 3600;
|
|
|
|
else
|
|
|
|
tzDST = 0;
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
// Lazy way to get local time in sec
|
2016-09-18 00:38:01 +00:00
|
|
|
gmTime = gmtime(&sysTime);
|
2014-04-01 22:20:08 +00:00
|
|
|
tzDiff = sysTime - mktime(gmTime);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2018-07-21 09:09:15 +00:00
|
|
|
return std::chrono::seconds(sysTime + tzDiff + tzDST);
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the current time formatted as Minutes:Seconds:Milliseconds
|
|
|
|
// in the form 00:00:000.
|
2016-09-18 00:38:01 +00:00
|
|
|
std::string Timer::GetTimeFormatted() {
|
2014-04-01 22:20:08 +00:00
|
|
|
time_t sysTime;
|
2016-09-18 00:38:01 +00:00
|
|
|
struct tm* gmTime;
|
2014-04-01 22:20:08 +00:00
|
|
|
char tmp[13];
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
time(&sysTime);
|
|
|
|
gmTime = localtime(&sysTime);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
strftime(tmp, 6, "%M:%S", gmTime);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2018-07-21 09:09:15 +00:00
|
|
|
u64 milliseconds = static_cast<u64>(GetTimeMs().count()) % 1000;
|
|
|
|
return fmt::format("{}:{:03}", tmp, milliseconds);
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a timestamp with decimals for precise time comparisons
|
|
|
|
// ----------------
|
2016-09-18 00:38:01 +00:00
|
|
|
double Timer::GetDoubleTime() {
|
2014-04-01 22:20:08 +00:00
|
|
|
// Get continuous timestamp
|
2018-07-21 09:09:15 +00:00
|
|
|
u64 TmpSeconds = static_cast<u64>(Common::Timer::GetTimeSinceJan1970().count());
|
|
|
|
double ms = static_cast<u64>(GetTimeMs().count()) % 1000;
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +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.
|
|
|
|
TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
// Make a smaller integer that fits in the double
|
2018-07-21 09:09:15 +00:00
|
|
|
u32 Seconds = static_cast<u32>(TmpSeconds);
|
2014-04-01 22:20:08 +00:00
|
|
|
double TmpTime = Seconds + ms;
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
return TmpTime;
|
2013-09-05 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace Common
|