Debian Codegraph @

chrono/timespan.h

raw symbols SCIP
1 #ifndef CHRONO_UTILITIES_TIMESPAN_H
2 #define CHRONO_UTILITIES_TIMESPAN_H
3
4 #include "../global.h"
5
6 #include <cstdint>
7 #include <functional>
8 #include <limits>
9 #include <string>
10
11 namespace CppUtilities {
12
13 class DateTime;
14
15 /*!
16 * \brief Specifies the output format.
17 * \sa TimeSpan::toString()
18 */
19 enum class TimeSpanOutputFormat {
20 Normal, /**< the normal form of specifying a time interval: hh:mm:ss */
21 WithMeasures, /**< measures are used, eg.: 34 d 5 h 10 min 7 s 31 ms */
22 TotalSeconds, /**< total seconds (as returned by totalSeconds()), eg. 2304.342 */
23 };
24
25 class CPP_UTILITIES_EXPORT TimeSpan {
26 friend class DateTime;
27
28 public:
29 using TickType = std::int64_t;
30
31 explicit constexpr TimeSpan();
32 explicit constexpr TimeSpan(TickType ticks);
33
34 static constexpr TimeSpan fromMilliseconds(double milliseconds);
35 static constexpr TimeSpan fromSeconds(double seconds);
36 static constexpr TimeSpan fromMinutes(double minutes);
37 static constexpr TimeSpan fromHours(double hours);
38 static constexpr TimeSpan fromDays(double days);
39 #ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
40 static constexpr TimeSpan fromMilliseconds(TickType milliseconds);
41 static constexpr TimeSpan fromSeconds(TickType seconds);
42 static constexpr TimeSpan fromMinutes(TickType minutes);
43 static constexpr TimeSpan fromHours(TickType hours);
44 static constexpr TimeSpan fromDays(TickType days);
45 #endif
46 static TimeSpan fromString(const std::string &str, char separator = ':');
47 static TimeSpan fromString(const char *str, char separator);
48 static constexpr TimeSpan negativeInfinity();
49 static constexpr TimeSpan infinity();
50
51 TickType &ticks();
52 constexpr TickType totalTicks() const;
53 constexpr double totalMicroseconds() const;
54 constexpr double totalMilliseconds() const;
55 constexpr double totalSeconds() const;
56 constexpr double totalMinutes() const;
57 constexpr double totalHours() const;
58 constexpr double totalDays() const;
59
60 constexpr int nanoseconds() const;
61 constexpr int microseconds() const;
62 constexpr int milliseconds() const;
63 constexpr int seconds() const;
64 constexpr int minutes() const;
65 constexpr int hours() const;
66 constexpr int days() const;
67
68 constexpr bool operator==(const TimeSpan &other) const;
69 constexpr bool operator!=(const TimeSpan &other) const;
70 constexpr bool operator<(const TimeSpan &other) const;
71 constexpr bool operator>(const TimeSpan &other) const;
72 constexpr bool operator<=(const TimeSpan &other) const;
73 constexpr bool operator>=(const TimeSpan &other) const;
74 constexpr TimeSpan operator+(const TimeSpan &other) const;
75 constexpr TimeSpan operator-(const TimeSpan &other) const;
76 constexpr TimeSpan operator*(double factor) const;
77 constexpr TimeSpan operator/(double factor) const;
78 #ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
79 constexpr TimeSpan operator*(TickType factor) const;
80 constexpr TimeSpan operator/(TickType factor) const;
81 #endif
82 constexpr double operator/(TimeSpan other) const;
83 TimeSpan &operator+=(const TimeSpan &other);
84 TimeSpan &operator-=(const TimeSpan &other);
85 TimeSpan &operator*=(double factor);
86 TimeSpan &operator/=(double factor);
87 #ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
88 TimeSpan &operator*=(TickType factor);
89 TimeSpan &operator/=(TickType factor);
90 #endif
91
92 std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
93 void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
94 constexpr bool isNull() const;
95 constexpr bool isNegative() const;
96 constexpr bool isNegativeInfinity() const;
97 constexpr bool isInfinity() const;
98
99 static constexpr TickType nanosecondsPerTick = 100L;
100 static constexpr TickType ticksPerMicrosecond = 10L;
101 static constexpr TickType ticksPerMillisecond = 10000L;
102 static constexpr TickType ticksPerSecond = 10000000L;
103 static constexpr TickType ticksPerMinute = 600000000L;
104 static constexpr TickType ticksPerHour = 36000000000L;
105 static constexpr TickType ticksPerDay = 864000000000L;
106
107 private:
108 TickType m_ticks;
109 };
110
111 /*!
112 * \brief Constructs a new instance of the TimeSpan class with zero ticks.
113 */
114 constexpr inline TimeSpan::TimeSpan()
115 : m_ticks(0)
116 {
117 }
118
119 /*!
120 * \brief Constructs a new instance of the TimeSpan class with the specified number of ticks.
121 */
122 constexpr inline TimeSpan::TimeSpan(TickType ticks)
123 : m_ticks(ticks)
124 {
125 }
126
127 /*!
128 * \brief Constructs a new instance of the TimeSpan class with the specified number of milliseconds.
129 */
130 constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
131 {
132 return TimeSpan(static_cast<TickType>(milliseconds * static_cast<double>(ticksPerMillisecond)));
133 }
134
135 /*!
136 * \brief Constructs a new instance of the TimeSpan class with the specified number of seconds.
137 */
138 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
139 {
140 return TimeSpan(static_cast<TickType>(seconds * static_cast<double>(ticksPerSecond)));
141 }
142
143 /*!
144 * \brief Constructs a new instance of the TimeSpan class with the specified number of minutes.
145 */
146 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
147 {
148 return TimeSpan(static_cast<TickType>(minutes * static_cast<double>(ticksPerMinute)));
149 }
150
151 /*!
152 * \brief Constructs a new instance of the TimeSpan class with the specified number of hours.
153 */
154 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
155 {
156 return TimeSpan(static_cast<TickType>(hours * static_cast<double>(ticksPerHour)));
157 }
158
159 /*!
160 * \brief Constructs a new instance of the TimeSpan class with the specified number of days.
161 */
162 constexpr inline TimeSpan TimeSpan::fromDays(double days)
163 {
164 return TimeSpan(static_cast<TickType>(days * static_cast<double>(ticksPerDay)));
165 }
166
167 #ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
168 /*!
169 * \brief Constructs a new instance of the TimeSpan class with the specified number of milliseconds.
170 */
171 constexpr inline TimeSpan TimeSpan::fromMilliseconds(TickType milliseconds)
172 {
173 return TimeSpan(milliseconds * ticksPerMillisecond);
174 }
175
176 /*!
177 * \brief Constructs a new instance of the TimeSpan class with the specified number of seconds.
178 */
179 constexpr inline TimeSpan TimeSpan::fromSeconds(TickType seconds)
180 {
181 return TimeSpan(seconds * ticksPerSecond);
182 }
183
184 /*!
185 * \brief Constructs a new instance of the TimeSpan class with the specified number of minutes.
186 */
187 constexpr inline TimeSpan TimeSpan::fromMinutes(TickType minutes)
188 {
189 return TimeSpan(minutes * ticksPerMinute);
190 }
191
192 /*!
193 * \brief Constructs a new instance of the TimeSpan class with the specified number of hours.
194 */
195 constexpr inline TimeSpan TimeSpan::fromHours(TickType hours)
196 {
197 return TimeSpan(hours * ticksPerHour);
198 }
199
200 /*!
201 * \brief Constructs a new instance of the TimeSpan class with the specified number of days.
202 */
203 constexpr inline TimeSpan TimeSpan::fromDays(TickType days)
204 {
205 return TimeSpan(days * ticksPerDay);
206 }
207 #endif
208
209 /*!
210 * \brief Parses the given std::string as TimeSpan.
211 * \throws Throws a ConversionException if the specified \a str does not match the expected format.
212 *
213 * The expected format is "days:hours:minutes:seconds", eg. "5:31:4.521" for 5 hours, 31 minutes
214 * and 4.521 seconds. So parts at the front can be omitted and the parts can be fractions. The
215 * colon can be changed by specifying another \a separator.
216 */
217 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
218 {
219 return TimeSpan::fromString(str.data(), separator);
220 }
221
222 /*!
223 * \brief Constructs a new instance of the TimeSpan class with the minimal number of ticks.
224 */
225 constexpr inline TimeSpan TimeSpan::negativeInfinity()
226 {
227 return TimeSpan(std::numeric_limits<TickType>::min());
228 }
229
230 /*!
231 * \brief Constructs a new instance of the TimeSpan class with the maximal number of ticks.
232 */
233 constexpr inline TimeSpan TimeSpan::infinity()
234 {
235 return TimeSpan(std::numeric_limits<TickType>::max());
236 }
237
238 /*!
239 * \brief Returns a mutable reference to the total ticks.
240 */
241 inline TimeSpan::TickType &TimeSpan::ticks()
242 {
243 return m_ticks;
244 }
245
246 /*!
247 * \brief Returns the number of ticks that represent the value of the current TimeSpan class.
248 */
249 constexpr inline TimeSpan::TickType TimeSpan::totalTicks() const
250 {
251 return m_ticks;
252 }
253
254 /*!
255 * \brief Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
256 */
257 constexpr double TimeSpan::totalMicroseconds() const
258 {
259 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
260 }
261
262 /*!
263 * \brief Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
264 */
265 constexpr inline double TimeSpan::totalMilliseconds() const
266 {
267 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
268 }
269
270 /*!
271 * \brief Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
272 */
273 constexpr inline double TimeSpan::totalSeconds() const
274 {
275 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
276 }
277
278 /*!
279 * \brief Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
280 */
281 constexpr inline double TimeSpan::totalMinutes() const
282 {
283 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
284 }
285
286 /*!
287 * \brief Returns the value of the current TimeSpan class expressed in whole and fractional hours.
288 */
289 constexpr inline double TimeSpan::totalHours() const
290 {
291 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
292 }
293
294 /*!
295 * \brief Returns the value of the current TimeSpan class expressed in whole and fractional days.
296 */
297 constexpr inline double TimeSpan::totalDays() const
298 {
299 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
300 }
301
302 /*!
303 * \brief Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
304 * \remarks The accuracy of the TimeSpan class is 100-nanoseconds. Hence the returned value
305 * will always have two zeros at the end (in decimal representation).
306 */
307 constexpr int TimeSpan::nanoseconds() const
308 {
309 return static_cast<int>(m_ticks % 10l * TimeSpan::nanosecondsPerTick);
310 }
311
312 /*!
313 * \brief Returns the microseconds component of the time interval represented by the current TimeSpan class.
314 */
315 constexpr int TimeSpan::microseconds() const
316 {
317 return static_cast<int>((m_ticks / ticksPerMicrosecond) % 1000l);
318 }
319
320 /*!
321 * \brief Returns the milliseconds component of the time interval represented by the current TimeSpan class.
322 */
323 constexpr inline int TimeSpan::milliseconds() const
324 {
325 return static_cast<int>((m_ticks / ticksPerMillisecond) % 1000l);
326 }
327
328 /*!
329 * \brief Returns the seconds component of the time interval represented by the current TimeSpan class.
330 */
331 constexpr inline int TimeSpan::seconds() const
332 {
333 return static_cast<int>((m_ticks / ticksPerSecond) % 60l);
334 }
335
336 /*!
337 * \brief Returns the minutes component of the time interval represented by the current TimeSpan class.
338 */
339 constexpr inline int TimeSpan::minutes() const
340 {
341 return static_cast<int>((m_ticks / ticksPerMinute) % 60l);
342 }
343
344 /*!
345 * \brief Returns the hours component of the time interval represented by the current TimeSpan class.
346 */
347 constexpr inline int TimeSpan::hours() const
348 {
349 return static_cast<int>((m_ticks / ticksPerHour) % 24l);
350 }
351
352 /*!
353 * \brief Returns the days component of the time interval represented by the current TimeSpan class.
354 */
355 constexpr inline int TimeSpan::days() const
356 {
357 return static_cast<int>((m_ticks / ticksPerDay));
358 }
359
360 /*!
361 * \brief Indicates whether two TimeSpan instances are equal.
362 */
363 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
364 {
365 return m_ticks == other.m_ticks;
366 }
367
368 /*!
369 * \brief Indicates whether two TimeSpan instances are not equal.
370 */
371 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
372 {
373 return m_ticks != other.m_ticks;
374 }
375
376 /*!
377 * \brief Indicates whether a specified TimeSpan is less than another specified TimeSpan.
378 */
379 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
380 {
381 return m_ticks < other.m_ticks;
382 }
383
384 /*!
385 * \brief Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
386 */
387 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
388 {
389 return m_ticks > other.m_ticks;
390 }
391
392 /*!
393 * \brief Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
394 */
395 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
396 {
397 return m_ticks <= other.m_ticks;
398 }
399
400 /*!
401 * \brief Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.
402 */
403 constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
404 {
405 return m_ticks >= other.m_ticks;
406 }
407
408 /*!
409 * \brief Adds two TimeSpan instances.
410 */
411 constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
412 {
413 return TimeSpan(m_ticks + other.m_ticks);
414 }
415
416 /*!
417 * \brief Subtracts one TimeSpan instance from another.
418 */
419 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
420 {
421 return TimeSpan(m_ticks - other.m_ticks);
422 }
423
424 /*!
425 * \brief Multiplies a TimeSpan by the specified \a factor.
426 */
427 constexpr inline TimeSpan TimeSpan::operator*(double factor) const
428 {
429 return TimeSpan(static_cast<std::int64_t>(static_cast<double>(m_ticks) * factor));
430 }
431
432 /*!
433 * \brief Divides a TimeSpan by the specified \a factor.
434 */
435 constexpr inline TimeSpan TimeSpan::operator/(double factor) const
436 {
437 return TimeSpan(static_cast<std::int64_t>(static_cast<double>(m_ticks) / factor));
438 }
439
440 #ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
441 /*!
442 * \brief Multiplies a TimeSpan by the specified \a factor.
443 */
444 constexpr inline TimeSpan TimeSpan::operator*(std::int64_t factor) const
445 {
446 return TimeSpan(m_ticks * factor);
447 }
448
449 /*!
450 * \brief Divides a TimeSpan by the specified \a factor.
451 */
452 constexpr inline TimeSpan TimeSpan::operator/(std::int64_t factor) const
453 {
454 return TimeSpan(m_ticks / factor);
455 }
456 #endif
457
458 /*!
459 * \brief Computes the ratio between two TimeSpan instances.
460 */
461 constexpr inline double TimeSpan::operator/(TimeSpan other) const
462 {
463 return static_cast<double>(m_ticks) / static_cast<double>(other.m_ticks);
464 }
465
466 /*!
467 * \brief Adds another TimeSpan to the current instance.
468 */
469 inline TimeSpan &TimeSpan::operator+=(const TimeSpan &other)
470 {
471 m_ticks += other.m_ticks;
472 return *this;
473 }
474
475 /*!
476 * \brief Subtracts another TimeSpan from the current instance.
477 */
478 inline TimeSpan &TimeSpan::operator-=(const TimeSpan &other)
479 {
480 m_ticks -= other.m_ticks;
481 return *this;
482 }
483
484 /*!
485 * \brief Multiplies the current instance by the specified \a factor.
486 */
487 inline TimeSpan &TimeSpan::operator*=(double factor)
488 {
489 m_ticks = static_cast<std::int64_t>(static_cast<double>(m_ticks) * factor);
490 return *this;
491 }
492
493 /*!
494 * \brief Divides the current instance by the specified \a factor.
495 */
496 inline TimeSpan &TimeSpan::operator/=(double factor)
497 {
498 m_ticks = static_cast<std::int64_t>(static_cast<double>(m_ticks) / factor);
499 return *this;
500 }
501
502 #ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
503 /*!
504 * \brief Multiplies the current instance by the specified \a factor.
505 */
506 inline TimeSpan &TimeSpan::operator*=(std::int64_t factor)
507 {
508 m_ticks *= factor;
509 return *this;
510 }
511
512 /*!
513 * \brief Divides the current instance by the specified \a factor.
514 */
515 inline TimeSpan &TimeSpan::operator/=(std::int64_t factor)
516 {
517 m_ticks /= factor;
518 return *this;
519 }
520 #endif
521
522 /*!
523 * \brief Converts the value of the current TimeSpan object to its equivalent std::string representation
524 * according the given \a format.
525 *
526 * If \a fullSeconds is true the time interval will be rounded to full seconds.
527 */
528 inline std::string TimeSpan::toString(TimeSpanOutputFormat format, bool fullSeconds) const
529 {
530 std::string result;
531 toString(result, format, fullSeconds);
532 return result;
533 }
534
535 /*!
536 * \brief Returns true if the time interval represented by the current TimeSpan class is null.
537 */
538 constexpr inline bool TimeSpan::isNull() const
539 {
540 return m_ticks == 0;
541 }
542
543 /*!
544 * \brief Returns true if the time interval represented by the current TimeSpan class is negative.
545 */
546 constexpr inline bool TimeSpan::isNegative() const
547 {
548 return m_ticks < 0;
549 }
550
551 /*!
552 * \brief Returns whether the time interval represented by the current instance is the smallest representable TimeSpan.
553 */
554 constexpr inline bool TimeSpan::isNegativeInfinity() const
555 {
556 return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
557 }
558
559 /*!
560 * \brief Returns whether the time interval represented by the current instance is the longest representable TimeSpan.
561 */
562 constexpr inline bool TimeSpan::isInfinity() const
563 {
564 return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
565 }
566 } // namespace CppUtilities
567
568 namespace std {
569 /// \brief Computes the hash for the CppUtilities::TimeSpan instance.
570 template <> struct hash<CppUtilities::TimeSpan> {
571 inline size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
572 {
573 return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
574 }
575 };
576 } // namespace std
577
578 #endif // CHRONO_UTILITIES_TIMESPAN_H