File: timespan.cpp

package info (click to toggle)
martchus-cpp-utilities 5.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: cpp: 12,679; awk: 18; ansic: 12; makefile: 10
file content (275 lines) | stat: -rw-r--r-- 10,103 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#define CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS

#include "./timespan.h"

#include "../conversion/stringbuilder.h"

#include <array>
#include <charconv>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <sstream>

using namespace std;

namespace CppUtilities {

/// \cond

#if defined(__GLIBCXX__) && _GLIBCXX_RELEASE < 10
enum class chars_format { scientific = 1, fixed = 2, hex = 4, general = fixed | scientific };
#else
using char_format = std::chars_format;
#endif

inline std::from_chars_result from_chars(const char *first, const char *last, double &value, chars_format fmt = chars_format::general) noexcept
{
#if defined(_LIBCPP_VERSION) || (defined(__GLIBCXX__) && _GLIBCXX_RELEASE < 11)
    // workaround std::from_chars() not being implemented for floating point numbers in libc++ and older libstdc++ versions
    CPP_UTILITIES_UNUSED(fmt)
    auto r = std::from_chars_result{ nullptr, std::errc() };
    auto s = std::string(first, last);
    auto l = s.data() + s.size();
    auto d = std::strtod(s.data(), &l);
    if (errno == ERANGE) {
        r.ec = std::errc::result_out_of_range;
    } else if (s.data() == l) {
        r.ec = std::errc::invalid_argument;
    } else {
        value = d;
        r.ptr = first + (l - s.data());
    }
    return r;
#else
    return std::from_chars(first, last, value, fmt);
#endif
}
/// \endcond

/*!
 * \class TimeSpan
 * \brief Represents a time interval.
 *
 * Note that the TimeSpan class is meant to express a time interval independently of the
 * concrete starting DateTime and end DateTime and hence can not be expressed in years
 * and month. For that use case, use the Period class instead.
 *
 * \remarks Time values are measured in 100-nanosecond units called ticks.
 */

/*!
 * \brief Parses the given C-style string as TimeSpan.
 * \throws Throws a ConversionException if the specified \a str does not match the expected format.
 *
 * The expected format is "days:hours:minutes:seconds", e.g. "5:31:4.521" for 5 hours, 31 minutes
 * and 4.521 seconds. So parts at the front can be omitted and the parts can be fractions. The
 * colon can be changed by specifying another \a separator. White-spaces before and after parts
 * are ignored.
 *
 * It is also possible to specify one or more values with a unit, e.g. "2w 1d 5h 1m 0.5s".
 * The units "w" (weeks), "d" (days), "h" (hours), "m" (minutes) and "s" (seconds) are supported.
 */
TimeSpan TimeSpan::fromString(const char *str, char separator)
{
    if (!*str) {
        return TimeSpan();
    }

    auto parts = std::array<double, 4>();
    auto partsPresent = std::size_t();
    auto specificationsWithUnits = TimeSpan();

    for (const char *i = str;; ++i) {
        // skip over white-spaces
        if (*i == ' ' && i == str) {
            str = i + 1;
            continue;
        }

        // consider non-separator and non-terminator characters as part to be interpreted as number
        if (*i != separator && *i != '\0') {
            continue;
        }

        // allow only up to 4 parts (days, hours, minutes and seconds)
        if (partsPresent == 4) {
            throw ConversionException("too many separators/parts");
        }

        // parse value of the part
        auto valuePart = 0.0;
        auto valueWithUnit = TimeSpan();
        if (str != i) {
            // parse value of the part as double
            const auto res = from_chars(str, i, valuePart);
            if (res.ec != std::errc()) {
                const auto part = std::string_view(str, static_cast<std::string_view::size_type>(i - str));
                if (res.ec == std::errc::result_out_of_range) {
                    throw ConversionException(argsToString("part \"", part, "\" is too large"));
                } else {
                    throw ConversionException(argsToString("part \"", part, "\" cannot be interpreted as floating point number"));
                }
            }
            // handle remaining characters; detect a possibly present unit suffix
            for (const char *suffix = res.ptr; suffix != i; ++suffix) {
                if (*suffix == ' ') {
                    continue;
                }
                if (valueWithUnit.isNull()) {
                    switch (*suffix) {
                    case 'w':
                        valueWithUnit = TimeSpan::fromDays(7.0 * valuePart);
                        continue;
                    case 'd':
                        valueWithUnit = TimeSpan::fromDays(valuePart);
                        continue;
                    case 'h':
                        valueWithUnit = TimeSpan::fromHours(valuePart);
                        continue;
                    case 'm':
                        valueWithUnit = TimeSpan::fromMinutes(valuePart);
                        continue;
                    case 's':
                        valueWithUnit = TimeSpan::fromSeconds(valuePart);
                        continue;
                    default:;
                    }
                }
                if (*suffix >= '0' && *suffix <= '9') {
                    str = i = suffix;
                    break;
                }
                throw ConversionException(argsToString("unexpected character \"", *suffix, '\"'));
            }
        }

        // set part value; add value with unit
        if (valueWithUnit.isNull()) {
            parts[partsPresent++] = valuePart;
        } else {
            specificationsWithUnits += valueWithUnit;
        }

        // expect next part starting after the separator or stop if terminator reached
        if (*i == separator) {
            str = i + 1;
        } else if (*i == '\0') {
            break;
        }
    }

    // compute and return total value from specifications with units and parts
    switch (partsPresent) {
    case 1:
        return specificationsWithUnits + TimeSpan::fromSeconds(parts.front());
    case 2:
        return specificationsWithUnits + TimeSpan::fromMinutes(parts.front()) + TimeSpan::fromSeconds(parts[1]);
    case 3:
        return specificationsWithUnits + TimeSpan::fromHours(parts.front()) + TimeSpan::fromMinutes(parts[1]) + TimeSpan::fromSeconds(parts[2]);
    default:
        return specificationsWithUnits + 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 fullSeconds 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 fullSeconds) 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();
        if (!fullSeconds) {
            const int milli(positive.milliseconds());
            const int micro(positive.microseconds());
            const int nano(positive.nanoseconds());
            if (milli || micro || nano) {
                s << '.' << setw(3) << milli;
                if (micro || nano) {
                    s << setw(3) << micro;
                    if (nano) {
                        s << nano / TimeSpan::nanosecondsPerTick;
                    }
                }
            }
        }
        break;
    case TimeSpanOutputFormat::WithMeasures:
        if (isNull()) {
            result = "0 s";
            return;
        } else {
            if (!fullSeconds && positive.totalMilliseconds() < 1.0) {
                s << setprecision(2) << positive.totalMicroseconds() << " µs";
            } else {
                bool needWhitespace = false;
                if (const int days = positive.days()) {
                    needWhitespace = true;
                    s << days << " d";
                }
                if (const int hours = positive.hours()) {
                    if (needWhitespace)
                        s << ' ';
                    needWhitespace = true;
                    s << hours << " h";
                }
                if (const int minutes = positive.minutes()) {
                    if (needWhitespace)
                        s << ' ';
                    needWhitespace = true;
                    s << minutes << " min";
                }
                if (const int seconds = positive.seconds()) {
                    if (needWhitespace)
                        s << ' ';
                    needWhitespace = true;
                    s << seconds << " s";
                }
                if (!fullSeconds) {
                    if (const int milliseconds = positive.milliseconds()) {
                        if (needWhitespace)
                            s << ' ';
                        needWhitespace = true;
                        s << milliseconds << " ms";
                    }
                    if (const int microseconds = positive.microseconds()) {
                        if (needWhitespace)
                            s << ' ';
                        needWhitespace = true;
                        s << microseconds << " µs";
                    }
                    if (const int nanoseconds = positive.nanoseconds()) {
                        if (needWhitespace)
                            s << ' ';
                        s << nanoseconds << " ns";
                    }
                }
            }
        }
        break;
    case TimeSpanOutputFormat::TotalSeconds:
        if (fullSeconds) {
            s << setprecision(0);
        } else {
            s << setprecision(10);
        }
        s << positive.totalSeconds();
        break;
    }
    result = s.str();
}

} // namespace CppUtilities