C++ Utilities  4.6.1
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 "../global.h"
5 #include "../conversion/types.h"
6 
7 #include <string>
8 #include <limits>
9 
13 namespace ChronoUtilities
14 {
15 
16 class DateTime;
17 
23 {
24  Normal,
26 };
27 
29 {
30  friend class DateTime;
31 public:
32  explicit constexpr TimeSpan();
33  explicit constexpr TimeSpan(int64 ticks);
34 
35  static constexpr TimeSpan fromMilliseconds(double milliseconds);
36  static constexpr TimeSpan fromSeconds(double seconds);
37  static constexpr TimeSpan fromMinutes(double minutes);
38  static constexpr TimeSpan fromHours(double hours);
39  static constexpr TimeSpan fromDays(double days);
40  static TimeSpan fromString(const std::string &str, char separator = ':');
41  static TimeSpan fromString(const char *str, char separator);
42  static constexpr TimeSpan negativeInfinity();
43  static constexpr TimeSpan infinity();
44 
45  constexpr int64 totalTicks() 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 milliseconds() const;
53  constexpr int seconds() const;
54  constexpr int minutes() const;
55  constexpr int hours() const;
56  constexpr int days() const;
57 
58  constexpr bool operator==(const TimeSpan &other) const;
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 TimeSpan operator+(const TimeSpan &other) const;
65  constexpr TimeSpan operator-(const TimeSpan &other) const;
66  TimeSpan &operator+=(const TimeSpan &other);
67  TimeSpan &operator-=(const TimeSpan &other);
68 
69  std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool noMilliseconds = false) const;
70  void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool noMilliseconds = false) const;
71  constexpr bool isNull() const;
72  constexpr bool isNegative() const;
73  constexpr bool isNegativeInfinity() const;
74  constexpr bool isInfinity() const;
75 
76  static constexpr uint64 ticksPerMillisecond = 10000uL;
77  static constexpr uint64 ticksPerSecond = 10000000uL;
78  static constexpr uint64 ticksPerMinute = 600000000uL;
79  static constexpr uint64 ticksPerHour = 36000000000uL;
80  static constexpr uint64 ticksPerDay = 864000000000uL;
81 
82 private:
83  int64 m_ticks;
84 };
85 
89 constexpr inline TimeSpan::TimeSpan() : m_ticks(0)
90 {}
91 
95 constexpr inline TimeSpan::TimeSpan(int64 ticks) : m_ticks(ticks)
96 {}
97 
102 {
103  return TimeSpan(static_cast<int64>(milliseconds * static_cast<double>(ticksPerMillisecond)));
104 }
105 
109 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
110 {
111  return TimeSpan(static_cast<int64>(seconds * static_cast<double>(ticksPerSecond)));
112 }
113 
117 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
118 {
119  return TimeSpan(static_cast<int64>(minutes * static_cast<double>(ticksPerMinute)));
120 }
121 
125 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
126 {
127  return TimeSpan(static_cast<int64>(hours * static_cast<double>(ticksPerHour)));
128 }
129 
133 constexpr inline TimeSpan TimeSpan::fromDays(double days)
134 {
135  return TimeSpan(static_cast<int64>(days * static_cast<double>(ticksPerDay)));
136 }
137 
141 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
142 {
143  return TimeSpan::fromString(str.data(), separator);
144 }
145 
150 {
151  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
152 }
153 
157 constexpr inline TimeSpan TimeSpan::infinity()
158 {
159  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
160 }
161 
165 constexpr inline int64 TimeSpan::totalTicks() const
166 {
167  return m_ticks;
168 }
169 
173 constexpr inline double TimeSpan::totalMilliseconds() const
174 {
175  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
176 }
177 
181 constexpr inline double TimeSpan::totalSeconds() const
182 {
183  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
184 }
185 
189 constexpr inline double TimeSpan::totalMinutes() const
190 {
191  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
192 }
193 
197 constexpr inline double TimeSpan::totalHours() const
198 {
199  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
200 }
201 
205 constexpr inline double TimeSpan::totalDays() const
206 {
207  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
208 }
209 
213 constexpr inline int TimeSpan::milliseconds() const
214 {
215  return (m_ticks / ticksPerMillisecond) % 1000l;
216 }
217 
221 constexpr inline int TimeSpan::seconds() const
222 {
223  return (m_ticks / ticksPerSecond) % 60l;
224 }
225 
229 constexpr inline int TimeSpan::minutes() const
230 {
231  return (m_ticks / ticksPerMinute) % 60l;
232 }
233 
237 constexpr inline int TimeSpan::hours() const
238 {
239  return (m_ticks / ticksPerHour) % 24l;
240 }
241 
245 constexpr inline int TimeSpan::days() const
246 {
247  return (m_ticks / ticksPerDay);
248 }
249 
253 constexpr inline bool TimeSpan::operator ==(const TimeSpan &other) const
254 {
255  return m_ticks == other.m_ticks;
256 }
257 
261 constexpr inline bool TimeSpan::operator !=(const TimeSpan &other) const
262 {
263  return m_ticks != other.m_ticks;
264 }
265 
269 constexpr inline bool TimeSpan::operator <(const TimeSpan &other) const
270 {
271  return m_ticks < other.m_ticks;
272 }
273 
277 constexpr inline bool TimeSpan::operator >(const TimeSpan &other) const
278 {
279  return m_ticks > other.m_ticks;
280 }
281 
285 constexpr inline bool TimeSpan::operator <=(const TimeSpan &other) const
286 {
287  return m_ticks <= other.m_ticks;
288 }
289 
293 constexpr inline bool TimeSpan::operator >=(const TimeSpan &other) const
294 {
295  return m_ticks >= other.m_ticks;
296 }
297 
301 constexpr inline TimeSpan TimeSpan::operator +(const TimeSpan &other) const
302 {
303  return TimeSpan(m_ticks + other.m_ticks);
304 }
305 
309 constexpr inline TimeSpan TimeSpan::operator -(const TimeSpan &other) const
310 {
311  return TimeSpan(m_ticks - other.m_ticks);
312 }
313 
318 {
319  m_ticks += other.m_ticks;
320  return *this;
321 }
322 
327 {
328  m_ticks -= other.m_ticks;
329  return *this;
330 }
331 
335 constexpr inline bool TimeSpan::isNull() const
336 {
337  return m_ticks == 0;
338 }
339 
343 constexpr inline bool TimeSpan::isNegative() const
344 {
345  return m_ticks < 0;
346 }
347 
351 constexpr inline bool TimeSpan::isNegativeInfinity() const
352 {
353  return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
354 }
355 
359 constexpr inline bool TimeSpan::isInfinity() const
360 {
361  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
362 }
363 
364 }
365 
366 #endif // CHRONO_UTILITIES_TIMESPAN_H
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:173
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:125
static constexpr TimeSpan negativeInfinity()
Constructs a new instace of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:149
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:343
static constexpr uint64 ticksPerDay
Definition: timespan.h:80
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition: timespan.h:285
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:55
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:165
constexpr int days() const
Gets the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:245
constexpr double totalMinutes() const
Gets the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition: timespan.h:189
Represents a time interval.
Definition: timespan.h:28
constexpr int hours() const
Gets the hours component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:237
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
Definition: timespan.h:101
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:89
static constexpr uint64 ticksPerMillisecond
Definition: timespan.h:76
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:109
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
TimeSpan & operator-=(const TimeSpan &other)
Substracts another TimeSpan from the current instance.
Definition: timespan.h:326
constexpr double totalHours() const
Gets the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:197
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:335
static constexpr uint64 ticksPerHour
Definition: timespan.h:79
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan...
Definition: timespan.h:293
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:269
constexpr int minutes() const
Gets the minutes component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:229
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:144
constexpr TimeSpan operator-(const TimeSpan &other) const
Substracts two TimeSpan instances.
Definition: timespan.h:309
constexpr int milliseconds() const
Gets the miliseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:213
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:133
constexpr bool isInfinity() const
Returns whether the time inverval represented by the current instance is the longest representable Ti...
Definition: timespan.h:359
constexpr double totalSeconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:181
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:261
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition: timespan.h:317
constexpr int seconds() const
Gets the seconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:221
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:253
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:205
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:277
constexpr bool isNegativeInfinity() const
Returns whether the time inverval represented by the current instance is the smallest representable T...
Definition: timespan.h:351
static constexpr uint64 ticksPerSecond
Definition: timespan.h:77
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:301
static constexpr TimeSpan infinity()
Constructs a new instace of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:157
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:141
#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:117
static constexpr uint64 ticksPerMinute
Definition: timespan.h:78