CatapultServer  v0.5.0.1 (Elephant)
TimeSpan.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/types.h"
23 #include <chrono>
24 #include <iosfwd>
25 #include <stdint.h>
26 
27 namespace catapult { namespace utils {
28 
30  class TimeSpan final {
31  private:
32  template<typename TRep, typename TPeriod>
33  constexpr explicit TimeSpan(const std::chrono::duration<TRep, TPeriod>& duration)
34  : m_millis(std::chrono::duration_cast<std::chrono::milliseconds>(duration))
35  {}
36 
37  public:
39  constexpr TimeSpan() : TimeSpan(std::chrono::milliseconds(0))
40  {}
41 
42  public:
44  static constexpr TimeSpan FromHours(uint64_t hours) {
45  return TimeSpan(std::chrono::hours(hours));
46  }
47 
49  static constexpr TimeSpan FromMinutes(uint64_t minutes) {
50  return TimeSpan(std::chrono::minutes(minutes));
51  }
52 
54  static constexpr TimeSpan FromSeconds(uint64_t seconds) {
55  return TimeSpan(std::chrono::seconds(seconds));
56  }
57 
59  static constexpr TimeSpan FromMilliseconds(uint64_t milliseconds) {
60  return TimeSpan(std::chrono::milliseconds(milliseconds));
61  }
62 
64  static constexpr TimeSpan FromDifference(Timestamp end, Timestamp start) {
65  return FromMilliseconds((end - start).unwrap());
66  }
67 
68  private:
69  template<typename TDuration>
70  static constexpr uint64_t ConvertToDuration(const std::chrono::milliseconds& millis) {
71  return static_cast<uint64_t>(std::chrono::duration_cast<TDuration>(millis).count());
72  }
73 
74  public:
76  constexpr uint64_t hours() const {
77  return ConvertToDuration<std::chrono::hours>(m_millis);
78  }
79 
81  constexpr uint64_t minutes() const {
82  return ConvertToDuration<std::chrono::minutes>(m_millis);
83  }
84 
86  constexpr uint64_t seconds() const {
87  return ConvertToDuration<std::chrono::seconds>(m_millis);
88  }
89 
91  constexpr uint64_t millis() const {
92  return static_cast<uint64_t>(m_millis.count());
93  }
94 
95  public:
97  constexpr bool operator==(const TimeSpan& rhs) const {
98  return m_millis == rhs.m_millis;
99  }
100 
102  constexpr bool operator!=(const TimeSpan& rhs) const {
103  return !(*this == rhs);
104  }
105 
107  constexpr bool operator>=(const TimeSpan& rhs) const {
108  return m_millis >= rhs.m_millis;
109  }
110 
112  constexpr bool operator>(const TimeSpan& rhs) const {
113  return m_millis > rhs.m_millis;
114  }
115 
117  constexpr bool operator<=(const TimeSpan& rhs) const {
118  return m_millis <= rhs.m_millis;
119  }
120 
122  constexpr bool operator<(const TimeSpan& rhs) const {
123  return m_millis < rhs.m_millis;
124  }
125 
126  private:
127  std::chrono::milliseconds m_millis;
128  };
129 
131  std::ostream& operator<<(std::ostream& out, const TimeSpan& timeSpan);
132 
134  constexpr Timestamp operator+(const Timestamp& timestamp, const TimeSpan& timeSpan) {
135  return timestamp + Timestamp(timeSpan.millis());
136  }
137 
139  constexpr Timestamp SubtractNonNegative(const Timestamp& timestamp, const TimeSpan& timeSpan) {
140  return Timestamp(timestamp.unwrap() < timeSpan.millis() ? 0u : timestamp.unwrap() - timeSpan.millis());
141  }
142 }}
catapult::utils::SubtractNonNegative
constexpr Timestamp SubtractNonNegative(const Timestamp &timestamp, const TimeSpan &timeSpan)
Subtracts timeSpan from timestamp and returns the maximum of the difference and zero.
Definition: TimeSpan.h:139
catapult::utils::TimeSpan::m_millis
std::chrono::milliseconds m_millis
Definition: TimeSpan.h:127
catapult::utils::TimeSpan::operator==
constexpr bool operator==(const TimeSpan &rhs) const
Returns true if this time span is equal to rhs.
Definition: TimeSpan.h:97
catapult::utils::TimeSpan::operator>=
constexpr bool operator>=(const TimeSpan &rhs) const
Returns true if this time span is greater than or equal to rhs.
Definition: TimeSpan.h:107
catapult::utils::TimeSpan::FromDifference
static constexpr TimeSpan FromDifference(Timestamp end, Timestamp start)
Creates a time span from the difference between start and end.
Definition: TimeSpan.h:64
catapult::utils::TimeSpan::FromMilliseconds
static constexpr TimeSpan FromMilliseconds(uint64_t milliseconds)
Creates a time span from the given number of milliseconds.
Definition: TimeSpan.h:59
catapult::utils::TimeSpan
Represents a time duration.
Definition: TimeSpan.h:30
IntegerMath.h
catapult::utils::BasicBaseValue::unwrap
constexpr ValueType unwrap() const
Unwraps this value and returns the underlying raw value.
Definition: BaseValue.h:53
catapult::utils::operator+
constexpr Timestamp operator+(const Timestamp &timestamp, const TimeSpan &timeSpan)
Adds timestamp and a timeSpan resulting in new timestamp.
Definition: TimeSpan.h:134
catapult::utils::BaseValue< uint64_t, Timestamp_tag >
catapult::utils::TimeSpan::FromSeconds
static constexpr TimeSpan FromSeconds(uint64_t seconds)
Creates a time span from the given number of seconds.
Definition: TimeSpan.h:54
StreamFormatGuard.h
catapult::utils::TimeSpan::ConvertToDuration
static constexpr uint64_t ConvertToDuration(const std::chrono::milliseconds &millis)
Definition: TimeSpan.h:70
catapult::utils::TimeSpan::seconds
constexpr uint64_t seconds() const
Returns the number of seconds.
Definition: TimeSpan.h:86
catapult::utils::TimeSpan::TimeSpan
constexpr TimeSpan(const std::chrono::duration< TRep, TPeriod > &duration)
Definition: TimeSpan.h:33
catapult::utils::TimeSpan::hours
constexpr uint64_t hours() const
Returns the number of hours.
Definition: TimeSpan.h:76
catapult::utils::TimeSpan::FromHours
static constexpr TimeSpan FromHours(uint64_t hours)
Creates a time span from the given number of hours.
Definition: TimeSpan.h:44
catapult::utils::TimeSpan::operator!=
constexpr bool operator!=(const TimeSpan &rhs) const
Returns true if this time span is not equal to rhs.
Definition: TimeSpan.h:102
catapult::utils::TimeSpan::TimeSpan
constexpr TimeSpan()
Creates a default (zero) time span.
Definition: TimeSpan.h:39
catapult::utils::TimeSpan::FromMinutes
static constexpr TimeSpan FromMinutes(uint64_t minutes)
Creates a time span from the given number of minutes.
Definition: TimeSpan.h:49
catapult::utils::operator<<
std::ostream & operator<<(std::ostream &out, const BlockSpan &blockSpan)
Insertion operator for outputting blockSpan to out.
Definition: BlockSpan.cpp:27
catapult::utils::TimeSpan::operator<=
constexpr bool operator<=(const TimeSpan &rhs) const
Returns true if this time span is less than or equal to rhs.
Definition: TimeSpan.h:117
catapult::utils::TimeSpan::millis
constexpr uint64_t millis() const
Returns the number of milliseconds.
Definition: TimeSpan.h:91
TimeSpan.h
catapult::utils::StreamFormatGuard
Definition: StreamFormatGuard.h:29
types.h
catapult::utils::TimeSpan::minutes
constexpr uint64_t minutes() const
Returns the number of minutes.
Definition: TimeSpan.h:81
catapult::utils::TimeSpan::operator>
constexpr bool operator>(const TimeSpan &rhs) const
Returns true if this time span is greater than rhs.
Definition: TimeSpan.h:112
catapult::Timestamp
utils::BaseValue< uint64_t, Timestamp_tag > Timestamp
Definition: src/catapult/types.h:73
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::TimeSpan::operator<
constexpr bool operator<(const TimeSpan &rhs) const
Returns true if this time span is less than rhs.
Definition: TimeSpan.h:122