rippled
Loading...
Searching...
No Matches
chrono.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_BASICS_CHRONO_H_INCLUDED
21#define RIPPLE_BASICS_CHRONO_H_INCLUDED
22
23#include <xrpl/beast/clock/abstract_clock.h>
24#include <xrpl/beast/clock/basic_seconds_clock.h>
25#include <xrpl/beast/clock/manual_clock.h>
26
27#include <date/date.h>
28
29#include <chrono>
30#include <cstdint>
31#include <ratio>
32#include <string>
33
34namespace ripple {
35
36// A few handy aliases
37
39 int,
41
42using weeks = std::chrono::
43 duration<int, std::ratio_multiply<days::period, std::ratio<7>>>;
44
56 date::sys_days{date::year{2000} / 1 / 1} -
57 date::sys_days{date::year{1970} / 1 / 1};
58
59static_assert(epoch_offset.count() == 946684800);
60
62{
63public:
64 explicit NetClock() = default;
65
70
71 static bool const is_steady = false;
72};
73
74template <class Duration>
76to_string(date::sys_time<Duration> tp)
77{
78 return date::format("%Y-%b-%d %T %Z", tp);
79}
80
81inline std::string
83{
84 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
85 using namespace std::chrono;
86 return to_string(
87 system_clock::time_point{tp.time_since_epoch() + epoch_offset});
88}
89
90template <class Duration>
92to_string_iso(date::sys_time<Duration> tp)
93{
94 using namespace std::chrono;
95 return date::format("%FT%TZ", tp);
96}
97
98inline std::string
100{
101 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
102 // Note, NetClock::duration is seconds, as checked by static_assert
104 return to_string_iso(date::sys_time<NetClock::duration>{
106}
107
113
116
118inline Stopwatch&
120{
121 using Clock = beast::basic_seconds_clock;
122 using Facade = Clock::Clock;
123 return beast::get_abstract_clock<Facade, Clock>();
124}
125
126} // namespace ripple
127
128#endif
A clock whose minimum resolution is one second.
static bool const is_steady
Definition chrono.h:71
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:25
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:119
static constexpr std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition chrono.h:55
std::string to_string_iso(date::sys_time< Duration > tp)
Definition chrono.h:92
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
T time_since_epoch(T... args)