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
14namespace xrpl {
15
16// A few handy aliases
17
18using days =
20
22
32
34 date::sys_days{date::year{2000} / 1 / 1} - date::sys_days{date::year{1970} / 1 / 1};
35
36static_assert(kEpochOffset.count() == 946684800);
37
39{
40public:
41 explicit NetClock() = default;
42
47
48 static bool const is_steady = false; // NOLINT(readability-identifier-naming)
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() + kEpochOffset});
64}
65
66template <class Duration>
68toStringIso(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 toStringIso(date::sys_time<NetClock::duration>{tp.time_since_epoch() + kEpochOffset});
81}
82
88
91
93inline Stopwatch&
95{
96 using Clock = beast::BasicSecondsClock;
97 using Facade = Clock::Clock;
99}
100
101} // 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:44
std::uint32_t rep
Definition chrono.h:43
NetClock()=default
std::chrono::time_point< NetClock > time_point
Definition chrono.h:46
static bool const is_steady
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:45
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:33
beast::AbstractClock< std::chrono::steady_clock > Stopwatch
A clock for measuring elapsed time.
Definition chrono.h:87
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:94
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
std::string toStringIso(date::sys_time< Duration > tp)
Definition chrono.h:68
std::chrono::duration< int, std::ratio_multiply< days::period, std::ratio< 7 > > > weeks
Definition chrono.h:21
std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 24 > > > days
Definition chrono.h:18
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition chrono.h:90
T time_since_epoch(T... args)