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
16 inline const struct timespec get_timespec_impl() noexcept {
17 // https://en.cppreference.com/w/c/chrono/timespec_get
18 struct timespec ts;
19# if defined(CLOCK_REALTIME)
20 clock_gettime(CLOCK_REALTIME, &ts); // Версия для POSIX
21# else
22 timespec_get(&ts, TIME_UTC);
23# endif
24 return ts;
25 }
26
30 template<class T = int>
31 inline const T ns_of_sec() noexcept {
32 const struct timespec ts = get_timespec_impl();
33 return static_cast<T>(ts.tv_nsec);
34 }
35
39 template<class T = int>
40 const T us_of_sec() noexcept {
41 const struct timespec ts = get_timespec_impl();
42 return ts.tv_nsec / NS_PER_US;
43 }
44
48 template<class T = int>
49 const T ms_of_sec() noexcept {
50 const struct timespec ts = get_timespec_impl();
51 return ts.tv_nsec / NS_PER_MS;
52 }
53
56 inline const ts_t ts() noexcept {
57 const struct timespec ts = get_timespec_impl();
58 return ts.tv_sec;
59 }
60
63 inline const ts_t timestamp() noexcept {
64 const struct timespec ts = get_timespec_impl();
65 return ts.tv_sec;
66 }
67
70 inline const fts_t fts() noexcept {
71 const struct timespec ts = get_timespec_impl();
72 return ts.tv_sec + static_cast<fts_t>(ts.tv_nsec) / static_cast<fts_t>(NS_PER_SEC);
73 }
74
77 inline const fts_t ftimestamp() noexcept {
78 const struct timespec ts = get_timespec_impl();
79 return ts.tv_sec + static_cast<fts_t>(ts.tv_nsec) / static_cast<fts_t>(NS_PER_SEC);
80 }
81
84 inline const ts_ms_t ts_ms() noexcept {
85 const struct timespec ts = get_timespec_impl();
86 return MS_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_MS;
87 }
88
91 inline const ts_ms_t timestamp_ms() noexcept {
92 const struct timespec ts = get_timespec_impl();
93 return MS_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_MS;
94 }
95
98 inline const ts_ms_t now() noexcept {
99 const struct timespec ts = get_timespec_impl();
100 return MS_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_MS;
101 }
102
105 inline const ts_us_t ts_us() noexcept {
106 const struct timespec ts = get_timespec_impl();
107 return US_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_US;
108 }
109
112 inline const ts_us_t timestamp_us() noexcept {
113 const struct timespec ts = get_timespec_impl();
114 return US_PER_SEC * ts.tv_sec + ts.tv_nsec / NS_PER_US;
115 }
116}; // namespace time_shield
117
118#endif // _TIME_SHIELD_TIME_UTILS_HPP_INCLUDED
Header file with preprocessor definitions.
Main namespace for the Time Shield library.
Definition constants.hpp:12
int64_t ts_t
Integer timestamp type.
Definition types.hpp:15
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.
constexpr int64_t NS_PER_US
Nanoseconds per microsecond.
Definition constants.hpp:15
TIME_SHIELD_CONSTEXPR const ts_t ts(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0)
Alias for to_timestamp function.
int64_t ts_ms_t
Integer timestamp milliseconds type.
Definition types.hpp:16
const ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition constants.hpp:21
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.
double fts_t
Floating point timestamp type.
Definition types.hpp:18
constexpr int64_t NS_PER_MS
Nanoseconds per millisecond.
Definition constants.hpp:16
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.
int64_t ts_us_t
Integer timestamp microseconds type.
Definition types.hpp:17
const struct timespec get_timespec_impl() noexcept
Get the current timespec.
constexpr int64_t NS_PER_SEC
Nanoseconds per second.
Definition constants.hpp:17
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.
constexpr int64_t US_PER_SEC
Microseconds per second.
Definition constants.hpp:20