3#ifndef _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
4#define _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
21#if TIME_SHIELD_PLATFORM_WINDOWS
22# ifndef WIN32_LEAN_AND_MEAN
23# define WIN32_LEAN_AND_MEAN
26#elif TIME_SHIELD_PLATFORM_UNIX
28# include <sys/resource.h>
29# include <sys/times.h>
32# error "Unsupported platform for get_cpu_time()"
43# if defined(CLOCK_REALTIME)
44 clock_gettime(CLOCK_REALTIME, &
ts);
46 timespec_get(&
ts, TIME_UTC);
62# if TIME_SHIELD_PLATFORM_WINDOWS
63 static std::once_flag init_flag;
64 static int64_t s_perf_freq = 0;
65 static int64_t s_anchor_perf = 0;
66 static int64_t s_anchor_realtime_us = 0;
68 std::call_once(init_flag, []() {
69 LARGE_INTEGER freq = {};
70 LARGE_INTEGER counter = {};
71 ::QueryPerformanceFrequency(&freq);
72 ::QueryPerformanceCounter(&counter);
74 s_perf_freq =
static_cast<int64_t
>(freq.QuadPart);
75 s_anchor_perf =
static_cast<int64_t
>(counter.QuadPart);
78 ::GetSystemTimeAsFileTime(&ft);
81 uli.LowPart = ft.dwLowDateTime;
82 uli.HighPart = ft.dwHighDateTime;
85 const int64_t k_epoch_diff_100ns = 116444736000000000LL;
87 const int64_t filetime_100ns =
static_cast<int64_t
>(uli.QuadPart);
89 s_anchor_realtime_us = (filetime_100ns - k_epoch_diff_100ns) / 10;
92 LARGE_INTEGER
now = {};
93 ::QueryPerformanceCounter(&
now);
95 const int64_t now_ticks =
static_cast<int64_t
>(
now.QuadPart);
96 const int64_t delta_ticks = now_ticks - s_anchor_perf;
99 const int64_t q = delta_ticks / s_perf_freq;
100 const int64_t r = delta_ticks % s_perf_freq;
102 const int64_t delta_us =
103 q * 1000000LL + (r * 1000000LL) / s_perf_freq;
105 return s_anchor_realtime_us + delta_us;
107 static std::once_flag init_flag;
108 static int64_t s_anchor_realtime_us = 0;
109 static int64_t s_anchor_mono_ns = 0;
111 std::call_once(init_flag, []() {
112 struct timespec realtime_ts{};
113 struct timespec mono_ts{};
115# if defined(CLOCK_MONOTONIC_RAW)
116 clock_gettime(CLOCK_MONOTONIC_RAW, &mono_ts);
118 clock_gettime(CLOCK_MONOTONIC, &mono_ts);
120 clock_gettime(CLOCK_REALTIME, &realtime_ts);
122 s_anchor_realtime_us =
static_cast<int64_t
>(realtime_ts.tv_sec) * 1000000LL
123 + realtime_ts.tv_nsec / 1000;
124 s_anchor_mono_ns =
static_cast<int64_t
>(mono_ts.tv_sec) * 1000000000LL
128 struct timespec mono_now_ts{};
129# if defined(CLOCK_MONOTONIC_RAW)
130 clock_gettime(CLOCK_MONOTONIC_RAW, &mono_now_ts);
132 clock_gettime(CLOCK_MONOTONIC, &mono_now_ts);
135 const int64_t mono_now_ns =
static_cast<int64_t
>(mono_now_ts.tv_sec) * 1000000000LL
136 + mono_now_ts.tv_nsec;
137 const int64_t delta_ns = mono_now_ns - s_anchor_mono_ns;
138 return s_anchor_realtime_us + delta_ns / 1000;
150 const auto ticks = std::chrono::steady_clock::now().time_since_epoch();
151 return static_cast<ts_t>(std::chrono::duration_cast<std::chrono::seconds>(ticks).count());
162 const auto ticks = std::chrono::steady_clock::now().time_since_epoch();
163 return static_cast<ts_ms_t>(std::chrono::duration_cast<std::chrono::milliseconds>(ticks).count());
174 const auto ticks = std::chrono::steady_clock::now().time_since_epoch();
175 return static_cast<ts_us_t>(std::chrono::duration_cast<std::chrono::microseconds>(ticks).count());
182 template<
class T =
int>
185 return static_cast<T
>(
ts.tv_nsec);
192 template<
class T =
int>
202 template<
class T =
int>
285# if TIME_SHIELD_PLATFORM_WINDOWS
286 FILETIME create_time{}, exit_time{}, kernel_time{}, user_time{};
287 if (GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time, &user_time)) {
289 li.LowPart = user_time.dwLowDateTime;
290 li.HighPart = user_time.dwHighDateTime;
291 return static_cast<double>(li.QuadPart) / 10000000.0;
293# elif TIME_SHIELD_PLATFORM_UNIX
295# if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
296 clockid_t
id = (clockid_t)-1;
297# if defined(_POSIX_CPUTIME) && (_POSIX_CPUTIME > 0)
298 if (clock_getcpuclockid(0, &
id) != 0) {
299# if defined(CLOCK_PROCESS_CPUTIME_ID)
300 id = CLOCK_PROCESS_CPUTIME_ID;
301# elif defined(CLOCK_VIRTUAL)
305# elif defined(CLOCK_PROCESS_CPUTIME_ID)
306 id = CLOCK_PROCESS_CPUTIME_ID;
307# elif defined(CLOCK_VIRTUAL)
310 if (
id != (clockid_t)-1) {
312 if (clock_gettime(
id, &
ts) == 0) {
313 return static_cast<double>(
ts.tv_sec) +
static_cast<double>(
ts.tv_nsec) / 1e9;
318# if defined(RUSAGE_SELF)
319 struct rusage usage{};
320 if (getrusage(RUSAGE_SELF, &usage) == 0) {
321 return static_cast<double>(usage.ru_utime.tv_sec) +
static_cast<double>(usage.ru_utime.tv_usec) / 1e6;
325# if defined(_SC_CLK_TCK)
327 if (times(&t) != (clock_t)-1) {
328 return static_cast<double>(t.tms_utime) /
static_cast<double>(sysconf(_SC_CLK_TCK));
332# if defined(CLOCKS_PER_SEC)
333 clock_t cl = clock();
334 if (cl != (clock_t)-1) {
335 return static_cast<double>(cl) /
static_cast<double>(CLOCKS_PER_SEC);
339# warning "get_cpu_time() may not work correctly: unsupported platform"
341 return std::numeric_limits<double>::quiet_NaN();
Configuration macros for the library.
Header file with time-related constants.
constexpr int64_t NS_PER_US
Nanoseconds per microsecond.
constexpr int64_t NS_PER_MS
Nanoseconds per millisecond.
constexpr int64_t MS_PER_SEC
Milliseconds per second.
constexpr int64_t NS_PER_SEC
Nanoseconds per second.
constexpr int64_t US_PER_SEC
Microseconds per second.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
double fts_t
Floating-point timestamp (fractional seconds since epoch).
int64_t ts_us_t
Unix timestamp in microseconds since epoch.
ts_us_t timestamp_us() noexcept
Get the current UTC timestamp in microseconds.
ts_us_t monotonic_us() noexcept
Return monotonic microseconds from a process-local reference.
ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
ts_t monotonic_sec() noexcept
Return monotonic seconds from a process-local reference.
T ns_of_sec() noexcept
Get the nanosecond part of the current second.
int64_t now_realtime_us()
Get current real time in microseconds using a platform-specific method.
ts_t ts() noexcept
Get the current UTC timestamp in seconds.
ts_t timestamp() noexcept
Get the current UTC timestamp in seconds.
fts_t ftimestamp() noexcept
Get the current UTC timestamp in floating-point seconds.
double get_cpu_time() noexcept
Get the CPU time used by the current process.
struct timespec get_timespec_impl() noexcept
Get the current timespec.
ts_us_t ts_us() noexcept
Get the current UTC timestamp in microseconds.
T ms_of_sec() noexcept
Get the millisecond part of the current second.
ts_ms_t monotonic_ms() noexcept
Return monotonic milliseconds from a process-local reference.
ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
T us_of_sec() noexcept
Get the microsecond part of the current second.
ts_ms_t timestamp_ms() noexcept
Get the current UTC timestamp in milliseconds.
fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
Main namespace for the Time Shield library.
Type definitions for time-related units and formats.