| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-timeout-factor 2 } |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <format> |
| 6 | #include <testsuite_hooks.h> |
| 7 | |
| 8 | void |
| 9 | test_ostream() |
| 10 | { |
| 11 | using std::format; |
| 12 | using namespace std::chrono; |
| 13 | |
| 14 | auto st = sys_days{2000y/January/1}; |
| 15 | auto tt = clock_cast<tai_clock>(st); |
| 16 | |
| 17 | auto s = format("{0:%F %T %Z} == {1:%F %T %Z}", st, tt); |
| 18 | VERIFY( s == "2000-01-01 00:00:00 UTC == 2000-01-01 00:00:32 TAI" ); |
| 19 | |
| 20 | s = std::format("{:=>21}", tt); |
| 21 | VERIFY( s == "==2000-01-01 00:00:32" ); |
| 22 | } |
| 23 | |
| 24 | void |
| 25 | test_format() |
| 26 | { |
| 27 | using std::format; |
| 28 | using namespace std::chrono; |
| 29 | |
| 30 | auto st = sys_days{2000y/January/1}; |
| 31 | auto tt = clock_cast<tai_clock>(st); |
| 32 | auto s = std::format("{}", tt); |
| 33 | VERIFY( s == "2000-01-01 00:00:32" ); |
| 34 | |
| 35 | // PR libstdc++/113500 |
| 36 | s = std::format("{}", tt + 150ms); |
| 37 | VERIFY( s == "2000-01-01 00:00:32.150" ); |
| 38 | |
| 39 | s = std::format("{:%Z %z %Ez %Oz}", tt); |
| 40 | VERIFY( s == "TAI +0000 +00:00 +00:00" ); |
| 41 | |
| 42 | s = std::format("{}", tai_seconds{}); |
| 43 | VERIFY( s == "1958-01-01 00:00:00" ); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | test_parse() |
| 48 | { |
| 49 | using namespace std::chrono; |
| 50 | const sys_seconds expected = sys_days(2023y/August/9) + 20h + 44min + 3s; |
| 51 | tai_seconds tp; |
| 52 | |
| 53 | minutes offset; |
| 54 | std::string abbrev; |
| 55 | std::istringstream is("8/9/23 214440 +1 BST#"); |
| 56 | VERIFY( is >> parse("%D %2H%2M%2S %Oz %Z", tp, abbrev, offset) ); |
| 57 | VERIFY( ! is.eof() ); |
| 58 | VERIFY( tp == clock_cast<tai_clock>(expected) ); |
| 59 | VERIFY( abbrev == "BST" ); |
| 60 | VERIFY( offset == 60min ); |
| 61 | |
| 62 | // Test round trip |
| 63 | std::stringstream ss; |
| 64 | ss << clock_cast<tai_clock>(expected) << " TAI 0123"; |
| 65 | VERIFY( ss >> parse("%F %T %Z %z", tp, abbrev, offset) ); |
| 66 | VERIFY( ! ss.eof() ); |
| 67 | VERIFY( (tp + offset) == clock_cast<tai_clock>(expected) ); |
| 68 | VERIFY( abbrev == "TAI" ); |
| 69 | VERIFY( offset == (1h + 23min) ); |
| 70 | |
| 71 | ss.str(""); |
| 72 | ss << tai_seconds{}; |
| 73 | VERIFY( ss >> parse("%F %T", tp) ); |
| 74 | VERIFY( tp.time_since_epoch() == 0s ); |
| 75 | } |
| 76 | |
| 77 | int main() |
| 78 | { |
| 79 | test_ostream(); |
| 80 | test_format(); |
| 81 | test_parse(); |
| 82 | } |