2#ifndef _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
3#define _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
21#elif defined(__unix__) || defined(__APPLE__)
23 #include <sys/resource.h>
24 #include <sys/times.h>
27 #error "Unsupported platform for get_cpu_time()"
38# if defined(CLOCK_REALTIME)
39 clock_gettime(CLOCK_REALTIME, &
ts);
41 timespec_get(&
ts, TIME_UTC);
57 static std::once_flag init_flag;
58 static int64_t s_perf_freq = 0;
59 static int64_t s_anchor_perf = 0;
60 static int64_t s_anchor_realtime_us = 0;
62 std::call_once(init_flag, [] {
63 LARGE_INTEGER freq, counter;
64 QueryPerformanceFrequency(&freq);
65 QueryPerformanceCounter(&counter);
67 s_perf_freq = freq.QuadPart;
68 s_anchor_perf = counter.QuadPart;
71 GetSystemTimeAsFileTime(&ft);
73 uli.LowPart = ft.dwLowDateTime;
74 uli.HighPart = ft.dwHighDateTime;
76 s_anchor_realtime_us = (uli.QuadPart - 116444736000000000ULL) / 10;
80 QueryPerformanceCounter(&
now);
82 int64_t delta_ticks =
now.QuadPart - s_anchor_perf;
83 int64_t delta_us = (delta_ticks * 1000000) / s_perf_freq;
85 return s_anchor_realtime_us + delta_us;
87# error "now_realtime_us() is only supported on Windows."
95 template<
class T =
int>
98 return static_cast<T
>(
ts.tv_nsec);
105 template<
class T =
int>
115 template<
class T =
int>
199 FILETIME create_time{}, exit_time{}, kernel_time{}, user_time{};
200 if (GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time, &user_time)) {
202 li.LowPart = user_time.dwLowDateTime;
203 li.HighPart = user_time.dwHighDateTime;
204 return static_cast<double>(li.QuadPart) / 10000000.0;
206# elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
208# if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
209 clockid_t
id = (clockid_t)-1;
210# if defined(_POSIX_CPUTIME) && (_POSIX_CPUTIME > 0)
211 if (clock_getcpuclockid(0, &
id) != 0) {
212# if defined(CLOCK_PROCESS_CPUTIME_ID)
213 id = CLOCK_PROCESS_CPUTIME_ID;
214# elif defined(CLOCK_VIRTUAL)
218# elif defined(CLOCK_PROCESS_CPUTIME_ID)
219 id = CLOCK_PROCESS_CPUTIME_ID;
220# elif defined(CLOCK_VIRTUAL)
223 if (
id != (clockid_t)-1) {
225 if (clock_gettime(
id, &
ts) == 0) {
226 return static_cast<double>(
ts.tv_sec) +
static_cast<double>(
ts.tv_nsec) / 1e9;
231# if defined(RUSAGE_SELF)
232 struct rusage usage{};
233 if (getrusage(RUSAGE_SELF, &usage) == 0) {
234 return static_cast<double>(usage.ru_utime.tv_sec) +
static_cast<double>(usage.ru_utime.tv_usec) / 1e6;
238# if defined(_SC_CLK_TCK)
240 if (times(&t) != (clock_t)-1) {
241 return static_cast<double>(t.tms_utime) /
static_cast<double>(sysconf(_SC_CLK_TCK));
245# if defined(CLOCKS_PER_SEC)
246 clock_t cl = clock();
247 if (cl != (clock_t)-1) {
248 return static_cast<double>(cl) /
static_cast<double>(CLOCKS_PER_SEC);
252# warning "get_cpu_time() may not work correctly: unsupported platform"
254 return std::numeric_limits<double>::quiet_NaN();
Header file with preprocessor definitions for C++ standards and constexpr usage.
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.
const ts_us_t timestamp_us() noexcept
Get the current UTC timestamp in microseconds.
const ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
int64_t now_realtime_us()
Get current real time in microseconds using a hybrid method.
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
const T us_of_sec() noexcept
Get the microsecond part of the current second.
const ts_t timestamp() noexcept
Get the current UTC timestamp in seconds.
const 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.
const T ns_of_sec() noexcept
Get the nanosecond part of the current second.
const ts_us_t ts_us() noexcept
Get the current UTC timestamp in microseconds.
const ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
const struct timespec get_timespec_impl() noexcept
Get the current timespec.
const ts_ms_t timestamp_ms() noexcept
Get the current UTC timestamp in milliseconds.
const fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
const T ms_of_sec() noexcept
Get the millisecond part of the current second.
Main namespace for the Time Shield library.
Type definitions for time-related units and formats.