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>
46 const std::chrono::duration<Rep, Period>& incoming_duration)
noexcept {
47 using IncomingDuration = std::chrono::duration<Rep, Period>;
49 if (incoming_duration.count() < 0) {
53 const auto now = TimePoint::clock::now();
54 constexpr auto max_now = TimePoint::clock::time_point::max();
63 static_assert(std::is_constructible_v<Duration, IncomingDuration>);
68 if (incoming_duration >
69 std::chrono::duration_cast<IncomingDuration>(Duration::max())) {
71 std::chrono::duration_cast<std::chrono::duration<
double>>(
77 UASSERT(max_now - now >= Duration{incoming_duration});
79 return Deadline(now + Duration{incoming_duration});
87 template <
typename Clock,
typename Duration>
89 const std::chrono::time_point<Clock, Duration>& time_point)
noexcept {
90 return FromDuration(time_point - Clock::now());
95 constexpr static Deadline FromTimePoint(
const TimePoint& time_point) {
96 return Deadline(time_point);
101 constexpr static Deadline
Passed()
noexcept {
return Deadline{kPassed}; }
103 constexpr bool operator==(
const Deadline& r)
const noexcept {
104 return value_ == r.value_;
107 constexpr bool operator<(
const Deadline& r)
const noexcept {
108 if (!IsReachable())
return false;
109 if (!r.IsReachable())
return true;
110 return value_ < r.value_;
114 constexpr explicit Deadline(TimePoint value)
noexcept : value_(value) {}
116 static void OnDurationOverflow(
117 std::chrono::duration<
double> incoming_duration);
119 static constexpr TimePoint kPassed = TimePoint::min();