From 538d8777e56cb2af05f053835379eb465a93a5b5 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 14 Sep 2024 07:15:39 -0700 Subject: [PATCH] Workaround a bug in libstdc++ --- include/fmt/chrono.h | 30 +++++++++++++++++++++++++----- include/fmt/xchar.h | 3 +-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index a1e67583..95ae26e5 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -23,6 +23,17 @@ #include "format.h" namespace fmt_detail { +struct time_zone { + template + auto to_sys(T) + -> std::chrono::time_point { + return {}; + } +}; +template inline auto current_zone(T...) -> time_zone* { + return nullptr; +} + template inline void _tzset(T...) {} } // namespace fmt_detail @@ -507,6 +518,14 @@ auto to_time_t(sys_time time_point) -> std::time_t { time_point.time_since_epoch()) .count(); } + +// Workaround a bug in libstc++ which sets __cpp_lib_chrono to 201907 without +// providing current_zone(): https://github.com/fmtlib/fmt/issues/4160. +template FMT_CONSTEXPR auto has_current_zone() -> bool { + using namespace std::chrono; + using namespace fmt_detail; + return !std::is_same::value; +} } // namespace detail FMT_BEGIN_EXPORT @@ -553,10 +572,12 @@ inline auto localtime(std::time_t time) -> std::tm { } #if FMT_USE_LOCAL_TIME -template +template ())> inline auto localtime(std::chrono::local_time time) -> std::tm { - return localtime( - detail::to_time_t(std::chrono::current_zone()->to_sys(time))); + using namespace std::chrono; + using namespace fmt_detail; + return localtime(detail::to_time_t(current_zone()->to_sys(time))); } #endif @@ -1559,9 +1580,8 @@ struct chrono_format_checker : null_chrono_spec_handler { FMT_CONSTEXPR void on_iso_time() {} FMT_CONSTEXPR void on_am_pm() {} FMT_CONSTEXPR void on_duration_value() const { - if (has_precision_integral) { + if (has_precision_integral) FMT_THROW(format_error("precision not allowed for this argument type")); - } } FMT_CONSTEXPR void on_duration_unit() {} }; diff --git a/include/fmt/xchar.h b/include/fmt/xchar.h index a43530f1..da593d33 100644 --- a/include/fmt/xchar.h +++ b/include/fmt/xchar.h @@ -117,8 +117,7 @@ template <> struct is_char : std::true_type {}; template <> struct is_char : std::true_type {}; #ifdef __cpp_char8_t -template <> -struct is_char : bool_constant {}; +template <> struct is_char : bool_constant {}; #endif template