C++ Logo


Advanced search

Re: [std-proposals] New draft proposal: Add "%s" (two-digit truncated-integer seconds) as a std::format conversion specifier for std::chrono time types.

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Wed, 3 May 2023 11:59:08 +0100
On Wed, 3 May 2023 at 11:43, Jonathan Wakely <cxx_at_[hidden]> wrote:

> > The currently required method may have the potential to be less
> efficient because it *requires* a conversion in user code, (and it's not
> easy for a user to determine whether this conversion is trivial). This is
> unlikely to be very important, however, as it's unlikely this function will
> be called often enough to noticeably affect performance.
> Why would it be any more efficient for std::format to do
> chrono::duration_cast<seconds> (or chrono::floor) than for the user code to
> do it?
> I don't really see a pressing need for this. If you don't want fractional
> seconds, don't use a representation that has subsecond resolution. One of
> the strengths of the std::chrono API is that you can choose your resolution
> and easily convert between them. I don't think std::format needs to provide
> that conversion as well.
I don't find the consistency argument convincing either:

> The other sub-day quantities (hours and minutes) are already two-digit
integer-truncated. (And there is no fractional minutes or fractional hours
specifier (and, IMHO, that's probably good)).

Yes, because otherwise to print HH:MM you'd have to do this:
std::format("{:%H}:{:%M}", floor<hours>(t), floor<minutes>(t));
instead of:
std::format("{:%H:%M}", t);
std::format("{:%R}", t);

And for HH:MM:SS it would be even worse, converting the same value three
times and formatting three separate replacement fields:
std::format("{:%H}:{:%M}:{:%S}", floor<hours>(t), floor<minutes>(t),
instead of:
std::format("{:%T}", t);

So printing hours and minutes is a case where the reduced verbosity is

But IMHO that doesn't apply for printing seconds, because a single
floor<seconds>(t) conversion produces a value that still gives the correct
output for %H and %M as well as for %S:

std::format("{:%T}", floor<seconds>(t));

The reduced verbosity of being able to say format("{:%H:%M:%s", t) is a
minor convenience at best (and because %T would still use %S, it's arguably
not any simpler).

Received on 2023-05-03 10:59:22