CppCommon  1.0.4.1
C++ Common Library
timespan.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_TIME_TIMESPAN_H
10 #define CPPCOMMON_TIME_TIMESPAN_H
11 
12 #include <chrono>
13 
14 #include "errors/exceptions.h"
15 
16 namespace CppCommon {
17 
19 
26 class Timespan
27 {
28 public:
30  Timespan() noexcept : _duration(0) {}
32 
35  explicit Timespan(int64_t duration) noexcept : _duration(duration) {}
37 
40  template <class Rep, class Period>
41  explicit Timespan(const std::chrono::duration<Rep, Period>& duration) noexcept : _duration(std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count()) {}
42  Timespan(const Timespan&) noexcept = default;
43  Timespan(Timespan&&) noexcept = default;
44  ~Timespan() noexcept = default;
45 
46  Timespan& operator=(int64_t duration) noexcept
47  { _duration = duration; return *this; }
48  Timespan& operator=(const Timespan&) noexcept = default;
49  Timespan& operator=(Timespan&&) noexcept = default;
50 
51  // Timespan offset operations
52  Timespan operator+() const
53  { return Timespan(+_duration); }
55  { return Timespan(-_duration); }
56 
57  Timespan& operator+=(int64_t offset) noexcept
58  { _duration += offset; return *this; }
59  Timespan& operator+=(const Timespan& offset) noexcept
60  { _duration += offset.total(); return *this; }
61 
62  Timespan& operator-=(int64_t offset) noexcept
63  { _duration -= offset; return *this; }
64  Timespan& operator-=(const Timespan& offset) noexcept
65  { _duration -= offset.total(); return *this; }
66 
67  friend Timespan operator+(const Timespan& timespan, int64_t offset) noexcept
68  { return Timespan(timespan.total() + offset); }
69  friend Timespan operator+(int64_t offset, const Timespan& timespan) noexcept
70  { return Timespan(offset + timespan.total()); }
71  friend Timespan operator+(const Timespan& timespan1, const Timespan& timespan2) noexcept
72  { return Timespan(timespan1.total() + timespan2.total()); }
73 
74  friend Timespan operator-(const Timespan& timespan, int64_t offset) noexcept
75  { return Timespan(timespan.total() - offset); }
76  friend Timespan operator-(int64_t offset, const Timespan& timespan) noexcept
77  { return Timespan(offset - timespan.total()); }
78  friend Timespan operator-(const Timespan& timespan1, const Timespan& timespan2) noexcept
79  { return Timespan(timespan1.total() - timespan2.total()); }
80 
81  // Timespan comparison
82  friend bool operator==(const Timespan& timespan, int64_t offset) noexcept
83  { return timespan.total() == offset; }
84  friend bool operator==(int64_t offset, const Timespan& timespan) noexcept
85  { return offset == timespan.total(); }
86  friend bool operator==(const Timespan& timespan1, const Timespan& timespan2) noexcept
87  { return timespan1.total() == timespan2.total(); }
88 
89  friend bool operator!=(const Timespan& timespan, int64_t offset) noexcept
90  { return timespan.total() != offset; }
91  friend bool operator!=(int64_t offset, const Timespan& timespan) noexcept
92  { return offset != timespan.total(); }
93  friend bool operator!=(const Timespan& timespan1, const Timespan& timespan2) noexcept
94  { return timespan1.total() != timespan2.total(); }
95 
96  friend bool operator>(const Timespan& timespan, int64_t offset) noexcept
97  { return timespan.total() > offset; }
98  friend bool operator>(int64_t offset, const Timespan& timespan) noexcept
99  { return offset > timespan.total(); }
100  friend bool operator>(const Timespan& timespan1, const Timespan& timespan2) noexcept
101  { return timespan1.total() > timespan2.total(); }
102 
103  friend bool operator<(const Timespan& timespan, int64_t offset) noexcept
104  { return timespan.total() < offset; }
105  friend bool operator<(int64_t offset, const Timespan& timespan) noexcept
106  { return offset < timespan.total(); }
107  friend bool operator<(const Timespan& timespan1, const Timespan& timespan2) noexcept
108  { return timespan1.total() < timespan2.total(); }
109 
110  friend bool operator>=(const Timespan& timespan, int64_t offset) noexcept
111  { return timespan.total() >= offset; }
112  friend bool operator>=(int64_t offset, const Timespan& timespan) noexcept
113  { return offset >= timespan.total(); }
114  friend bool operator>=(const Timespan& timespan1, const Timespan& timespan2) noexcept
115  { return timespan1.total() >= timespan2.total(); }
116 
117  friend bool operator<=(const Timespan& timespan, int64_t offset) noexcept
118  { return timespan.total() <= offset; }
119  friend bool operator<=(int64_t offset, const Timespan& timespan) noexcept
120  { return offset <= timespan.total(); }
121  friend bool operator<=(const Timespan& timespan1, const Timespan& timespan2) noexcept
122  { return timespan1.total() <= timespan2.total(); }
123 
125  std::chrono::system_clock::duration chrono() const noexcept
126  { return std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::nanoseconds(_duration)); }
127 
129  int64_t days() const noexcept
130  { return _duration / (24 * 60 * 60 * 1000000000ll); }
132  int64_t hours() const noexcept
133  { return _duration / (60 * 60 * 1000000000ll); }
135  int64_t minutes() const noexcept
136  { return _duration / (60 * 1000000000ll); }
138  int64_t seconds() const noexcept
139  { return _duration / 1000000000; }
141  int64_t milliseconds() const noexcept
142  { return _duration / 1000000; }
144  int64_t microseconds() const noexcept
145  { return _duration / 1000; }
147  int64_t nanoseconds() const noexcept
148  { return _duration; }
149 
151  int64_t total() const noexcept
152  { return _duration; }
153 
155  static Timespan days(int64_t days) noexcept
156  { return Timespan(days * 24 * 60 * 60 * 1000000000ll); }
158  static Timespan hours(int64_t hours) noexcept
159  { return Timespan(hours * 60 * 60 * 1000000000ll); }
161  static Timespan minutes(int64_t minutes) noexcept
162  { return Timespan(minutes * 60 * 1000000000ll); }
164  static Timespan seconds(int64_t seconds) noexcept
165  { return Timespan(seconds * 1000000000); }
167  static Timespan milliseconds(int64_t milliseconds) noexcept
168  { return Timespan(milliseconds * 1000000); }
170  static Timespan microseconds(int64_t microseconds) noexcept
171  { return Timespan(microseconds * 1000); }
173  static Timespan nanoseconds(int64_t nanoseconds) noexcept
174  { return Timespan(nanoseconds); }
175 
177 
182  static Timespan zero() noexcept { return Timespan(0); }
183 
185  void swap(Timespan& timespan) noexcept;
186  friend void swap(Timespan& timespan1, Timespan& timespan2) noexcept;
187 
188 private:
189  int64_t _duration;
190 };
191 
194 } // namespace CppCommon
195 
196 #include "timespan.inl"
197 
198 #endif // CPPCOMMON_TIME_TIMESPAN_H
friend Timespan operator-(const Timespan &timespan1, const Timespan &timespan2) noexcept
Definition: timespan.h:78
static Timespan milliseconds(int64_t milliseconds) noexcept
Create the timespan based on the given milliseconds value.
Definition: timespan.h:167
Timespan() noexcept
Initialize timespan with a zero time duration.
Definition: timespan.h:30
Timespan & operator+=(const Timespan &offset) noexcept
Definition: timespan.h:59
int64_t total() const noexcept
Get total value of the current timespan (total nanoseconds)
Definition: timespan.h:151
friend Timespan operator+(int64_t offset, const Timespan &timespan) noexcept
Definition: timespan.h:69
static Timespan microseconds(int64_t microseconds) noexcept
Create the timespan based on the given microseconds value.
Definition: timespan.h:170
friend bool operator<(const Timespan &timespan1, const Timespan &timespan2) noexcept
Definition: timespan.h:107
Timespan & operator-=(const Timespan &offset) noexcept
Definition: timespan.h:64
friend bool operator<=(const Timespan &timespan1, const Timespan &timespan2) noexcept
Definition: timespan.h:121
Timespan & operator=(Timespan &&) noexcept=default
Timespan(const Timespan &) noexcept=default
int64_t seconds() const noexcept
Get total seconds of the current timespan.
Definition: timespan.h:138
Timespan & operator+=(int64_t offset) noexcept
Definition: timespan.h:57
Timespan(Timespan &&) noexcept=default
static Timespan seconds(int64_t seconds) noexcept
Create the timespan based on the given seconds value.
Definition: timespan.h:164
friend bool operator<=(const Timespan &timespan, int64_t offset) noexcept
Definition: timespan.h:117
friend Timespan operator-(const Timespan &timespan, int64_t offset) noexcept
Definition: timespan.h:74
friend bool operator>(const Timespan &timespan1, const Timespan &timespan2) noexcept
Definition: timespan.h:100
friend bool operator<(const Timespan &timespan, int64_t offset) noexcept
Definition: timespan.h:103
Timespan & operator-=(int64_t offset) noexcept
Definition: timespan.h:62
int64_t days() const noexcept
Get total days of the current timespan.
Definition: timespan.h:129
static Timespan hours(int64_t hours) noexcept
Create the timespan based on the given hours value.
Definition: timespan.h:158
Timespan(int64_t duration) noexcept
Initialize timespan with a given time duration value in nanoseconds.
Definition: timespan.h:35
static Timespan minutes(int64_t minutes) noexcept
Create the timespan based on the given minutes value.
Definition: timespan.h:161
friend bool operator>=(const Timespan &timespan, int64_t offset) noexcept
Definition: timespan.h:110
friend bool operator==(const Timespan &timespan, int64_t offset) noexcept
Definition: timespan.h:82
friend bool operator==(const Timespan &timespan1, const Timespan &timespan2) noexcept
Definition: timespan.h:86
int64_t microseconds() const noexcept
Get total microseconds of the current timespan.
Definition: timespan.h:144
Timespan operator-() const
Definition: timespan.h:54
friend bool operator>(int64_t offset, const Timespan &timespan) noexcept
Definition: timespan.h:98
friend Timespan operator+(const Timespan &timespan, int64_t offset) noexcept
Definition: timespan.h:67
int64_t milliseconds() const noexcept
Get total milliseconds of the current timespan.
Definition: timespan.h:141
std::chrono::system_clock::duration chrono() const noexcept
Convert timespan to the std::chrono nanoseconds duration.
Definition: timespan.h:125
int64_t minutes() const noexcept
Get total minutes of the current timespan.
Definition: timespan.h:135
friend bool operator<(int64_t offset, const Timespan &timespan) noexcept
Definition: timespan.h:105
static Timespan nanoseconds(int64_t nanoseconds) noexcept
Create the timespan based on the given nanoseconds value.
Definition: timespan.h:173
friend bool operator==(int64_t offset, const Timespan &timespan) noexcept
Definition: timespan.h:84
Timespan(const std::chrono::duration< Rep, Period > &duration) noexcept
Initialize timespan with a given std::chrono duration.
Definition: timespan.h:41
int64_t nanoseconds() const noexcept
Get total nanoseconds of the current timespan.
Definition: timespan.h:147
static Timespan days(int64_t days) noexcept
Create the timespan based on the given days value.
Definition: timespan.h:155
friend bool operator!=(const Timespan &timespan, int64_t offset) noexcept
Definition: timespan.h:89
void swap(Timespan &timespan) noexcept
Swap two instances.
Definition: timespan.inl:11
friend bool operator!=(int64_t offset, const Timespan &timespan) noexcept
Definition: timespan.h:91
Timespan & operator=(const Timespan &) noexcept=default
friend Timespan operator+(const Timespan &timespan1, const Timespan &timespan2) noexcept
Definition: timespan.h:71
friend bool operator<=(int64_t offset, const Timespan &timespan) noexcept
Definition: timespan.h:119
friend bool operator!=(const Timespan &timespan1, const Timespan &timespan2) noexcept
Definition: timespan.h:93
friend bool operator>(const Timespan &timespan, int64_t offset) noexcept
Definition: timespan.h:96
friend bool operator>=(int64_t offset, const Timespan &timespan) noexcept
Definition: timespan.h:112
friend bool operator>=(const Timespan &timespan1, const Timespan &timespan2) noexcept
Definition: timespan.h:114
static Timespan zero() noexcept
Get zero timespan.
Definition: timespan.h:182
int64_t hours() const noexcept
Get total hours of the current timespan.
Definition: timespan.h:132
friend Timespan operator-(int64_t offset, const Timespan &timespan) noexcept
Definition: timespan.h:76
Exceptions definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Timespan inline implementation.