summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Timer.cpp
blob: 9ac36458419118a9949e0301d28d265ec038035a (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
// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#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/chrono.h>
#include <fmt/format.h>

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

namespace Common
{
template <typename Clock, typename Duration>
static typename Clock::rep time_now()
{
  return std::chrono::time_point_cast<Duration>(Clock::now()).time_since_epoch().count();
}

template <typename Duration>
static auto steady_time_now()
{
  return time_now<std::chrono::steady_clock, Duration>();
}

u64 Timer::NowUs()
{
  return steady_time_now<std::chrono::microseconds>();
}

u64 Timer::NowMs()
{
  return steady_time_now<std::chrono::milliseconds>();
}

void Timer::Start()
{
  m_start_ms = NowMs();
  m_end_ms = 0;
  m_running = true;
}

void Timer::StartWithOffset(u64 offset)
{
  Start();
  if (m_start_ms > offset)
    m_start_ms -= offset;
}

void Timer::Stop()
{
  m_end_ms = NowMs();
  m_running = false;
}

u64 Timer::ElapsedMs() const
{
  // If we have not started yet, return zero
  if (m_start_ms == 0)
    return 0;

  if (m_running)
  {
    u64 now = NowMs();
    if (m_start_ms >= now)
      return 0;
    return now - m_start_ms;
  }
  else
  {
    if (m_start_ms >= m_end_ms)
      return 0;
    return m_end_ms - m_start_ms;
  }
}

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);
}

double Timer::GetSystemTimeAsDouble()
{
  // 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();
}

std::string Timer::SystemTimeAsDoubleToString(double time)
{
  // revert adjustments from GetSystemTimeAsDouble() 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, std::size(tmp), L"%x %X", localTime);
  return WStringToUTF8(tmp);
#else
  char tmp[32] = {};
  strftime(tmp, sizeof(tmp), "%x %X", localTime);
  return tmp;
#endif
}

void Timer::IncreaseResolution()
{
#ifdef _WIN32
  timeBeginPeriod(1);
#endif
}

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

}  // Namespace Common