summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/tools/Time.h
blob: 31c29fd3a27bf2af241cde6d471bd7173fd36171 (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
#pragma once
/*
 *  Copyright (C) 2005-2019 Team Kodi
 *  Copyright (C) 2011-2012 Pulse-Eight Limited.
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#if defined(TARGET_DARWIN)
#include <mach/mach_time.h>
#include <CoreVideo/CVHostTime.h>
#elif defined(TARGET_WINDOWS)
#include <Windows.h>
#include <time.h>
#else
#include <time.h>
#endif

namespace kodi
{
namespace time
{

//===============================================================================
/// @brief Function to get current time in milliseconds
///
/// @return Current time in milliseconds as a double value
///
///
/// -----------------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.cpp}
///
/// #include <kodi/tools/Time.h>
///
/// ...
/// double time = kodi::time::GetTimeMs();
/// ...
/// ~~~~~~~~~~~~~
///
inline double GetTimeMs()
{
#if defined(TARGET_DARWIN)
  return static_cast<double>(CVGetCurrentHostTime() / static_cast<double>(CVGetHostClockFrequency() * 0.001));
#elif defined(TARGET_WINDOWS)
  LARGE_INTEGER tickPerSecond;
  LARGE_INTEGER tick;
  if (QueryPerformanceFrequency(&tickPerSecond))
  {
    QueryPerformanceCounter(&tick);
    return static_cast<double>(tick.QuadPart) / (tickPerSecond.QuadPart / 1000.0);
  }
  return 0.0;
#else
  timespec time;
  clock_gettime(CLOCK_MONOTONIC, &time);
  return static_cast<double>(time.tv_sec) * 1000.0 + time.tv_nsec / 1000000.0;
#endif
}
//-------------------------------------------------------------------------------

//===============================================================================
/// @brief Function to get current time in seconds
///
/// @return Current time in seconds with the value type defined in the template
///
///
/// -----------------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.cpp}
///
/// #include <kodi/tools/Time.h>
///
/// ...
/// double time = kodi::time::GetTimeSec<double>();
/// ...
/// ~~~~~~~~~~~~~
///
template <class T>
inline T GetTimeSec()
{
  return static_cast<T>(GetTimeMs()) / static_cast<T>(1000.0);
}
//-------------------------------------------------------------------------------

} /* namespace time */
} /* namespace kodi */