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
18using days =
20
22
34 date::sys_days{date::year{2000} / 1 / 1} - date::sys_days{date::year{1970} / 1 / 1};
35
36static_assert(epoch_offset.count() == 946684800);
37
39{
40public:
41 explicit NetClock() = default;
42
47
48 static bool const is_steady = false;
49};
50
51template <class Duration>
53to_string(date::sys_time<Duration> tp)
54{
55 return date::format("%Y-%b-%d %T %Z", tp);
56}
57
58inline std::string
60{
61 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
62 using namespace std::chrono;
63 return to_string(system_clock::time_point{tp.time_since_epoch() + epoch_offset});
64}
65
66template <class Duration>
68to_string_iso(date::sys_time<Duration> tp)
69{
70 using namespace std::chrono;
71 return date::format("%FT%TZ", tp);
72}
73
74inline std::string
76{
77 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
78 // Note, NetClock::duration is seconds, as checked by static_assert
80 return to_string_iso(date::sys_time<NetClock::duration>{tp.time_since_epoch() + epoch_offset});
81}
82
88
91
93inline Stopwatch&
95{
96 using Clock = beast::basic_seconds_clock;
97 using Facade = Clock::Clock;
98 return beast::get_abstract_clock<Facade, Clock>();
99}
100
101} // namespace xrpl
A clock whose minimum resolution is one second.
NetClock()=default
static bool const is_steady
Definition chrono.h:48
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:94
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
std::string to_string_iso(date::sys_time< Duration > tp)
Definition chrono.h:68
static constexpr std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition chrono.h:33
T time_since_epoch(T... args)