Skip to content

Add precision modifier for seconds in chrono format - #3122

Closed
SappyJoy wants to merge 5 commits into
fmtlib:masterfrom
SappyJoy:add-precision-modifier-for-seconds-in-chrono-format
Closed

SappyJoy wants to merge 5 commits into
fmtlib:masterfrom
SappyJoy:add-precision-modifier-for-seconds-in-chrono-format

Conversation

@SappyJoy

Copy link
Copy Markdown
Contributor

Add precision modifier for seconds in chrono format and test for it.

It's became important for custom datetime types that store date, time and subseconds. I want to use chrono format for those types, but I have no way to control subsecond precision.

Use case:

EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%.6S %Z}", datetime), "2022-09-28 12:14:25.123456 MSK");

@vitaut

vitaut commented Sep 28, 2022

Copy link
Copy Markdown
Contributor

Thanks for the PR. Is this feature part of the standard?

@SappyJoy

SappyJoy commented Sep 28, 2022

Copy link
Copy Markdown
Contributor Author

No, it is not in standard. The standard only supports the precision modifier for strings and floating point numbers. Precision available only for particular argument, but not for format-spec.

@vitaut vitaut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Adding precision support to durations sounds reasonable but for consistency with the standard it should go at the beginning, not in %S (https://eel.is/c++draft/time.format), e.g.

fmt::print("{:.6%S}", std::chrono::nanoseconds{1234});

@SappyJoy

SappyJoy commented Oct 5, 2022

Copy link
Copy Markdown
Contributor Author

{:.6%S} looks like precision for argument. There is test for this:

  EXPECT_EQ("1.2 ms   ", fmt::format("{:7.1%Q %q}", dms(1.234)));

It is not obvious for user to use {:.6%S} and expect that this precision modifier belong to %S. But it can be done this way if the specifier has a higher priority on getting the modifier than the argument.

@vitaut

vitaut commented Oct 8, 2022

Copy link
Copy Markdown
Contributor

But it can be done this way if the specifier has a higher priority on getting the modifier than the argument.

We should make existing precision apply to %S and not introduce a new one to avoid diverging too much from the standard.

@SappyJoy SappyJoy closed this Oct 19, 2022
@SappyJoy
SappyJoy deleted the add-precision-modifier-for-seconds-in-chrono-format branch October 19, 2022 11:19
@SappyJoy

SappyJoy commented Oct 19, 2022

Copy link
Copy Markdown
Contributor Author

Oh, I squashed last commits to resolve conflict. It led to close this PR. @vitaut, please reopen it, I made changes. Now it should work.

@vitaut

vitaut commented Oct 19, 2022

Copy link
Copy Markdown
Contributor

I am not able to reopen this diff for some reason. Please submit a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants