Skip to content

[libc++][format][chrono] Subsecond part of duration is not correctly formatted #62082

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

Closed
frederick-vs-ja opened this issue Apr 12, 2023 · 3 comments
Assignees
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Comments

@frederick-vs-ja
Copy link
Contributor

Example

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

int main()
{
    std::cout << std::format("{0:%M:%S} {0}\n", std::chrono::duration<int, std::ratio<101, 103>>{40});
    std::cout << std::format("{0:%M:%S} {0}\n", std::chrono::duration<int, std::ratio<1, 1024>>{1511});
}

Current output (Godbolt link):

00:39.000023 40[101/103]s
00:01.0000000487 1511[1/1024]s

Expected output:

00:39.223300 40[101/103]s
00:01.4755859375 1511[1/1024]s

It seems that we should convert (via duration_cast) __fraction to precision before writing its count().

auto __fraction = __value - chrono::duration_cast<chrono::seconds>(__value);

std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
_LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}.0f}"),
__fraction.count(),
chrono::hh_mm_ss<__duration>::fractional_width);
else
std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
_LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}}"),
__fraction.count(),
chrono::hh_mm_ss<__duration>::fractional_width);

@EugeneZelenko EugeneZelenko added libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. and removed new issue labels Apr 12, 2023
@mordante mordante self-assigned this Apr 12, 2023
@mordante
Copy link
Member

mordante commented May 6, 2023

https://reviews.llvm.org/D149868 should fix this.

@mordante
Copy link
Member

The author of the previous fix seems to be MIA, instead I created https://reviews.llvm.org/D154851

@mordante
Copy link
Member

The patch has landed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

No branches or pull requests

3 participants