C++ Utilities  5.7.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 "../global.h"
5 
6 #include <cstdint>
7 #include <functional>
8 #include <limits>
9 #include <string>
10 
11 namespace CppUtilities {
12 
13 class DateTime;
14 
20  Normal,
21  WithMeasures,
22  TotalSeconds,
23 };
24 
26  friend class DateTime;
27 
28 public:
29  explicit constexpr TimeSpan();
30  explicit constexpr TimeSpan(std::int64_t ticks);
31 
32  static constexpr TimeSpan fromMilliseconds(double milliseconds);
33  static constexpr TimeSpan fromSeconds(double seconds);
34  static constexpr TimeSpan fromMinutes(double minutes);
35  static constexpr TimeSpan fromHours(double hours);
36  static constexpr TimeSpan fromDays(double days);
37  static TimeSpan fromString(const std::string &str, char separator = ':');
38  static TimeSpan fromString(const char *str, char separator);
39  static constexpr TimeSpan negativeInfinity();
40  static constexpr TimeSpan infinity();
41 
42  std::int64_t &ticks();
43  constexpr std::int64_t totalTicks() const;
44  constexpr double totalMicroseconds() 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 nanoseconds() const;
52  constexpr int microseconds() const;
53  constexpr int milliseconds() const;
54  constexpr int seconds() const;
55  constexpr int minutes() const;
56  constexpr int hours() const;
57  constexpr int days() const;
58 
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 bool operator>=(const TimeSpan &other) const;
65  constexpr TimeSpan operator+(const TimeSpan &other) const;
66  constexpr TimeSpan operator-(const TimeSpan &other) const;
67  TimeSpan &operator+=(const TimeSpan &other);
68  TimeSpan &operator-=(const TimeSpan &other);
69 
70  std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
71  void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
72  constexpr bool isNull() const;
73  constexpr bool isNegative() const;
74  constexpr bool isNegativeInfinity() const;
75  constexpr bool isInfinity() const;
76 
77  static constexpr std::int64_t nanosecondsPerTick = 100uL;
78  static constexpr std::int64_t ticksPerMicrosecond = 10uL;
79  static constexpr std::int64_t ticksPerMillisecond = 10000uL;
80  static constexpr std::int64_t ticksPerSecond = 10000000uL;
81  static constexpr std::int64_t ticksPerMinute = 600000000uL;
82  static constexpr std::int64_t ticksPerHour = 36000000000uL;
83  static constexpr std::int64_t ticksPerDay = 864000000000uL;
84 
85 private:
86  std::int64_t m_ticks;
87 };
88 
92 constexpr inline TimeSpan::TimeSpan()
93  : m_ticks(0)
94 {
95 }
96 
100 constexpr inline TimeSpan::TimeSpan(std::int64_t ticks)
101  : m_ticks(ticks)
102 {
103 }
104 
108 constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
109 {
110  return TimeSpan(static_cast<std::int64_t>(milliseconds * static_cast<double>(ticksPerMillisecond)));
111 }
112 
116 constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
117 {
118  return TimeSpan(static_cast<std::int64_t>(seconds * static_cast<double>(ticksPerSecond)));
119 }
120 
124 constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
125 {
126  return TimeSpan(static_cast<std::int64_t>(minutes * static_cast<double>(ticksPerMinute)));
127 }
128 
132 constexpr inline TimeSpan TimeSpan::fromHours(double hours)
133 {
134  return TimeSpan(static_cast<std::int64_t>(hours * static_cast<double>(ticksPerHour)));
135 }
136 
140 constexpr inline TimeSpan TimeSpan::fromDays(double days)
141 {
142  return TimeSpan(static_cast<std::int64_t>(days * static_cast<double>(ticksPerDay)));
143 }
144 
153 inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
154 {
155  return TimeSpan::fromString(str.data(), separator);
156 }
157 
162 {
163  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
164 }
165 
169 constexpr inline TimeSpan TimeSpan::infinity()
170 {
171  return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
172 }
173 
177 inline std::int64_t &TimeSpan::ticks()
178 {
179  return m_ticks;
180 }
181 
185 constexpr inline std::int64_t TimeSpan::totalTicks() const
186 {
187  return m_ticks;
188 }
189 
193 constexpr double TimeSpan::totalMicroseconds() const
194 {
195  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
196 }
197 
201 constexpr inline double TimeSpan::totalMilliseconds() const
202 {
203  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
204 }
205 
209 constexpr inline double TimeSpan::totalSeconds() const
210 {
211  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
212 }
213 
217 constexpr inline double TimeSpan::totalMinutes() const
218 {
219  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
220 }
221 
225 constexpr inline double TimeSpan::totalHours() const
226 {
227  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
228 }
229 
233 constexpr inline double TimeSpan::totalDays() const
234 {
235  return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
236 }
237 
243 constexpr int TimeSpan::nanoseconds() const
244 {
245  return m_ticks % 10l * TimeSpan::nanosecondsPerTick;
246 }
247 
251 constexpr int TimeSpan::microseconds() const
252 {
253  return (m_ticks / ticksPerMicrosecond) % 1000l;
254 }
255 
259 constexpr inline int TimeSpan::milliseconds() const
260 {
261  return (m_ticks / ticksPerMillisecond) % 1000l;
262 }
263 
267 constexpr inline int TimeSpan::seconds() const
268 {
269  return (m_ticks / ticksPerSecond) % 60l;
270 }
271 
275 constexpr inline int TimeSpan::minutes() const
276 {
277  return (m_ticks / ticksPerMinute) % 60l;
278 }
279 
283 constexpr inline int TimeSpan::hours() const
284 {
285  return (m_ticks / ticksPerHour) % 24l;
286 }
287 
291 constexpr inline int TimeSpan::days() const
292 {
293  return (m_ticks / ticksPerDay);
294 }
295 
299 constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
300 {
301  return m_ticks == other.m_ticks;
302 }
303 
307 constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
308 {
309  return m_ticks != other.m_ticks;
310 }
311 
315 constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
316 {
317  return m_ticks < other.m_ticks;
318 }
319 
323 constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
324 {
325  return m_ticks > other.m_ticks;
326 }
327 
331 constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
332 {
333  return m_ticks <= other.m_ticks;
334 }
335 
339 constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
340 {
341  return m_ticks >= other.m_ticks;
342 }
343 
347 constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
348 {
349  return TimeSpan(m_ticks + other.m_ticks);
350 }
351 
355 constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
356 {
357  return TimeSpan(m_ticks - other.m_ticks);
358 }
359 
364 {
365  m_ticks += other.m_ticks;
366  return *this;
367 }
368 
373 {
374  m_ticks -= other.m_ticks;
375  return *this;
376 }
377 
384 inline std::string TimeSpan::toString(TimeSpanOutputFormat format, bool fullSeconds) const
385 {
386  std::string result;
387  toString(result, format, fullSeconds);
388  return result;
389 }
390 
394 constexpr inline bool TimeSpan::isNull() const
395 {
396  return m_ticks == 0;
397 }
398 
402 constexpr inline bool TimeSpan::isNegative() const
403 {
404  return m_ticks < 0;
405 }
406 
410 constexpr inline bool TimeSpan::isNegativeInfinity() const
411 {
412  return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
413 }
414 
418 constexpr inline bool TimeSpan::isInfinity() const
419 {
420  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
421 }
422 } // namespace CppUtilities
423 
424 namespace std {
426 template <> struct hash<CppUtilities::TimeSpan> {
427  inline size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
428  {
429  return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
430  }
431 };
432 } // namespace std
433 
434 #endif // CHRONO_UTILITIES_TIMESPAN_H
CppUtilities::TimeSpan::operator!=
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition: timespan.h:307
CppUtilities::TimeSpan::ticksPerDay
static constexpr std::int64_t ticksPerDay
Definition: timespan.h:83
CppUtilities::TimeSpan::totalMilliseconds
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:201
CppUtilities::TimeSpan::ticksPerMillisecond
static constexpr std::int64_t ticksPerMillisecond
Definition: timespan.h:79
CppUtilities::TimeSpan::operator<
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition: timespan.h:315
CppUtilities::TimeSpan::fromMilliseconds
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
Definition: timespan.h:108
CppUtilities::TimeSpan::totalMicroseconds
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition: timespan.h:193
CppUtilities::TimeSpan::operator-=
TimeSpan & operator-=(const TimeSpan &other)
Substracts another TimeSpan from the current instance.
Definition: timespan.h:372
CppUtilities::TimeSpan::negativeInfinity
static constexpr TimeSpan negativeInfinity()
Constructs a new instace of the TimeSpan class with the minimal number of ticks.
Definition: timespan.h:161
CppUtilities::operator+
CPP_UTILITIES_EXPORT DateTime operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
Definition: period.cpp:60
CppUtilities::TimeSpan::TimeSpan
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:92
CppUtilities::TimeSpan::milliseconds
constexpr int milliseconds() const
Returns the miliseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:259
CppUtilities::TimeSpan::isNegativeInfinity
constexpr bool isNegativeInfinity() const
Returns whether the time inverval represented by the current instance is the smallest representable T...
Definition: timespan.h:410
CppUtilities::TimeSpan::seconds
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:267
CppUtilities::TimeSpan::ticksPerMicrosecond
static constexpr std::int64_t ticksPerMicrosecond
Definition: timespan.h:78
CppUtilities::TimeSpan::ticks
std::int64_t & ticks()
Returns a mutable reference to the total ticks.
Definition: timespan.h:177
CppUtilities::TimeSpan::fromString
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:153
CppUtilities::max
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:100
CppUtilities::TimeSpan::toString
std::string toString(TimeSpanOutputFormat format=TimeSpanOutputFormat::Normal, bool fullSeconds=false) const
Converts the value of the current TimeSpan object to its equivalent std::string representation accord...
Definition: timespan.h:384
CppUtilities::TimeSpan::operator>
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition: timespan.h:323
CppUtilities::TimeSpan::totalDays
constexpr double totalDays() const
Returns the value of the current TimeSpan class expressed in whole and fractional days.
Definition: timespan.h:233
CppUtilities::TimeSpan::operator>=
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.
Definition: timespan.h:339
CppUtilities::TimeSpan::totalTicks
constexpr std::int64_t totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
Definition: timespan.h:185
CppUtilities::TimeSpanOutputFormat::Normal
@ Normal
std::hash< CppUtilities::TimeSpan >::operator()
size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
Definition: timespan.h:427
CppUtilities::TimeSpanOutputFormat
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:19
CppUtilities::TimeSpan::operator-
constexpr TimeSpan operator-(const TimeSpan &other) const
Substracts two TimeSpan instances.
Definition: timespan.h:355
CppUtilities::TimeSpan::hours
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:283
CppUtilities::TimeSpan::minutes
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:275
CppUtilities
Contains all utilities provides by the c++utilities library.
Definition: argumentparser.h:17
CppUtilities::TimeSpan::ticksPerHour
static constexpr std::int64_t ticksPerHour
Definition: timespan.h:82
CppUtilities::TimeSpan::fromMinutes
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
Definition: timespan.h:124
CppUtilities::TimeSpan::microseconds
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:251
CppUtilities::FlagEnumClassOperations::operator+=
constexpr FlagEnumClass & operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition: flagenumclass.h:64
CppUtilities::TimeSpan::fromHours
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:132
CppUtilities::FlagEnumClassOperations::operator-=
constexpr FlagEnumClass & operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition: flagenumclass.h:71
CppUtilities::TimeSpan::operator+=
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition: timespan.h:363
CppUtilities::min
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition: math.h:88
CppUtilities::TimeSpan
Represents a time interval.
Definition: timespan.h:25
CppUtilities::TimeSpan::totalHours
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:225
CppUtilities::TimeSpan::nanosecondsPerTick
static constexpr std::int64_t nanosecondsPerTick
Definition: timespan.h:77
CppUtilities::TimeSpan::isNegative
constexpr bool isNegative() const
Returns ture if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:402
CppUtilities::TimeSpan::operator==
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition: timespan.h:299
CppUtilities::DateTime
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:53
CppUtilities::TimeSpan::ticksPerSecond
static constexpr std::int64_t ticksPerSecond
Definition: timespan.h:80
CppUtilities::TimeSpan::isInfinity
constexpr bool isInfinity() const
Returns whether the time inverval represented by the current instance is the longest representable Ti...
Definition: timespan.h:418
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CppUtilities::TimeSpan::infinity
static constexpr TimeSpan infinity()
Constructs a new instace of the TimeSpan class with the maximal number of ticks.
Definition: timespan.h:169
CppUtilities::TimeSpan::totalMinutes
constexpr double totalMinutes() const
Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition: timespan.h:217
CppUtilities::operator==
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:215
CppUtilities::TimeSpan::ticksPerMinute
static constexpr std::int64_t ticksPerMinute
Definition: timespan.h:81
CppUtilities::TimeSpan::nanoseconds
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:243
CppUtilities::TimeSpan::operator+
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition: timespan.h:347
CppUtilities::TimeSpan::fromSeconds
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:116
CppUtilities::TimeSpan::operator<=
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition: timespan.h:331
CppUtilities::TimeSpan::isNull
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:394
CppUtilities::TimeSpan::fromDays
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:140
CppUtilities::TimeSpan::days
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:291
CppUtilities::TimeSpan::totalSeconds
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:209