-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Date overflow with std::chrono::time_point #3725
Comments
I think the issue is here: Lines 2097 to 2103 in dd6f657
and here: Lines 526 to 529 in dd6f657
Hence, simply using this alternative template<typename Duration>
inline std::tm gmtime(
std::chrono::time_point<std::chrono::system_clock, Duration> time_point) {
using large_seconds = std::chrono::duration<std::int64_t>;
return gmtime(std::chrono::time_point_cast<large_seconds>(time_point).time_since_epoch().count());
} (and we can remove the NB: this is assuming that |
I opened a PR with a suggested fix #3727. |
cschreib
added a commit
to cschreib/fmt
that referenced
this issue
Nov 25, 2023
The function is now more generic and will handle all casts. It also takes care of toggling safe vs unsafe casts using FMT_SAFE_DURATION_CAST.
vitaut
pushed a commit
that referenced
this issue
Nov 25, 2023
* Fix #3725 and rename fmt_safe_duration_cast to fmt_duration_cast The function is now more generic and will handle all casts. It also takes care of toggling safe vs unsafe casts using FMT_SAFE_DURATION_CAST. * Refactor fmt_duration_cast to put #ifdef inside the function * Fix compilation error with FMT_USE_LOCAL_TIME
happymonkey1
pushed a commit
to happymonkey1/fmt
that referenced
this issue
Apr 7, 2024
* Fix fmtlib#3725 and rename fmt_safe_duration_cast to fmt_duration_cast The function is now more generic and will handle all casts. It also takes care of toggling safe vs unsafe casts using FMT_SAFE_DURATION_CAST. * Refactor fmt_duration_cast to put #ifdef inside the function * Fix compilation error with FMT_USE_LOCAL_TIME
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
std::chrono::system_clock::time_point
on linux platforms uses nanosecond resolution and a 64 bit signed counter, hence is unable to represent dates beyond year 2262. This can be circumvented by using a customtime_point
with a lower time resolution when doing time calculations, such asusing my_time_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>;
Unfortunately, it seems that fmtlib is unable to correctly format such time points despite the lower time resolution. Here is an example that reproduces the problem:
Output from latest master commit on my system (Ubuntu GCC 11.4; doesn't have
<format>
):Output lines 2 and 3 show the problem of the standard
std::chrono::system_clock::time_point
; this is the output I expect.Output line 5 shows that, using
TimePointGood
with millisecond resolution, the time point itself is able to represent dates far in the future.Output line 6 shows that fmtlib still overflows.
Godbolt link; this is running an older version of fmtlib but it also affected. Using gcc trunk with
std::format
shows that the standardformat
doesn't have the issue and outputs what one would expect:The text was updated successfully, but these errors were encountered: