diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 50c777c841aa..30ee8dc18da9 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,14 @@ 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