Improve chrono formatting

This commit is contained in:
Victor Zverovich
2025-04-12 09:00:43 -07:00
parent f470b9c566
commit f10b6dd816
2 changed files with 8 additions and 11 deletions

View File

@ -2206,7 +2206,8 @@ template <typename Char> struct formatter<std::tm, Char> {
detail::arg_ref<Char> width_ref_;
protected:
basic_string_view<Char> fmt_;
basic_string_view<Char> fmt_ =
detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>();
template <typename Duration, typename FormatContext>
auto do_format(const std::tm& tm, FormatContext& ctx,
@ -2261,10 +2262,6 @@ template <typename Char> struct formatter<std::tm, Char> {
template <typename Char, typename Duration>
struct formatter<sys_time<Duration>, Char> : formatter<std::tm, Char> {
FMT_CONSTEXPR formatter() {
this->fmt_ = detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>();
}
template <typename FormatContext>
auto format(sys_time<Duration> val, FormatContext& ctx) const
-> decltype(ctx.out()) {
@ -2303,10 +2300,6 @@ struct formatter<utc_time<Duration>, Char>
template <typename Duration, typename Char>
struct formatter<local_time<Duration>, Char> : formatter<std::tm, Char> {
FMT_CONSTEXPR formatter() {
this->fmt_ = detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>();
}
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
return this->do_parse(ctx, true);
}

View File

@ -16,6 +16,7 @@
using fmt::runtime;
using fmt::sys_time;
using fmt::sys_time;
using testing::Contains;
#if defined(__MINGW32__) && !defined(_UCRT)
@ -238,8 +239,6 @@ TEST(chrono_test, format_to_empty_container) {
EXPECT_EQ(s, "42");
}
TEST(chrono_test, empty_result) { EXPECT_EQ(fmt::format("{}", std::tm()), ""); }
TEST(chrono_test, gmtime) {
auto t = std::time(nullptr);
auto expected = *std::gmtime(&t);
@ -337,6 +336,11 @@ TEST(chrono_test, local_time) {
fmt::format_error, "no timezone");
}
TEST(chrono_test, tm) {
auto time = fmt::gmtime(290088000);
test_time(time);
}
TEST(chrono_test, daylight_savings_time_end) {
// 2024-10-27 03:05 as the number of seconds since epoch in Europe/Kyiv time.
// It is slightly after the DST end and passing it to to_sys will result in