1#define CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
5#include "../conversion/stringbuilder.h"
6#include "../conversion/stringconversion.h"
20inline std::from_chars_result from_chars(
21 const char *first,
const char *last,
double &value, std::chars_format fmt = std::chars_format::general)
noexcept
26 auto r = std::from_chars_result{
nullptr, std::errc() };
27 auto s = std::string(first, last);
28 auto l = s.data() + s.size();
29 auto d = std::strtod(s.data(), &l);
30 if (errno == ERANGE) {
31 r.ec = std::errc::result_out_of_range;
32 }
else if (s.data() == l) {
33 r.ec = std::errc::invalid_argument;
36 r.ptr = first + (l - s.data());
40 return std::from_chars(first, last, value, fmt);
74 auto parts = std::array<double, 4>();
75 auto partsPresent = std::size_t();
76 auto specificationsWithUnits =
TimeSpan();
78 for (
const char *
i = str;; ++
i) {
80 if (*
i ==
' ' &&
i == str) {
86 if (*
i != separator && *
i !=
'\0') {
91 if (partsPresent == 4) {
100 const auto res = from_chars(str,
i, valuePart);
101 if (res.ec != std::errc()) {
102 const auto part = std::string_view(str,
static_cast<std::string_view::size_type
>(
i - str));
103 if (res.ec == std::errc::result_out_of_range) {
110 for (
const char *suffix = res.ptr; suffix !=
i; ++suffix) {
111 if (*suffix ==
' ') {
114 if (valueWithUnit.isNull()) {
134 if (*suffix >=
'0' && *suffix <=
'9') {
143 if (valueWithUnit.isNull()) {
144 parts[partsPresent++] = valuePart;
146 specificationsWithUnits += valueWithUnit;
150 if (*
i == separator) {
152 }
else if (*
i ==
'\0') {
158 switch (partsPresent) {
181 stringstream s(stringstream::in | stringstream::out);
185 positive.m_ticks = -positive.m_ticks;
189 s << setfill(
'0') << setw(2) << floor(positive.
totalHours()) <<
":" << setw(2) << positive.
minutes() <<
":" << setw(2) << positive.
seconds();
194 if (milli || micro || nano) {
195 s <<
'.' << setw(3) << milli;
197 s << setw(3) << micro;
213 bool needWhitespace =
false;
214 if (
const int days = positive.
days()) {
215 needWhitespace =
true;
221 needWhitespace =
true;
227 needWhitespace =
true;
233 needWhitespace =
true;
240 needWhitespace =
true;
246 needWhitespace =
true;
260 s << setprecision(0);
262 s << setprecision(10);
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
The ConversionException class is thrown by the various conversion functions of this library when a co...
Represents a time interval.
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
constexpr bool isNull() const
Returns true if the time interval represented by the current TimeSpan class is null.
static constexpr TickType nanosecondsPerTick
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
constexpr int minutes() const
Returns the minutes 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 TimeSpan()
Constructs a new instance of the TimeSpan class with zero ticks.
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
constexpr int milliseconds() const
Returns the milliseconds component of the time interval represented by the current TimeSpan class.
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
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...
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
constexpr bool isNegative() const
Returns true if the time interval represented by the current TimeSpan class is negative.
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Contains all utilities provides by the c++utilities library.
TimeSpanOutputFormat
Specifies the output format.
StringType argsToString(Args &&...args)