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>: Some (weird) durations still cannot be formatted #3644

Closed
JMazurkiewicz opened this issue Apr 10, 2023 · 1 comment · Fixed by #3649
Closed

<chrono>: Some (weird) durations still cannot be formatted #3644

JMazurkiewicz opened this issue Apr 10, 2023 · 1 comment · Fixed by #3649
Labels
bug Something isn't working chrono C++20 chrono fixed Something works now, yay! format C++20/23 format

Comments

@JMazurkiewicz
Copy link
Contributor

JMazurkiewicz commented Apr 10, 2023

Repro:

#include <chrono>
#include <format>
#include <iostream>

int main() {
  // remove `+ 1` to get rid of an error (see https://godbolt.org/z/9WGMoWeeo)
  using R = std::ratio<128 + 64 + 8 + 1, std::numeric_limits<int>::max()>;
  using D = std::chrono::duration<int, R>;
  std::cout << std::format("{:%Q %q}\n", D{4});
}

Build:

PS D:\stl-playground> cl /std:c++20 /EHsc /nologo .\chronat.cpp
chronat.cpp
D:\stl\out\build\x64\out\inc\chrono(4949): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'std::chrono::hh_mm_ss<_Ty>'
        with
        [
            _Ty=std::chrono::duration<int,R>
        ]
D:\stl\out\build\x64\out\inc\chrono(4949): note: No constructor could take the source type, or constructor overload resolution was ambiguous
D:\stl\out\build\x64\out\inc\chrono(5013): note: see reference to function template instantiation 'std::chrono::hh_mm_ss<_Ty> std::chrono::_Hh_mm_ss_part_underflow_to_zero<_Ty>(const _Duration &)' being compiled
        with
        [
            _Ty=std::chrono::duration<int,R>,
            _Duration=std::chrono::duration<int,R>
        ]
# [...]

Compiler explorer: https://godbolt.org/z/o71s74c8K

@frederick-vs-ja
Copy link
Contributor

std::ratio<53, 101> also triggers the bug.

It seems that the return type of _Hh_mm_ss_part_underflow_to_zero is sometimes incorect. Perhaps we should use hh_mm_ss<common_type_t<_Duration, days>> (where common_type_t<_Duration, days> is the return type of _Remove_duration_part) as the return type.

STL/stl/inc/chrono

Lines 4947 to 4950 in e6a12f7

template <class _Duration>
_NODISCARD constexpr hh_mm_ss<_Duration> _Hh_mm_ss_part_underflow_to_zero(const _Duration& _Val) {
return hh_mm_ss<_Duration>{_Remove_duration_part<days>(_Val)};
}

@StephanTLavavej StephanTLavavej added bug Something isn't working format C++20/23 format chrono C++20 chrono labels Apr 10, 2023
@StephanTLavavej StephanTLavavej added the fixed Something works now, yay! label Apr 28, 2023
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