13 #ifndef __STOUT_DURATION_HPP__ 14 #define __STOUT_DURATION_HPP__ 46 int64_t
ns()
const {
return nanos; }
49 double secs()
const {
return static_cast<double>(nanos) /
SECONDS; }
50 double mins()
const {
return static_cast<double>(nanos) /
MINUTES; }
51 double hrs()
const {
return static_cast<double>(nanos) /
HOURS; }
52 double days()
const {
return static_cast<double>(nanos) /
DAYS; }
53 double weeks()
const {
return static_cast<double>(nanos) /
WEEKS; }
61 t.tv_sec =
static_cast<decltype(t.tv_sec)
>(
ns() /
SECONDS);
62 t.tv_usec =
static_cast<decltype(t.tv_usec)
>(
89 nanos =
static_cast<int64_t
>(nanos * multiplier);
96 nanos =
static_cast<int64_t
>(nanos / divisor);
114 template <
typename T>
118 product *= multiplier;
122 template <
typename T>
150 : nanos(value * unit) {}
154 constexpr
Duration(
double value, int64_t unit)
155 : nanos(static_cast<int64_t>(value * unit)) {}
160 std::ostream& stream,
161 const Duration& duration);
173 double value()
const {
return static_cast<double>(this->
ns()); }
175 static std::string
units() {
return "ns"; }
189 static std::string
units() {
return "us"; }
203 static std::string
units() {
return "ms"; }
217 static std::string
units() {
return "secs"; }
231 static std::string
units() {
return "mins"; }
238 explicit constexpr
Hours(int64_t hours)
245 static std::string
units() {
return "hrs"; }
259 static std::string
units() {
return "days"; }
272 static std::string
units() {
return "weeks"; }
279 std::streamsize precision =
280 stream.precision(std::numeric_limits<double>::digits10);
291 duration = duration_ * -1;
301 int64_t nanoseconds = duration.
ns();
312 }
else if (duration <
Seconds(1)) {
319 }
else if (duration <
Minutes(1)) {
326 }
else if (duration <
Hours(1)) {
333 }
else if (duration <
Days(1)) {
340 }
else if (duration <
Weeks(1)) {
357 stream.precision(precision);
367 while (index < s.size()) {
368 if (isdigit(s[index]) || s[
index] ==
'.') {
373 Try<double> value = numify<double>(s.substr(0, index));
379 const std::string unit = s.substr(index);
384 }
else if (unit ==
"us") {
386 }
else if (unit ==
"ms") {
388 }
else if (unit ==
"secs") {
390 }
else if (unit ==
"mins") {
392 }
else if (unit ==
"hrs") {
394 }
else if (unit ==
"days") {
396 }
else if (unit ==
"weeks") {
400 "Unknown duration unit '" + unit +
"'; supported units are" 401 " 'ns', 'us', 'ms', 'secs', 'mins', 'hrs', 'days', and 'weeks'");
404 double nanos = value.
get() * factor;
405 if (nanos >
max().nanos || nanos <
min().nanos) {
407 "Argument out of the range that a Duration can represent due" 408 " to int64_t's size limit");
414 return Error(
"Invalid duration '" + s +
"'");
421 return Error(
"Argument out of the range that a Duration can represent due " 422 "to int64_t's size limit");
440 #endif // __STOUT_DURATION_HPP__ double value() const
Definition: duration.hpp:257
double weeks() const
Definition: duration.hpp:53
Duration operator/(T divisor) const
Definition: duration.hpp:123
static std::string units()
Definition: duration.hpp:189
double value() const
Definition: duration.hpp:201
Definition: errorbase.hpp:36
static constexpr int64_t MICROSECONDS
Definition: duration.hpp:140
double value() const
Definition: duration.hpp:173
Duration & operator-=(const Duration &that)
Definition: duration.hpp:80
bool operator!=(const Duration &d) const
Definition: duration.hpp:72
double value() const
Definition: duration.hpp:243
T & get()&
Definition: try.hpp:80
bool operator>=(const Duration &d) const
Definition: duration.hpp:70
static std::string units()
Definition: duration.hpp:231
Duration operator*(T multiplier) const
Definition: duration.hpp:115
Duration operator+(const Duration &that) const
Definition: duration.hpp:100
double value() const
Definition: duration.hpp:229
constexpr Hours(const Duration &d)
Definition: duration.hpp:241
bool operator>(const Duration &d) const
Definition: duration.hpp:69
constexpr Duration(int64_t value, int64_t unit)
Definition: duration.hpp:149
static constexpr int64_t HOURS
Definition: duration.hpp:144
static constexpr int64_t MINUTES
Definition: duration.hpp:143
static constexpr int64_t SECONDS
Definition: duration.hpp:142
constexpr Microseconds(const Duration &d)
Definition: duration.hpp:185
constexpr Days(int64_t days)
Definition: duration.hpp:252
static Try< Duration > create(double seconds)
Definition: duration.hpp:418
double ms() const
Definition: duration.hpp:48
constexpr Weeks(int64_t value)
Definition: duration.hpp:266
bool operator==(const Duration &d) const
Definition: duration.hpp:71
Definition: duration.hpp:32
Duration(const timeval &t)
Definition: duration.hpp:41
constexpr Milliseconds(const Duration &d)
Definition: duration.hpp:199
double mins() const
Definition: duration.hpp:50
Duration operator-(const Duration &that) const
Definition: duration.hpp:107
static constexpr int64_t DAYS
Definition: duration.hpp:145
static std::string units()
Definition: duration.hpp:245
Result< int > index(const std::string &link)
double days() const
Definition: duration.hpp:52
bool operator<=(const Duration &d) const
Definition: duration.hpp:68
static std::string units()
Definition: duration.hpp:259
Definition: duration.hpp:193
Option< std::string > diff(const FrameworkInfo &left, const FrameworkInfo &right)
Duration & operator*=(T multiplier)
Definition: duration.hpp:87
constexpr Minutes(int64_t minutes)
Definition: duration.hpp:224
Definition: duration.hpp:221
Option< T > max(const Option< T > &left, const Option< T > &right)
Definition: option.hpp:214
Definition: duration.hpp:207
static constexpr int64_t MILLISECONDS
Definition: duration.hpp:141
constexpr Days(const Duration &d)
Definition: duration.hpp:255
constexpr Minutes(const Duration &d)
Definition: duration.hpp:227
int64_t ns() const
Definition: duration.hpp:46
Option< T > min(const Option< T > &left, const Option< T > &right)
Definition: option.hpp:185
constexpr Weeks(const Duration &d)
Definition: duration.hpp:268
double value() const
Definition: duration.hpp:215
Duration & operator+=(const Duration &that)
Definition: duration.hpp:74
Definition: duration.hpp:249
double secs() const
Definition: duration.hpp:49
bool operator<(const Duration &d) const
Definition: duration.hpp:67
static Try error(const E &e)
Definition: try.hpp:43
static constexpr int64_t NANOSECONDS
Definition: duration.hpp:139
static Try< Duration > parse(const std::string &s)
Definition: duration.hpp:363
Definition: duration.hpp:235
Duration & operator/=(T divisor)
Definition: duration.hpp:94
double hrs() const
Definition: duration.hpp:51
constexpr Seconds(const Duration &d)
Definition: duration.hpp:213
Definition: duration.hpp:179
bool isError() const
Definition: try.hpp:78
static std::string units()
Definition: duration.hpp:217
static constexpr Duration zero()
Definition: duration.hpp:136
double value() const
Definition: duration.hpp:187
constexpr Hours(int64_t hours)
Definition: duration.hpp:238
double us() const
Definition: duration.hpp:47
struct timeval timeval() const
Definition: duration.hpp:55
friend std::ostream & operator<<(std::ostream &stream, const Duration &duration)
Definition: duration.hpp:276
constexpr Nanoseconds(const Duration &d)
Definition: duration.hpp:171
static std::string units()
Definition: duration.hpp:272
Definition: duration.hpp:165
double value() const
Definition: duration.hpp:270
static constexpr int64_t WEEKS
Definition: duration.hpp:146
constexpr Duration()
Definition: duration.hpp:39
constexpr Milliseconds(int64_t milliseconds)
Definition: duration.hpp:196
Definition: duration.hpp:263
static constexpr Duration min()
Definition: duration.hpp:435
static std::string units()
Definition: duration.hpp:175
static constexpr Duration max()
Definition: duration.hpp:429
static std::string units()
Definition: duration.hpp:203
constexpr Seconds(int64_t seconds)
Definition: duration.hpp:210
constexpr Microseconds(int64_t microseconds)
Definition: duration.hpp:182
constexpr Nanoseconds(int64_t nanoseconds)
Definition: duration.hpp:168