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 <date/date.h>
24
25#include <xrpl/beast/clock/abstract_clock.h>
26#include <xrpl/beast/clock/basic_seconds_clock.h>
27#include <xrpl/beast/clock/manual_clock.h>
28
29#include <chrono>
30#include <cstdint>
31#include <ratio>
32#include <string>
33#include <type_traits>
34
35namespace ripple {
36
37// A few handy aliases
38
40 int,
42
43using weeks = std::chrono::
44 duration<int, std::ratio_multiply<days::period, std::ratio<7>>>;
45
57 date::sys_days{date::year{2000} / 1 / 1} -
58 date::sys_days{date::year{1970} / 1 / 1};
59
60static_assert(epoch_offset.count() == 946684800);
61
63{
64public:
65 explicit NetClock() = default;
66
71
72 static bool const is_steady = false;
73};
74
75template <class Duration>
77to_string(date::sys_time<Duration> tp)
78{
79 return date::format("%Y-%b-%d %T %Z", tp);
80}
81
82inline std::string
84{
85 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
86 using namespace std::chrono;
87 return to_string(
88 system_clock::time_point{tp.time_since_epoch() + epoch_offset});
89}
90
91template <class Duration>
93to_string_iso(date::sys_time<Duration> tp)
94{
95 using namespace std::chrono;
96 return date::format("%FT%TZ", tp);
97}
98
99inline std::string
101{
102 // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
103 // Note, NetClock::duration is seconds, as checked by static_assert
104 static_assert(std::is_same_v<NetClock::duration::period, std::ratio<1>>);
105 return to_string_iso(date::sys_time<NetClock::duration>{
107}
108
114
117
119inline Stopwatch&
121{
122 using Clock = beast::basic_seconds_clock;
123 using Facade = Clock::Clock;
124 return beast::get_abstract_clock<Facade, Clock>();
125}
126
127} // namespace ripple
128
129#endif
A clock whose minimum resolution is one second.
static bool const is_steady
Definition: chrono.h:72
NetClock()=default
T count(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:120
static constexpr std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition: chrono.h:56
std::string to_string_iso(date::sys_time< Duration > tp)
Definition: chrono.h:93
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
T time_since_epoch(T... args)