Skip to content

Commit

Permalink
refactor: time report supports minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Nov 22, 2024
1 parent 59fe95d commit 5cd44bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
38 changes: 31 additions & 7 deletions src/lib/Support/Chrono.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,46 @@
namespace clang {
namespace mrdocs {

/** Formats a duration into a human-readable string.
This function takes a `std::chrono::duration` object and formats it into a
string that represents the duration in a human-readable format. The format
includes hours, minutes, seconds, and milliseconds as appropriate.
@param delta The duration to format.
@return A string representing the formatted duration.
*/
template <class Rep, class Period>
std::string
format_duration(
std::chrono::duration<Rep, Period> delta)
{
auto delta_ms = std::chrono::duration_cast<
std::chrono::milliseconds>(delta).count();
if (delta_ms < 1000)
auto delta_ms = std::chrono::duration_cast<std::chrono::milliseconds>(delta);
if (delta < std::chrono::seconds(1))
{
return fmt::format("{} ms", delta_ms);
return fmt::format("{}ms", delta_ms.count());
}
else

auto delta_s = std::chrono::duration_cast<std::chrono::seconds>(delta);
if (delta < std::chrono::minutes(1))
{
double const delta_s = static_cast<double>(delta_ms) / 1000.0;
return fmt::format("{:.02f} s", delta_s);
delta_ms -= delta_s;
return fmt::format("{}s {}ms", delta_s.count(), delta_ms.count());
}

auto delta_min = std::chrono::duration_cast<std::chrono::minutes>(delta);
if (delta < std::chrono::hours(1))
{
delta_ms -= delta_s;
delta_s -= delta_min;
return fmt::format("{}min {}s {}ms", delta_min.count(), delta_s.count(), delta_ms.count());
}

auto delta_h = std::chrono::duration_cast<std::chrono::hours>(delta);
delta_ms -= delta_s;
delta_s -= delta_min;
delta_min -= delta_h;
return fmt::format("{}h {}min {}s {}ms", delta_h.count(), delta_min.count(), delta_s.count(), delta_ms.count());
}

} // mrdocs
Expand Down
3 changes: 2 additions & 1 deletion src/lib/Support/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ build(Corpus const& corpus) const
corpus.config->configDir));
MRDOCS_TRY(build(absOutput, corpus));
report::info(
"Generated documentation in {}",
"Generated {} documentation in {}",
this->displayName(),
format_duration(clock_type::now() - start_time));
return {};
}
Expand Down

0 comments on commit 5cd44bd

Please sign in to comment.