C++ Utilities 5.27.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
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
11namespace CppUtilities {
12
13class DateTime;
14
24
26 friend class DateTime;
27
28public:
29 using TickType = std::int64_t;
30
31 explicit constexpr TimeSpan();
32 explicit constexpr TimeSpan(TickType 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#ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
40 static constexpr TimeSpan fromMilliseconds(TickType milliseconds);
41 static constexpr TimeSpan fromSeconds(TickType seconds);
42 static constexpr TimeSpan fromMinutes(TickType minutes);
43 static constexpr TimeSpan fromHours(TickType hours);
44 static constexpr TimeSpan fromDays(TickType days);
45#endif
46 static TimeSpan fromString(const std::string &str, char separator = ':');
47 static TimeSpan fromString(const char *str, char separator);
48 static constexpr TimeSpan negativeInfinity();
49 static constexpr TimeSpan infinity();
50
51 TickType &ticks();
52 constexpr TickType totalTicks() const;
53 constexpr double totalMicroseconds() const;
54 constexpr double totalMilliseconds() const;
55 constexpr double totalSeconds() const;
56 constexpr double totalMinutes() const;
57 constexpr double totalHours() const;
58 constexpr double totalDays() const;
59
60 constexpr int nanoseconds() const;
61 constexpr int microseconds() const;
62 constexpr int milliseconds() const;
63 constexpr int seconds() const;
64 constexpr int minutes() const;
65 constexpr int hours() const;
66 constexpr int days() const;
67
68 constexpr bool operator==(const TimeSpan &other) const;
69 constexpr bool operator!=(const TimeSpan &other) const;
70 constexpr bool operator<(const TimeSpan &other) const;
71 constexpr bool operator>(const TimeSpan &other) const;
72 constexpr bool operator<=(const TimeSpan &other) const;
73 constexpr bool operator>=(const TimeSpan &other) const;
74 constexpr TimeSpan operator+(const TimeSpan &other) const;
75 constexpr TimeSpan operator-(const TimeSpan &other) const;
76 constexpr TimeSpan operator*(double factor) const;
77 constexpr TimeSpan operator/(double factor) const;
78#ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
79 constexpr TimeSpan operator*(TickType factor) const;
80 constexpr TimeSpan operator/(TickType factor) const;
81#endif
82 constexpr double operator/(TimeSpan other) const;
83 TimeSpan &operator+=(const TimeSpan &other);
84 TimeSpan &operator-=(const TimeSpan &other);
85 TimeSpan &operator*=(double factor);
86 TimeSpan &operator/=(double factor);
87#ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
88 TimeSpan &operator*=(TickType factor);
89 TimeSpan &operator/=(TickType factor);
90#endif
91
92 std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
93 void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
94 constexpr bool isNull() const;
95 constexpr bool isNegative() const;
96 constexpr bool isNegativeInfinity() const;
97 constexpr bool isInfinity() const;
98
99 static constexpr TickType nanosecondsPerTick = 100L;
100 static constexpr TickType ticksPerMicrosecond = 10L;
101 static constexpr TickType ticksPerMillisecond = 10000L;
102 static constexpr TickType ticksPerSecond = 10000000L;
103 static constexpr TickType ticksPerMinute = 600000000L;
104 static constexpr TickType ticksPerHour = 36000000000L;
105 static constexpr TickType ticksPerDay = 864000000000L;
106
107private:
108 TickType m_ticks;
109};
110
114constexpr inline TimeSpan::TimeSpan()
115 : m_ticks(0)
116{
117}
118
122constexpr inline TimeSpan::TimeSpan(TickType ticks)
123 : m_ticks(ticks)
124{
125}
126
130constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
131{
132 return TimeSpan(static_cast<TickType>(milliseconds * static_cast<double>(ticksPerMillisecond)));
133}
134
138constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
139{
140 return TimeSpan(static_cast<TickType>(seconds * static_cast<double>(ticksPerSecond)));
141}
142
146constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
147{
148 return TimeSpan(static_cast<TickType>(minutes * static_cast<double>(ticksPerMinute)));
149}
150
154constexpr inline TimeSpan TimeSpan::fromHours(double hours)
155{
156 return TimeSpan(static_cast<TickType>(hours * static_cast<double>(ticksPerHour)));
157}
158
162constexpr inline TimeSpan TimeSpan::fromDays(double days)
163{
164 return TimeSpan(static_cast<TickType>(days * static_cast<double>(ticksPerDay)));
165}
166
167#ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
171constexpr inline TimeSpan TimeSpan::fromMilliseconds(TickType milliseconds)
172{
174}
175
179constexpr inline TimeSpan TimeSpan::fromSeconds(TickType seconds)
180{
182}
183
187constexpr inline TimeSpan TimeSpan::fromMinutes(TickType minutes)
188{
190}
191
195constexpr inline TimeSpan TimeSpan::fromHours(TickType hours)
196{
197 return TimeSpan(hours * ticksPerHour);
198}
199
203constexpr inline TimeSpan TimeSpan::fromDays(TickType days)
204{
205 return TimeSpan(days * ticksPerDay);
206}
207#endif
208
217inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
218{
219 return TimeSpan::fromString(str.data(), separator);
220}
221
226{
227 return TimeSpan(std::numeric_limits<TickType>::min());
228}
229
233constexpr inline TimeSpan TimeSpan::infinity()
234{
235 return TimeSpan(std::numeric_limits<TickType>::max());
236}
237
242{
243 return m_ticks;
244}
245
250{
251 return m_ticks;
252}
253
257constexpr double TimeSpan::totalMicroseconds() const
258{
259 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
260}
261
265constexpr inline double TimeSpan::totalMilliseconds() const
266{
267 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
268}
269
273constexpr inline double TimeSpan::totalSeconds() const
274{
275 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
276}
277
281constexpr inline double TimeSpan::totalMinutes() const
282{
283 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
284}
285
289constexpr inline double TimeSpan::totalHours() const
290{
291 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
292}
293
297constexpr inline double TimeSpan::totalDays() const
298{
299 return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
300}
301
307constexpr int TimeSpan::nanoseconds() const
308{
309 return static_cast<int>(m_ticks % 10l * TimeSpan::nanosecondsPerTick);
310}
311
315constexpr int TimeSpan::microseconds() const
316{
317 return static_cast<int>((m_ticks / ticksPerMicrosecond) % 1000l);
318}
319
323constexpr inline int TimeSpan::milliseconds() const
324{
325 return static_cast<int>((m_ticks / ticksPerMillisecond) % 1000l);
326}
327
331constexpr inline int TimeSpan::seconds() const
332{
333 return static_cast<int>((m_ticks / ticksPerSecond) % 60l);
334}
335
339constexpr inline int TimeSpan::minutes() const
340{
341 return static_cast<int>((m_ticks / ticksPerMinute) % 60l);
342}
343
347constexpr inline int TimeSpan::hours() const
348{
349 return static_cast<int>((m_ticks / ticksPerHour) % 24l);
350}
351
355constexpr inline int TimeSpan::days() const
356{
357 return static_cast<int>((m_ticks / ticksPerDay));
358}
359
363constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
364{
365 return m_ticks == other.m_ticks;
366}
367
371constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
372{
373 return m_ticks != other.m_ticks;
374}
375
379constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
380{
381 return m_ticks < other.m_ticks;
382}
383
387constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
388{
389 return m_ticks > other.m_ticks;
390}
391
395constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
396{
397 return m_ticks <= other.m_ticks;
398}
399
403constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
404{
405 return m_ticks >= other.m_ticks;
406}
407
411constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
412{
413 return TimeSpan(m_ticks + other.m_ticks);
414}
415
419constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
420{
421 return TimeSpan(m_ticks - other.m_ticks);
422}
423
427constexpr inline TimeSpan TimeSpan::operator*(double factor) const
428{
429 return TimeSpan(static_cast<std::int64_t>(static_cast<double>(m_ticks) * factor));
430}
431
435constexpr inline TimeSpan TimeSpan::operator/(double factor) const
436{
437 return TimeSpan(static_cast<std::int64_t>(static_cast<double>(m_ticks) / factor));
438}
439
440#ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
444constexpr inline TimeSpan TimeSpan::operator*(std::int64_t factor) const
445{
446 return TimeSpan(m_ticks * factor);
447}
448
452constexpr inline TimeSpan TimeSpan::operator/(std::int64_t factor) const
453{
454 return TimeSpan(m_ticks / factor);
455}
456#endif
457
461constexpr inline double TimeSpan::operator/(TimeSpan other) const
462{
463 return static_cast<double>(m_ticks) / static_cast<double>(other.m_ticks);
464}
465
470{
471 m_ticks += other.m_ticks;
472 return *this;
473}
474
479{
480 m_ticks -= other.m_ticks;
481 return *this;
482}
483
487inline TimeSpan &TimeSpan::operator*=(double factor)
488{
489 m_ticks = static_cast<std::int64_t>(static_cast<double>(m_ticks) * factor);
490 return *this;
491}
492
496inline TimeSpan &TimeSpan::operator/=(double factor)
497{
498 m_ticks = static_cast<std::int64_t>(static_cast<double>(m_ticks) / factor);
499 return *this;
500}
501
502#ifdef CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
506inline TimeSpan &TimeSpan::operator*=(std::int64_t factor)
507{
508 m_ticks *= factor;
509 return *this;
510}
511
515inline TimeSpan &TimeSpan::operator/=(std::int64_t factor)
516{
517 m_ticks /= factor;
518 return *this;
519}
520#endif
521
528inline std::string TimeSpan::toString(TimeSpanOutputFormat format, bool fullSeconds) const
529{
530 std::string result;
531 toString(result, format, fullSeconds);
532 return result;
533}
534
538constexpr inline bool TimeSpan::isNull() const
539{
540 return m_ticks == 0;
541}
542
546constexpr inline bool TimeSpan::isNegative() const
547{
548 return m_ticks < 0;
549}
550
554constexpr inline bool TimeSpan::isNegativeInfinity() const
555{
556 return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
557}
558
562constexpr inline bool TimeSpan::isInfinity() const
563{
564 return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
565}
566} // namespace CppUtilities
567
568namespace std {
570template <> struct hash<CppUtilities::TimeSpan> {
571 inline size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
572 {
573 return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
574 }
575};
576} // namespace std
577
578#endif // CHRONO_UTILITIES_TIMESPAN_H
Represents an instant in time, typically expressed as a date and time of day.
Definition datetime.h:55
Represents a time interval.
Definition timespan.h:25
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
Definition timespan.h:289
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition timespan.h:273
constexpr bool isNull() const
Returns true if the time interval represented by the current TimeSpan class is null.
Definition timespan.h:538
constexpr bool operator<(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
Definition timespan.h:379
constexpr TimeSpan operator*(double factor) const
Multiplies a TimeSpan by the specified factor.
Definition timespan.h:427
static constexpr TickType nanosecondsPerTick
Definition timespan.h:99
constexpr bool operator!=(const TimeSpan &other) const
Indicates whether two TimeSpan instances are not equal.
Definition timespan.h:371
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
Definition timespan.h:331
TimeSpan & operator*=(double factor)
Multiplies the current instance by the specified factor.
Definition timespan.h:487
constexpr bool isNegativeInfinity() const
Returns whether the time interval represented by the current instance is the smallest representable T...
Definition timespan.h:554
static constexpr TickType ticksPerMillisecond
Definition timespan.h:101
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition timespan.h:339
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition timespan.h:257
constexpr TimeSpan operator/(double factor) const
Divides a TimeSpan by the specified factor.
Definition timespan.h:435
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition timespan.h:114
constexpr bool operator>(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
Definition timespan.h:387
constexpr TickType totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
Definition timespan.h:249
constexpr bool operator==(const TimeSpan &other) const
Indicates whether two TimeSpan instances are equal.
Definition timespan.h:363
static constexpr TickType ticksPerMinute
Definition timespan.h:103
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Definition timespan.h:355
static constexpr TickType ticksPerMicrosecond
Definition timespan.h:100
TickType & ticks()
Returns a mutable reference to the total ticks.
Definition timespan.h:241
std::int64_t TickType
Definition timespan.h:29
constexpr int milliseconds() const
Returns the milliseconds component of the time interval represented by the current TimeSpan class.
Definition timespan.h:323
constexpr TimeSpan operator-(const TimeSpan &other) const
Subtracts one TimeSpan instance from another.
Definition timespan.h:419
constexpr bool operator>=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.
Definition timespan.h:403
TimeSpan & operator+=(const TimeSpan &other)
Adds another TimeSpan to the current instance.
Definition timespan.h:469
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition timespan.h:162
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition timespan.h:154
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
Definition timespan.h:146
static constexpr TimeSpan infinity()
Constructs a new instance of the TimeSpan class with the maximal number of ticks.
Definition timespan.h:233
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition timespan.h:138
constexpr bool isInfinity() const
Returns whether the time interval represented by the current instance is the longest representable Ti...
Definition timespan.h:562
static constexpr TickType ticksPerSecond
Definition timespan.h:102
static constexpr TimeSpan negativeInfinity()
Constructs a new instance of the TimeSpan class with the minimal number of ticks.
Definition timespan.h:225
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
Definition timespan.h:315
constexpr double totalDays() const
Returns the value of the current TimeSpan class expressed in whole and fractional days.
Definition timespan.h:297
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:528
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition timespan.h:217
static constexpr TickType ticksPerDay
Definition timespan.h:105
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition timespan.h:265
static constexpr TickType ticksPerHour
Definition timespan.h:104
constexpr bool isNegative() const
Returns true if the time interval represented by the current TimeSpan class is negative.
Definition timespan.h:546
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition timespan.h:347
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two TimeSpan instances.
Definition timespan.h:411
constexpr double totalMinutes() const
Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
Definition timespan.h:281
constexpr bool operator<=(const TimeSpan &other) const
Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
Definition timespan.h:395
TimeSpan & operator/=(double factor)
Divides the current instance by the specified factor.
Definition timespan.h:496
TimeSpan & operator-=(const TimeSpan &other)
Subtracts another TimeSpan from the current instance.
Definition timespan.h:478
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Definition timespan.h:307
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of milliseconds.
Definition timespan.h:130
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT DateTime operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
Definition period.cpp:60
TimeSpanOutputFormat
Specifies the output format.
Definition timespan.h:19
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition testutils.h:241
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition math.h:100
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition math.h:88
STL namespace.
size_t operator()(const CppUtilities::TimeSpan &timeSpan) const
Definition timespan.h:571