fix: assertion failed no timezone in fmt::chrono (#8344) (#8358)

* fix: in tr_logGetTimeStr() do not use tz info on platforms that do not have it

* fix: simplify the supports-timezone check



---------

Co-authored-by: Yat Ho <lagoho7@gmail.com>
This commit is contained in:
Charles Kerr
2026-02-04 10:24:15 -06:00
committed by GitHub
parent c53016e523
commit 734d0bc8b7

View File

@@ -12,6 +12,7 @@
#include <optional> #include <optional>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <type_traits>
#include <utility> #include <utility>
#ifdef _WIN32 #ifdef _WIN32
@@ -37,6 +38,11 @@ using namespace std::literals;
namespace namespace
{ {
template<typename, typename = void>
inline constexpr bool HasTmGmtoffV = false;
template<typename T>
inline constexpr bool HasTmGmtoffV<T, std::void_t<decltype(std::declval<T>().tm_gmtoff)>> = true;
class tr_log_state class tr_log_state
{ {
@@ -203,13 +209,9 @@ std::string_view tr_logGetTimeStr(std::chrono::system_clock::time_point const no
auto* walk = buf; auto* walk = buf;
auto const now_time_t = std::chrono::system_clock::to_time_t(now); auto const now_time_t = std::chrono::system_clock::to_time_t(now);
auto const now_tm = *std::localtime(&now_time_t); auto const now_tm = *std::localtime(&now_time_t);
walk = fmt::format_to_n( static bool constexpr HasTmGmtoff = HasTmGmtoffV<std::tm>;
walk, static auto constexpr Fmt = HasTmGmtoff ? "{0:%FT%R:}{1:%S}{0:%z}"sv : "{0:%FT%R:}{1:%S}"sv;
buflen, walk = fmt::format_to_n(walk, buflen, Fmt, now_tm, std::chrono::time_point_cast<std::chrono::milliseconds>(now)).out;
"{0:%FT%R:}{1:%S}" TR_IF_WIN32("", "{0:%z}"),
now_tm,
std::chrono::time_point_cast<std::chrono::milliseconds>(now))
.out;
#ifdef _WIN32 #ifdef _WIN32
if (auto tz_info = TIME_ZONE_INFORMATION{}; GetTimeZoneInformation(&tz_info) != TIME_ZONE_ID_INVALID) if (auto tz_info = TIME_ZONE_INFORMATION{}; GetTimeZoneInformation(&tz_info) != TIME_ZONE_ID_INVALID)
{ {