summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Timer.cpp
blob: 69188cd303efc020cf1ff9ce08970ff617d9f210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "Common/Timer.h"

#include <chrono>
#include <string>

#ifdef _WIN32
#include <cwchar>

#include <windows.h>
#include <mmsystem.h>
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif

#include <fmt/format.h>

#include "Common/CommonTypes.h"
#include "Common/StringUtil.h"

namespace Common
{
u32 Timer::GetTimeMs()
{
#ifdef _WIN32
  return timeGetTime();
#elif defined __APPLE__
  struct timeval t;
  (void)gettimeofday(&t, nullptr);
  return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000));
#else
  struct timespec t;
  (void)clock_gettime(CLOCK_MONOTONIC, &t);
  return ((u32)(t.tv_sec * 1000 + t.tv_nsec / 1000000));
#endif
}

#ifdef _WIN32
double GetFreq()
{
  LARGE_INTEGER freq;
  QueryPerformanceFrequency(&freq);
  return 1000000.0 / double(freq.QuadPart);
}
#endif

u64 Timer::GetTimeUs()
{
#ifdef _WIN32
  LARGE_INTEGER time;
  static double freq = GetFreq();
  QueryPerformanceCounter(&time);
  return u64(double(time.QuadPart) * freq);
#elif defined __APPLE__
  struct timeval t;
  (void)gettimeofday(&t, nullptr);
  return ((u64)(t.tv_sec * 1000000 + t.tv_usec));
#else
  struct timespec t;
  (void)clock_gettime(CLOCK_MONOTONIC, &t);
  return ((u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000));
#endif
}

// --------------------------------------------
// Initiate, Start, Stop, and Update the time
// --------------------------------------------

// Set initial values for the class
Timer::Timer() : m_LastTime(0), m_StartTime(0), m_Running(false)
{
  Update();
}

// Write the starting time
void Timer::Start()
{
  m_StartTime = GetTimeMs();
  m_Running = true;
}

// Stop the timer
void Timer::Stop()
{
  // Write the final time
  m_LastTime = GetTimeMs();
  m_Running = false;
}

// Update the last time variable
void Timer::Update()
{
  m_LastTime = GetTimeMs();
  // TODO(ector) - QPF
}

// -------------------------------------
// Get time difference and elapsed time
// -------------------------------------

// Get the number of milliseconds since the last Update()
u64 Timer::GetTimeDifference()
{
  return GetTimeMs() - m_LastTime;
}

// Add the time difference since the last Update() to the starting time.
// This is used to compensate for a paused game.
void Timer::AddTimeDifference()
{
  m_StartTime += GetTimeDifference();
}

// Get the time elapsed since the Start()
u64 Timer::GetTimeElapsed()
{
  // If we have not started yet, return zero
  if (m_StartTime == 0)
    return 0;

  // Return the final timer time if the timer is stopped
  if (!m_Running)
    return (m_LastTime - m_StartTime);

  return (GetTimeMs() - m_StartTime);
}

// Get the formatted time elapsed since the Start()
std::string Timer::GetTimeElapsedFormatted() const
{
  // 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;

  return fmt::format("{:02}:{:02}:{:02}:{:03}", Hours, Minutes % 60, Seconds % 60,
                     Milliseconds % 1000);
}

// Get current time
void Timer::IncreaseResolution()
{
#ifdef _WIN32
  timeBeginPeriod(1);
#endif
}

void Timer::RestoreResolution()
{
#ifdef _WIN32
  timeEndPeriod(1);
#endif
}

// Get the number of seconds since January 1 1970
u64 Timer::GetTimeSinceJan1970()
{
  time_t ltime;
  time(&ltime);
  return ((u64)ltime);
}

u64 Timer::GetLocalTimeSinceJan1970()
{
  time_t sysTime, tzDiff, tzDST;
  time(&sysTime);
  tm* gmTime = localtime(&sysTime);

  // Account for DST where needed
  if (gmTime->tm_isdst == 1)
    tzDST = 3600;
  else
    tzDST = 0;

  // Lazy way to get local time in sec
  gmTime = gmtime(&sysTime);
  tzDiff = sysTime - mktime(gmTime);

  return static_cast<u64>(sysTime + tzDiff + tzDST);
}

// Return the current time formatted as Minutes:Seconds:Milliseconds
// in the form 00:00:000.
std::string Timer::GetTimeFormatted()
{
  time_t sysTime;
  time(&sysTime);

  struct tm* gmTime = localtime(&sysTime);

#ifdef _WIN32
  wchar_t tmp[13];
  wcsftime(tmp, 6, L"%M:%S", gmTime);
#else
  char tmp[13];
  strftime(tmp, 6, "%M:%S", gmTime);
#endif

// Now tack on the milliseconds
#ifdef _WIN32
  struct timeb tp;
  (void)::ftime(&tp);
  return UTF16ToUTF8(tmp) + fmt::format(":{:03}", tp.millitm);
#elif defined __APPLE__
  struct timeval t;
  (void)gettimeofday(&t, nullptr);
  return fmt::format("{}:{:03}", tmp, t.tv_usec / 1000);
#else
  struct timespec t;
  (void)clock_gettime(CLOCK_MONOTONIC, &t);
  return fmt::format("{}:{:03}", tmp, t.tv_nsec / 1000000);
#endif
}

// Returns a timestamp with decimals for precise time comparisons
double Timer::GetDoubleTime()
{
  // FYI: std::chrono::system_clock epoch is not required to be 1970 until c++20.
  // We will however assume time_t IS unix time.
  using Clock = std::chrono::system_clock;

  // TODO: Use this on switch to c++20:
  // const auto since_epoch = Clock::now().time_since_epoch();
  const auto unix_epoch = Clock::from_time_t({});
  const auto since_epoch = Clock::now() - unix_epoch;

  const auto since_double_time_epoch = since_epoch - std::chrono::seconds(DOUBLE_TIME_OFFSET);
  return std::chrono::duration_cast<std::chrono::duration<double>>(since_double_time_epoch).count();
}

// Formats a timestamp from GetDoubleTime() into a date and time string
std::string Timer::GetDateTimeFormatted(double time)
{
  // revert adjustments from GetDoubleTime() to get a normal Unix timestamp again
  time_t seconds = (time_t)time + DOUBLE_TIME_OFFSET;
  tm* localTime = localtime(&seconds);

#ifdef _WIN32
  wchar_t tmp[32] = {};
  wcsftime(tmp, sizeof(tmp), L"%x %X", localTime);
  return UTF16ToUTF8(tmp);
#else
  char tmp[32] = {};
  strftime(tmp, sizeof(tmp), "%x %X", localTime);
  return tmp;
#endif
}

}  // Namespace Common