C++ Utilities  4.10.0
Common C++ classes and routines used by my applications 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 "../conversion/types.h"
5 #include "../global.h"
6 
7 #include <functional>
8 #include <limits>
9 #include <string>
10 
14 namespace ChronoUtilities {
15 
16 class DateTime;
17 
23  Normal,
25 };
26 
28  friend class DateTime;
29 
30 public:
31  explicit constexpr TimeSpan();
32  explicit constexpr TimeSpan(int64 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  static TimeSpan fromString(const std::string &str, char separator = ':');
40  static TimeSpan fromString(const char *str, char separator);
41  static constexpr TimeSpan negativeInfinity();
42  static constexpr TimeSpan infinity();
43 
44  constexpr int64 totalTicks() const;
45  constexpr double totalMicroseconds() const;
46  constexpr double totalMilliseconds() const;
47  constexpr double totalSeconds() const;
48  constexpr double totalMinutes() const;
49  constexpr double totalHours() const;
50  constexpr double totalDays() const;
51 
52  constexpr int nanoseconds() const;
53  constexpr int microseconds() const;
54  constexpr int milliseconds() const;
55  constexpr int seconds() const;
56  constexpr int minutes() const;
57  constexpr int hours() const;
58  constexpr int days() const;
59 
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 bool operator>=(const TimeSpan &other) const;
66  constexpr TimeSpan operator+(const TimeSpan &other) const;
67  constexpr TimeSpan operator-(const TimeSpan &other) const;
68  TimeSpan &operator+=(const TimeSpan &other);
69  TimeSpan &operator-=(const TimeSpan &other);
70 
71  std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
72  void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
73  constexpr bool isNull() const;
74  constexpr bool isNegative() const;
75  constexpr bool isNegativeInfinity() const;
76  constexpr bool isInfinity() const;
77 
78  // TODO: make those public constants signed in next major release and remove private ones then
79  static constexpr int64 nanosecondsPerTick = 100uL;
80  static constexpr int64 ticksPerMicrosecond = 10uL;
81  static constexpr uint64 ticksPerMillisecond = 10000uL;
82  static constexpr uint64 ticksPerSecond = 10000000uL;
83  static constexpr uint64 ticksPerMinute = 600000000uL;
84  static constexpr uint64 ticksPerHour = 36000000000uL;
85  static constexpr uint64 ticksPerDay = 864000000000uL;
86 
87 private:
88  static constexpr int64 m_ticksPerMillisecond = 10000L;
89  static constexpr int64 m_ticksPerSecond = 10000000L;
90  static constexpr int64 m_ticksPerMinute = 600000000L;
91  static constexpr int64 m_ticksPerHour = 36000000000L;
92  static constexpr int64 m_ticksPerDay = 864000000000L;
93 
94 private:
95  int64 m_ticks;
96 };
97 
101 constexpr inline TimeSpan::TimeSpan()
102  : m_ticks(0)
103 {
104 }
105 
109 constexpr inline TimeSpan::TimeSpan(int64 ticks)
110  : m_ticks(ticks)
111 {
112 }
113 
118 {
119  return TimeSpan(static_cast<int64>(milliseconds * static_cast<double>(m_ticksPerMillisecond)));
120 }
121 
125 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
126 {
127  return TimeSpan(static_cast<int64>(seconds * static_cast<double>(m_ticksPerSecond)));
128 }
129 
133 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
134 {
135  return TimeSpan(static_cast<int64>(minutes * static_cast<double>(m_ticksPerMinute)));
136 }
137 
141 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
142 {
143  return TimeSpan(static_cast<int64>(hours * static_cast<double>(m_ticksPerHour)));
144 }
145 
149 constexpr inline TimeSpan TimeSpan::fromDays(double days)
150 {
151  return TimeSpan(static_cast<int64>(days * static_cast<double>(m_ticksPerDay)));
152 }
153 
157 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
158 {
159  return TimeSpan::fromString(str.data(), separator);
160 }
161 
166 {
167  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
168 }
169 
173 constexpr inline TimeSpan TimeSpan::infinity()
174 {
175  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
176 }
177 
181 constexpr inline int64 TimeSpan::totalTicks() const
182 {
183  return m_ticks;
184 }
185 
189 constexpr double TimeSpan::totalMicroseconds() const
190 {
191  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
192 }
193 
197 constexpr inline double TimeSpan::totalMilliseconds() const
198 {
199  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMillisecond);
200 }
201 
205 constexpr inline double TimeSpan::totalSeconds() const
206 {
207  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerSecond);
208 }
209 
213 constexpr inline double TimeSpan::totalMinutes() const
214 {
215  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMinute);
216 }
217 
221 constexpr inline double TimeSpan::totalHours() const
222 {
223  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerHour);
224 }
225 
229 constexpr inline double TimeSpan::totalDays() const
230 {
231  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerDay);
232 }
233 
239 constexpr int TimeSpan::nanoseconds() const
240 {
241  return m_ticks % 10l * TimeSpan::nanosecondsPerTick;
242 }
243 
247 constexpr int TimeSpan::microseconds() const
248 {
249  return (m_ticks / ticksPerMicrosecond) % 1000l;
250 }
251 
255 constexpr inline int TimeSpan::milliseconds() const
256 {
257  return (m_ticks / m_ticksPerMillisecond) % 1000l;
258 }
259 
263 constexpr inline int TimeSpan::seconds() const
264 {
265  return (m_ticks / m_ticksPerSecond) % 60l;
266 }
267 
271 constexpr inline int TimeSpan::minutes() const
272 {
273  return (m_ticks / m_ticksPerMinute) % 60l;
274 }
275 
279 constexpr inline int TimeSpan::hours() const
280 {
281  return (m_ticks / m_ticksPerHour) % 24l;
282 }
283 
287 constexpr inline int TimeSpan::days() const
288 {
289  return (m_ticks / m_ticksPerDay);
290 }
291 
295 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
296 {
297  return m_ticks == other.m_ticks;
298 }
299 
303 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
304 {
305  return m_ticks != other.m_ticks;
306 }
307 
311 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
312 {
313  return m_ticks < other.m_ticks;
314 }
315 
319 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
320 {
321  return m_ticks > other.m_ticks;
322 }
323 
327 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
328 {
329  return m_ticks <= other.m_ticks;
330 }
331 
335 constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
336 {
337  return m_ticks >= other.m_ticks;
338 }
339 
343 constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
344 {
345  return TimeSpan(m_ticks + other.m_ticks);
346 }
347 
351 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
352 {
353  return TimeSpan(m_ticks - other.m_ticks);
354 }
355 
360 {
361  m_ticks += other.m_ticks;
362  return *this;
363 }
364 
369 {
370  m_ticks -= other.m_ticks;
371  return *this;
372 }
373 
377 constexpr inline bool TimeSpan::isNull() const
378 {
379  return m_ticks == 0;
380 }
381 
385 constexpr inline bool TimeSpan::isNegative() const
386 {
387  return m_ticks < 0;
388 }
389 
393 constexpr inline bool TimeSpan::isNegativeInfinity() const
394 {
395  return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
396 }
397 
401 constexpr inline bool TimeSpan::isInfinity() const
402 {
403  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
404 }
405 } // namespace ChronoUtilities
406 
407 namespace std {
408 template <> struct hash<ChronoUtilities::TimeSpan> {
409  inline size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
410  {
411  return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
412  }
413 };
414 } // namespace std
415 
416 #endif // CHRONO_UTILITIES_TIMESPAN_H
constexpr int nanoseconds() const
Gets the nanoseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:239
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:22
constexpr double totalMilliseconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:197
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:141
static constexpr TimeSpan negativeInfinity()
Constructs a new instace of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:165
size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
Definition: timespan.h:409
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
constexpr bool isNegative() const
Returns ture if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:385
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition: timespan.h:327
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:51
Contains classes providing a means for handling date and time information.
Definition: datetime.h:12
constexpr int64 totalTicks() const
Gets the number of ticks that represent the value of the current TimeSpan class.
Definition: timespan.h:181
constexpr int days() const
Gets the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:287
constexpr double totalMinutes() const
Gets the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition: timespan.h:213
Represents a time interval.
Definition: timespan.h:27
constexpr int hours() const
Gets the hours component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:279
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
Definition: timespan.h:117
constexpr double totalMicroseconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition: timespan.h:189
STL namespace.
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:101
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:125
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
static constexpr int64 ticksPerMicrosecond
Definition: timespan.h:80
TimeSpan & operator-=(const TimeSpan &other)
Substracts another TimeSpan from the current instance.
Definition: timespan.h:368
constexpr double totalHours() const
Gets the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:221
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:377
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan...
Definition: timespan.h:335
static constexpr int64 nanosecondsPerTick
Definition: timespan.h:79
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:311
constexpr int minutes() const
Gets the minutes component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:271
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:149
constexpr TimeSpan operator-(const TimeSpan &other) const
Substracts two TimeSpan instances.
Definition: timespan.h:351
constexpr int milliseconds() const
Gets the miliseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:255
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:149
constexpr bool isInfinity() const
Returns whether the time inverval represented by the current instance is the longest representable Ti...
Definition: timespan.h:401
constexpr double totalSeconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:205
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:303
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition: timespan.h:359
constexpr int seconds() const
Gets the seconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:263
constexpr int microseconds() const
Gets the microseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:247
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:295
std::string operator+(const Tuple &lhs, const std::string &rhs)
Allows construction of final string from previously constructed string-tuple and trailing string via ...
constexpr double totalDays() const
Gets the value of the current TimeSpan class expressed in whole and fractional days.
Definition: timespan.h:229
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:319
constexpr bool isNegativeInfinity() const
Returns whether the time inverval represented by the current instance is the smallest representable T...
Definition: timespan.h:393
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:343
static constexpr TimeSpan infinity()
Constructs a new instace of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:173
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:157
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
Definition: timespan.h:133