C++ Utilities  4.7.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 <limits>
8 #include <string>
9 
13 namespace ChronoUtilities {
14 
15 class DateTime;
16 
22  Normal,
24 };
25 
27  friend class DateTime;
28 
29 public:
30  explicit constexpr TimeSpan();
31  explicit constexpr TimeSpan(int64 ticks);
32 
33  static constexpr TimeSpan fromMilliseconds(double milliseconds);
34  static constexpr TimeSpan fromSeconds(double seconds);
35  static constexpr TimeSpan fromMinutes(double minutes);
36  static constexpr TimeSpan fromHours(double hours);
37  static constexpr TimeSpan fromDays(double days);
38  static TimeSpan fromString(const std::string &str, char separator = ':');
39  static TimeSpan fromString(const char *str, char separator);
40  static constexpr TimeSpan negativeInfinity();
41  static constexpr TimeSpan infinity();
42 
43  constexpr int64 totalTicks() const;
44  constexpr double totalMilliseconds() const;
45  constexpr double totalSeconds() const;
46  constexpr double totalMinutes() const;
47  constexpr double totalHours() const;
48  constexpr double totalDays() const;
49 
50  constexpr int milliseconds() const;
51  constexpr int seconds() const;
52  constexpr int minutes() const;
53  constexpr int hours() const;
54  constexpr int days() const;
55 
56  constexpr bool operator==(const TimeSpan &other) const;
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 TimeSpan operator+(const TimeSpan &other) const;
63  constexpr TimeSpan operator-(const TimeSpan &other) const;
64  TimeSpan &operator+=(const TimeSpan &other);
65  TimeSpan &operator-=(const TimeSpan &other);
66 
67  std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool noMilliseconds = false) const;
68  void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool noMilliseconds = false) const;
69  constexpr bool isNull() const;
70  constexpr bool isNegative() const;
71  constexpr bool isNegativeInfinity() const;
72  constexpr bool isInfinity() const;
73 
74  static constexpr uint64 ticksPerMillisecond = 10000uL;
75  static constexpr uint64 ticksPerSecond = 10000000uL;
76  static constexpr uint64 ticksPerMinute = 600000000uL;
77  static constexpr uint64 ticksPerHour = 36000000000uL;
78  static constexpr uint64 ticksPerDay = 864000000000uL;
79 
80 private:
81  int64 m_ticks;
82 };
83 
87 constexpr inline TimeSpan::TimeSpan()
88  : m_ticks(0)
89 {
90 }
91 
95 constexpr inline TimeSpan::TimeSpan(int64 ticks)
96  : m_ticks(ticks)
97 {
98 }
99 
104 {
105  return TimeSpan(static_cast<int64>(milliseconds * static_cast<double>(ticksPerMillisecond)));
106 }
107 
111 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
112 {
113  return TimeSpan(static_cast<int64>(seconds * static_cast<double>(ticksPerSecond)));
114 }
115 
119 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
120 {
121  return TimeSpan(static_cast<int64>(minutes * static_cast<double>(ticksPerMinute)));
122 }
123 
127 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
128 {
129  return TimeSpan(static_cast<int64>(hours * static_cast<double>(ticksPerHour)));
130 }
131 
135 constexpr inline TimeSpan TimeSpan::fromDays(double days)
136 {
137  return TimeSpan(static_cast<int64>(days * static_cast<double>(ticksPerDay)));
138 }
139 
143 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
144 {
145  return TimeSpan::fromString(str.data(), separator);
146 }
147 
152 {
153  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
154 }
155 
159 constexpr inline TimeSpan TimeSpan::infinity()
160 {
161  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
162 }
163 
167 constexpr inline int64 TimeSpan::totalTicks() const
168 {
169  return m_ticks;
170 }
171 
175 constexpr inline double TimeSpan::totalMilliseconds() const
176 {
177  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
178 }
179 
183 constexpr inline double TimeSpan::totalSeconds() const
184 {
185  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
186 }
187 
191 constexpr inline double TimeSpan::totalMinutes() const
192 {
193  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
194 }
195 
199 constexpr inline double TimeSpan::totalHours() const
200 {
201  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
202 }
203 
207 constexpr inline double TimeSpan::totalDays() const
208 {
209  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
210 }
211 
215 constexpr inline int TimeSpan::milliseconds() const
216 {
217  return (m_ticks / ticksPerMillisecond) % 1000l;
218 }
219 
223 constexpr inline int TimeSpan::seconds() const
224 {
225  return (m_ticks / ticksPerSecond) % 60l;
226 }
227 
231 constexpr inline int TimeSpan::minutes() const
232 {
233  return (m_ticks / ticksPerMinute) % 60l;
234 }
235 
239 constexpr inline int TimeSpan::hours() const
240 {
241  return (m_ticks / ticksPerHour) % 24l;
242 }
243 
247 constexpr inline int TimeSpan::days() const
248 {
249  return (m_ticks / ticksPerDay);
250 }
251 
255 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
256 {
257  return m_ticks == other.m_ticks;
258 }
259 
263 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
264 {
265  return m_ticks != other.m_ticks;
266 }
267 
271 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
272 {
273  return m_ticks < other.m_ticks;
274 }
275 
279 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
280 {
281  return m_ticks > other.m_ticks;
282 }
283 
287 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
288 {
289  return m_ticks <= other.m_ticks;
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 TimeSpan TimeSpan::operator+(const TimeSpan &other) const
304 {
305  return TimeSpan(m_ticks + other.m_ticks);
306 }
307 
311 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
312 {
313  return TimeSpan(m_ticks - other.m_ticks);
314 }
315 
320 {
321  m_ticks += other.m_ticks;
322  return *this;
323 }
324 
329 {
330  m_ticks -= other.m_ticks;
331  return *this;
332 }
333 
337 constexpr inline bool TimeSpan::isNull() const
338 {
339  return m_ticks == 0;
340 }
341 
345 constexpr inline bool TimeSpan::isNegative() const
346 {
347  return m_ticks < 0;
348 }
349 
353 constexpr inline bool TimeSpan::isNegativeInfinity() const
354 {
355  return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
356 }
357 
361 constexpr inline bool TimeSpan::isInfinity() const
362 {
363  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
364 }
365 }
366 
367 namespace std {
368 template <> struct hash<ChronoUtilities::TimeSpan> {
369  inline size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
370  {
371  return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
372  }
373 };
374 }
375 
376 #endif // CHRONO_UTILITIES_TIMESPAN_H
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:21
constexpr double totalMilliseconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:175
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:127
static constexpr TimeSpan negativeInfinity()
Constructs a new instace of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:151
size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
Definition: timespan.h:369
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:345
static constexpr uint64 ticksPerDay
Definition: timespan.h:78
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition: timespan.h:287
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:167
constexpr int days() const
Gets the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:247
constexpr double totalMinutes() const
Gets the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition: timespan.h:191
Represents a time interval.
Definition: timespan.h:26
constexpr int hours() const
Gets the hours component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:239
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
Definition: timespan.h:103
STL namespace.
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:87
static constexpr uint64 ticksPerMillisecond
Definition: timespan.h:74
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:111
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:328
constexpr double totalHours() const
Gets the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:199
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:337
static constexpr uint64 ticksPerHour
Definition: timespan.h:77
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan...
Definition: timespan.h:295
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:271
constexpr int minutes() const
Gets the minutes component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:231
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:153
constexpr TimeSpan operator-(const TimeSpan &other) const
Substracts two TimeSpan instances.
Definition: timespan.h:311
constexpr int milliseconds() const
Gets the miliseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:215
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:135
constexpr bool isInfinity() const
Returns whether the time inverval represented by the current instance is the longest representable Ti...
Definition: timespan.h:361
constexpr double totalSeconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:183
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:263
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition: timespan.h:319
constexpr int seconds() const
Gets the seconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:223
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:255
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:207
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:279
constexpr bool isNegativeInfinity() const
Returns whether the time inverval represented by the current instance is the smallest representable T...
Definition: timespan.h:353
static constexpr uint64 ticksPerSecond
Definition: timespan.h:75
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:303
static constexpr TimeSpan infinity()
Constructs a new instace of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:159
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:143
#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:119
static constexpr uint64 ticksPerMinute
Definition: timespan.h:76