rippled
Loading...
Searching...
No Matches
chrono.h
1#ifndef XRPL_BASICS_CHRONO_H_INCLUDED
2#define XRPL_BASICS_CHRONO_H_INCLUDED
3
4#include <xrpl/beast/clock/abstract_clock.h>
5#include <xrpl/beast/clock/basic_seconds_clock.h>
6#include <xrpl/beast/clock/manual_clock.h>
7
8#include <date/date.h>
9
10#include <chrono>
11#include <cstdint>
12#include <ratio>
13#include <string>
14
15namespace ripple {
16
17// A few handy aliases
18
20 int,
22
23using weeks = std::chrono::
24 duration<int, std::ratio_multiply<days::period, std::ratio<7>>>;
25
37 date::sys_days{date::year{2000} / 1 / 1} -
38 date::sys_days{date::year{1970} / 1 / 1};
39
40static_assert(epoch_offset.count() == 946684800);
41
43{
44public:
45 explicit NetClock() = default;
46
51
52 static bool const is_steady = false;
53};
54
55template <class Duration>
57to_string(date::sys_time<Duration> tp)
58{
59 return date::format("%Y-%b-%d %T %Z", tp);
60}
61
62inline std::string
64{
65 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
66 using namespace std::chrono;
67 return to_string(
68 system_clock::time_point{tp.time_since_epoch() + epoch_offset});
69}
70
71template <class Duration>
73to_string_iso(date::sys_time<Duration> tp)
74{
75 using namespace std::chrono;
76 return date::format("%FT%TZ", tp);
77}
78
79inline std::string
81{
82 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
83 // Note, NetClock::duration is seconds, as checked by static_assert
85 return to_string_iso(date::sys_time<NetClock::duration>{
87}
88
94
97
99inline Stopwatch&
101{
102 using Clock = beast::basic_seconds_clock;
103 using Facade = Clock::Clock;
104 return beast::get_abstract_clock<Facade, Clock>();
105}
106
107} // namespace ripple
108
109#endif
A clock whose minimum resolution is one second.
static bool const is_steady
Definition chrono.h:52
NetClock()=default
T count(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:100
static constexpr std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition chrono.h:36
std::string to_string_iso(date::sys_time< Duration > tp)
Definition chrono.h:73
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
T time_since_epoch(T... args)