3 #include "../conversion/stringconversion.h" 36 TimeSpan TimeSpan::fromString(
const char *str,
char separator)
44 for (
const char *
i = str; *
i; ++
i) {
45 *
i == separator && ++partsSize;
47 parts.reserve(partsSize);
49 for (
const char *
i = str;;) {
50 if (*
i == separator) {
51 parts.emplace_back(stringToNumber<double>(
string(str,
i)));
53 }
else if (*
i ==
'\0') {
54 parts.emplace_back(stringToNumber<double>(
string(str,
i)));
61 switch (parts.size()) {
63 return TimeSpan::fromSeconds(parts.front());
65 return TimeSpan::fromMinutes(parts.front()) + TimeSpan::fromSeconds(parts[1]);
67 return TimeSpan::fromHours(parts.front()) + TimeSpan::fromMinutes(parts[1]) + TimeSpan::fromSeconds(parts[2]);
69 return TimeSpan::fromDays(parts.front()) + TimeSpan::fromHours(parts[1]) + TimeSpan::fromMinutes(parts[2]) + TimeSpan::fromSeconds(parts[3]);
82 toString(result, format, fullSeconds);
96 stringstream s(stringstream::in | stringstream::out);
100 positive.m_ticks = -positive.m_ticks;
103 case TimeSpanOutputFormat::Normal:
104 s << setfill(
'0') << setw(2) << floor(positive.
totalHours()) <<
":" << setw(2) << positive.
minutes() <<
":" << setw(2) << positive.
seconds();
109 if (milli || micro || nano) {
110 s <<
'.' << setw(3) << milli;
112 s << setw(3) << micro;
114 s << nano / TimeSpan::nanosecondsPerTick;
120 case TimeSpanOutputFormat::WithMeasures:
128 bool needWhitespace =
false;
129 if (
const int days = positive.
days()) {
130 needWhitespace =
true;
133 if (
const int hours = positive.
hours()) {
136 needWhitespace =
true;
139 if (
const int minutes = positive.
minutes()) {
142 needWhitespace =
true;
143 s << minutes <<
" min";
145 if (
const int seconds = positive.
seconds()) {
148 needWhitespace =
true;
149 s << seconds <<
" s";
155 needWhitespace =
true;
156 s << milliseconds <<
" ms";
161 needWhitespace =
true;
162 s << microseconds <<
" µs";
164 if (
const int nanoseconds = positive.
nanoseconds()) {
167 s << nanoseconds <<
" ns";
173 case TimeSpanOutputFormat::TotalSeconds:
175 s << setprecision(0);
177 s << setprecision(10);
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
TimeSpanOutputFormat
Specifies the output format.
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
constexpr bool isNegative() const
Returns ture if the time interval represented by the current TimeSpan class is negative.
Contains classes providing a means for handling date and time information.
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Represents a time interval.
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Contains several functions providing conversions between different data types.
constexpr int milliseconds() const
Returns the miliseconds component of the time interval represented by the current TimeSpan class.
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.