Skip to content

Commit

Permalink
Fix formatting of extreme durations (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed May 11, 2019
1 parent ea2976e commit de5da50
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
17 changes: 6 additions & 11 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,8 @@ template <
typename std::enable_if<std::is_floating_point<Rep>::value, int>::type = 0>
inline std::chrono::duration<Rep, std::milli> get_milliseconds(
std::chrono::duration<Rep, Period> d) {
auto ms =
std::chrono::duration_cast<std::chrono::duration<Rep, std::milli>>(d);
return std::chrono::duration<Rep, std::milli>(mod(ms.count(), 1000));
return std::chrono::duration<Rep, std::milli>(
mod(d.count() * Period::num / Period::den * 1000, 1000));
}

template <typename Rep, typename OutputIt>
Expand All @@ -431,12 +430,9 @@ OutputIt static format_chrono_duration_value(OutputIt out, Rep val,

template <typename Period, typename OutputIt>
static OutputIt format_chrono_duration_unit(OutputIt out) {
if (const char* unit = get_units<Period>())
return format_to(out, "{}", unit);
else if (Period::den == 1)
return format_to(out, "[{}]s", Period::num);
else
return format_to(out, "[{}/{}]s", Period::num, Period::den);
if (const char* unit = get_units<Period>()) return format_to(out, "{}", unit);
if (Period::den == 1) return format_to(out, "[{}]s", Period::num);
return format_to(out, "[{}/{}]s", Period::num, Period::den);
}

template <typename FormatContext, typename OutputIt, typename Rep,
Expand All @@ -449,7 +445,6 @@ struct chrono_formatter {
typedef std::chrono::duration<Rep> seconds;
seconds s;
typedef std::chrono::duration<Rep, std::milli> milliseconds;
milliseconds ms;

typedef typename FormatContext::char_type char_type;

Expand All @@ -461,7 +456,6 @@ struct chrono_formatter {
*out++ = '-';
}
s = std::chrono::duration_cast<seconds>(d);
ms = get_milliseconds(d);
}

Rep hour() const { return mod((s.count() / 3600), 24); }
Expand Down Expand Up @@ -547,6 +541,7 @@ struct chrono_formatter {
void on_second(numeric_system ns) {
if (ns == numeric_system::standard) {
write(second(), 2);
auto ms = get_milliseconds(std::chrono::duration<Rep, Period>(val));
if (ms != std::chrono::milliseconds(0)) {
*out++ = '.';
write(ms.count(), 3);
Expand Down
12 changes: 4 additions & 8 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace uintptr {
struct uintptr_t {
unsigned char value[sizeof(void*)];
};
}
} // namespace uintptr
using uintptr::uintptr_t;
typedef std::numeric_limits<uintptr_t> numutil;

Expand Down Expand Up @@ -1113,10 +1113,7 @@ FMT_CONSTEXPR unsigned basic_parse_context<Char, ErrorHandler>::next_arg_id() {
namespace internal {

namespace grisu_options {
enum {
fixed = 1,
grisu3 = 2
};
enum { fixed = 1, grisu3 = 2 };
}

// Formats value using the Grisu algorithm:
Expand Down Expand Up @@ -1190,8 +1187,7 @@ It grisu_prettify(const char* digits, int size, int exp, It it,
if (!params.trailing_zeros) {
// Remove trailing zeros.
while (size > full_exp && digits[size - 1] == '0') --size;
if (size != full_exp)
*it++ = static_cast<Char>('.');
if (size != full_exp) *it++ = static_cast<Char>('.');
return copy_str<Char>(digits + full_exp, digits + size, it);
}
*it++ = static_cast<Char>('.');
Expand Down Expand Up @@ -2877,7 +2873,7 @@ void basic_writer<Range>::write_double(T value, const format_specs& spec) {
(spec.type != 'a' && spec.type != 'A' && spec.type != 'e' &&
spec.type != 'E') &&
internal::grisu_format(static_cast<double>(value), buffer,
precision, options, exp);
precision, options, exp);
if (!use_grisu) internal::sprintf_format(value, buffer, spec);

if (handler.as_percentage) {
Expand Down
4 changes: 2 additions & 2 deletions include/fmt/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "chrono.h"

#ifdef _MSC_VER
#pragma message("fmt/time.h is deprecated, use fmt/chrono.h instead")
# pragma message("fmt/time.h is deprecated, use fmt/chrono.h instead")
#else
#warning fmt/time.h is deprecated, use fmt/chrono.h instead
# warning fmt/time.h is deprecated, use fmt/chrono.h instead
#endif

#endif // FMT_TIME_H_
4 changes: 4 additions & 0 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ TEST(ChronoTest, SpecialDurations) {
fmt::format("{:%I %H %M %S %R %r}", std::chrono::duration<double>(nan)));
fmt::format("{:%S}",
std::chrono::duration<float, std::atto>(1.79400457e+31f));
EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::exa>(1)),
"1Es");
EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::atto>(1)),
"1as");
}

#endif // FMT_STATIC_THOUSANDS_SEPARATOR

0 comments on commit de5da50

Please sign in to comment.