C++ Utilities 5.11.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
timespan.cpp
Go to the documentation of this file.
1#include "./timespan.h"
2
3#include "../conversion/stringconversion.h"
4
5#include <cmath>
6#include <iomanip>
7#include <sstream>
8#include <vector>
9
10using namespace std;
11
12namespace CppUtilities {
13
36TimeSpan TimeSpan::fromString(const char *str, char separator)
37{
38 if (!*str) {
39 return TimeSpan();
40 }
41
42 vector<double> parts;
43 size_t partsSize = 1;
44 for (const char *i = str; *i; ++i) {
45 *i == separator && ++partsSize;
46 }
47 parts.reserve(partsSize);
48
49 for (const char *i = str;;) {
50 if (*i == separator) {
51 parts.emplace_back(stringToNumber<double>(string(str, i)));
52 str = ++i;
53 } else if (*i == '\0') {
54 parts.emplace_back(stringToNumber<double>(string(str, i)));
55 break;
56 } else {
57 ++i;
58 }
59 }
60
61 switch (parts.size()) {
62 case 1:
63 return TimeSpan::fromSeconds(parts.front());
64 case 2:
65 return TimeSpan::fromMinutes(parts.front()) + TimeSpan::fromSeconds(parts[1]);
66 case 3:
67 return TimeSpan::fromHours(parts.front()) + TimeSpan::fromMinutes(parts[1]) + TimeSpan::fromSeconds(parts[2]);
68 default:
69 return TimeSpan::fromDays(parts.front()) + TimeSpan::fromHours(parts[1]) + TimeSpan::fromMinutes(parts[2]) + TimeSpan::fromSeconds(parts[3]);
70 }
71}
72
81void TimeSpan::toString(string &result, TimeSpanOutputFormat format, bool fullSeconds) const
82{
83 stringstream s(stringstream::in | stringstream::out);
84 TimeSpan positive(m_ticks);
85 if (positive.isNegative()) {
86 s << '-';
87 positive.m_ticks = -positive.m_ticks;
88 }
89 switch (format) {
91 s << setfill('0') << setw(2) << floor(positive.totalHours()) << ":" << setw(2) << positive.minutes() << ":" << setw(2) << positive.seconds();
92 if (!fullSeconds) {
93 const int milli(positive.milliseconds());
94 const int micro(positive.microseconds());
95 const int nano(positive.nanoseconds());
96 if (milli || micro || nano) {
97 s << '.' << setw(3) << milli;
98 if (micro || nano) {
99 s << setw(3) << micro;
100 if (nano) {
102 }
103 }
104 }
105 }
106 break;
108 if (isNull()) {
109 result = "0 s";
110 return;
111 } else {
112 if (!fullSeconds && positive.totalMilliseconds() < 1.0) {
113 s << setprecision(2) << positive.totalMicroseconds() << " µs";
114 } else {
115 bool needWhitespace = false;
116 if (const int days = positive.days()) {
117 needWhitespace = true;
118 s << days << " d";
119 }
120 if (const int hours = positive.hours()) {
121 if (needWhitespace)
122 s << ' ';
123 needWhitespace = true;
124 s << hours << " h";
125 }
126 if (const int minutes = positive.minutes()) {
127 if (needWhitespace)
128 s << ' ';
129 needWhitespace = true;
130 s << minutes << " min";
131 }
132 if (const int seconds = positive.seconds()) {
133 if (needWhitespace)
134 s << ' ';
135 needWhitespace = true;
136 s << seconds << " s";
137 }
138 if (!fullSeconds) {
139 if (const int milliseconds = positive.milliseconds()) {
140 if (needWhitespace)
141 s << ' ';
142 needWhitespace = true;
143 s << milliseconds << " ms";
144 }
145 if (const int microseconds = positive.microseconds()) {
146 if (needWhitespace)
147 s << ' ';
148 needWhitespace = true;
149 s << microseconds << " µs";
150 }
151 if (const int nanoseconds = positive.nanoseconds()) {
152 if (needWhitespace)
153 s << ' ';
154 s << nanoseconds << " ns";
155 }
156 }
157 }
158 }
159 break;
161 if (fullSeconds) {
162 s << setprecision(0);
163 } else {
164 s << setprecision(10);
165 }
166 s << positive.totalSeconds();
167 break;
168 }
169 result = s.str();
170}
171
172} // namespace CppUtilities
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:230
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:214
constexpr bool isNull() const
Returns true if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:441
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:272
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:280
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition: timespan.h:198
constexpr TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
Definition: timespan.h:97
static constexpr std::int64_t nanosecondsPerTick
Definition: timespan.h:82
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:296
constexpr int milliseconds() const
Returns the milliseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:264
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
Definition: timespan.h:145
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
Definition: timespan.h:137
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
Definition: timespan.h:129
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
Definition: timespan.h:121
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:256
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:431
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
Definition: timespan.h:158
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:206
constexpr bool isNegative() const
Returns true if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:449
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:288
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:248
Contains all utilities provides by the c++utilities library.
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:19
STL namespace.
constexpr int i