Skip to content

Commit

Permalink
<chrono>: Fix formatter ignoring dynamically provided width (#4283)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
mt3d and StephanTLavavej authored Jan 11, 2024
1 parent 7a3a4a5 commit 629216a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
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>
#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 ]");
}

0 comments on commit 629216a

Please sign in to comment.