3 #include "../conversion/stringconversion.h" 23 TimeSpan TimeSpan::fromString(
const char *str,
char separator)
27 for (
const char *i = str; *i; ++i) {
28 *i == separator && ++partsSize;
30 parts.reserve(partsSize);
32 for (
const char *i = str;;) {
33 if (*i == separator) {
34 parts.emplace_back(stringToNumber<double>(
string(str, i)));
36 }
else if (*i ==
'\0') {
37 parts.emplace_back(stringToNumber<double>(
string(str, i)));
44 switch (parts.size()) {
48 return TimeSpan::fromSeconds(parts.front());
50 return TimeSpan::fromMinutes(parts.front()) + TimeSpan::fromSeconds(parts[1]);
52 return TimeSpan::fromHours(parts.front()) + TimeSpan::fromMinutes(parts[1]) + TimeSpan::fromSeconds(parts[2]);
54 return TimeSpan::fromDays(parts.front()) + TimeSpan::fromHours(parts[1]) + TimeSpan::fromMinutes(parts[2]) + TimeSpan::fromSeconds(parts[3]);
67 toString(result, format, noMilliseconds);
81 stringstream s(stringstream::in | stringstream::out);
85 case TimeSpanOutputFormat::Normal:
86 s << setfill(
'0') << setw(2) << floor(fabs(totalHours())) <<
":" << setw(2) << minutes() <<
":" << setw(2) << seconds() <<
" ";
88 case TimeSpanOutputFormat::WithMeasures:
91 }
else if (totalMilliseconds() < 1.0) {
92 s << setprecision(2) << (m_ticks / 10.0) <<
" µs ";
98 s << hours() <<
" h ";
101 s << minutes() <<
" min ";
104 s << seconds() <<
" s ";
106 if (!noMilliseconds && milliseconds()) {
107 s << milliseconds() <<
" ms ";
112 result = s.str().substr(0, static_cast<string::size_type>(s.tellp()) - 1);
TimeSpanOutputFormat
Specifies the output format.
Contains classes providing a means for handling date and time information.
std::string toString(TimeSpanOutputFormat format=TimeSpanOutputFormat::Normal, bool noMilliseconds=false) const
Converts the value of the current TimeSpan object to its equivalent std::string representation accord...
Represents a time interval.
Contains several functions providing conversions between different data types.