Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default zoned time conversions to use earliest time #4352

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Duration>
using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>;

Expand Down Expand Up @@ -577,7 +584,14 @@ template <typename Duration,
inline auto localtime(std::chrono::local_time<Duration> time) -> std::tm {
using namespace std::chrono;
using namespace fmt_detail;
return localtime(detail::to_time_t(current_zone()->to_sys<Duration>(time)));

# if FMT_USE_EARLIEST_TIME
return localtime(detail::to_time_t(
current_zone()->to_sys<Duration>(time, std::chrono::choose::earliest)));
# else
return localtime(detail::to_time_t(
current_zone()->to_sys<Duration>(time, std::chrono::choose::latest)));
# endif
}
#endif

Expand Down
Loading