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

Support strftime - extension #2959

Closed
jayache80 opened this issue Jul 3, 2022 · 5 comments · Fixed by #3271
Closed

Support strftime - extension #2959

jayache80 opened this issue Jul 3, 2022 · 5 comments · Fixed by #3271

Comments

@jayache80
Copy link

With strftime a single digit 12-hour hour can be formatted to not have a leading zero using %-I. This seems to not be possible with fmt::format.

#include <iostream>
#include <fmt/chrono.h>

int main() {
    std::cout << "%-I:%M using strftime:" << std::endl;
    std::time_t t_ = std::time(nullptr);
    char mbstr[100];
    if (std::strftime(mbstr, sizeof(mbstr), "%-I:%M", std::localtime(&t_))) {
        std::cout << mbstr << std::endl;
    }

    std::cout << "%-I:%M using fmt::format:" << std::endl;
    const auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
    const auto time_str = fmt::format("{:%-I:%M}", fmt::localtime(t));
    // terminate called after throwing an instance of 'fmt::v8::format_error'
    //   what():  invalid format
    std::cout << time_str << std::endl;

    return 0;
}

There are probably more instances where strftime parity is not quite met. It would great if it was, or if any user error could be pointed out :)

@vitaut
Copy link
Contributor

vitaut commented Jul 3, 2022

I don't see %-I documented in https://man7.org/linux/man-pages/man3/strftime.3.html. Is it some kind of an extension?

@jayache80
Copy link
Author

On the man page you linked:

Glibc notes
Glibc provides some extensions for conversion specifications.
(These extensions are not specified in POSIX.1-2001, but a few
other systems provide similar features.) Between the '%'
character and the conversion specifier character, an optional
flag and field width may be specified. (These precede the E or O
modifiers, if present.)

   The following flag characters are permitted:

   _      (underscore) Pad a numeric result string with spaces.

   -      (dash) Do not pad a numeric result string

so I'd suppose it is a glibc extension. Does that preclude it?

@vitaut
Copy link
Contributor

vitaut commented Jul 4, 2022

{fmt} supports only standard specifiers at the moment but a PR to add - would be welcome.

@vitaut vitaut changed the title Support strftime semantics Support strftime - extension Jul 4, 2022
jayache80 added a commit to jayache80/fmt that referenced this issue Jul 31, 2022
* support `-` for no padding
@jayache80
Copy link
Author

Hi @vitaut
I've started working on this but have some questions about differing behavior I'm seeing between platforms (some of tests are failing on Windows only). Does {fmt} have an IRC development channel or something of the sort?

@vitaut
Copy link
Contributor

vitaut commented Aug 17, 2022

There is no IRC, we usually discuss such issues on github.

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

Successfully merging a pull request may close this issue.

2 participants