| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-timeout-factor 2 } |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <sstream> |
| 6 | #include <testsuite_hooks.h> |
| 7 | |
| 8 | void |
| 9 | test_ostream() |
| 10 | { |
| 11 | using std::ostringstream; |
| 12 | using std::chrono::hh_mm_ss; |
| 13 | using namespace std::chrono_literals; |
| 14 | |
| 15 | std::locale::global(std::locale::classic()); |
| 16 | |
| 17 | { |
| 18 | hh_mm_ss hms{-4083007ms}; |
| 19 | ostringstream out; |
| 20 | out << hms; |
| 21 | VERIFY( out.str() == "-01:08:03.007" ); |
| 22 | } |
| 23 | |
| 24 | { |
| 25 | hh_mm_ss hms{4083007ms}; |
| 26 | ostringstream out; |
| 27 | out << hms; |
| 28 | VERIFY( out.str() == "01:08:03.007" ); |
| 29 | } |
| 30 | |
| 31 | { |
| 32 | hh_mm_ss hms{65745123ms}; |
| 33 | ostringstream out; |
| 34 | out << hms; |
| 35 | VERIFY( out.str() == "18:15:45.123" ); |
| 36 | } |
| 37 | |
| 38 | { |
| 39 | ostringstream out; |
| 40 | out << hh_mm_ss{65745s}; |
| 41 | VERIFY( out.str() == "18:15:45" ); |
| 42 | } |
| 43 | |
| 44 | { |
| 45 | ostringstream out; |
| 46 | out << hh_mm_ss{0.020s}; |
| 47 | // hh_mm_ss<duration<long double>>::fractional_width == 0 so no subseconds: |
| 48 | VERIFY( out.str() == "00:00:00" ); |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | ostringstream out; |
| 53 | out << hh_mm_ss<std::chrono::duration<long double, std::nano>>{0.020s}; |
| 54 | // hh_mm_ss<duration<long double, nano>>::fractional_width == 9: |
| 55 | VERIFY( out.str() == "00:00:00.020000000" ); |
| 56 | } |
| 57 | |
| 58 | { |
| 59 | ostringstream out; |
| 60 | out << hh_mm_ss{65745s + 20ms}; |
| 61 | VERIFY( out.str() == "18:15:45.020" ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | test_format() |
| 67 | { |
| 68 | using namespace std::chrono; |
| 69 | |
| 70 | auto s = std::format("{}", hh_mm_ss{1h + 23min + 45s}); |
| 71 | VERIFY( s == "01:23:45" ); |
| 72 | s = std::format("{}", hh_mm_ss{-42min}); |
| 73 | VERIFY( s == "-00:42:00" ); |
| 74 | |
| 75 | auto ws = std::format(L"{}", hh_mm_ss{1h + 23min + 45s}); |
| 76 | VERIFY( ws == L"01:23:45" ); |
| 77 | ws = std::format(L"{}", hh_mm_ss{-42min}); |
| 78 | VERIFY( ws == L"-00:42:00" ); |
| 79 | |
| 80 | // Locale-specific formats: |
| 81 | auto loc = std::locale::classic(); |
| 82 | s = std::format(loc, "{:%r %OH:%OM:%OS}", hh_mm_ss{123456ms}); |
| 83 | VERIFY( s == "12:02:03 AM 00:02:03" ); |
| 84 | ws = std::format(loc, L"{:%r %OH:%OM:%OS}", hh_mm_ss{123456ms}); |
| 85 | VERIFY( ws == L"12:02:03 AM 00:02:03" ); |
| 86 | } |
| 87 | |
| 88 | int main() |
| 89 | { |
| 90 | test_ostream(); |
| 91 | test_format(); |
| 92 | } |