C++ Utilities 5.12.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
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
20 Normal,
23};
24
26 friend class DateTime;
27
28public:
29 explicit constexpr TimeSpan();
30 explicit constexpr TimeSpan(std::int64_t ticks);
31
32 static constexpr TimeSpan fromMilliseconds(double milliseconds);
33 static constexpr TimeSpan fromSeconds(double seconds);
34 static constexpr TimeSpan fromMinutes(double minutes);
35 static constexpr TimeSpan fromHours(double hours);
36 static constexpr TimeSpan fromDays(double days);
37 static TimeSpan fromString(const std::string &str, char separator = ':');
38 static TimeSpan fromString(const char *str, char separator);
39 static constexpr TimeSpan negativeInfinity();
40 static constexpr TimeSpan infinity();
41
42 std::int64_t &ticks();
43 constexpr std::int64_t totalTicks() const;
44 constexpr double totalMicroseconds() const;
45 constexpr double totalMilliseconds() const;
46 constexpr double totalSeconds() const;
47 constexpr double totalMinutes() const;
48 constexpr double totalHours() const;
49 constexpr double totalDays() const;
50
51 constexpr int nanoseconds() const;
52 constexpr int microseconds() const;
53 constexpr int milliseconds() const;
54 constexpr int seconds() const;
55 constexpr int minutes() const;
56 constexpr int hours() const;
57 constexpr int days() const;
58
59 constexpr bool operator==(const TimeSpan &other) const;
60 constexpr bool operator!=(const TimeSpan &other) const;
61 constexpr bool operator<(const TimeSpan &other) const;
62 constexpr bool operator>(const TimeSpan &other) const;
63 constexpr bool operator<=(const TimeSpan &other) const;
64 constexpr bool operator>=(const TimeSpan &other) const;
65 constexpr TimeSpan operator+(const TimeSpan &other) const;
66 constexpr TimeSpan operator-(const TimeSpan &other) const;
67 constexpr TimeSpan operator*(double factor) const;
68 constexpr TimeSpan operator/(double factor) const;
69 constexpr double operator/(TimeSpan other) const;
70 TimeSpan &operator+=(const TimeSpan &other);
71 TimeSpan &operator-=(const TimeSpan &other);
72 TimeSpan &operator*=(double factor);
73 TimeSpan &operator/=(double factor);
74
75 std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
76 void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
77 constexpr bool isNull() const;
78 constexpr bool isNegative() const;
79 constexpr bool isNegativeInfinity() const;
80 constexpr bool isInfinity() const;
81
82 static constexpr std::int64_t nanosecondsPerTick = 100uL;
83 static constexpr std::int64_t ticksPerMicrosecond = 10uL;
84 static constexpr std::int64_t ticksPerMillisecond = 10000uL;
85 static constexpr std::int64_t ticksPerSecond = 10000000uL;
86 static constexpr std::int64_t ticksPerMinute = 600000000uL;
87 static constexpr std::int64_t ticksPerHour = 36000000000uL;
88 static constexpr std::int64_t ticksPerDay = 864000000000uL;
89
90private:
91 std::int64_t m_ticks;
92};
93
97constexpr inline TimeSpan::TimeSpan()
98 : m_ticks(0)
99{
100}
101
105constexpr inline TimeSpan::TimeSpan(std::int64_t ticks)
106 : m_ticks(ticks)
107{
108}
109
113constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
114{
115 return TimeSpan(static_cast<std::int64_t>(milliseconds * static_cast<double>(ticksPerMillisecond)));
116}
117
121constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
122{
123 return TimeSpan(static_cast<std::int64_t>(seconds * static_cast<double>(ticksPerSecond)));
124}
125
129constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
130{
131 return TimeSpan(static_cast<std::int64_t>(minutes * static_cast<double>(ticksPerMinute)));
132}
133
137constexpr inline TimeSpan TimeSpan::fromHours(double hours)
138{
139 return TimeSpan(static_cast<std::int64_t>(hours * static_cast<double>(ticksPerHour)));
140}
141
145constexpr inline TimeSpan TimeSpan::fromDays(double days)
146{
147 return TimeSpan(static_cast<std::int64_t>(days * static_cast<double>(ticksPerDay)));
148}
149
158inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
159{
160 return TimeSpan::fromString(str.data(), separator);
161}
162
167{
168 return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
169}
170
174constexpr inline TimeSpan TimeSpan::infinity()
175{
176 return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
177}
178
182inline std::int64_t &TimeSpan::ticks()
183{
184 return m_ticks;
185}
186
190constexpr inline std::int64_t TimeSpan::totalTicks() const
191{
192 return m_ticks;
193}
194
198constexpr double TimeSpan::totalMicroseconds() const
199{
200 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
201}
202
206constexpr inline double TimeSpan::totalMilliseconds() const
207{
208 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
209}
210
214constexpr inline double TimeSpan::totalSeconds() const
215{
216 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
217}
218
222constexpr inline double TimeSpan::totalMinutes() const
223{
224 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
225}
226
230constexpr inline double TimeSpan::totalHours() const
231{
232 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
233}
234
238constexpr inline double TimeSpan::totalDays() const
239{
240 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
241}
242
248constexpr int TimeSpan::nanoseconds() const
249{
250 return static_cast<int>(m_ticks % 10l * TimeSpan::nanosecondsPerTick);
251}
252
256constexpr int TimeSpan::microseconds() const
257{
258 return static_cast<int>((m_ticks / ticksPerMicrosecond) % 1000l);
259}
260
264constexpr inline int TimeSpan::milliseconds() const
265{
266 return static_cast<int>((m_ticks / ticksPerMillisecond) % 1000l);
267}
268
272constexpr inline int TimeSpan::seconds() const
273{
274 return static_cast<int>((m_ticks / ticksPerSecond) % 60l);
275}
276
280constexpr inline int TimeSpan::minutes() const
281{
282 return static_cast<int>((m_ticks / ticksPerMinute) % 60l);
283}
284
288constexpr inline int TimeSpan::hours() const
289{
290 return static_cast<int>((m_ticks / ticksPerHour) % 24l);
291}
292
296constexpr inline int TimeSpan::days() const
297{
298 return static_cast<int>((m_ticks / ticksPerDay));
299}
300
304constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
305{
306 return m_ticks == other.m_ticks;
307}
308
312constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
313{
314 return m_ticks != other.m_ticks;
315}
316
320constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
321{
322 return m_ticks < other.m_ticks;
323}
324
328constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
329{
330 return m_ticks > other.m_ticks;
331}
332
336constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
337{
338 return m_ticks <= other.m_ticks;
339}
340
344constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
345{
346 return m_ticks >= other.m_ticks;
347}
348
352constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
353{
354 return TimeSpan(m_ticks + other.m_ticks);
355}
356
360constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
361{
362 return TimeSpan(m_ticks - other.m_ticks);
363}
364
368constexpr inline TimeSpan TimeSpan::operator*(double factor) const
369{
370 return TimeSpan(static_cast<std::int64_t>(static_cast<double>(m_ticks) * factor));
371}
372
376constexpr inline TimeSpan TimeSpan::operator/(double factor) const
377{
378 return TimeSpan(static_cast<std::int64_t>(static_cast<double>(m_ticks) / factor));
379}
380
384constexpr inline double TimeSpan::operator/(TimeSpan other) const
385{
386 return static_cast<double>(m_ticks) / static_cast<double>(other.m_ticks);
387}
388
393{
394 m_ticks += other.m_ticks;
395 return *this;
396}
397
402{
403 m_ticks -= other.m_ticks;
404 return *this;
405}
406
410inline TimeSpan &TimeSpan::operator*=(double factor)
411{
412 m_ticks = static_cast<std::int64_t>(static_cast<double>(m_ticks) * factor);
413 return *this;
414}
415
419inline TimeSpan &TimeSpan::operator/=(double factor)
420{
421 m_ticks = static_cast<std::int64_t>(static_cast<double>(m_ticks) / factor);
422 return *this;
423}
424
431inline std::string TimeSpan::toString(TimeSpanOutputFormat format, bool fullSeconds) const
432{
433 std::string result;
434 toString(result, format, fullSeconds);
435 return result;
436}
437
441constexpr inline bool TimeSpan::isNull() const
442{
443 return m_ticks == 0;
444}
445
449constexpr inline bool TimeSpan::isNegative() const
450{
451 return m_ticks < 0;
452}
453
457constexpr inline bool TimeSpan::isNegativeInfinity() const
458{
459 return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
460}
461
465constexpr inline bool TimeSpan::isInfinity() const
466{
467 return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
468}
469} // namespace CppUtilities
470
471namespace std {
473template <> struct hash<CppUtilities::TimeSpan> {
474 inline size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
475 {
476 return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
477 }
478};
479} // namespace std
480
481#endif // CHRONO_UTILITIES_TIMESPAN_H
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:53
Represents a time interval.
Definition: timespan.h:25
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:230
static constexpr std::int64_t ticksPerMinute
Definition: timespan.h:86
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:214
constexpr bool isNull() const
Returns true if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:441
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:320
constexpr TimeSpan operator*(double factor) const
Multiplies a TimeSpan by the specified factor.
Definition: timespan.h:368
static constexpr std::int64_t ticksPerDay
Definition: timespan.h:88
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:312
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:272
constexpr std::int64_t totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
Definition: timespan.h:190
TimeSpan & operator*=(double factor)
Multiplies the current instance by the specified factor.
Definition: timespan.h:410
constexpr bool isNegativeInfinity() const
Returns whether the time interval represented by the current instance is the smallest representable T...
Definition: timespan.h:457
static constexpr std::int64_t ticksPerSecond
Definition: timespan.h:85
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:280
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition: timespan.h:198
constexpr TimeSpan operator/(double factor) const
Divides a TimeSpan by the specified factor.
Definition: timespan.h:376
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:97
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:328
static constexpr std::int64_t nanosecondsPerTick
Definition: timespan.h:82
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:304
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:296
std::int64_t & ticks()
Returns a mutable reference to the total ticks.
Definition: timespan.h:182
constexpr int milliseconds() const
Returns the milliseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:264
static constexpr std::int64_t ticksPerHour
Definition: timespan.h:87
constexpr TimeSpan operator-(const TimeSpan &other) const
Subtracts one TimeSpan instance from another.
Definition: timespan.h:360
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.
Definition: timespan.h:344
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition: timespan.h:392
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:145
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:137
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
Definition: timespan.h:129
static constexpr TimeSpan infinity()
Constructs a new instance of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:174
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:121
constexpr bool isInfinity() const
Returns whether the time interval represented by the current instance is the longest representable Ti...
Definition: timespan.h:465
static constexpr TimeSpan negativeInfinity()
Constructs a new instance of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:166
static constexpr std::int64_t ticksPerMicrosecond
Definition: timespan.h:83
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:256
constexpr double totalDays() const
Returns the value of the current TimeSpan class expressed in whole and fractional days.
Definition: timespan.h:238
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:431
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:158
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:206
constexpr bool isNegative() const
Returns true if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:449
static constexpr std::int64_t ticksPerMillisecond
Definition: timespan.h:84
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:288
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:352
constexpr double totalMinutes() const
Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition: timespan.h:222
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition: timespan.h:336
TimeSpan & operator/=(double factor)
Divides the current instance by the specified factor.
Definition: timespan.h:419
TimeSpan & operator-=(const TimeSpan &other)
Subtracts another TimeSpan from the current instance.
Definition: timespan.h:401
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:248
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of milliseconds.
Definition: timespan.h:113
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
constexpr FlagEnumClass & operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition: flagenumclass.h:71
constexpr FlagEnumClass & operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition: flagenumclass.h:64
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT DateTime operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
Definition: period.cpp:60
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:19
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:216
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:100
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition: math.h:88
STL namespace.
size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
Definition: timespan.h:474