xrpld
Loading...
Searching...
No Matches
chrono.h
1#pragma once
2
3#include <xrpl/beast/clock/abstract_clock.h>
4#include <xrpl/beast/clock/basic_seconds_clock.h>
5#include <xrpl/beast/clock/manual_clock.h>
6
7#include <date/date.h>
8
9#include <chrono>
10#include <cstdint>
11#include <ratio>
12#include <string>
13#include <type_traits>
14
15namespace xrpl {
16
17// A few handy aliases
18
19using days =
21
23
34
36 date::sys_days{date::year{2000} / 1 / 1} - date::sys_days{date::year{1970} / 1 / 1};
37
38static_assert(kEpochOffset.count() == 946684800);
39
41{
42public:
43 explicit NetClock() = default;
44
49
50 static bool const is_steady = false; // NOLINT(readability-identifier-naming)
51};
52
53template <class Duration>
55to_string(date::sys_time<Duration> tp)
56{
57 return date::format("%Y-%b-%d %T %Z", tp);
58}
59
60inline std::string
62{
63 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
64 using namespace std::chrono;
65 return to_string(system_clock::time_point{tp.time_since_epoch() + kEpochOffset});
66}
67
68template <class Duration>
70toStringIso(date::sys_time<Duration> tp)
71{
72 using namespace std::chrono;
73 return date::format("%FT%TZ", tp);
74}
75
76inline std::string
78{
79 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
80 // Note, NetClock::duration is seconds, as checked by static_assert
82 return toStringIso(date::sys_time<NetClock::duration>{tp.time_since_epoch() + kEpochOffset});
83}
84
91
96
100inline Stopwatch&
102{
103 using Clock = beast::BasicSecondsClock;
104 using Facade = Clock::Clock;
106}
107
108} // namespace xrpl
Abstract interface to a clock.
A clock whose minimum resolution is one second.
Manual clock implementation.
std::ratio< 1 > period
Definition chrono.h:46
std::uint32_t rep
Definition chrono.h:45
NetClock()=default
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
static bool const is_steady
Definition chrono.h:50
std::chrono::duration< rep, period > duration
Definition chrono.h:47
T is_same_v
AbstractClock< Facade > & getAbstractClock()
Returns a global instance of an abstract clock.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static constexpr std::chrono::seconds kEpochOffset
Clock for measuring the network time.
Definition chrono.h:35
beast::AbstractClock< std::chrono::steady_clock > Stopwatch
A clock for measuring elapsed time.
Definition chrono.h:90
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:101
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
std::string toStringIso(date::sys_time< Duration > tp)
Definition chrono.h:70
std::chrono::duration< int, std::ratio_multiply< days::period, std::ratio< 7 > > > weeks
Definition chrono.h:22
std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 24 > > > days
Definition chrono.h:19
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition chrono.h:95
T time_since_epoch(T... args)