| 1 |
// { dg-do run { target c++20 } } |
| 2 |
// { dg-timeout-factor 2 } |
| 3 |
|
| 4 |
#include <chrono> |
| 5 |
#include <format> |
| 6 |
#include <sstream> |
| 7 |
#include <testsuite_hooks.h> |
| 8 |
|
| 9 |
void |
| 10 |
test_ostream() |
| 11 |
{ |
| 12 |
using std::format; |
| 13 |
using namespace std::chrono; |
| 14 |
|
| 15 |
auto st = sys_days{2000y/January/1}; |
| 16 |
auto gt = clock_cast<gps_clock>(st); |
| 17 |
|
| 18 |
auto s = format("{0:%F %T %Z} == {1:%F %T %Z}", st, gt); |
| 19 |
VERIFY( s == "2000-01-01 00:00:00 UTC == 2000-01-01 00:00:13 GPS" ); |
| 20 |
|
| 21 |
std::ostringstream ss; |
| 22 |
ss << gt; |
| 23 |
VERIFY( ss.str() == "2000-01-01 00:00:13" ); |
| 24 |
|
| 25 |
ss.str(""); |
| 26 |
ss.clear(); |
| 27 |
ss << (gps_time<duration<long double>>(gt) + 20ms); |
| 28 |
VERIFY( ss.str() == "2000-01-01 00:00:13.020" ); |
| 29 |
} |
| 30 |
|
| 31 |
void |
| 32 |
test_format() |
| 33 |
{ |
| 34 |
using std::format; |
| 35 |
using namespace std::chrono; |
| 36 |
|
| 37 |
auto st = sys_days{2000y/January/1}; |
| 38 |
auto gt = clock_cast<gps_clock>(st); |
| 39 |
auto s = std::format("{}", gt); |
| 40 |
VERIFY( s == "2000-01-01 00:00:13" ); |
| 41 |
|
| 42 |
// PR libstdc++/113500 |
| 43 |
s = std::format("{}", gt + 150ms + 10.5s); |
| 44 |
VERIFY( s == "2000-01-01 00:00:23.650" ); |
| 45 |
|
| 46 |
s = std::format("{:%Z %z %Ez %Oz}", gt); |
| 47 |
VERIFY( s == "GPS +0000 +00:00 +00:00" ); |
| 48 |
|
| 49 |
s = std::format("{}", gps_seconds{}); |
| 50 |
VERIFY( s == "1980-01-06 00:00:00" ); |
| 51 |
} |
| 52 |
|
| 53 |
void |
| 54 |
test_parse() |
| 55 |
{ |
| 56 |
using namespace std::chrono; |
| 57 |
const sys_seconds expected = sys_days(2023y/August/9) + 20h + 43min + 45s; |
| 58 |
gps_seconds tp; |
| 59 |
|
| 60 |
minutes offset; |
| 61 |
std::string abbrev; |
| 62 |
std::istringstream is("2023-8-9 21:44:3 +1 BST#"); |
| 63 |
VERIFY( is >> parse("%9F %T %Oz %Z", tp, abbrev, offset) ); |
| 64 |
VERIFY( ! is.eof() ); |
| 65 |
VERIFY( tp == clock_cast<gps_clock>(expected) ); |
| 66 |
VERIFY( abbrev == "BST" ); |
| 67 |
VERIFY( offset == 60min ); |
| 68 |
|
| 69 |
// Test round trip |
| 70 |
std::stringstream ss; |
| 71 |
ss << clock_cast<gps_clock>(expected) << " GPS -1234"; |
| 72 |
VERIFY( ss >> parse("%F %T %Z %z", tp, abbrev, offset) ); |
| 73 |
VERIFY( ! ss.eof() ); |
| 74 |
VERIFY( (tp + offset) == clock_cast<gps_clock>(expected) ); |
| 75 |
VERIFY( abbrev == "GPS" ); |
| 76 |
VERIFY( offset == -(12h + 34min) ); |
| 77 |
|
| 78 |
// Test rounding |
| 79 |
ss.clear(); |
| 80 |
ss.str("2224-09-06 23"); |
| 81 |
gps_time<days> d; |
| 82 |
ss >> parse("%F %H", d); // Should be truncated to start of day, not rounded. |
| 83 |
ss.str(""); |
| 84 |
ss << d; |
| 85 |
VERIFY( ss.str() == "2224-09-06 00:00:00" ); |
| 86 |
ss.str("1969-12-31 23"); |
| 87 |
ss >> parse("%F %H", d); // Should be truncated to start of day, not rounded. |
| 88 |
ss.str(""); |
| 89 |
ss << d; |
| 90 |
VERIFY( ss.str() == "1969-12-31 00:00:00" ); |
| 91 |
|
| 92 |
gps_time<duration<long, std::ratio<10>>> ds; // decaseconds |
| 93 |
ss.str("2224-09-06 15:07:06"); |
| 94 |
ss >> parse("%F %T", ds); // Should be rounded to nearest decasecond. |
| 95 |
ss << ds; |
| 96 |
VERIFY( ss.str() == "2224-09-06 15:07:10" ); |
| 97 |
|
| 98 |
ss.str(""); |
| 99 |
ss << gps_seconds{}; |
| 100 |
VERIFY( ss >> parse("%F %T", tp) ); |
| 101 |
VERIFY( tp.time_since_epoch() == 0s ); |
| 102 |
} |
| 103 |
|
| 104 |
int main() |
| 105 |
{ |
| 106 |
test_ostream(); |
| 107 |
test_format(); |
| 108 |
test_parse(); |
| 109 |
} |