9#include <userver/utils/assert.hpp>
11USERVER_NAMESPACE_BEGIN
18 using Clock = std::chrono::steady_clock;
19 using TimePoint = Clock::time_point;
20 using Duration = TimePoint::duration;
26 constexpr bool IsReachable()
const noexcept {
return value_ != TimePoint{}; }
44 template <
typename Rep,
typename Period>
45 static Deadline
FromDuration(
const std::chrono::duration<Rep, Period>& incoming_duration)
noexcept {
46 using IncomingDuration = std::chrono::duration<Rep, Period>;
48 if (incoming_duration.count() < 0) {
52 const auto now = TimePoint::clock::now();
53 constexpr auto max_now = TimePoint::clock::time_point::max();
62 static_assert(std::is_constructible_v<Duration, IncomingDuration>);
67 if (incoming_duration > std::chrono::duration_cast<IncomingDuration>(Duration::max())) {
68 OnDurationOverflow(std::chrono::duration_cast<std::chrono::duration<
double>>(incoming_duration));
73 UASSERT(max_now - now >= Duration{incoming_duration});
75 return Deadline(now + Duration{incoming_duration});
83 template <
typename Clock,
typename Duration>
84 static Deadline
FromTimePoint(
const std::chrono::time_point<Clock, Duration>& time_point)
noexcept {
85 return FromDuration(time_point - Clock::now());
90 constexpr static Deadline FromTimePoint(
const TimePoint& time_point) {
return Deadline(time_point); }
94 constexpr static Deadline
Passed()
noexcept {
return Deadline{kPassed}; }
96 constexpr bool operator==(
const Deadline& r)
const noexcept {
return value_ == r.value_; }
98 constexpr bool operator<(
const Deadline& r)
const noexcept {
99 if (!IsReachable())
return false;
100 if (!r.IsReachable())
return true;
101 return value_ < r.value_;
105 constexpr explicit Deadline(TimePoint value)
noexcept : value_(value) {}
107 static void OnDurationOverflow(std::chrono::duration<
double> incoming_duration);
109 static constexpr TimePoint kPassed = TimePoint::min();