mirror of
https://github.com/matrix-construct/construct
synced 2025-01-13 08:23:56 +01:00
ircd::util: Add a format option for pretty_nanoseconds() et al for abbreviated units.
This commit is contained in:
parent
ba7ad88d7a
commit
43a354da49
2 changed files with 24 additions and 20 deletions
|
@ -37,20 +37,21 @@ namespace ircd::util
|
|||
std::string pretty_only(const human_readable_size &);
|
||||
|
||||
// Human readable time suite (for timers and counts; otherwise see date.h)
|
||||
string_view pretty_nanoseconds(const mutable_buffer &out, const long double &);
|
||||
template<class r, class p> string_view pretty(const mutable_buffer &out, const duration<r, p> &);
|
||||
template<class r, class p> std::string pretty(const duration<r, p> &);
|
||||
string_view pretty_nanoseconds(const mutable_buffer &out, const long double &, const uint &fmt = 0);
|
||||
template<class r, class p> string_view pretty(const mutable_buffer &out, const duration<r, p> &, const uint &fmt = 0);
|
||||
template<class r, class p> std::string pretty(const duration<r, p> &, const uint &fmt = 0);
|
||||
}
|
||||
|
||||
template<class rep,
|
||||
class period>
|
||||
std::string
|
||||
ircd::util::pretty(const duration<rep, period> &d)
|
||||
ircd::util::pretty(const duration<rep, period> &d,
|
||||
const uint &fmt)
|
||||
{
|
||||
return util::string(32, [&d]
|
||||
return util::string(32, [&d, &fmt]
|
||||
(const mutable_buffer &out)
|
||||
{
|
||||
return pretty(out, d);
|
||||
return pretty(out, d, fmt);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -58,8 +59,9 @@ template<class rep,
|
|||
class period>
|
||||
ircd::string_view
|
||||
ircd::util::pretty(const mutable_buffer &out,
|
||||
const duration<rep, period> &d)
|
||||
const duration<rep, period> &d,
|
||||
const uint &fmt)
|
||||
{
|
||||
const auto &ns(duration_cast<nanoseconds>(d));
|
||||
return pretty_nanoseconds(out, ns.count());
|
||||
return pretty_nanoseconds(out, ns.count(), fmt);
|
||||
}
|
||||
|
|
26
ircd/util.cc
26
ircd/util.cc
|
@ -63,18 +63,20 @@ ircd::util::getenv(const string_view &key)
|
|||
|
||||
ircd::string_view
|
||||
ircd::util::pretty_nanoseconds(const mutable_buffer &out,
|
||||
const long double &ns)
|
||||
const long double &ns,
|
||||
const uint &fmt)
|
||||
{
|
||||
static const std::array<string_view, 7> unit
|
||||
{
|
||||
"nanoseconds",
|
||||
"microseconds",
|
||||
"milliseconds",
|
||||
"seconds",
|
||||
"minutes",
|
||||
"hours",
|
||||
"days",
|
||||
};
|
||||
static const std::array<std::array<string_view, 2>, 7> unit
|
||||
{{
|
||||
// fmt=0 fmt=1
|
||||
{ "nanoseconds", "ns" },
|
||||
{ "microseconds", "us" },
|
||||
{ "milliseconds", "ms" },
|
||||
{ "seconds", "s" },
|
||||
{ "minutes", "m" },
|
||||
{ "hours", "h" },
|
||||
{ "days", "d" },
|
||||
}};
|
||||
|
||||
auto pos(0);
|
||||
long double val(ns);
|
||||
|
@ -132,7 +134,7 @@ ircd::util::pretty_nanoseconds(const mutable_buffer &out,
|
|||
{
|
||||
out, "%.2lf %s",
|
||||
val,
|
||||
unit.at(pos)
|
||||
unit.at(pos).at(fmt)
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue