3#ifndef _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
4#define _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
20#if TIME_SHIELD_PLATFORM_WINDOWS
21# ifndef WIN32_LEAN_AND_MEAN
22# define WIN32_LEAN_AND_MEAN
25#elif TIME_SHIELD_PLATFORM_UNIX
27# include <sys/resource.h>
28# include <sys/times.h>
31# error "Unsupported platform for get_cpu_time()"
42# if defined(CLOCK_REALTIME)
43 clock_gettime(CLOCK_REALTIME, &
ts);
45 timespec_get(&
ts, TIME_UTC);
60# if TIME_SHIELD_PLATFORM_WINDOWS
61 static std::once_flag init_flag;
62 static int64_t s_perf_freq = 0;
63 static int64_t s_anchor_perf = 0;
64 static int64_t s_anchor_realtime_us = 0;
66 std::call_once(init_flag, [] {
67 LARGE_INTEGER freq, counter;
68 QueryPerformanceFrequency(&freq);
69 QueryPerformanceCounter(&counter);
71 s_perf_freq = freq.QuadPart;
72 s_anchor_perf = counter.QuadPart;
75 GetSystemTimeAsFileTime(&ft);
77 uli.LowPart = ft.dwLowDateTime;
78 uli.HighPart = ft.dwHighDateTime;
80 s_anchor_realtime_us = (uli.QuadPart - 116444736000000000ULL) / 10;
84 QueryPerformanceCounter(&
now);
86 int64_t delta_ticks =
now.QuadPart - s_anchor_perf;
87 int64_t delta_us = (delta_ticks * 1000000) / s_perf_freq;
89 return s_anchor_realtime_us + delta_us;
100 template<
class T =
int>
103 return static_cast<T
>(
ts.tv_nsec);
110 template<
class T =
int>
120 template<
class T =
int>
203# if TIME_SHIELD_PLATFORM_WINDOWS
204 FILETIME create_time{}, exit_time{}, kernel_time{}, user_time{};
205 if (GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time, &user_time)) {
207 li.LowPart = user_time.dwLowDateTime;
208 li.HighPart = user_time.dwHighDateTime;
209 return static_cast<double>(li.QuadPart) / 10000000.0;
211# elif TIME_SHIELD_PLATFORM_UNIX
213# if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
214 clockid_t
id = (clockid_t)-1;
215# if defined(_POSIX_CPUTIME) && (_POSIX_CPUTIME > 0)
216 if (clock_getcpuclockid(0, &
id) != 0) {
217# if defined(CLOCK_PROCESS_CPUTIME_ID)
218 id = CLOCK_PROCESS_CPUTIME_ID;
219# elif defined(CLOCK_VIRTUAL)
223# elif defined(CLOCK_PROCESS_CPUTIME_ID)
224 id = CLOCK_PROCESS_CPUTIME_ID;
225# elif defined(CLOCK_VIRTUAL)
228 if (
id != (clockid_t)-1) {
230 if (clock_gettime(
id, &
ts) == 0) {
231 return static_cast<double>(
ts.tv_sec) +
static_cast<double>(
ts.tv_nsec) / 1e9;
236# if defined(RUSAGE_SELF)
237 struct rusage usage{};
238 if (getrusage(RUSAGE_SELF, &usage) == 0) {
239 return static_cast<double>(usage.ru_utime.tv_sec) +
static_cast<double>(usage.ru_utime.tv_usec) / 1e6;
243# if defined(_SC_CLK_TCK)
245 if (times(&t) != (clock_t)-1) {
246 return static_cast<double>(t.tms_utime) /
static_cast<double>(sysconf(_SC_CLK_TCK));
250# if defined(CLOCKS_PER_SEC)
251 clock_t cl = clock();
252 if (cl != (clock_t)-1) {
253 return static_cast<double>(cl) /
static_cast<double>(CLOCKS_PER_SEC);
257# warning "get_cpu_time() may not work correctly: unsupported platform"
259 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_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
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 hybrid 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 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.