3#ifndef _TIME_SHIELD_DEADLINE_TIMER_HPP_INCLUDED
4#define _TIME_SHIELD_DEADLINE_TIMER_HPP_INCLUDED
28 using clock = std::chrono::steady_clock;
41 template<
class Rep,
class Period>
42 explicit DeadlineTimer(std::chrono::duration<Rep, Period> timeout)
noexcept {
59 template<
class Rep,
class Period>
91 template<
class Rep,
class Period>
92 void start(std::chrono::duration<Rep, Period> timeout)
noexcept {
94 if (timeout <=
decltype(timeout)::zero()) {
99 duration safe_duration = std::chrono::duration_cast<duration>(timeout);
100 if (safe_duration <= duration::zero()) {
104 const time_point max_time = (time_point::max)();
106 if (safe_duration >= max_offset) {
116 start(std::chrono::seconds(timeout_sec));
121 start(std::chrono::milliseconds(timeout_ms));
153 return std::chrono::duration_cast<std::chrono::milliseconds>(
m_deadline.time_since_epoch()).count();
158 return std::chrono::duration_cast<std::chrono::seconds>(
m_deadline.time_since_epoch()).count();
168 const duration since_epoch = std::chrono::duration_cast<duration>(std::chrono::milliseconds(now_ms));
174 const duration since_epoch = std::chrono::duration_cast<duration>(std::chrono::seconds(now_sec));
190 return std::chrono::duration_cast<std::chrono::milliseconds>(
remaining_time()).count();
195 return std::chrono::duration_cast<std::chrono::seconds>(
remaining_time()).count();
203 return duration::zero();
216 const duration max_offset = (time_point::max)() - base;
217 const duration safe_offset = extend_by < max_offset ? extend_by : max_offset;
223 if (extend_by_sec <= 0) {
226 add(std::chrono::seconds(extend_by_sec));
231 if (extend_by_ms <= 0) {
234 add(std::chrono::milliseconds(extend_by_ms));
static DeadlineTimer from_timeout(duration timeout) noexcept
Creates a timer that expires after the specified timeout.
void start(std::chrono::duration< Rep, Period > timeout) noexcept
Starts the timer so it expires after the specified timeout.
void add_ms(ts_ms_t extend_by_ms) noexcept
Extends deadline by the specified number of milliseconds while preventing overflow.
TIME_SHIELD_NODISCARD ts_t deadline_sec() const noexcept
Returns stored deadline as seconds since the steady epoch.
void add(duration extend_by) noexcept
Extends deadline by the specified duration while preventing overflow.
void start_sec(ts_t timeout_sec) noexcept
Starts the timer so it expires after the specified number of seconds.
TIME_SHIELD_NODISCARD time_point deadline() const noexcept
Returns stored deadline.
TIME_SHIELD_NODISCARD bool has_expired_sec(ts_t now_sec) const noexcept
Checks if the deadline has expired relative to the provided second timestamp.
TIME_SHIELD_NODISCARD ts_t remaining_time_sec() const noexcept
Returns remaining time in seconds until the deadline.
DeadlineTimer(std::chrono::duration< Rep, Period > timeout) noexcept
Constructs a timer that expires after the given timeout.
std::chrono::steady_clock clock
TIME_SHIELD_NODISCARD bool has_expired() const noexcept
Checks if the deadline has already expired.
void start(time_point deadline) noexcept
Sets the absolute deadline and marks the timer as active.
static DeadlineTimer from_timeout(std::chrono::duration< Rep, Period > timeout) noexcept
Creates a timer that expires after the specified timeout.
clock::time_point time_point
TIME_SHIELD_NODISCARD bool has_expired(time_point now) const noexcept
Checks if the deadline has expired relative to the provided time point.
DeadlineTimer(ts_ms_t timeout_ms) noexcept
Constructs a timer that expires after the given number of milliseconds.
static DeadlineTimer from_timeout_ms(ts_ms_t timeout_ms) noexcept
Creates a timer that expires after the specified number of milliseconds.
void start_ms(ts_ms_t timeout_ms) noexcept
Starts the timer so it expires after the specified number of milliseconds.
void add_sec(ts_t extend_by_sec) noexcept
Extends deadline by the specified number of seconds while preventing overflow.
TIME_SHIELD_NODISCARD ts_ms_t deadline_ms() const noexcept
Returns stored deadline as milliseconds since the steady epoch.
static DeadlineTimer from_timeout_sec(ts_t timeout_sec) noexcept
Creates a timer that expires after the specified number of seconds.
TIME_SHIELD_NODISCARD bool is_running() const noexcept
Checks whether the timer currently tracks a deadline.
TIME_SHIELD_NODISCARD duration remaining_time() const noexcept
Returns time remaining until the deadline.
void stop() noexcept
Stops the timer and invalidates the stored deadline.
void set_forever() noexcept
Marks the timer as running forever (no timeout).
DeadlineTimer() noexcept=default
Constructs an inactive timer.
TIME_SHIELD_NODISCARD bool has_expired_ms(ts_ms_t now_ms) const noexcept
Checks if the deadline has expired relative to the provided millisecond timestamp.
TIME_SHIELD_NODISCARD bool is_forever() const noexcept
Checks whether the timer is configured for an infinite timeout.
TIME_SHIELD_NODISCARD ts_ms_t remaining_time_ms() const noexcept
Returns remaining time in milliseconds until the deadline.
TIME_SHIELD_NODISCARD duration remaining_time(time_point now) const noexcept
Returns remaining time relative to the provided time point.
Configuration macros for the library.
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.
ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
Main namespace for the Time Shield library.
Type definitions for time-related units and formats.