12 return static_cast<uint64_t
>(
13 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch())
19 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch())
24template <
typename Rep,
typename Period>
25constexpr std::optional<std::chrono::duration<Rep, Period>>
age(std::chrono::duration<Rep, Period> timestamp,
26 std::chrono::duration<Rep, Period> now)
28 if (timestamp <= timestamp.zero() || timestamp > now) {
31 return now - timestamp;
34constexpr std::optional<std::chrono::microseconds>
age(uint64_t timestampUs, uint64_t
nowUs)
36 return age(std::chrono::microseconds(timestampUs), std::chrono::microseconds(
nowUs));
40constexpr std::chrono::microseconds
remaining(uint64_t timestampUs, uint64_t
nowUs, std::chrono::microseconds lifetime)
42 const auto receiptAge =
age(timestampUs,
nowUs);
43 if (!receiptAge || *receiptAge >= lifetime) {
44 return std::chrono::microseconds::zero();
46 return lifetime - *receiptAge;
50constexpr bool fresh(uint64_t timestampUs, uint64_t
nowUs, std::chrono::microseconds lifetime)
52 return remaining(timestampUs,
nowUs, lifetime) > std::chrono::microseconds::zero();
56constexpr bool withinAge(uint64_t timestampUs, uint64_t
nowUs, std::chrono::microseconds maximumAge)
58 const auto receiptAge =
age(timestampUs,
nowUs);
59 return receiptAge && *receiptAge <= maximumAge;
constexpr std::optional< std::chrono::duration< Rep, Period > > age(std::chrono::duration< Rep, Period > timestamp, std::chrono::duration< Rep, Period > now)
Age of timestamp at now, both in the same unit; empty for a missing or future timestamp.
constexpr bool withinAge(uint64_t timestampUs, uint64_t nowUs, std::chrono::microseconds maximumAge)
Whether a receipt is at most maximumAge old, its limit included.
constexpr bool fresh(uint64_t timestampUs, uint64_t nowUs, std::chrono::microseconds lifetime)
Whether a receipt is younger than lifetime.
constexpr std::chrono::microseconds remaining(uint64_t timestampUs, uint64_t nowUs, std::chrono::microseconds lifetime)
Time left before a receipt reaches lifetime; zero for a missing, future or expired one.