C++ Utilities  4.14.1
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  constexpr int64 totalTicks() const;
46  constexpr double totalMicroseconds() const;
47  constexpr double totalMilliseconds() const;
48  constexpr double totalSeconds() const;
49  constexpr double totalMinutes() const;
50  constexpr double totalHours() const;
51  constexpr double totalDays() const;
52 
53  constexpr int nanoseconds() const;
54  constexpr int microseconds() const;
55  constexpr int milliseconds() const;
56  constexpr int seconds() const;
57  constexpr int minutes() const;
58  constexpr int hours() const;
59  constexpr int days() const;
60 
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 bool operator>=(const TimeSpan &other) const;
67  constexpr TimeSpan operator+(const TimeSpan &other) const;
68  constexpr TimeSpan operator-(const TimeSpan &other) const;
69  TimeSpan &operator+=(const TimeSpan &other);
70  TimeSpan &operator-=(const TimeSpan &other);
71 
72  std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
73  void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
74  constexpr bool isNull() const;
75  constexpr bool isNegative() const;
76  constexpr bool isNegativeInfinity() const;
77  constexpr bool isInfinity() const;
78 
79  // TODO: make those public constants signed in next major release and remove private ones then
80  static constexpr int64 nanosecondsPerTick = 100uL;
81  static constexpr int64 ticksPerMicrosecond = 10uL;
82  static constexpr uint64 ticksPerMillisecond = 10000uL;
83  static constexpr uint64 ticksPerSecond = 10000000uL;
84  static constexpr uint64 ticksPerMinute = 600000000uL;
85  static constexpr uint64 ticksPerHour = 36000000000uL;
86  static constexpr uint64 ticksPerDay = 864000000000uL;
87 
88 private:
89  static constexpr int64 m_ticksPerMillisecond = 10000L;
90  static constexpr int64 m_ticksPerSecond = 10000000L;
91  static constexpr int64 m_ticksPerMinute = 600000000L;
92  static constexpr int64 m_ticksPerHour = 36000000000L;
93  static constexpr int64 m_ticksPerDay = 864000000000L;
94 
95 private:
96  int64 m_ticks;
97 };
98 
102 constexpr inline TimeSpan::TimeSpan()
103  : m_ticks(0)
104 {
105 }
106 
110 constexpr inline TimeSpan::TimeSpan(int64 ticks)
111  : m_ticks(ticks)
112 {
113 }
114 
118 constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
119 {
120  return TimeSpan(static_cast<int64>(milliseconds * static_cast<double>(m_ticksPerMillisecond)));
121 }
122 
126 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
127 {
128  return TimeSpan(static_cast<int64>(seconds * static_cast<double>(m_ticksPerSecond)));
129 }
130 
134 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
135 {
136  return TimeSpan(static_cast<int64>(minutes * static_cast<double>(m_ticksPerMinute)));
137 }
138 
142 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
143 {
144  return TimeSpan(static_cast<int64>(hours * static_cast<double>(m_ticksPerHour)));
145 }
146 
150 constexpr inline TimeSpan TimeSpan::fromDays(double days)
151 {
152  return TimeSpan(static_cast<int64>(days * static_cast<double>(m_ticksPerDay)));
153 }
154 
163 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
164 {
165  return TimeSpan::fromString(str.data(), separator);
166 }
167 
172 {
173  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
174 }
175 
179 constexpr inline TimeSpan TimeSpan::infinity()
180 {
181  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
182 }
183 
187 constexpr inline int64 TimeSpan::totalTicks() const
188 {
189  return m_ticks;
190 }
191 
195 constexpr double TimeSpan::totalMicroseconds() const
196 {
197  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
198 }
199 
203 constexpr inline double TimeSpan::totalMilliseconds() const
204 {
205  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMillisecond);
206 }
207 
211 constexpr inline double TimeSpan::totalSeconds() const
212 {
213  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerSecond);
214 }
215 
219 constexpr inline double TimeSpan::totalMinutes() const
220 {
221  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMinute);
222 }
223 
227 constexpr inline double TimeSpan::totalHours() const
228 {
229  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerHour);
230 }
231 
235 constexpr inline double TimeSpan::totalDays() const
236 {
237  return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerDay);
238 }
239 
245 constexpr int TimeSpan::nanoseconds() const
246 {
247  return m_ticks % 10l * TimeSpan::nanosecondsPerTick;
248 }
249 
253 constexpr int TimeSpan::microseconds() const
254 {
255  return (m_ticks / ticksPerMicrosecond) % 1000l;
256 }
257 
261 constexpr inline int TimeSpan::milliseconds() const
262 {
263  return (m_ticks / m_ticksPerMillisecond) % 1000l;
264 }
265 
269 constexpr inline int TimeSpan::seconds() const
270 {
271  return (m_ticks / m_ticksPerSecond) % 60l;
272 }
273 
277 constexpr inline int TimeSpan::minutes() const
278 {
279  return (m_ticks / m_ticksPerMinute) % 60l;
280 }
281 
285 constexpr inline int TimeSpan::hours() const
286 {
287  return (m_ticks / m_ticksPerHour) % 24l;
288 }
289 
293 constexpr inline int TimeSpan::days() const
294 {
295  return (m_ticks / m_ticksPerDay);
296 }
297 
301 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
302 {
303  return m_ticks == other.m_ticks;
304 }
305 
309 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
310 {
311  return m_ticks != other.m_ticks;
312 }
313 
317 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
318 {
319  return m_ticks < other.m_ticks;
320 }
321 
325 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
326 {
327  return m_ticks > other.m_ticks;
328 }
329 
333 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
334 {
335  return m_ticks <= other.m_ticks;
336 }
337 
341 constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
342 {
343  return m_ticks >= other.m_ticks;
344 }
345 
349 constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
350 {
351  return TimeSpan(m_ticks + other.m_ticks);
352 }
353 
357 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
358 {
359  return TimeSpan(m_ticks - other.m_ticks);
360 }
361 
366 {
367  m_ticks += other.m_ticks;
368  return *this;
369 }
370 
375 {
376  m_ticks -= other.m_ticks;
377  return *this;
378 }
379 
383 constexpr inline bool TimeSpan::isNull() const
384 {
385  return m_ticks == 0;
386 }
387 
391 constexpr inline bool TimeSpan::isNegative() const
392 {
393  return m_ticks < 0;
394 }
395 
399 constexpr inline bool TimeSpan::isNegativeInfinity() const
400 {
402 }
403 
407 constexpr inline bool TimeSpan::isInfinity() const
408 {
410 }
411 } // namespace ChronoUtilities
412 
413 namespace std {
414 template <> struct hash<ChronoUtilities::TimeSpan> {
415  inline size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
416  {
417  return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
418  }
419 };
420 } // namespace std
421 
422 #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:245
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:203
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:142
static constexpr TimeSpan negativeInfinity()
Constructs a new instace of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:171
size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
Definition: timespan.h:415
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:391
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition: timespan.h:333
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:187
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:293
constexpr double totalMinutes() const
Returns the value of the current TimeSpan class expressed in whole and fractional minutes...
Definition: timespan.h:219
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:285
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
Definition: timespan.h:118
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds...
Definition: timespan.h:195
STL namespace.
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:102
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:126
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
static constexpr int64 ticksPerMicrosecond
Definition: timespan.h:81
TimeSpan & operator-=(const TimeSpan &other)
Substracts another TimeSpan from the current instance.
Definition: timespan.h:374
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours...
Definition: timespan.h:227
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:383
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:341
static constexpr int64 nanosecondsPerTick
Definition: timespan.h:80
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:317
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:277
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:162
constexpr TimeSpan operator-(const TimeSpan &other) const
Substracts two TimeSpan instances.
Definition: timespan.h:357
constexpr int milliseconds() const
Returns the miliseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:261
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:150
constexpr bool isInfinity() const
Returns whether the time inverval represented by the current instance is the longest representable Ti...
Definition: timespan.h:407
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds...
Definition: timespan.h:211
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:309
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:365
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:269
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:253
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:301
constexpr double totalDays() const
Returns the value of the current TimeSpan class expressed in whole and fractional days...
Definition: timespan.h:235
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:325
constexpr bool isNegativeInfinity() const
Returns whether the time inverval represented by the current instance is the smallest representable T...
Definition: timespan.h:399
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:349
static constexpr TimeSpan infinity()
Constructs a new instace of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:179
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:163
#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:134