diff --git a/src/utils/logging.cc b/src/utils/logging.cc index b4d4aa19c2..286b517f29 100644 --- a/src/utils/logging.cc +++ b/src/utils/logging.cc @@ -27,6 +27,11 @@ #include "utils/logging.h" +#include +#if __cpp_lib_format >= 201907L +#include +#include +#endif #include #include #include @@ -100,8 +105,26 @@ std::chrono::time_point SteadyClockToSystemClock( time - std::chrono::steady_clock::now()); } +namespace { +#if __cpp_lib_format >= 201907L +const std::chrono::time_zone* GetCurrentTimeZone() { + try { + return std::chrono::current_zone(); + } catch (const std::runtime_error&) { + // If the current time zone cannot be determined, fall back to UTC. + return nullptr; + } +} +#endif +} // namespace + std::string FormatTime( std::chrono::time_point time) { +#if __cpp_lib_format >= 201907L + static auto* zone = GetCurrentTimeZone(); + return zone ? std::format("{0:%m%d %T}", zone->to_local(time)) + : std::format("{0:%m%d %T}", time); +#else static Mutex mutex; std::ostringstream ss; @@ -121,6 +144,7 @@ std::string FormatTime( << std::setfill('0') << std::setw(6) << us; } return ss.str(); +#endif } -} // namespace lczero \ No newline at end of file +} // namespace lczero