C++ Utilities  4.17.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 "../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,
24  WithMeasures,
25  TotalSeconds,
26 };
27 
29  friend class DateTime;
30 
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  int64 &ticks();
46  constexpr int64 totalTicks() const;
47  constexpr double totalMicroseconds() const;
48  constexpr double totalMilliseconds() const;
49  constexpr double totalSeconds() const;
50  constexpr double totalMinutes() const;
51  constexpr double totalHours() const;
52  constexpr double totalDays() const;
53 
54  constexpr int nanoseconds() const;
55  constexpr int microseconds() const;
56  constexpr int milliseconds() const;
57  constexpr int seconds() const;
58  constexpr int minutes() const;
59  constexpr int hours() const;
60  constexpr int days() const;
61 
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 bool operator<=(const TimeSpan &other) const;
67  constexpr bool operator>=(const TimeSpan &other) const;
68  constexpr TimeSpan operator+(const TimeSpan &other) const;
69  constexpr TimeSpan operator-(const TimeSpan &other) const;
70  TimeSpan &operator+=(const TimeSpan &other);
71  TimeSpan &operator-=(const TimeSpan &other);
72 
73  std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
74  void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
75  constexpr bool isNull() const;
76  constexpr bool isNegative() const;
77  constexpr bool isNegativeInfinity() const;
78  constexpr bool isInfinity() const;
79 
80  // TODO: make those public constants signed in next major release and remove private ones then
81  static constexpr int64 nanosecondsPerTick = 100uL;
82  static constexpr int64 ticksPerMicrosecond = 10uL;
83  static constexpr uint64 ticksPerMillisecond = 10000uL;
84  static constexpr uint64 ticksPerSecond = 10000000uL;
85  static constexpr uint64 ticksPerMinute = 600000000uL;
86  static constexpr uint64 ticksPerHour = 36000000000uL;
87  static constexpr uint64 ticksPerDay = 864000000000uL;
88 
89 private:
90  static constexpr int64 m_ticksPerMillisecond = 10000L;
91  static constexpr int64 m_ticksPerSecond = 10000000L;
92  static constexpr int64 m_ticksPerMinute = 600000000L;
93  static constexpr int64 m_ticksPerHour = 36000000000L;
94  static constexpr int64 m_ticksPerDay = 864000000000L;
95 
96 private:
97  int64 m_ticks;
98 };
99 
103 constexpr inline TimeSpan::TimeSpan()
104  : m_ticks(0)
105 {
106 }
107 
111 constexpr inline TimeSpan::TimeSpan(int64 ticks)
112  : m_ticks(ticks)
113 {
114 }
115 
119 constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
120 {
121  return TimeSpan(static_cast<int64>(milliseconds * static_cast<double>(m_ticksPerMillisecond)));
122 }
123 
127 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
128 {
129  return TimeSpan(static_cast<int64>(seconds * static_cast<double>(m_ticksPerSecond)));
130 }
131 
135 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
136 {
137  return TimeSpan(static_cast<int64>(minutes * static_cast<double>(m_ticksPerMinute)));
138 }
139 
143 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
144 {
145  return TimeSpan(static_cast<int64>(hours * static_cast<double>(m_ticksPerHour)));
146 }
147 
151 constexpr inline TimeSpan TimeSpan::fromDays(double days)
152 {
153  return TimeSpan(static_cast<int64>(days * static_cast<double>(m_ticksPerDay)));
154 }
155 
164 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
165 {
166  return TimeSpan::fromString(str.data(), separator);
167 }
168 
173 {
174  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
175 }
176 
180 constexpr inline TimeSpan TimeSpan::infinity()
181 {
182  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
183 }
184 
189 {
190  return m_ticks;
191 }
192 
196 constexpr inline int64 TimeSpan::totalTicks() const
197 {
198  return m_ticks;
199 }
200 
204 constexpr double TimeSpan::totalMicroseconds() const
205 {
206  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
207 }
208 
212 constexpr inline double TimeSpan::totalMilliseconds() const
213 {
214  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMillisecond);
215 }
216 
220 constexpr inline double TimeSpan::totalSeconds() const
221 {
222  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerSecond);
223 }
224 
228 constexpr inline double TimeSpan::totalMinutes() const
229 {
230  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMinute);
231 }
232 
236 constexpr inline double TimeSpan::totalHours() const
237 {
238  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerHour);
239 }
240 
244 constexpr inline double TimeSpan::totalDays() const
245 {
246  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerDay);
247 }
248 
254 constexpr int TimeSpan::nanoseconds() const
255 {
256  return m_ticks % 10l * TimeSpan::nanosecondsPerTick;
257 }
258 
262 constexpr int TimeSpan::microseconds() const
263 {
264  return (m_ticks / ticksPerMicrosecond) % 1000l;
265 }
266 
270 constexpr inline int TimeSpan::milliseconds() const
271 {
272  return (m_ticks / m_ticksPerMillisecond) % 1000l;
273 }
274 
278 constexpr inline int TimeSpan::seconds() const
279 {
280  return (m_ticks / m_ticksPerSecond) % 60l;
281 }
282 
286 constexpr inline int TimeSpan::minutes() const
287 {
288  return (m_ticks / m_ticksPerMinute) % 60l;
289 }
290 
294 constexpr inline int TimeSpan::hours() const
295 {
296  return (m_ticks / m_ticksPerHour) % 24l;
297 }
298 
302 constexpr inline int TimeSpan::days() const
303 {
304  return (m_ticks / m_ticksPerDay);
305 }
306 
310 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
311 {
312  return m_ticks == other.m_ticks;
313 }
314 
318 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
319 {
320  return m_ticks != other.m_ticks;
321 }
322 
326 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
327 {
328  return m_ticks < other.m_ticks;
329 }
330 
334 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
335 {
336  return m_ticks > other.m_ticks;
337 }
338 
342 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
343 {
344  return m_ticks <= other.m_ticks;
345 }
346 
350 constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
351 {
352  return m_ticks >= other.m_ticks;
353 }
354 
358 constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
359 {
360  return TimeSpan(m_ticks + other.m_ticks);
361 }
362 
366 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
367 {
368  return TimeSpan(m_ticks - other.m_ticks);
369 }
370 
375 {
376  m_ticks += other.m_ticks;
377  return *this;
378 }
379 
384 {
385  m_ticks -= other.m_ticks;
386  return *this;
387 }
388 
392 constexpr inline bool TimeSpan::isNull() const
393 {
394  return m_ticks == 0;
395 }
396 
400 constexpr inline bool TimeSpan::isNegative() const
401 {
402  return m_ticks < 0;
403 }
404 
408 constexpr inline bool TimeSpan::isNegativeInfinity() const
409 {
410  return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
411 }
412 
416 constexpr inline bool TimeSpan::isInfinity() const
417 {
418  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
419 }
420 } // namespace ChronoUtilities
421 
422 namespace std {
423 template <> struct hash<ChronoUtilities::TimeSpan> {
424  inline size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
425  {
426  return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
427  }
428 };
429 } // namespace std
430 
431 #endif // CHRONO_UTILITIES_TIMESPAN_H
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:254
DateTime CPP_UTILITIES_EXPORT operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
Definition: period.cpp:62
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:22
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:212
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:143
static constexpr TimeSpan negativeInfinity()
Constructs a new instace of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:172
size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
Definition: timespan.h:424
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:400
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition: timespan.h:342
#define CPP_UTILITIES_EXPORT
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:52
Contains classes providing a means for handling date and time information.
Definition: datetime.h:12
constexpr int64 totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
Definition: timespan.h:196
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:302
constexpr double totalMinutes() const
Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition: timespan.h:228
Represents a time interval.
Definition: timespan.h:28
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:294
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
Definition: timespan.h:119
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition: timespan.h:204
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:103
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:127
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
static constexpr int64 ticksPerMicrosecond
Definition: timespan.h:82
TimeSpan & operator-=(const TimeSpan &other)
Substracts another TimeSpan from the current instance.
Definition: timespan.h:383
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:236
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:392
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:29
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.
Definition: timespan.h:350
static constexpr int64 nanosecondsPerTick
Definition: timespan.h:81
int64 & ticks()
Returns a mutable reference to the total ticks.
Definition: timespan.h:188
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:326
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:286
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:172
constexpr TimeSpan operator-(const TimeSpan &other) const
Substracts two TimeSpan instances.
Definition: timespan.h:366
constexpr int milliseconds() const
Returns the miliseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:270
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:151
constexpr bool isInfinity() const
Returns whether the time inverval represented by the current instance is the longest representable Ti...
Definition: timespan.h:416
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:220
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:318
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition: math.h:17
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition: timespan.h:374
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:278
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:262
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:310
constexpr double totalDays() const
Returns the value of the current TimeSpan class expressed in whole and fractional days.
Definition: timespan.h:244
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:334
constexpr bool isNegativeInfinity() const
Returns whether the time inverval represented by the current instance is the smallest representable T...
Definition: timespan.h:408
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:358
static constexpr TimeSpan infinity()
Constructs a new instace of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:180
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:164
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
Definition: timespan.h:135