From df94bb03e0e418c8f6d01fd9c36df726a92f478c Mon Sep 17 00:00:00 2001 From: cpplearner Date: Fri, 8 Dec 2023 11:06:24 +0800 Subject: [PATCH] : Make format() accept %X and %EX for duration and hh_mm_ss --- stl/inc/chrono | 4 ++-- .../P0355R7_calendars_and_time_zones_formatting/test.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/stl/inc/chrono b/stl/inc/chrono index 7969744d2da..2abef30a3e1 100644 --- a/stl/inc/chrono +++ b/stl/inc/chrono @@ -5398,7 +5398,7 @@ namespace chrono { || _Is_valid_type(_Type) || _Is_valid_type(_Type) || _Is_valid_type(_Type); } else if constexpr (_Is_specialization_v<_Ty, hh_mm_ss>) { return _Type == 'H' || _Type == 'I' || _Type == 'M' || _Type == 'S' || _Type == 'r' || _Type == 'R' - || _Type == 'T' || _Type == 'p'; + || _Type == 'T' || _Type == 'p' || _Type == 'X'; } else if constexpr (_Is_any_of_v<_Ty, sys_info, local_info>) { return _Type == 'z' || _Type == 'Z'; } else if constexpr (_Is_specialization_v<_Ty, time_point>) { @@ -5407,7 +5407,7 @@ namespace chrono { return true; } } - return _Type == 'c' || _Type == 'X' || _Is_valid_type(_Type) + return _Type == 'c' || _Is_valid_type(_Type) || _Is_valid_type>(_Type); } else if constexpr (_Is_specialization_v<_Ty, _Local_time_format_t>) { return _Type == 'z' || _Type == 'Z' || _Is_valid_type(_Type); diff --git a/tests/std/tests/P0355R7_calendars_and_time_zones_formatting/test.cpp b/tests/std/tests/P0355R7_calendars_and_time_zones_formatting/test.cpp index bf0baaa112b..2901002420e 100644 --- a/tests/std/tests/P0355R7_calendars_and_time_zones_formatting/test.cpp +++ b/tests/std/tests/P0355R7_calendars_and_time_zones_formatting/test.cpp @@ -282,6 +282,10 @@ void test_duration_formatter() { assert(format(STR("{:%T %j}"), -days{4} - 23h - 30min) == STR("-119:30:00 4")); assert(format(STR("{:%T %j}"), duration{1.55f}) == STR("37:11:59 1")); assert(format(STR("{:%T %j}"), duration{-1.55f}) == STR("-37:11:59 1")); + + // GH-4247: : format() should accept %X and %EX for duration and hh_mm_ss + assert(format(STR("{:%X}"), 9h + 7min + 5s) == STR("09:07:05")); + assert(format(STR("{:%EX}"), 9h + 7min + 5s) == STR("09:07:05")); } template @@ -765,6 +769,10 @@ void test_hh_mm_ss_formatter() { assert(format(STR("{:%H}"), hh_mm_ss{24h}) == STR("24")); assert(format(STR("{:%H}"), hh_mm_ss{-24h}) == STR("-24")); assert(format(STR("{:%M %S}"), hh_mm_ss{27h + 12min + 30s}) == STR("12 30")); + + // GH-4247: : format() should accept %X and %EX for duration and hh_mm_ss + assert(format(STR("{:%X}"), hh_mm_ss{9h + 7min + 5s}) == STR("09:07:05")); + assert(format(STR("{:%EX}"), hh_mm_ss{9h + 7min + 5s}) == STR("09:07:05")); } void test_exception_classes() {