C++ Utilities: chrono/timespan.h Source File

C++ Utilities 5.32.1 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
timespan.h
Go to the documentation of this file.
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
11namespace CppUtilities {
12
13class DateTime;
14
19 enum class TimeSpanOutputFormat {
20 Normal,
21 WithMeasures,
22 TotalSeconds,
23};
24
25 class CPP_UTILITIES_EXPORT TimeSpan {
26 friend class DateTime;
27
28public:
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
107private:
108 TickType m_ticks;
109};
110
114 constexpr inline TimeSpan::TimeSpan()
115 : m_ticks(0)
116{
117}
118
122 constexpr inline TimeSpan::TimeSpan(TickType ticks)
123 : m_ticks(ticks)
124{
125}
126
130 constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
131{
132 return TimeSpan(static_cast<TickType>(milliseconds * static_cast<double>(ticksPerMillisecond)));
133}
134
138 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
139{
140 return TimeSpan(static_cast<TickType>(seconds * static_cast<double>(ticksPerSecond)));
141}
142
146 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
147{
148 return TimeSpan(static_cast<TickType>(minutes * static_cast<double>(ticksPerMinute)));
149}
150
154 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
155{
156 return TimeSpan(static_cast<TickType>(hours * static_cast<double>(ticksPerHour)));
157}
158
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
171constexpr inline TimeSpan TimeSpan::fromMilliseconds(TickType milliseconds)
172{
173 return TimeSpan(milliseconds * ticksPerMillisecond);
174}
175
179constexpr inline TimeSpan TimeSpan::fromSeconds(TickType seconds)
180{
181 return TimeSpan(seconds * ticksPerSecond);
182}
183
187constexpr inline TimeSpan TimeSpan::fromMinutes(TickType minutes)
188{
189 return TimeSpan(minutes * ticksPerMinute);
190}
191
195constexpr inline TimeSpan TimeSpan::fromHours(TickType hours)
196{
197 return TimeSpan(hours * ticksPerHour);
198}
199
203constexpr inline TimeSpan TimeSpan::fromDays(TickType days)
204{
205 return TimeSpan(days * ticksPerDay);
206}
207#endif
208
217 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
218{
219 return TimeSpan::fromString(str.data(), separator);
220}
221
225 constexpr inline TimeSpan TimeSpan::negativeInfinity()
226{
227 return TimeSpan(std::numeric_limits<TickType>::min());
228}
229
233 constexpr inline TimeSpan TimeSpan::infinity()
234{
235 return TimeSpan(std::numeric_limits<TickType>::max());
236}
237
241 inline TimeSpan::TickType &TimeSpan::ticks()
242{
243 return m_ticks;
244}
245
249 constexpr inline TimeSpan::TickType TimeSpan::totalTicks() const
250{
251 return m_ticks;
252}
253
257 constexpr double TimeSpan::totalMicroseconds() const
258{
259 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
260}
261
265 constexpr inline double TimeSpan::totalMilliseconds() const
266{
267 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
268}
269
273 constexpr inline double TimeSpan::totalSeconds() const
274{
275 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
276}
277
281 constexpr inline double TimeSpan::totalMinutes() const
282{
283 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
284}
285
289 constexpr inline double TimeSpan::totalHours() const
290{
291 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
292}
293
297 constexpr inline double TimeSpan::totalDays() const
298{
299 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
300}
301
307 constexpr int TimeSpan::nanoseconds() const
308{
309 return static_cast<int>(m_ticks % 10l * TimeSpan::nanosecondsPerTick);
310}
311
315 constexpr int TimeSpan::microseconds() const
316{
317 return static_cast<int>((m_ticks / ticksPerMicrosecond) % 1000l);
318}
319
323 constexpr inline int TimeSpan::milliseconds() const
324{
325 return static_cast<int>((m_ticks / ticksPerMillisecond) % 1000l);
326}
327
331 constexpr inline int TimeSpan::seconds() const
332{
333 return static_cast<int>((m_ticks / ticksPerSecond) % 60l);
334}
335
339 constexpr inline int TimeSpan::minutes() const
340{
341 return static_cast<int>((m_ticks / ticksPerMinute) % 60l);
342}
343
347 constexpr inline int TimeSpan::hours() const
348{
349 return static_cast<int>((m_ticks / ticksPerHour) % 24l);
350}
351
355 constexpr inline int TimeSpan::days() const
356{
357 return static_cast<int>((m_ticks / ticksPerDay));
358}
359
363 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
364{
365 return m_ticks == other.m_ticks;
366}
367
371 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
372{
373 return m_ticks != other.m_ticks;
374}
375
379 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
380{
381 return m_ticks < other.m_ticks;
382}
383
387 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
388{
389 return m_ticks > other.m_ticks;
390}
391
395 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
396{
397 return m_ticks <= other.m_ticks;
398}
399
403 constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
404{
405 return m_ticks >= other.m_ticks;
406}
407
411 constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
412{
413 return TimeSpan(m_ticks + other.m_ticks);
414}
415
419 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
420{
421 return TimeSpan(m_ticks - other.m_ticks);
422}
423
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
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
444constexpr inline TimeSpan TimeSpan::operator*(std::int64_t factor) const
445{
446 return TimeSpan(m_ticks * factor);
447}
448
452constexpr inline TimeSpan TimeSpan::operator/(std::int64_t factor) const
453{
454 return TimeSpan(m_ticks / factor);
455}
456#endif
457
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
469 inline TimeSpan &TimeSpan::operator+=(const TimeSpan &other)
470{
471 m_ticks += other.m_ticks;
472 return *this;
473}
474
478 inline TimeSpan &TimeSpan::operator-=(const TimeSpan &other)
479{
480 m_ticks -= other.m_ticks;
481 return *this;
482}
483
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
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
506inline TimeSpan &TimeSpan::operator*=(std::int64_t factor)
507{
508 m_ticks *= factor;
509 return *this;
510}
511
515inline TimeSpan &TimeSpan::operator/=(std::int64_t factor)
516{
517 m_ticks /= factor;
518 return *this;
519}
520#endif
521
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
538 constexpr inline bool TimeSpan::isNull() const
539{
540 return m_ticks == 0;
541}
542
546 constexpr inline bool TimeSpan::isNegative() const
547{
548 return m_ticks < 0;
549}
550
554 constexpr inline bool TimeSpan::isNegativeInfinity() const
555{
556 return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
557}
558
562 constexpr inline bool TimeSpan::isInfinity() const
563{
564 return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
565}
566} // namespace CppUtilities
567
568namespace std {
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
CppUtilities::DateTime
Represents an instant in time, typically expressed as a date and time of day.
Definition datetime.h:55
CppUtilities::TimeSpan
Represents a time interval.
Definition timespan.h:25
CppUtilities::TimeSpan::totalHours
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
Definition timespan.h:289
CppUtilities::TimeSpan::totalSeconds
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition timespan.h:273
CppUtilities::TimeSpan::isNull
constexpr bool isNull() const
Returns true if the time interval represented by the current TimeSpan class is null.
Definition timespan.h:538
CppUtilities::TimeSpan::operator<
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition timespan.h:379
CppUtilities::TimeSpan::operator*
constexpr TimeSpan operator*(double factor) const
Multiplies a TimeSpan by the specified factor.
Definition timespan.h:427
CppUtilities::TimeSpan::nanosecondsPerTick
static constexpr TickType nanosecondsPerTick
Definition timespan.h:99
CppUtilities::TimeSpan::operator!=
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition timespan.h:371
CppUtilities::TimeSpan::seconds
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
Definition timespan.h:331
CppUtilities::TimeSpan::operator*=
TimeSpan & operator*=(double factor)
Multiplies the current instance by the specified factor.
Definition timespan.h:487
CppUtilities::TimeSpan::isNegativeInfinity
constexpr bool isNegativeInfinity() const
Returns whether the time interval represented by the current instance is the smallest representable T...
Definition timespan.h:554
CppUtilities::TimeSpan::ticksPerMillisecond
static constexpr TickType ticksPerMillisecond
Definition timespan.h:101
CppUtilities::TimeSpan::minutes
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition timespan.h:339
CppUtilities::TimeSpan::totalMicroseconds
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition timespan.h:257
CppUtilities::TimeSpan::operator/
constexpr TimeSpan operator/(double factor) const
Divides a TimeSpan by the specified factor.
Definition timespan.h:435
CppUtilities::TimeSpan::TimeSpan
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition timespan.h:114
CppUtilities::TimeSpan::operator>
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition timespan.h:387
CppUtilities::TimeSpan::totalTicks
constexpr TickType totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
Definition timespan.h:249
CppUtilities::TimeSpan::operator==
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition timespan.h:363
CppUtilities::TimeSpan::ticksPerMinute
static constexpr TickType ticksPerMinute
Definition timespan.h:103
CppUtilities::TimeSpan::days
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Definition timespan.h:355
CppUtilities::TimeSpan::ticksPerMicrosecond
static constexpr TickType ticksPerMicrosecond
Definition timespan.h:100
CppUtilities::TimeSpan::ticks
TickType & ticks()
Returns a mutable reference to the total ticks.
Definition timespan.h:241
CppUtilities::TimeSpan::TickType
std::int64_t TickType
Definition timespan.h:29
CppUtilities::TimeSpan::milliseconds
constexpr int milliseconds() const
Returns the milliseconds component of the time interval represented by the current TimeSpan class.
Definition timespan.h:323
CppUtilities::TimeSpan::operator-
constexpr TimeSpan operator-(const TimeSpan &other) const
Subtracts one TimeSpan instance from another.
Definition timespan.h:419
CppUtilities::TimeSpan::operator>=
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.
Definition timespan.h:403
CppUtilities::TimeSpan::operator+=
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition timespan.h:469
CppUtilities::TimeSpan::fromDays
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition timespan.h:162
CppUtilities::TimeSpan::fromHours
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition timespan.h:154
CppUtilities::TimeSpan::fromMinutes
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
Definition timespan.h:146
CppUtilities::TimeSpan::infinity
static constexpr TimeSpan infinity()
Constructs a new instance of the TimeSpan class with the maximal number of ticks.
Definition timespan.h:233
CppUtilities::TimeSpan::fromSeconds
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition timespan.h:138
CppUtilities::TimeSpan::isInfinity
constexpr bool isInfinity() const
Returns whether the time interval represented by the current instance is the longest representable Ti...
Definition timespan.h:562
CppUtilities::TimeSpan::ticksPerSecond
static constexpr TickType ticksPerSecond
Definition timespan.h:102
CppUtilities::TimeSpan::negativeInfinity
static constexpr TimeSpan negativeInfinity()
Constructs a new instance of the TimeSpan class with the minimal number of ticks.
Definition timespan.h:225
CppUtilities::TimeSpan::microseconds
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
Definition timespan.h:315
CppUtilities::TimeSpan::totalDays
constexpr double totalDays() const
Returns the value of the current TimeSpan class expressed in whole and fractional days.
Definition timespan.h:297
CppUtilities::TimeSpan::toString
std::string toString(TimeSpanOutputFormat format=TimeSpanOutputFormat::Normal, bool fullSeconds=false) const
Converts the value of the current TimeSpan object to its equivalent std::string representation accord...
Definition timespan.h:528
CppUtilities::TimeSpan::fromString
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition timespan.h:217
CppUtilities::TimeSpan::ticksPerDay
static constexpr TickType ticksPerDay
Definition timespan.h:105
CppUtilities::TimeSpan::totalMilliseconds
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition timespan.h:265
CppUtilities::TimeSpan::ticksPerHour
static constexpr TickType ticksPerHour
Definition timespan.h:104
CppUtilities::TimeSpan::isNegative
constexpr bool isNegative() const
Returns true if the time interval represented by the current TimeSpan class is negative.
Definition timespan.h:546
CppUtilities::TimeSpan::DateTime
friend class DateTime
Definition timespan.h:26
CppUtilities::TimeSpan::hours
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition timespan.h:347
CppUtilities::TimeSpan::operator+
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition timespan.h:411
CppUtilities::TimeSpan::totalMinutes
constexpr double totalMinutes() const
Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition timespan.h:281
CppUtilities::TimeSpan::operator<=
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition timespan.h:395
CppUtilities::TimeSpan::operator/=
TimeSpan & operator/=(double factor)
Divides the current instance by the specified factor.
Definition timespan.h:496
CppUtilities::TimeSpan::operator-=
TimeSpan & operator-=(const TimeSpan &other)
Subtracts another TimeSpan from the current instance.
Definition timespan.h:478
CppUtilities::TimeSpan::nanoseconds
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Definition timespan.h:307
CppUtilities::TimeSpan::fromMilliseconds
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of milliseconds.
Definition timespan.h:130
global.h
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::TimeSpanOutputFormat
TimeSpanOutputFormat
Specifies the output format.
Definition timespan.h:19
CppUtilities::TimeSpanOutputFormat::TotalSeconds
@ TotalSeconds
Definition timespan.h:22
CppUtilities::TimeSpanOutputFormat::WithMeasures
@ WithMeasures
Definition timespan.h:21
CppUtilities::TimeSpanOutputFormat::Normal
@ Normal
Definition timespan.h:20
CppUtilities::max
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition math.h:96
CppUtilities::min
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition math.h:84
std
STL namespace.
std::hash< CppUtilities::TimeSpan >::operator()
size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
Definition timespan.h:571