#include "./timespan.h" #include "../conversion/stringconversion.h" #include #include #include #include using namespace std; using namespace ChronoUtilities; using namespace ConversionUtilities; /*! * \class ChronoUtilities::TimeSpan * \brief Represents a time interval. * \remarks Time values are measured in 100-nanosecond units called ticks. */ /*! * \brief Parses the given C-style string as TimeSpan. */ TimeSpan TimeSpan::fromString(const char *str, char separator) { if (!*str) { return TimeSpan(); } vector parts; size_t partsSize = 1; for (const char *i = str; *i; ++i) { *i == separator && ++partsSize; } parts.reserve(partsSize); for (const char *i = str;;) { if (*i == separator) { parts.emplace_back(stringToNumber(string(str, i))); str = ++i; } else if (*i == '\0') { parts.emplace_back(stringToNumber(string(str, i))); break; } else { ++i; } } switch (parts.size()) { case 1: return TimeSpan::fromSeconds(parts.front()); case 2: return TimeSpan::fromMinutes(parts.front()) + TimeSpan::fromSeconds(parts[1]); case 3: return TimeSpan::fromHours(parts.front()) + TimeSpan::fromMinutes(parts[1]) + TimeSpan::fromSeconds(parts[2]); default: return TimeSpan::fromDays(parts.front()) + TimeSpan::fromHours(parts[1]) + TimeSpan::fromMinutes(parts[2]) + TimeSpan::fromSeconds(parts[3]); } } /*! * \brief Converts the value of the current TimeSpan object to its equivalent std::string representation * according the given \a format. * * If \a noMilliseconds is true the time interval will be rounded to full seconds. */ string TimeSpan::toString(TimeSpanOutputFormat format, bool noMilliseconds) const { string result; toString(result, format, noMilliseconds); return result; } /*! * \brief Converts the value of the current TimeSpan object to its equivalent std::string representation * according the given \a format. * * If \a noMilliseconds is true the time interval will be rounded to full seconds. * * The string representation will be stored in \a result. */ void TimeSpan::toString(string &result, TimeSpanOutputFormat format, bool noMilliseconds) const { stringstream s(stringstream::in | stringstream::out); TimeSpan positive(m_ticks); if (positive.isNegative()) { s << '-'; positive.m_ticks = -positive.m_ticks; } switch (format) { case TimeSpanOutputFormat::Normal: s << setfill('0') << setw(2) << floor(positive.totalHours()) << ":" << setw(2) << positive.minutes() << ":" << setw(2) << positive.seconds() << " "; break; case TimeSpanOutputFormat::WithMeasures: if (isNull()) { s << "0 s "; } else { if (positive.totalMilliseconds() < 1.0) { s << setprecision(2) << (m_ticks / 10.0) << " µs "; } else { if (const int days = positive.days()) { s << days << " d "; } if (const int hours = positive.hours()) { s << hours << " h "; } if (const int minutes = positive.minutes()) { s << minutes << " min "; } if (const int seconds = positive.seconds()) { s << seconds << " s "; } if (!noMilliseconds) { if (const int milliseconds = positive.milliseconds()) { s << milliseconds << " ms "; } } } } break; } result = s.str().substr(0, static_cast(s.tellp()) - 1); }