Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customisation of padding character for chrono #3516

Closed
bradrn opened this issue Jun 30, 2023 · 6 comments
Closed

Allow customisation of padding character for chrono #3516

bradrn opened this issue Jun 30, 2023 · 6 comments
Labels

Comments

@bradrn
Copy link

bradrn commented Jun 30, 2023

Reading through the chrono documentation, it appears there is no easy way to customise the padding/prefix character for presentation types like C, d, e, g, H, I, etc. Thus, it seems impossible for me to format a time string like ‘1:08 PM’: the closest I can get is ‘01:08 PM’. It would be nice to have a way to customise the padding character… perhaps the modifier field could be extended so that characters other than E or O are treated as the character to use?

@vitaut
Copy link
Contributor

vitaut commented Jun 30, 2023

Thanks for the suggestion but these format specs mostly follow strftime / standard chrono specs and there are no plans to significantly deviate from them.

@bradrn
Copy link
Author

bradrn commented Jun 30, 2023

Ah, didn’t realise there was a standard around these things. Thanks for letting me know! What a pity that C strftime doesn’t support this.

That being said, one Python resource suggests that %-I might do what I want — and indeed, Python strftime("%-I") seems to do what I want when I test it. I know this format code is nonstandard, but it seems like quite a useful thing to have. (In my particular case, I’m using fmt via Waybar, and it would be nice to display the current time in a way which I find most readable.)

@vitaut
Copy link
Contributor

vitaut commented Jun 30, 2023

- has been implemented in #3271 since it's a common strftime extension.

@bradrn
Copy link
Author

bradrn commented Jun 30, 2023

In that case, there must be a bug, because this program:

#include <fmt/chrono.h>

int main() {
    auto t = tm();
    t.tm_year = 2010 - 1900;
    t.tm_mon = 7;
    t.tm_mday = 4;
    t.tm_hour = 1;
    t.tm_min = 15;
    t.tm_sec = 58;
    fmt::print("{:%-I}", t);
    return 0;
}

Fails with the following error message:

terminate called after throwing an instance of 'fmt::v9::format_error'
  what():  invalid format
Aborted (core dumped)

Whereas the same program works fine when I use %I instead.

@vitaut
Copy link
Contributor

vitaut commented Jun 30, 2023

Works on godbolt: https://godbolt.org/z/3Gona4K4P. You might be using an older version.

@bradrn
Copy link
Author

bradrn commented Jun 30, 2023

You might be using an older version.

A-ha, so I am!

$ pacman -Q fmt
fmt 9.1.0-4

And indeed, when I try that version on Godbolt, it gives that error. I wonder why the Arch package is lagging behind… in any case, apologies for cluttering up the repo with this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants