Implementation of the idea:
I'm not sure if we should add more special cases but it would be nice to get rid of strftime completely in formatter::format by parsing the format string and using the optimization when possible. It would be not as fast as special casing but would be more generally applicable.
Originally posted by @vitaut in #2500 (comment)
My implementation
https://github.com/phprus/fmt/tree/optimize-tm-formatting-3
Functions https://github.com/phprus/fmt/blob/6c13ec65d06c5c0bba7806a01252179ae76cb1c9/include/fmt/chrono.h#L1545-L1576
can be optimized.
Performance
Not tested yet.
Problem
I don't know why, but on Windows the detail::write ( https://github.com/phprus/fmt/blob/6c13ec65d06c5c0bba7806a01252179ae76cb1c9/include/fmt/chrono.h#L364-L369 ) function works with a different locale than strftime.
3/19 Test #3: chrono-test ......................***Failed 0.06 sec
[ RUN ] chrono_test.time_point
D:\a\fmt\fmt\test\chrono-test.cc(179): error: Expected equality of these values:
output
Which is: "Sat Oct 9 19:37:48 2021"
fmt::format(fmt::runtime(fmt_spec), t1)
Which is: "10/09/21 19:37:48"
D:\a\fmt\fmt\test\chrono-test.cc(180): error: Expected equality of these values:
output
Which is: "Sat Oct 9 19:37:48 2021"
fmt::format(fmt::runtime(fmt_spec), tm)
Which is: "10/09/21 19:37:48"
D:\a\fmt\fmt\test\chrono-test.cc(179): error: Expected equality of these values:
output
Which is: "Sat Oct 9 19:37:48 2021"
fmt::format(fmt::runtime(fmt_spec), t1)
Which is: "10/09/21 19:37:48"
D:\a\fmt\fmt\test\chrono-test.cc(180): error: Expected equality of these values:
output
Which is: "Sat Oct 9 19:37:48 2021"
fmt::format(fmt::runtime(fmt_spec), tm)
Which is: "10/09/21 19:37:48"
D:\a\fmt\fmt\test\chrono-test.cc(179): error: Expected equality of these values:
output
Which is: "07:37:48 PM"
fmt::format(fmt::runtime(fmt_spec), t1)
Which is: "19:37:48"
D:\a\fmt\fmt\test\chrono-test.cc(180): error: Expected equality of these values:
output
Which is: "07:37:48 PM"
fmt::format(fmt::runtime(fmt_spec), tm)
Which is: "19:37:48"
[ FAILED ] chrono_test.time_point (3 ms)
16/19 Test #16: xchar-test .......................***Failed 0.03 sec
[ RUN ] chrono_test.time_point
D:\a\fmt\fmt\test\xchar-test.cc(289): error: Expected equality of these values:
output
Which is: L"Sat Oct 9 19:37:49 2021"
fmt::format(fmt_spec, t1)
Which is: L"10/09/21 19:37:49"
D:\a\fmt\fmt\test\xchar-test.cc(290): error: Expected equality of these values:
output
Which is: L"Sat Oct 9 19:37:49 2021"
fmt::format(fmt_spec, tm)
Which is: L"10/09/21 19:37:49"
D:\a\fmt\fmt\test\xchar-test.cc(289): error: Expected equality of these values:
output
Which is: L"Sat Oct 9 19:37:49 2021"
fmt::format(fmt_spec, t1)
Which is: L"10/09/21 19:37:49"
D:\a\fmt\fmt\test\xchar-test.cc(290): error: Expected equality of these values:
output
Which is: L"Sat Oct 9 19:37:49 2021"
fmt::format(fmt_spec, tm)
Which is: L"10/09/21 19:37:49"
D:\a\fmt\fmt\test\xchar-test.cc(289): error: Expected equality of these values:
output
Which is: L"07:37:49 PM"
fmt::format(fmt_spec, t1)
Which is: L"19:37:49"
D:\a\fmt\fmt\test\xchar-test.cc(290): error: Expected equality of these values:
output
Which is: L"07:37:49 PM"
fmt::format(fmt_spec, tm)
Which is: L"19:37:49"
[ FAILED ] chrono_test.time_point (3 ms)
Implementation of the idea:
My implementation
https://github.com/phprus/fmt/tree/optimize-tm-formatting-3
Functions https://github.com/phprus/fmt/blob/6c13ec65d06c5c0bba7806a01252179ae76cb1c9/include/fmt/chrono.h#L1545-L1576
can be optimized.
Performance
Not tested yet.
Problem
I don't know why, but on Windows the
detail::write( https://github.com/phprus/fmt/blob/6c13ec65d06c5c0bba7806a01252179ae76cb1c9/include/fmt/chrono.h#L364-L369 ) function works with a different locale thanstrftime.