rippled
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 <ripple/beast/clock/abstract_clock.h>
26 #include <ripple/beast/clock/basic_seconds_clock.h>
27 #include <ripple/beast/clock/manual_clock.h>
28 
29 #include <chrono>
30 #include <cstdint>
31 #include <ratio>
32 #include <string>
33 #include <type_traits>
34 
35 namespace ripple {
36 
37 // A few handy aliases
38 
40  int,
42 
43 using 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 
60 static_assert(epoch_offset.count() == 946684800);
61 
62 class NetClock
63 {
64 public:
65  explicit NetClock() = default;
66 
67  using rep = std::uint32_t;
71 
72  static bool const is_steady = false;
73 };
74 
75 template <class Duration>
77 to_string(date::sys_time<Duration> tp)
78 {
79  return date::format("%Y-%b-%d %T %Z", tp);
80 }
81 
82 inline 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 
91 template <class Duration>
93 to_string_iso(date::sys_time<Duration> tp)
94 {
95  using namespace std::chrono;
96  return date::format("%FT%TZ", tp);
97 }
98 
99 inline 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 
119 inline 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
std::is_same_v
T is_same_v
std::string
STL class.
std::chrono::duration
ripple::NetClock::NetClock
NetClock()=default
ripple::to_string_iso
std::string to_string_iso(date::sys_time< Duration > tp)
Definition: chrono.h:93
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:120
ripple::epoch_offset
constexpr static std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition: chrono.h:56
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
beast::basic_seconds_clock
A clock whose minimum resolution is one second.
Definition: basic_seconds_clock.h:36
ratio
chrono
std::chrono::time_point
cstdint
std::uint32_t
beast::abstract_clock< std::chrono::steady_clock >
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NetClock::is_steady
static const bool is_steady
Definition: chrono.h:72
std::chrono::seconds::count
T count(T... args)
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:38
ripple::days
std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 24 > >> days
Definition: chrono.h:41
beast::manual_clock< std::chrono::steady_clock >
ripple::NetClock
Definition: chrono.h:62
std::ratio_multiply
type_traits
std::chrono
string