Debian Codegraph @

chrono/timespan.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/chrono/timespan.cpp`/define CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADScxx . . $ `chrono/timespan.cpp:1:9`!
2
3 #include "./timespan.h"cxx . . $ `<file>/chrono/timespan.h`/
4
5 #include "../conversion/stringbuilder.h"cxx . . $ `<file>/conversion/stringbuilder.h`/
6
7 #include <array>
8 #include <charconv>
9 #include <cmath>
10 #include <cstdlib>
11 #include <iomanip>
12 #include <sstream>
13
14 using namespace std;
15
16 namespace CppUtilitiescxx . . $ CppUtilities/ {
17
18 /// \cond
19
20 #if defined(__GLIBCXX__cxx . . $ `<external>/14996551269519278535/c++config.h:49:9`!) && _GLIBCXX_RELEASEcxx . . $ `<external>/14996551269519278535/c++config.h:45:9`! < 10
21 enum class chars_format { scientific = 1, fixed = 2, hex = 4, general = fixed | scientific };
22 #else
23 using char_formatcxx . . $ CppUtilities/char_format# = std::chars_formatcxx . . $ std/chars_format#;
24 #endif
25
26 inline std::from_chars_resultcxx . . $ std/from_chars_result# from_charscxx . . $ CppUtilities/from_chars(6b9256b4c80ad7bc).(const char *first, const char *last, double &value, chars_formatcxx . . $ std/chars_format# fmt = chars_formatcxx . . $ std/chars_format#::generalcxx . . $ std/chars_format#general.) noexcept
27 {
28 #if defined(_LIBCPP_VERSION) || (defined(__GLIBCXX__cxx . . $ `<external>/14996551269519278535/c++config.h:49:9`!) && _GLIBCXX_RELEASEcxx . . $ `<external>/14996551269519278535/c++config.h:45:9`! < 11)
29 // workaround std::from_chars() not being implemented for floating point numbers in libc++ and older libstdc++ versions
30 CPP_UTILITIES_UNUSED(fmt)
31 auto r = std::from_chars_result{ nullptr, std::errc() };
32 auto s = std::string(first, last);
33 auto l = s.data() + s.size();
34 auto d = std::strtod(s.data(), &l);
35 if (errno == ERANGE) {
36 r.ec = std::errc::result_out_of_range;
37 } else if (s.data() == l) {
38 r.ec = std::errc::invalid_argument;
39 } else {
40 value = d;
41 r.ptr = first + (l - s.data());
42 }
43 return r;
44 #else
45 return std::from_charscxx . . $ std/from_chars(6b9256b4c80ad7bc).(first, last, value, fmt);
46 #endif
47 }
48 /// \endcond
49
50 /*!
51 * \class TimeSpan
52 * \brief Represents a time interval.
53 *
54 * Note that the TimeSpan class is meant to express a time interval independently of the
55 * concrete starting DateTime and end DateTime and hence can not be expressed in years
56 * and month. For that use case, use the Period class instead.
57 *
58 * \remarks Time values are measured in 100-nanosecond units called ticks.
59 */
60
61 /*!
62 * \brief Parses the given C-style string as TimeSpan.
63 * \throws Throws a ConversionException if the specified \a str does not match the expected format.
64 *
65 * The expected format is "days:hours:minutes:seconds", e.g. "5:31:4.521" for 5 hours, 31 minutes
66 * and 4.521 seconds. So parts at the front can be omitted and the parts can be fractions. The
67 * colon can be changed by specifying another \a separator. White-spaces before and after parts
68 * are ignored.
69 *
70 * It is also possible to specify one or more values with a unit, e.g. "2w 1d 5h 1m 0.5s".
71 * The units "w" (weeks), "d" (days), "h" (hours), "m" (minutes) and "s" (seconds) are supported.
72 */
73 TimeSpancxx . . $ CppUtilities/TimeSpan# TimeSpancxx . . $ CppUtilities/TimeSpan#::fromStringcxx . . $ CppUtilities/TimeSpan#fromString(9eca79b0e5ad183a).(const char *str, char separator)
74 {
75 if (!*str) {
76 return TimeSpancxx . . $ CppUtilities/TimeSpan#TimeSpan(49f6e7a06ebc5aa8).();
77 }
78
79 auto parts = std::arraycxx . . $ std/array#<double, 4>();
80 auto partsPresent = std::size_tcxx . . $ std/size_t#();
81 auto specificationsWithUnits = TimeSpancxx . . $ CppUtilities/TimeSpan#TimeSpan(49f6e7a06ebc5aa8).();
82
83 for (const char *i = str;; ++i) {
84 // skip over white-spaces
85 if (*i == ' ' && i == str) {
86 str = i + 1;
87 continue;
88 }
89
90 // consider non-separator and non-terminator characters as part to be interpreted as number
91 if (*i != separator && *i != '\0') {
92 continue;
93 }
94
95 // allow only up to 4 parts (days, hours, minutes and seconds)
96 if (partsPresent == 4) {
97 throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#("too many separators/parts");
98 }
99
100 // parse value of the part
101 auto valuePart = 0.0;
102 auto valueWithUnit = TimeSpancxx . . $ CppUtilities/TimeSpan#TimeSpan(49f6e7a06ebc5aa8).();
103 if (str != i) {
104 // parse value of the part as double
105 const auto res = from_charscxx . . $ CppUtilities/from_chars(6b9256b4c80ad7bc).(str, i, valuePart);
106 if (res.eccxx . . $ std/from_chars_result#ec. != std::errccxx . . $ std/errc#()) {
107 const auto part = stdcxx . . $ std/basic_string_view#basic_string_view(f8b699e64a75d442).::string_viewcxx . . $ std/string_view#(str, static_cast<std::string_viewcxx . . $ std/string_view#::size_typecxx . . $ std/basic_string_view#size_type#>(i - str));
108 if (res.eccxx . . $ std/from_chars_result#ec. == std::errccxx . . $ std/errc#::result_out_of_rangecxx . . $ std/errc#result_out_of_range.) {
109 throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#(argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).("part \"", part, "\" is too large"));
110 } else {
111 throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#(argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).("part \"", part, "\" cannot be interpreted as floating point number"));
112 }
113 }
114 // handle remaining characters; detect a possibly present unit suffix
115 for (const char *suffix = res.ptrcxx . . $ std/from_chars_result#ptr.; suffix != i; ++suffix) {
116 if (*suffix == ' ') {
117 continue;
118 }
119 if (valueWithUnit.isNullcxx . . $ CppUtilities/TimeSpan#isNull(50ce9a9e25b4a850).()) {
120 switch (*suffix) {
121 case 'w':
122 valueWithUnit =cxx . . $ CppUtilities/TimeSpan#`operator=`(12696833f50715fa). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromDayscxx . . $ CppUtilities/TimeSpan#fromDays(1a0c28e1b314a5ba).(7.0 * valuePart);
123 continue;
124 case 'd':
125 valueWithUnit =cxx . . $ CppUtilities/TimeSpan#`operator=`(12696833f50715fa). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromDayscxx . . $ CppUtilities/TimeSpan#fromDays(1a0c28e1b314a5ba).(valuePart);
126 continue;
127 case 'h':
128 valueWithUnit =cxx . . $ CppUtilities/TimeSpan#`operator=`(12696833f50715fa). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromHourscxx . . $ CppUtilities/TimeSpan#fromHours(1a0c28e1b314a5ba).(valuePart);
129 continue;
130 case 'm':
131 valueWithUnit =cxx . . $ CppUtilities/TimeSpan#`operator=`(12696833f50715fa). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromMinutescxx . . $ CppUtilities/TimeSpan#fromMinutes(1a0c28e1b314a5ba).(valuePart);
132 continue;
133 case 's':
134 valueWithUnit =cxx . . $ CppUtilities/TimeSpan#`operator=`(12696833f50715fa). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromSecondscxx . . $ CppUtilities/TimeSpan#fromSeconds(1a0c28e1b314a5ba).(valuePart);
135 continue;
136 default:;
137 }
138 }
139 if (*suffix >= '0' && *suffix <= '9') {
140 str = i = suffix;
141 break;
142 }
143 throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#(argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).("unexpected character \"", *suffix, '\"'));
144 }
145 }
146
147 // set part value; add value with unit
148 if (valueWithUnit.isNullcxx . . $ CppUtilities/TimeSpan#isNull(50ce9a9e25b4a850).()) {
149 parts[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).partsPresent++] = valuePart;
150 } else {
151 specificationsWithUnits +=cxx . . $ CppUtilities/TimeSpan#`operator+=`(ef23eeb8e62ed3de). valueWithUnit;
152 }
153
154 // expect next part starting after the separator or stop if terminator reached
155 if (*i == separator) {
156 str = i + 1;
157 } else if (*i == '\0') {
158 break;
159 }
160 }
161
162 // compute and return total value from specifications with units and parts
163 switch (partsPresent) {
164 case 1:
165 return specificationsWithUnits +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromSecondscxx . . $ CppUtilities/TimeSpan#fromSeconds(1a0c28e1b314a5ba).(parts.frontcxx . . $ std/array#front(80ef4fb0f39170b5).());
166 case 2:
167 return specificationsWithUnits +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromMinutescxx . . $ CppUtilities/TimeSpan#fromMinutes(1a0c28e1b314a5ba).(parts.frontcxx . . $ std/array#front(80ef4fb0f39170b5).()) +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromSecondscxx . . $ CppUtilities/TimeSpan#fromSeconds(1a0c28e1b314a5ba).(parts[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1]);
168 case 3:
169 return specificationsWithUnits +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromHourscxx . . $ CppUtilities/TimeSpan#fromHours(1a0c28e1b314a5ba).(parts.frontcxx . . $ std/array#front(80ef4fb0f39170b5).()) +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromMinutescxx . . $ CppUtilities/TimeSpan#fromMinutes(1a0c28e1b314a5ba).(parts[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1]) +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromSecondscxx . . $ CppUtilities/TimeSpan#fromSeconds(1a0c28e1b314a5ba).(parts[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).2]);
170 default:
171 return specificationsWithUnits +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromDayscxx . . $ CppUtilities/TimeSpan#fromDays(1a0c28e1b314a5ba).(parts.frontcxx . . $ std/array#front(80ef4fb0f39170b5).()) +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromHourscxx . . $ CppUtilities/TimeSpan#fromHours(1a0c28e1b314a5ba).(parts[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1]) +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromMinutescxx . . $ CppUtilities/TimeSpan#fromMinutes(1a0c28e1b314a5ba).(parts[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).2])
172 +cxx . . $ CppUtilities/TimeSpan#operator+(e5ca4f88e5212dc0). TimeSpancxx . . $ CppUtilities/TimeSpan#::fromSecondscxx . . $ CppUtilities/TimeSpan#fromSeconds(1a0c28e1b314a5ba).(parts[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).3]);
173 }
174 }
175
176 /*!
177 * \brief Converts the value of the current TimeSpan object to its equivalent std::string representation
178 * according the given \a format.
179 *
180 * If \a fullSeconds is true the time interval will be rounded to full seconds.
181 *
182 * The string representation will be stored in \a result.
183 */
184 void TimeSpancxx . . $ CppUtilities/TimeSpan#::toStringcxx . . $ CppUtilities/TimeSpan#toString(72d541dfcf10d9ed).(stringcxx . . $ std/string# &result, TimeSpanOutputFormatcxx . . $ CppUtilities/TimeSpanOutputFormat# format, bool fullSeconds) const
185 {
186 stringstreamcxx . . $ std/stringstream# scxx . . $ std/__cxx11/basic_stringstream#basic_stringstream(322f8929844464d3).(stringstreamcxx . . $ std/stringstream#::incxx . . $ std/ios_base#in. |cxx . . $ std/`operator|`(2dd9eb40323da8bf). stringstreamcxx . . $ std/stringstream#::outcxx . . $ std/ios_base#out.);
187 TimeSpancxx . . $ CppUtilities/TimeSpan# positivecxx . . $ CppUtilities/TimeSpan#TimeSpan(1624ee8275b10bda).(m_tickscxx . . $ CppUtilities/TimeSpan#m_ticks.);
188 if (positive.isNegativecxx . . $ CppUtilities/TimeSpan#isNegative(50ce9a9e25b4a850).()) {
189 s <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '-';
190 positive.m_tickscxx . . $ CppUtilities/TimeSpan#m_ticks. = -positive.m_tickscxx . . $ CppUtilities/TimeSpan#m_ticks.;
191 }
192 switch (format) {
193 case TimeSpanOutputFormatcxx . . $ CppUtilities/TimeSpanOutputFormat#::Normalcxx . . $ CppUtilities/TimeSpanOutputFormat#Normal.:
194 s <<cxx . . $ std/`operator<<`(47f1cf32aeb3f1a2). setfillcxx . . $ std/setfill(ade351e831a1374a).('0') <<cxx . . $ std/`operator<<`(fe9a269018418773). setwcxx . . $ std/setw(f06ce2e9220601fb).(2) <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). floor(positive.totalHourscxx . . $ CppUtilities/TimeSpan#totalHours(21b6c25e127938e2).()) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ":" <<cxx . . $ std/`operator<<`(fe9a269018418773). setwcxx . . $ std/setw(f06ce2e9220601fb).(2) <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). positive.minutescxx . . $ CppUtilities/TimeSpan#minutes(455f465bc33b4cdf).() <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ":" <<cxx . . $ std/`operator<<`(fe9a269018418773). setwcxx . . $ std/setw(f06ce2e9220601fb).(2) <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). positive.secondscxx . . $ CppUtilities/TimeSpan#seconds(455f465bc33b4cdf).();
195 if (!fullSeconds) {
196 const int milli(positive.millisecondscxx . . $ CppUtilities/TimeSpan#milliseconds(455f465bc33b4cdf).());
197 const int micro(positive.microsecondscxx . . $ CppUtilities/TimeSpan#microseconds(455f465bc33b4cdf).());
198 const int nano(positive.nanosecondscxx . . $ CppUtilities/TimeSpan#nanoseconds(455f465bc33b4cdf).());
199 if (milli || micro || nano) {
200 s <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '.' <<cxx . . $ std/`operator<<`(fe9a269018418773). setwcxx . . $ std/setw(f06ce2e9220601fb).(3) <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). milli;
201 if (micro || nano) {
202 s <<cxx . . $ std/`operator<<`(fe9a269018418773). setwcxx . . $ std/setw(f06ce2e9220601fb).(3) <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). micro;
203 if (nano) {
204 s <<cxx . . $ std/basic_ostream#`operator<<`(761e6a6e6922c86). nano / TimeSpancxx . . $ CppUtilities/TimeSpan#::nanosecondsPerTickcxx . . $ CppUtilities/TimeSpan#nanosecondsPerTick.;
205 }
206 }
207 }
208 }
209 break;
210 case TimeSpanOutputFormatcxx . . $ CppUtilities/TimeSpanOutputFormat#::WithMeasurescxx . . $ CppUtilities/TimeSpanOutputFormat#WithMeasures.:
211 if (isNullcxx . . $ CppUtilities/TimeSpan#isNull(50ce9a9e25b4a850).()) {
212 result =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). "0 s";
213 return;
214 } else {
215 if (!fullSeconds && positive.totalMillisecondscxx . . $ CppUtilities/TimeSpan#totalMilliseconds(21b6c25e127938e2).() < 1.0) {
216 s <<cxx . . $ std/`operator<<`(f54f2ca72cfad5a6). setprecisioncxx . . $ std/setprecision(94726062283fda89).(2) <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). positive.totalMicrosecondscxx . . $ CppUtilities/TimeSpan#totalMicroseconds(21b6c25e127938e2).() <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " µs";
217 } else {
218 bool needWhitespace = false;
219 if (const int days = positive.dayscxx . . $ CppUtilities/TimeSpan#days(455f465bc33b4cdf).()) {
220 needWhitespace = true;
221 s <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). days <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " d";
222 }
223 if (const int hours = positive.hourscxx . . $ CppUtilities/TimeSpan#hours(455f465bc33b4cdf).()) {
224 if (needWhitespace)
225 s <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ';
226 needWhitespace = true;
227 s <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). hours <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " h";
228 }
229 if (const int minutes = positive.minutescxx . . $ CppUtilities/TimeSpan#minutes(455f465bc33b4cdf).()) {
230 if (needWhitespace)
231 s <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ';
232 needWhitespace = true;
233 s <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). minutes <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " min";
234 }
235 if (const int seconds = positive.secondscxx . . $ CppUtilities/TimeSpan#seconds(455f465bc33b4cdf).()) {
236 if (needWhitespace)
237 s <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ';
238 needWhitespace = true;
239 s <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). seconds <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " s";
240 }
241 if (!fullSeconds) {
242 if (const int milliseconds = positive.millisecondscxx . . $ CppUtilities/TimeSpan#milliseconds(455f465bc33b4cdf).()) {
243 if (needWhitespace)
244 s <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ';
245 needWhitespace = true;
246 s <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). milliseconds <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " ms";
247 }
248 if (const int microseconds = positive.microsecondscxx . . $ CppUtilities/TimeSpan#microseconds(455f465bc33b4cdf).()) {
249 if (needWhitespace)
250 s <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ';
251 needWhitespace = true;
252 s <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). microseconds <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " µs";
253 }
254 if (const int nanoseconds = positive.nanosecondscxx . . $ CppUtilities/TimeSpan#nanoseconds(455f465bc33b4cdf).()) {
255 if (needWhitespace)
256 s <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ';
257 s <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). nanoseconds <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " ns";
258 }
259 }
260 }
261 }
262 break;
263 case TimeSpanOutputFormatcxx . . $ CppUtilities/TimeSpanOutputFormat#::TotalSecondscxx . . $ CppUtilities/TimeSpanOutputFormat#TotalSeconds.:
264 if (fullSeconds) {
265 s <<cxx . . $ std/`operator<<`(f54f2ca72cfad5a6). setprecisioncxx . . $ std/setprecision(94726062283fda89).(0);
266 } else {
267 s <<cxx . . $ std/`operator<<`(f54f2ca72cfad5a6). setprecisioncxx . . $ std/setprecision(94726062283fda89).(10);
268 }
269 s <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). positive.totalSecondscxx . . $ CppUtilities/TimeSpan#totalSeconds(21b6c25e127938e2).();
270 break;
271 }
272 result =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). s.strcxx . . $ std/__cxx11/basic_stringstream#str(d33e1a6fd36255f7).();
273 }
274
275 } // namespace CppUtilities