From a90ad5d6fde4b39417f63a034d566dd7f09b0a55 Mon Sep 17 00:00:00 2001 From: Jiami Lin Date: Tue, 2 Jun 2026 09:16:49 +0800 Subject: [PATCH] Fix formatting std::tm with null tm_zone (#4790) --- include/fmt/chrono.h | 1 + test/chrono-test.cc | 3 +++ 2 files changed, 4 insertions(+) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 517f9576..e4bc1ce4 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -1192,6 +1192,7 @@ class tm_writer { template ::value)> void format_tz_name(const T& tm) { + if (!tm.tm_zone) FMT_THROW(format_error("no timezone")); out_ = write_tm_str(out_, tm.tm_zone, loc_); } template ::value)> diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 7b4bf402..a9153598 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -358,6 +358,9 @@ TEST(chrono_test, tm) { char tz[] = "EET"; if (fmt::detail::set_tm_zone(time, tz)) { EXPECT_EQ(fmt::format(fmt::runtime("{:%Z}"), time), "EET"); + fmt::detail::set_tm_zone(time, nullptr); + EXPECT_THROW_MSG((void)fmt::format(fmt::runtime("{:%Z}"), time), + fmt::format_error, "no timezone"); } else { EXPECT_THROW_MSG((void)fmt::format(fmt::runtime("{:%Z}"), time), fmt::format_error, "no timezone");