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>: Fix formatter ignoring dynamically provided width #4283

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion stl/inc/chrono
Original file line number Diff line number Diff line change
Expand Up @@ -5477,7 +5477,14 @@ namespace chrono {

int _Estimated_width = -1;
(void) _Measure_string_prefix(_Stream.view(), _Estimated_width);
return _Write_aligned(_STD move(_FormatCtx.out()), _Estimated_width, _Specs, _Fmt_align::_Left,

auto _Format_specs = _Specs;
if (_Specs._Dynamic_width_index >= 0) {
_Format_specs._Width = _Get_dynamic_specs<_Width_checker>(
_FormatCtx.arg(static_cast<size_t>(_Specs._Dynamic_width_index)));
}

return _Write_aligned(_STD move(_FormatCtx.out()), _Estimated_width, _Format_specs, _Fmt_align::_Left,
[&](auto _Out) { return _Fmt_write(_STD move(_Out), _Stream.view()); });
}

Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ tests\GH_003840_tellg_when_reading_lf_file_in_text_mode
tests\GH_003867_output_nan
tests\GH_004023_mdspan_fwd_prod_overflow
tests\GH_004040_container_nonmember_functions
tests\GH_004201_chrono_formatter
tests\GH_004275_seeking_fancy_iterators
tests\LWG2381_num_get_floating_point
tests\LWG2597_complex_branch_cut
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/GH_004201_chrono_formatter/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\concepts_20_matrix.lst
16 changes: 16 additions & 0 deletions tests/std/tests/GH_004201_chrono_formatter/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cassert>
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
#include <chrono>
#include <format>
#include <string>

using namespace std::literals::chrono_literals;

int main() {
assert(std::format("[{:20%T}]", 314159s) == "[87:15:59 ]");

// std::formatter specializations for <chrono> types used to ignore dynamically provided width
assert(std::format("[{:{}%T}]", 314159s, 20) == "[87:15:59 ]");
}