Thanks to visit codestin.com
Credit goes to github.com

Skip to content

<chrono> <format>: duration formatting ignores dynamic width #4201

@StephanTLavavej

Description

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

int main() {
#ifdef __GLIBCXX__
    cout << "libstdc++\n";
#elif defined(_LIBCPP_VERSION)
    cout << "libc++\n";
#elif defined(_MSVC_STL_UPDATE)
    cout << "microsoft/STL\n";
#endif
    const auto id = this_thread::get_id();
    cout << format("       int {{:20}}: [{:20}]\n", 1729);
    cout << format("       int {{:{{}}}}: [{:{}}]\n", 1729, 20);
    cout << format("thread::id {{:20}}: [{:20}]\n", id);
    cout << format("thread::id {{:{{}}}}: [{:{}}]\n", id, 20);
    cout << format(" seconds {{:20%T}}: [{:20%T}]\n", 314159s);
    cout << format(" seconds {{:{{}}%T}}: [{:{}%T}]\n", 314159s, 20);
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /MTd /Od meow.cpp && meow
meow.cpp
microsoft/STL
       int {:20}: [                1729]
       int {:{}}: [                1729]
thread::id {:20}: [               34680]
thread::id {:{}}: [               34680]
 seconds {:20%T}: [87:15:59            ]
 seconds {:{}%T}: [87:15:59]

https://godbolt.org/z/YbM9qnocf

When formatting a duration, the fact that {:20%T} respects the statically provided width but {:{}%T} ignores the dynamically provided width is super buggy, and neither libstdc++ nor libc++ exhibit that behavior.

(There's a separate question of whether duration should default to being left-aligned or right-aligned. MSVC and libstdc++ chose left, libc++ chose right.)

(libc++ is also printing the hours mod 24, unlike MSVC and libstdc++.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingchronoC++20 chronofixedSomething works now, yay!formatC++20/23 format

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions