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