rippled
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
14namespace xrpl {
15
16// A few handy aliases
17
19
21
33 date::sys_days{date::year{2000} / 1 / 1} - date::sys_days{date::year{1970} / 1 / 1};
34
35static_assert(epoch_offset.count() == 946684800);
36
38{
39public:
40 explicit NetClock() = default;
41
46
47 static bool const is_steady = false;
48};
49
50template <class Duration>
52to_string(date::sys_time<Duration> tp)
53{
54 return date::format("%Y-%b-%d %T %Z", tp);
55}
56
57inline std::string
59{
60 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
61 using namespace std::chrono;
62 return to_string(system_clock::time_point{tp.time_since_epoch() + epoch_offset});
63}
64
65template <class Duration>
67to_string_iso(date::sys_time<Duration> tp)
68{
69 using namespace std::chrono;
70 return date::format("%FT%TZ", tp);
71}
72
73inline std::string
75{
76 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
77 // Note, NetClock::duration is seconds, as checked by static_assert
79 return to_string_iso(date::sys_time<NetClock::duration>{tp.time_since_epoch() + epoch_offset});
80}
81
87
90
92inline Stopwatch&
94{
95 using Clock = beast::basic_seconds_clock;
96 using Facade = Clock::Clock;
97 return beast::get_abstract_clock<Facade, Clock>();
98}
99
100} // namespace xrpl
A clock whose minimum resolution is one second.
NetClock()=default
static bool const is_steady
Definition chrono.h:47
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:5
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:93
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
std::string to_string_iso(date::sys_time< Duration > tp)
Definition chrono.h:67
static constexpr std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition chrono.h:32
T time_since_epoch(T... args)