Debian Codegraph @

libstdc++-v3/testsuite/std/time/clock/file/io.cc

raw
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 namespace std::chrono;
12
13 file_time<file_clock::duration> t = file_clock::now();
14 std::ostringstream ss1, ss2;
15 ss1 << floor<seconds>(t);
16 ss2 << floor<seconds>(clock_cast<system_clock>(t));
17 VERIFY( ss1.str() == ss2.str() );
18 }
19
20 void
21 test_format()
22 {
23 using namespace std::chrono;
24 auto t = file_clock::now();
25
26 auto s = std::format("{}", t);
27 std::ostringstream ss;
28 ss << t;
29 VERIFY( s == ss.str() );
30
31 // PR libstdc++/113500
32 auto ft = clock_cast<file_clock>(sys_days(2024y/January/21)) + 0ms + 2.5s;
33 s = std::format("{}", ft);
34 VERIFY( s == "2024-01-21 00:00:02.500");
35
36 const std::chrono::file_time<std::chrono::seconds> t0{};
37 s = std::format("{:%Z %z %Ez %Oz}", t0);
38 VERIFY( s == "UTC +0000 +00:00 +00:00" );
39
40 s = std::format("{}", t0);
41 // chrono::file_clock epoch is unspecified, so this is libstdc++-specific.
42 VERIFY( s == "2174-01-01 00:00:00" );
43 }
44
45 void
46 test_parse()
47 {
48 using namespace std::chrono;
49 const sys_seconds expected = sys_days(2023y/August/9) + 20h + 44min;
50 file_time<seconds> tp;
51
52 minutes offset;
53 std::string abbrev;
54 std::istringstream is("002023-08-09 21:44 +01 BST!");
55 VERIFY( is >> parse("%6F %R %z %Z", tp, abbrev, offset) );
56 VERIFY( ! is.eof() );
57 VERIFY( tp == clock_cast<file_clock>(expected) );
58 VERIFY( abbrev == "BST" );
59 VERIFY( offset == 60min );
60
61 // Test round trip
62 std::stringstream ss;
63 ss << clock_cast<file_clock>(expected) << " 0123456";
64 VERIFY( ss >> parse("%F %T %z%Z", tp, abbrev, offset) );
65 VERIFY( ss.eof() );
66 VERIFY( (tp + offset) == clock_cast<file_clock>(expected) );
67 VERIFY( abbrev == "456" );
68 VERIFY( offset == (1h + 23min) );
69
70 // Test rounding
71 ss.clear();
72 ss.str("2224-09-06 23");
73 file_time<days> d;
74 ss >> parse("%F %H", d); // Should be truncated to start of day, not rounded.
75 ss.str("");
76 ss << d;
77 VERIFY( ss.str() == "2224-09-06 00:00:00" );
78 ss.str("1969-12-31 23");
79 ss >> parse("%F %H", d); // Should be truncated to start of day, not rounded.
80 ss.str("");
81 ss << d;
82 VERIFY( ss.str() == "1969-12-31 00:00:00" );
83
84 file_time<duration<long, std::ratio<10>>> ds; // decaseconds
85 ss.str("2224-09-06 15:07:06");
86 ss >> parse("%F %T", ds); // Should be rounded to nearest decasecond.
87 ss << ds;
88 VERIFY( ss.str() == "2224-09-06 15:07:10" );
89
90 ss.str("");
91 ss << file_time<seconds>{};
92 VERIFY( ss >> parse("%F %T", tp) );
93 VERIFY( tp.time_since_epoch() == 0s );
94 }
95
96 int main()
97 {
98 test_ostream();
99 test_format();
100 test_parse();
101 }