C++ Utilities  5.10.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 
11 namespace CppUtilities {
12 
13 class DateTime;
14 
20  Normal,
21  WithMeasures,
22  TotalSeconds,
23 };
24 
26  friend class DateTime;
27 
28 public:
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 
90 private:
91  std::int64_t m_ticks;
92 };
93 
97 constexpr inline TimeSpan::TimeSpan()
98  : m_ticks(0)
99 {
100 }
101 
105 constexpr inline TimeSpan::TimeSpan(std::int64_t ticks)
106  : m_ticks(ticks)
107 {
108 }
109 
113 constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
114 {
115  return TimeSpan(static_cast<std::int64_t>(milliseconds * static_cast<double>(ticksPerMillisecond)));
116 }
117 
121 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
122 {
123  return TimeSpan(static_cast<std::int64_t>(seconds * static_cast<double>(ticksPerSecond)));
124 }
125 
129 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
130 {
131  return TimeSpan(static_cast<std::int64_t>(minutes * static_cast<double>(ticksPerMinute)));
132 }
133 
137 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
138 {
139  return TimeSpan(static_cast<std::int64_t>(hours * static_cast<double>(ticksPerHour)));
140 }
141 
145 constexpr inline TimeSpan TimeSpan::fromDays(double days)
146 {
147  return TimeSpan(static_cast<std::int64_t>(days * static_cast<double>(ticksPerDay)));
148 }
149 
158 inline 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 
174 constexpr inline TimeSpan TimeSpan::infinity()
175 {
176  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
177 }
178 
182 inline std::int64_t &TimeSpan::ticks()
183 {
184  return m_ticks;
185 }
186 
190 constexpr inline std::int64_t TimeSpan::totalTicks() const
191 {
192  return m_ticks;
193 }
194 
198 constexpr double TimeSpan::totalMicroseconds() const
199 {
200  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
201 }
202 
206 constexpr inline double TimeSpan::totalMilliseconds() const
207 {
208  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
209 }
210 
214 constexpr inline double TimeSpan::totalSeconds() const
215 {
216  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
217 }
218 
222 constexpr inline double TimeSpan::totalMinutes() const
223 {
224  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
225 }
226 
230 constexpr inline double TimeSpan::totalHours() const
231 {
232  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
233 }
234 
238 constexpr inline double TimeSpan::totalDays() const
239 {
240  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
241 }
242 
248 constexpr int TimeSpan::nanoseconds() const
249 {
250  return m_ticks % 10l * TimeSpan::nanosecondsPerTick;
251 }
252 
256 constexpr int TimeSpan::microseconds() const
257 {
258  return (m_ticks / ticksPerMicrosecond) % 1000l;
259 }
260 
264 constexpr inline int TimeSpan::milliseconds() const
265 {
266  return (m_ticks / ticksPerMillisecond) % 1000l;
267 }
268 
272 constexpr inline int TimeSpan::seconds() const
273 {
274  return (m_ticks / ticksPerSecond) % 60l;
275 }
276 
280 constexpr inline int TimeSpan::minutes() const
281 {
282  return (m_ticks / ticksPerMinute) % 60l;
283 }
284 
288 constexpr inline int TimeSpan::hours() const
289 {
290  return (m_ticks / ticksPerHour) % 24l;
291 }
292 
296 constexpr inline int TimeSpan::days() const
297 {
298  return (m_ticks / ticksPerDay);
299 }
300 
304 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
305 {
306  return m_ticks == other.m_ticks;
307 }
308 
312 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
313 {
314  return m_ticks != other.m_ticks;
315 }
316 
320 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
321 {
322  return m_ticks < other.m_ticks;
323 }
324 
328 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
329 {
330  return m_ticks > other.m_ticks;
331 }
332 
336 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
337 {
338  return m_ticks <= other.m_ticks;
339 }
340 
344 constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
345 {
346  return m_ticks >= other.m_ticks;
347 }
348 
352 constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
353 {
354  return TimeSpan(m_ticks + other.m_ticks);
355 }
356 
360 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
361 {
362  return TimeSpan(m_ticks - other.m_ticks);
363 }
364 
368 constexpr inline TimeSpan TimeSpan::operator*(double factor) const
369 {
370  return TimeSpan(m_ticks * factor);
371 }
372 
376 constexpr inline TimeSpan TimeSpan::operator/(double factor) const
377 {
378  return TimeSpan(m_ticks / factor);
379 }
380 
384 constexpr 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 
410 inline TimeSpan &TimeSpan::operator*=(double factor)
411 {
412  m_ticks = m_ticks * factor;
413  return *this;
414 }
415 
419 inline TimeSpan &TimeSpan::operator/=(double factor)
420 {
421  m_ticks = m_ticks / factor;
422  return *this;
423 }
424 
431 inline std::string TimeSpan::toString(TimeSpanOutputFormat format, bool fullSeconds) const
432 {
433  std::string result;
434  toString(result, format, fullSeconds);
435  return result;
436 }
437 
441 constexpr inline bool TimeSpan::isNull() const
442 {
443  return m_ticks == 0;
444 }
445 
449 constexpr inline bool TimeSpan::isNegative() const
450 {
451  return m_ticks < 0;
452 }
453 
457 constexpr inline bool TimeSpan::isNegativeInfinity() const
458 {
459  return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
460 }
461 
465 constexpr inline bool TimeSpan::isInfinity() const
466 {
467  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
468 }
469 } // namespace CppUtilities
470 
471 namespace std {
473 template <> 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
CppUtilities::TimeSpan::operator!=
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:312
CppUtilities::TimeSpan::ticksPerDay
static constexpr std::int64_t ticksPerDay
Definition: timespan.h:88
CppUtilities::TimeSpan::totalMilliseconds
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:206
CppUtilities::TimeSpan::ticksPerMillisecond
static constexpr std::int64_t ticksPerMillisecond
Definition: timespan.h:84
CppUtilities::TimeSpan::operator<
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:320
CppUtilities::TimeSpan::fromMilliseconds
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
Definition: timespan.h:113
CppUtilities::TimeSpan::totalMicroseconds
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition: timespan.h:198
CppUtilities::TimeSpan::operator-=
TimeSpan & operator-=(const TimeSpan &other)
Substracts another TimeSpan from the current instance.
Definition: timespan.h:401
CppUtilities::TimeSpan::negativeInfinity
static constexpr TimeSpan negativeInfinity()
Constructs a new instace of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:166
CppUtilities::operator+
CPP_UTILITIES_EXPORT DateTime operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
Definition: period.cpp:60
CppUtilities::TimeSpan::TimeSpan
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:97
CppUtilities::TimeSpan::milliseconds
constexpr int milliseconds() const
Returns the miliseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:264
CppUtilities::TimeSpan::isNegativeInfinity
constexpr bool isNegativeInfinity() const
Returns whether the time inverval represented by the current instance is the smallest representable T...
Definition: timespan.h:457
CppUtilities::TimeSpan::seconds
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:272
CppUtilities::TimeSpan::ticksPerMicrosecond
static constexpr std::int64_t ticksPerMicrosecond
Definition: timespan.h:83
CppUtilities::TimeSpan::operator/
constexpr TimeSpan operator/(double factor) const
Divides a TimeSpan by the specified factor.
Definition: timespan.h:376
CppUtilities::TimeSpan::operator/=
TimeSpan & operator/=(double factor)
Divides the current instance by the specified factor.
Definition: timespan.h:419
CppUtilities::TimeSpan::ticks
std::int64_t & ticks()
Returns a mutable reference to the total ticks.
Definition: timespan.h:182
CppUtilities::TimeSpan::operator*
constexpr TimeSpan operator*(double factor) const
Multiplies a TimeSpan by the specified factor.
Definition: timespan.h:368
CppUtilities::TimeSpan::fromString
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:158
CppUtilities::max
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:100
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:431
CppUtilities::TimeSpan::operator>
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:328
CppUtilities::TimeSpan::totalDays
constexpr double totalDays() const
Returns the value of the current TimeSpan class expressed in whole and fractional days.
Definition: timespan.h:238
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:344
CppUtilities::TimeSpan::totalTicks
constexpr std::int64_t totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
Definition: timespan.h:190
CppUtilities::TimeSpanOutputFormat::Normal
@ Normal
std::hash< CppUtilities::TimeSpan >::operator()
size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
Definition: timespan.h:474
CppUtilities::TimeSpanOutputFormat
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:19
CppUtilities::TimeSpan::operator-
constexpr TimeSpan operator-(const TimeSpan &other) const
Substracts one TimeSpan instance from another.
Definition: timespan.h:360
CppUtilities::TimeSpan::hours
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:288
CppUtilities::TimeSpan::minutes
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:280
CppUtilities
Contains all utilities provides by the c++utilities library.
Definition: argumentparser.h:17
CppUtilities::TimeSpan::ticksPerHour
static constexpr std::int64_t ticksPerHour
Definition: timespan.h:87
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:129
CppUtilities::TimeSpan::microseconds
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:256
CppUtilities::FlagEnumClassOperations::operator+=
constexpr FlagEnumClass & operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition: flagenumclass.h:64
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:137
CppUtilities::FlagEnumClassOperations::operator-=
constexpr FlagEnumClass & operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition: flagenumclass.h:71
CppUtilities::TimeSpan::operator+=
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition: timespan.h:392
CppUtilities::min
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition: math.h:88
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:230
CppUtilities::TimeSpan::nanosecondsPerTick
static constexpr std::int64_t nanosecondsPerTick
Definition: timespan.h:82
CppUtilities::TimeSpan::operator*=
TimeSpan & operator*=(double factor)
Multiplies the current instance by the specified factor.
Definition: timespan.h:410
CppUtilities::TimeSpan::isNegative
constexpr bool isNegative() const
Returns ture if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:449
CppUtilities::TimeSpan::operator==
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:304
CppUtilities::DateTime
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:53
CppUtilities::TimeSpan::ticksPerSecond
static constexpr std::int64_t ticksPerSecond
Definition: timespan.h:85
CppUtilities::TimeSpan::isInfinity
constexpr bool isInfinity() const
Returns whether the time inverval represented by the current instance is the longest representable Ti...
Definition: timespan.h:465
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CppUtilities::TimeSpan::infinity
static constexpr TimeSpan infinity()
Constructs a new instace of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:174
CppUtilities::TimeSpan::totalMinutes
constexpr double totalMinutes() const
Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition: timespan.h:222
CppUtilities::operator==
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:215
CppUtilities::TimeSpan::ticksPerMinute
static constexpr std::int64_t ticksPerMinute
Definition: timespan.h:86
CppUtilities::TimeSpan::nanoseconds
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:248
CppUtilities::TimeSpan::operator+
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:352
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:121
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:336
CppUtilities::TimeSpan::isNull
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:441
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:145
CppUtilities::TimeSpan::days
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:296
CppUtilities::TimeSpan::totalSeconds
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:214