From 7e5eacc9db2c53c4142f6c9a49ccd660f2aa5217 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 19 Feb 2025 19:59:39 +1100 Subject: [PATCH 1/2] Default zoned time conversions to use earliest time FMT_USE_EARLIEST_TIME is used to control if earliest or latest time is preferred --- include/fmt/chrono.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 50c777c841aa..37dc9051422d 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -281,6 +281,13 @@ struct local_t {}; } // namespace detail +// When converting a local zoned time to system time, it could be ambigous +// By default prefer the earliest time but this can be set to 0 to use latest +// refer to std::chrono::time_zone::to_sys and std::chrono::choose for more info +#ifndef FMT_USE_EARLIEST_TIME +# define FMT_USE_EARLIEST_TIME 1 +#endif + template using sys_time = std::chrono::time_point; @@ -577,7 +584,12 @@ template time) -> std::tm { using namespace std::chrono; using namespace fmt_detail; - return localtime(detail::to_time_t(current_zone()->to_sys(time))); + +#if FMT_USE_EARLIEST_TIME + return localtime(detail::to_time_t(current_zone()->to_sys(time, std::chrono::choose::earliest))); +#else + return localtime(detail::to_time_t(current_zone()->to_sys(time, std::chrono::choose::latest))); +#endif } #endif From cbed981acc9cb1ec2276ec332942fe9a500c7fef Mon Sep 17 00:00:00 2001 From: musshorn Date: Wed, 19 Feb 2025 21:33:33 +1100 Subject: [PATCH 2/2] Run clang-format --- include/fmt/chrono.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 37dc9051422d..30ee8dc18da9 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -585,11 +585,13 @@ inline auto localtime(std::chrono::local_time time) -> std::tm { using namespace std::chrono; using namespace fmt_detail; -#if FMT_USE_EARLIEST_TIME - return localtime(detail::to_time_t(current_zone()->to_sys(time, std::chrono::choose::earliest))); -#else - return localtime(detail::to_time_t(current_zone()->to_sys(time, std::chrono::choose::latest))); -#endif +# if FMT_USE_EARLIEST_TIME + return localtime(detail::to_time_t( + current_zone()->to_sys(time, std::chrono::choose::earliest))); +# else + return localtime(detail::to_time_t( + current_zone()->to_sys(time, std::chrono::choose::latest))); +# endif } #endif