/src/poco/Foundation/include/Poco/Timespan.h
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Timespan.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: DateTime |
6 | | // Module: Timespan |
7 | | // |
8 | | // Definition of the Timespan class. |
9 | | // |
10 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_Timespan_INCLUDED |
18 | | #define Foundation_Timespan_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | #include "Poco/Timestamp.h" |
23 | | #include <chrono> |
24 | | |
25 | | |
26 | | namespace Poco { |
27 | | |
28 | | |
29 | | class Foundation_API Timespan |
30 | | /// A class that represents time spans up to microsecond resolution. |
31 | | { |
32 | | public: |
33 | | using TimeDiff = Timestamp::TimeDiff; |
34 | | |
35 | | Timespan(); |
36 | | /// Creates a zero Timespan. |
37 | | |
38 | | Timespan(TimeDiff microseconds); |
39 | | /// Creates a Timespan. |
40 | | |
41 | | Timespan(long seconds, long microseconds); |
42 | | /// Creates a Timespan. Useful for creating |
43 | | /// a Timespan from a struct timeval. |
44 | | |
45 | | Timespan(int days, int hours, int minutes, int seconds, int microSeconds); |
46 | | /// Creates a Timespan. |
47 | | |
48 | | Timespan(const Timespan& timespan); |
49 | | /// Creates a Timespan from another one. |
50 | | |
51 | | template <class T, class Period> |
52 | | Timespan(const std::chrono::duration<T, Period>& dtime) : |
53 | | _span(std::chrono::duration_cast<std::chrono::microseconds>(dtime).count()) {} |
54 | | /// Creates a Timespan from std::chrono::duration |
55 | | |
56 | | ~Timespan(); |
57 | | /// Destroys the Timespan. |
58 | | |
59 | | Timespan& operator = (const Timespan& timespan); |
60 | | /// Assignment operator. |
61 | | |
62 | | Timespan& operator = (TimeDiff microseconds); |
63 | | /// Assignment operator. |
64 | | |
65 | | Timespan& assign(int days, int hours, int minutes, int seconds, int microSeconds); |
66 | | /// Assigns a new span. |
67 | | |
68 | | Timespan& assign(long seconds, long microseconds); |
69 | | /// Assigns a new span. Useful for assigning |
70 | | /// from a struct timeval. |
71 | | |
72 | | template <class T, class Period> |
73 | | Timespan& assign(const std::chrono::duration<T, Period>& dtime) |
74 | | /// Assigns a new span from std::chrono::duration. |
75 | | { |
76 | | _span = std::chrono::duration_cast<std::chrono::microseconds>(dtime).count(); |
77 | | return *this; |
78 | | } |
79 | | |
80 | | void swap(Timespan& timespan) noexcept; |
81 | | /// Swaps the Timespan with another one. |
82 | | |
83 | | bool operator == (const Timespan& ts) const; |
84 | | bool operator != (const Timespan& ts) const; |
85 | | bool operator > (const Timespan& ts) const; |
86 | | bool operator >= (const Timespan& ts) const; |
87 | | bool operator < (const Timespan& ts) const; |
88 | | bool operator <= (const Timespan& ts) const; |
89 | | |
90 | | bool operator == (TimeDiff microSeconds) const; |
91 | | bool operator != (TimeDiff microSeconds) const; |
92 | | bool operator > (TimeDiff microSeconds) const; |
93 | | bool operator >= (TimeDiff microSeconds) const; |
94 | | bool operator < (TimeDiff microSeconds) const; |
95 | | bool operator <= (TimeDiff microSeconds) const; |
96 | | |
97 | | Timespan operator + (const Timespan& d) const; |
98 | | Timespan operator - (const Timespan& d) const; |
99 | | Timespan& operator += (const Timespan& d); |
100 | | Timespan& operator -= (const Timespan& d); |
101 | | |
102 | | Timespan operator + (TimeDiff microSeconds) const; |
103 | | Timespan operator - (TimeDiff microSeconds) const; |
104 | | Timespan& operator += (TimeDiff microSeconds); |
105 | | Timespan& operator -= (TimeDiff microSeconds); |
106 | | |
107 | | int days() const; |
108 | | /// Returns the number of days. |
109 | | |
110 | | int hours() const; |
111 | | /// Returns the number of hours (0 to 23). |
112 | | |
113 | | int totalHours() const; |
114 | | /// Returns the total number of hours. |
115 | | |
116 | | int minutes() const; |
117 | | /// Returns the number of minutes (0 to 59). |
118 | | |
119 | | int totalMinutes() const; |
120 | | /// Returns the total number of minutes. |
121 | | |
122 | | int seconds() const; |
123 | | /// Returns the number of seconds (0 to 59). |
124 | | |
125 | | int totalSeconds() const; |
126 | | /// Returns the total number of seconds. |
127 | | |
128 | | int milliseconds() const; |
129 | | /// Returns the number of milliseconds (0 to 999). |
130 | | |
131 | | TimeDiff totalMilliseconds() const; |
132 | | /// Returns the total number of milliseconds. |
133 | | |
134 | | int microseconds() const; |
135 | | /// Returns the fractions of a millisecond |
136 | | /// in microseconds (0 to 999). |
137 | | |
138 | | int useconds() const; |
139 | | /// Returns the fractions of a second |
140 | | /// in microseconds (0 to 999999). |
141 | | |
142 | | TimeDiff totalMicroseconds() const; |
143 | | /// Returns the total number of microseconds. |
144 | | |
145 | | static const TimeDiff MILLISECONDS; /// The number of microseconds in a millisecond. |
146 | | static const TimeDiff SECONDS; /// The number of microseconds in a second. |
147 | | static const TimeDiff MINUTES; /// The number of microseconds in a minute. |
148 | | static const TimeDiff HOURS; /// The number of microseconds in a hour. |
149 | | static const TimeDiff DAYS; /// The number of microseconds in a day. |
150 | | |
151 | | private: |
152 | | TimeDiff _span; |
153 | | }; |
154 | | |
155 | | |
156 | | // |
157 | | // inlines |
158 | | // |
159 | | inline int Timespan::days() const |
160 | 0 | { |
161 | 0 | return int(_span/DAYS); |
162 | 0 | } |
163 | | |
164 | | |
165 | | inline int Timespan::hours() const |
166 | 36.9k | { |
167 | 36.9k | return int((_span/HOURS) % 24); |
168 | 36.9k | } |
169 | | |
170 | | |
171 | | inline int Timespan::totalHours() const |
172 | 0 | { |
173 | 0 | return int(_span/HOURS); |
174 | 0 | } |
175 | | |
176 | | |
177 | | inline int Timespan::minutes() const |
178 | 36.9k | { |
179 | 36.9k | return int((_span/MINUTES) % 60); |
180 | 36.9k | } |
181 | | |
182 | | |
183 | | inline int Timespan::totalMinutes() const |
184 | 0 | { |
185 | 0 | return int(_span/MINUTES); |
186 | 0 | } |
187 | | |
188 | | |
189 | | inline int Timespan::seconds() const |
190 | 36.9k | { |
191 | 36.9k | return int((_span/SECONDS) % 60); |
192 | 36.9k | } |
193 | | |
194 | | |
195 | | inline int Timespan::totalSeconds() const |
196 | 0 | { |
197 | 0 | return int(_span/SECONDS); |
198 | 0 | } |
199 | | |
200 | | |
201 | | inline int Timespan::milliseconds() const |
202 | 36.9k | { |
203 | 36.9k | return int((_span/MILLISECONDS) % 1000); |
204 | 36.9k | } |
205 | | |
206 | | |
207 | | inline Timespan::TimeDiff Timespan::totalMilliseconds() const |
208 | 0 | { |
209 | 0 | return _span/MILLISECONDS; |
210 | 0 | } |
211 | | |
212 | | |
213 | | inline int Timespan::microseconds() const |
214 | 36.9k | { |
215 | 36.9k | return int(_span % 1000); |
216 | 36.9k | } |
217 | | |
218 | | |
219 | | inline int Timespan::useconds() const |
220 | 0 | { |
221 | 0 | return int(_span % 1000000); |
222 | 0 | } |
223 | | |
224 | | |
225 | | inline Timespan::TimeDiff Timespan::totalMicroseconds() const |
226 | 3.92k | { |
227 | 3.92k | return _span; |
228 | 3.92k | } |
229 | | |
230 | | |
231 | | inline bool Timespan::operator == (const Timespan& ts) const |
232 | 0 | { |
233 | 0 | return _span == ts._span; |
234 | 0 | } |
235 | | |
236 | | |
237 | | inline bool Timespan::operator != (const Timespan& ts) const |
238 | 0 | { |
239 | 0 | return _span != ts._span; |
240 | 0 | } |
241 | | |
242 | | |
243 | | inline bool Timespan::operator > (const Timespan& ts) const |
244 | 0 | { |
245 | 0 | return _span > ts._span; |
246 | 0 | } |
247 | | |
248 | | |
249 | | inline bool Timespan::operator >= (const Timespan& ts) const |
250 | 0 | { |
251 | 0 | return _span >= ts._span; |
252 | 0 | } |
253 | | |
254 | | |
255 | | inline bool Timespan::operator < (const Timespan& ts) const |
256 | 0 | { |
257 | 0 | return _span < ts._span; |
258 | 0 | } |
259 | | |
260 | | |
261 | | inline bool Timespan::operator <= (const Timespan& ts) const |
262 | 0 | { |
263 | 0 | return _span <= ts._span; |
264 | 0 | } |
265 | | |
266 | | |
267 | | inline bool Timespan::operator == (TimeDiff microSeconds) const |
268 | 0 | { |
269 | 0 | return _span == microSeconds; |
270 | 0 | } |
271 | | |
272 | | |
273 | | inline bool Timespan::operator != (TimeDiff microSeconds) const |
274 | 0 | { |
275 | 0 | return _span != microSeconds; |
276 | 0 | } |
277 | | |
278 | | |
279 | | inline bool Timespan::operator > (TimeDiff microSeconds) const |
280 | 0 | { |
281 | 0 | return _span > microSeconds; |
282 | 0 | } |
283 | | |
284 | | |
285 | | inline bool Timespan::operator >= (TimeDiff microSeconds) const |
286 | 0 | { |
287 | 0 | return _span >= microSeconds; |
288 | 0 | } |
289 | | |
290 | | |
291 | | inline bool Timespan::operator < (TimeDiff microSeconds) const |
292 | 0 | { |
293 | 0 | return _span < microSeconds; |
294 | 0 | } |
295 | | |
296 | | |
297 | | inline bool Timespan::operator <= (TimeDiff microSeconds) const |
298 | 0 | { |
299 | 0 | return _span <= microSeconds; |
300 | 0 | } |
301 | | |
302 | | |
303 | | inline void swap(Timespan& s1, Timespan& s2) noexcept |
304 | 0 | { |
305 | 0 | s1.swap(s2); |
306 | 0 | } |
307 | | |
308 | 40.8k | inline Timespan::~Timespan() = default; |
309 | | |
310 | | } // namespace Poco |
311 | | |
312 | | |
313 | | #endif // Foundation_Timespan_INCLUDED |