C++ Utilities  4.9.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 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 milliseconds() const;
52  constexpr int seconds() const;
53  constexpr int minutes() const;
54  constexpr int hours() const;
55  constexpr int days() const;
56 
57  constexpr bool operator==(const TimeSpan &other) const;
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 TimeSpan operator+(const TimeSpan &other) const;
64  constexpr TimeSpan operator-(const TimeSpan &other) const;
65  TimeSpan &operator+=(const TimeSpan &other);
66  TimeSpan &operator-=(const TimeSpan &other);
67 
68  std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool noMilliseconds = false) const;
69  void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool noMilliseconds = false) const;
70  constexpr bool isNull() const;
71  constexpr bool isNegative() const;
72  constexpr bool isNegativeInfinity() const;
73  constexpr bool isInfinity() const;
74 
75  // TODO: make those public constants signed in next major release and remove private ones then
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  static constexpr int64 m_ticksPerMillisecond = 10000L;
84  static constexpr int64 m_ticksPerSecond = 10000000L;
85  static constexpr int64 m_ticksPerMinute = 600000000L;
86  static constexpr int64 m_ticksPerHour = 36000000000L;
87  static constexpr int64 m_ticksPerDay = 864000000000L;
88 
89 private:
90  int64 m_ticks;
91 };
92 
96 constexpr inline TimeSpan::TimeSpan()
97  : m_ticks(0)
98 {
99 }
100 
104 constexpr inline TimeSpan::TimeSpan(int64 ticks)
105  : m_ticks(ticks)
106 {
107 }
108 
113 {
114  return TimeSpan(static_cast<int64>(milliseconds * static_cast<double>(m_ticksPerMillisecond)));
115 }
116 
120 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
121 {
122  return TimeSpan(static_cast<int64>(seconds * static_cast<double>(m_ticksPerSecond)));
123 }
124 
128 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
129 {
130  return TimeSpan(static_cast<int64>(minutes * static_cast<double>(m_ticksPerMinute)));
131 }
132 
136 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
137 {
138  return TimeSpan(static_cast<int64>(hours * static_cast<double>(m_ticksPerHour)));
139 }
140 
144 constexpr inline TimeSpan TimeSpan::fromDays(double days)
145 {
146  return TimeSpan(static_cast<int64>(days * static_cast<double>(m_ticksPerDay)));
147 }
148 
152 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
153 {
154  return TimeSpan::fromString(str.data(), separator);
155 }
156 
161 {
162  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
163 }
164 
168 constexpr inline TimeSpan TimeSpan::infinity()
169 {
170  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
171 }
172 
176 constexpr inline int64 TimeSpan::totalTicks() const
177 {
178  return m_ticks;
179 }
180 
184 constexpr inline double TimeSpan::totalMilliseconds() const
185 {
186  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMillisecond);
187 }
188 
192 constexpr inline double TimeSpan::totalSeconds() const
193 {
194  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerSecond);
195 }
196 
200 constexpr inline double TimeSpan::totalMinutes() const
201 {
202  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMinute);
203 }
204 
208 constexpr inline double TimeSpan::totalHours() const
209 {
210  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerHour);
211 }
212 
216 constexpr inline double TimeSpan::totalDays() const
217 {
218  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerDay);
219 }
220 
224 constexpr inline int TimeSpan::milliseconds() const
225 {
226  return (m_ticks / m_ticksPerMillisecond) % 1000l;
227 }
228 
232 constexpr inline int TimeSpan::seconds() const
233 {
234  return (m_ticks / m_ticksPerSecond) % 60l;
235 }
236 
240 constexpr inline int TimeSpan::minutes() const
241 {
242  return (m_ticks / m_ticksPerMinute) % 60l;
243 }
244 
248 constexpr inline int TimeSpan::hours() const
249 {
250  return (m_ticks / m_ticksPerHour) % 24l;
251 }
252 
256 constexpr inline int TimeSpan::days() const
257 {
258  return (m_ticks / m_ticksPerDay);
259 }
260 
264 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
265 {
266  return m_ticks == other.m_ticks;
267 }
268 
272 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
273 {
274  return m_ticks != other.m_ticks;
275 }
276 
280 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
281 {
282  return m_ticks < other.m_ticks;
283 }
284 
288 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
289 {
290  return m_ticks > other.m_ticks;
291 }
292 
296 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
297 {
298  return m_ticks <= other.m_ticks;
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 TimeSpan TimeSpan::operator+(const TimeSpan &other) const
313 {
314  return TimeSpan(m_ticks + other.m_ticks);
315 }
316 
320 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
321 {
322  return TimeSpan(m_ticks - other.m_ticks);
323 }
324 
329 {
330  m_ticks += other.m_ticks;
331  return *this;
332 }
333 
338 {
339  m_ticks -= other.m_ticks;
340  return *this;
341 }
342 
346 constexpr inline bool TimeSpan::isNull() const
347 {
348  return m_ticks == 0;
349 }
350 
354 constexpr inline bool TimeSpan::isNegative() const
355 {
356  return m_ticks < 0;
357 }
358 
362 constexpr inline bool TimeSpan::isNegativeInfinity() const
363 {
364  return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
365 }
366 
370 constexpr inline bool TimeSpan::isInfinity() const
371 {
372  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
373 }
374 }
375 
376 namespace std {
377 template <> struct hash<ChronoUtilities::TimeSpan> {
378  inline size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
379  {
380  return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
381  }
382 };
383 }
384 
385 #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:184
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:136
static constexpr TimeSpan negativeInfinity()
Constructs a new instace of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:160
size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
Definition: timespan.h:378
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:354
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition: timespan.h:296
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:176
constexpr int days() const
Gets the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:256
constexpr double totalMinutes() const
Gets the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition: timespan.h:200
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:248
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
Definition: timespan.h:112
STL namespace.
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:96
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:120
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:337
constexpr double totalHours() const
Gets the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:208
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:346
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan...
Definition: timespan.h:304
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:280
constexpr int minutes() const
Gets the minutes component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:240
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:148
constexpr TimeSpan operator-(const TimeSpan &other) const
Substracts two TimeSpan instances.
Definition: timespan.h:320
constexpr int milliseconds() const
Gets the miliseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:224
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:144
constexpr bool isInfinity() const
Returns whether the time inverval represented by the current instance is the longest representable Ti...
Definition: timespan.h:370
constexpr double totalSeconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:192
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:272
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition: timespan.h:328
constexpr int seconds() const
Gets the seconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:232
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:264
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:216
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:288
constexpr bool isNegativeInfinity() const
Returns whether the time inverval represented by the current instance is the smallest representable T...
Definition: timespan.h:362
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:312
static constexpr TimeSpan infinity()
Constructs a new instace of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:168
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:152
#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:128