Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_utils.hpp
Go to the documentation of this file.
1#pragma once
6#ifndef _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
7#define _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
8
9#include "config.hpp"
10#include <ctime>
11
12namespace time_shield {
13
43
46 inline const struct timespec get_timespec_impl() noexcept {
47 // https://en.cppreference.com/w/c/chrono/timespec_get
48 struct timespec ts;
49# if defined(CLOCK_REALTIME)
50 clock_gettime(CLOCK_REALTIME, &ts); // Версия для POSIX
51# else
52 timespec_get(&ts, TIME_UTC);
53# endif
54 return ts;
55 }
56
60 template<class T = int>
61 inline const T ns_of_sec() noexcept {
62 const struct timespec ts = get_timespec_impl();
63 return static_cast<T>(ts.tv_nsec);
64 }
65
69 template<class T = int>
70 const T us_of_sec() noexcept {
71 const struct timespec ts = get_timespec_impl();
72 return ts.tv_nsec / NS_PER_US;
73 }
74
78 template<class T = int>
79 const T ms_of_sec() noexcept {
80 const struct timespec ts = get_timespec_impl();
81 return ts.tv_nsec / NS_PER_MS;
82 }
83
86 inline const ts_t ts() noexcept {
87 const struct timespec ts = get_timespec_impl();
88 return ts.tv_sec;
89 }
90
93 inline const ts_t timestamp() noexcept {
94 const struct timespec ts = get_timespec_impl();
95 return ts.tv_sec;
96 }
97
100 inline const fts_t fts() noexcept {
101 const struct timespec ts = get_timespec_impl();
102 return ts.tv_sec + static_cast<fts_t>(ts.tv_nsec) / static_cast<fts_t>(NS_PER_SEC);
103 }
104
107 inline const fts_t ftimestamp() noexcept {
108 const struct timespec ts = get_timespec_impl();
109 return ts.tv_sec + static_cast<fts_t>(ts.tv_nsec) / static_cast<fts_t>(NS_PER_SEC);
110 }
111
114 inline const ts_ms_t ts_ms() noexcept {
115 const struct timespec ts = get_timespec_impl();
116 return MS_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_MS;
117 }
118
121 inline const ts_ms_t timestamp_ms() noexcept {
122 const struct timespec ts = get_timespec_impl();
123 return MS_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_MS;
124 }
125
128 inline const ts_ms_t now() noexcept {
129 const struct timespec ts = get_timespec_impl();
130 return MS_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_MS;
131 }
132
135 inline const ts_us_t ts_us() noexcept {
136 const struct timespec ts = get_timespec_impl();
137 return US_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_US;
138 }
139
142 inline const ts_us_t timestamp_us() noexcept {
143 const struct timespec ts = get_timespec_impl();
144 return US_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_US;
145 }
146
148
149}; // namespace time_shield
150
151#endif // _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
Header file with preprocessor definitions for C++ standards and constexpr usage.
constexpr int64_t NS_PER_US
Nanoseconds per microsecond.
Definition constants.hpp:32
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition constants.hpp:38
constexpr int64_t NS_PER_MS
Nanoseconds per millisecond.
Definition constants.hpp:33
constexpr int64_t NS_PER_SEC
Nanoseconds per second.
Definition constants.hpp:34
constexpr int64_t US_PER_SEC
Microseconds per second.
Definition constants.hpp:37
TIME_SHIELD_CONSTEXPR const ts_t ts(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0)
Alias for to_timestamp function.
int64_t ts_t
Type for representing timestamps in seconds.
Definition types.hpp:33
int64_t ts_ms_t
Type for representing timestamps in milliseconds.
Definition types.hpp:34
double fts_t
Type for representing timestamps as floating-point numbers (e.g., fractional seconds).
Definition types.hpp:36
int64_t ts_us_t
Type for representing timestamps in microseconds.
Definition types.hpp:35
const ts_ms_t timestamp_ms() noexcept
Get the current UTC timestamp in milliseconds.
const ts_us_t ts_us() noexcept
Get the current UTC timestamp in microseconds.
const T us_of_sec() noexcept
Get the microsecond part of the current second.
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.
const ts_t timestamp() noexcept
Get the current UTC timestamp in seconds.
const ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
const T ns_of_sec() noexcept
Get the nanosecond part of the current second.
const fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
const fts_t ftimestamp() noexcept
Get the current UTC timestamp in floating-point seconds.
const struct timespec get_timespec_impl() noexcept
Get the current timespec.
const T ms_of_sec() noexcept
Get the millisecond part of the current second.
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
Main namespace for the Time Shield library.
Definition constants.hpp:12