Commit 772e5ce4 authored by Philip Pronin's avatar Philip Pronin Committed by Facebook Github Bot

add PRETTY_TIME_HMS pretty printer

Summary:
`PRETTY_TIME_HMS` extends `PRETTY_TIME` by adding additional minutes
and hours units which are more useful for human-reporting of time intervals.

Reviewed By: yfeldblum

Differential Revision: D7762524

fbshipit-source-id: 881936916db182c163a3df14196f4f11fb649527
parent 5e15f45f
......@@ -283,6 +283,18 @@ const PrettySuffix kPrettyTimeSuffixes[] = {
{ nullptr, 0 },
};
const PrettySuffix kPrettyTimeHmsSuffixes[] = {
{ "h ", 60L * 60L },
{ "m ", 60L },
{ "s ", 1e0L },
{ "ms", 1e-3L },
{ "us", 1e-6L },
{ "ns", 1e-9L },
{ "ps", 1e-12L },
{ "s ", 0 },
{ nullptr, 0 },
};
const PrettySuffix kPrettyBytesMetricSuffixes[] = {
{ "EB", 1e18L },
{ "PB", 1e15L },
......@@ -376,6 +388,7 @@ const PrettySuffix kPrettySISuffixes[] = {
const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = {
kPrettyTimeSuffixes,
kPrettyTimeHmsSuffixes,
kPrettyBytesMetricSuffixes,
kPrettyBytesBinarySuffixes,
kPrettyBytesBinaryIECSuffixes,
......
......@@ -287,7 +287,7 @@ OutputString unhexlify(StringPiece input) {
return output;
}
/*
/**
* A pretty-printer for numbers that appends suffixes of units of the
* given type. It prints 4 sig-figs of value with the most
* appropriate unit.
......@@ -297,6 +297,7 @@ OutputString unhexlify(StringPiece input) {
*
* Current types are:
* PRETTY_TIME - s, ms, us, ns, etc.
* PRETTY_TIME_HMS - h, m, s, ms, us, ns, etc.
* PRETTY_BYTES_METRIC - kB, MB, GB, etc (goes up by 10^3 = 1000 each time)
* PRETTY_BYTES - kB, MB, GB, etc (goes up by 2^10 = 1024 each time)
* PRETTY_BYTES_IEC - KiB, MiB, GiB, etc
......@@ -305,10 +306,12 @@ OutputString unhexlify(StringPiece input) {
* PRETTY_UNITS_BINARY_IEC - Ki, Mi, Gi, etc
* PRETTY_SI - full SI metric prefixes from yocto to Yotta
* http://en.wikipedia.org/wiki/Metric_prefix
*
* @author Mark Rabkin <mrabkin@fb.com>
*/
enum PrettyType {
PRETTY_TIME,
PRETTY_TIME_HMS,
PRETTY_BYTES_METRIC,
PRETTY_BYTES_BINARY,
......@@ -345,7 +348,7 @@ std::string prettyPrint(double val, PrettyType, bool addSpace = true);
double prettyToDouble(folly::StringPiece *const prettyString,
const PrettyType type);
/*
/**
* Same as prettyToDouble(folly::StringPiece*, PrettyType), but
* expects whole string to be correctly parseable. Throws std::range_error
* otherwise
......
......@@ -297,6 +297,7 @@ double pow2(int exponent) {
}
} // namespace
struct PrettyTestCase{
std::string prettyString;
double realValue;
......@@ -305,6 +306,13 @@ struct PrettyTestCase{
PrettyTestCase prettyTestCases[] =
{
{string("853 ms"), 85.3e-2, PRETTY_TIME_HMS},
{string("8.53 s "), 85.3e-1, PRETTY_TIME_HMS},
{string("1.422 m "), 85.3, PRETTY_TIME_HMS},
{string("14.22 m "), 85.3e1, PRETTY_TIME_HMS},
{string("2.369 h "), 85.3e2, PRETTY_TIME_HMS},
{string("2.369e+04 h "), 85.3e6, PRETTY_TIME_HMS},
{string("8.53e+07 s "), 85.3e6, PRETTY_TIME},
{string("8.53e+07 s "), 85.3e6, PRETTY_TIME},
{string("85.3 ms"), 85.3e-3, PRETTY_TIME},
......@@ -417,6 +425,7 @@ PrettyTestCase prettyTestCases[] =
{string("-85.3 ms"), -85.3e-3, PRETTY_TIME},
{string("-85.3 us"), -85.3e-6, PRETTY_TIME},
{string("-85.3 ns"), -85.3e-9, PRETTY_TIME},
// end of test
{string("endoftest"), 0, PRETTY_NUM_TYPES}
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment