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

<chrono>: Formatting large hh_mm_ss values should be permitted #3676

Closed
StephanTLavavej opened this issue Apr 27, 2023 · 4 comments · Fixed by #3727
Closed

<chrono>: Formatting large hh_mm_ss values should be permitted #3676

StephanTLavavej opened this issue Apr 27, 2023 · 4 comments · Fixed by #3727
Labels
bug Something isn't working chrono C++20 chrono fixed Something works now, yay! format C++20/23 format

Comments

@StephanTLavavej
Copy link
Member

Reported by @josuttis to the LWG mailing list. @HowardHinnant confirmed that this is a bug. After checking the Standard, I agree that I don't see any direct justification for the exception that we're throwing. [tab:time.format.spec] simply says "%H The hour (24-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0." and the parenthetical "(24-hour clock)" doesn't seem to justify emitting an error. (If the type contains information, we should print it.)

C:\Temp>type meow.cpp
#include <chrono>
#include <format>
#include <iostream>
using namespace std;

int main() {
    try {
        const chrono::days d{3};
        const chrono::hh_mm_ss hms{d};
        cout << "d: " << d << "\n";
        cout << "hms: " << hms << "\n";
    } catch (const format_error& fe) {
        cout << fe.what() << "\n";
    }
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /MTd /Od meow.cpp && meow
meow.cpp
d: 3d
hms: Cannot localize hh_mm_ss with an absolute value of 24 hours or more.

Compare https://godbolt.org/z/WjsPGP771 where libstdc++ prints:

d: 3d
hms: 72:00:00

The fix may be as simple as removing this code:

STL/stl/inc/chrono

Lines 5683 to 5688 in 0a2d59e

if constexpr (_Is_specialization_v<_Ty, hh_mm_ss>) {
if (_Spec._Type == 'H' && _Val.hours() >= hours{24}) {
_Throw_format_error("Cannot localize hh_mm_ss with an absolute value of 24 hours or more.");
}
return;
}

@StephanTLavavej StephanTLavavej added bug Something isn't working format C++20/23 format chrono C++20 chrono labels Apr 27, 2023
@josuttis
Copy link

josuttis commented Apr 28, 2023 via email

@cpplearner
Copy link
Contributor

inconsistencies of using std::format()? Sounds like LWG-3856

@josuttis
Copy link

josuttis commented Apr 28, 2023 via email

@StephanTLavavej
Copy link
Member Author

It's likely the same bug but we can check when we're looking at a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working chrono C++20 chrono fixed Something works now, yay! format C++20/23 format
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants