xref: /llvm-project/libcxx/test/std/time/time.clock/time.clock.local/ostream.verify.cpp (revision 042a6a1349d512edaaa225380771c64a8d92810a)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 
13 // TODO FMT This test should not require std::to_chars(floating-point)
14 // XFAIL: availability-fp_to_chars-missing
15 
16 // <chrono>
17 
18 // class system_clock;
19 
20 // template<class charT, class traits, class Duration>
21 //   basic_ostream<charT, traits>&
22 //     operator<<(basic_ostream<charT, traits>& os, const local_time<Duration>& tp);
23 
24 // The function uses the system_clock which has two overloads
25 
26 // template<class charT, class traits, class Duration>
27 //   basic_ostream<charT, traits>&
28 //     operator<<(basic_ostream<charT, traits>& os, const sys_time<Duration>& tp);
29 // Constraints: treat_as_floating_point_v<typename Duration::rep> is false, and Duration{1} < days{1} is true.
30 
31 // template<class charT, class traits>
32 //   basic_ostream<charT, traits>&
33 //     operator<<(basic_ostream<charT, traits>& os, const sys_days& dp);
34 
35 // Note local_time's operator<< is specified a
36 //   Effects: os << sys_time<Duration>{lt.time_since_epoch()};
37 // since it uses Effects and not Effects: Equivalent to the constrains of
38 // sys_time do not apply to this operator. This means it's not possible to use
39 // a SFINAE test.
40 
41 #include <chrono>
42 #include <ratio>
43 #include <sstream>
44 #include <type_traits>
45 
test()46 void test() {
47   std::stringstream sstr;
48 
49   // floating-point values
50 
51   sstr << // expected-error@*:* {{invalid operands to binary expression}}
52       std::chrono::local_time<std::chrono::duration<float, std::ratio<1, 1>>>{
53           std::chrono::duration<float, std::ratio<1, 1>>{0}};
54 
55   sstr << // expected-error@*:* {{invalid operands to binary expression}}
56       std::chrono::local_time<std::chrono::duration<double, std::ratio<1, 1>>>{
57           std::chrono::duration<double, std::ratio<1, 1>>{0}};
58 
59   sstr << // expected-error@*:* {{invalid operands to binary expression}}
60       std::chrono::local_time<std::chrono::duration<long double, std::ratio<1, 1>>>{
61           std::chrono::duration<long double, std::ratio<1, 1>>{0}};
62 
63   // duration >= day
64 
65   sstr << // valid since day has its own formatter
66       std::chrono::local_days{std::chrono::days{0}};
67 
68   using rep = std::conditional_t<std::is_same_v<std::chrono::days::rep, int>, long, int>;
69   sstr << // a different rep does not matter,
70       std::chrono::local_time<std::chrono::duration<rep, std::ratio<86400>>>{
71           std::chrono::duration<rep, std::ratio<86400>>{0}};
72 
73   sstr << // expected-error@*:* {{invalid operands to binary expression}}
74       std::chrono::local_time<std::chrono::duration<typename std::chrono::days::rep, std::ratio<86401>>>{
75           std::chrono::duration<typename std::chrono::days::rep, std::ratio<86401>>{0}};
76 
77   sstr << // These are considered days.
78       std::chrono::local_time<std::chrono::weeks>{std::chrono::weeks{3}};
79 
80   sstr << // These too.
81       std::chrono::local_time<std::chrono::duration<rep, std::ratio<20 * 86400>>>{
82           std::chrono::duration<rep, std::ratio<20 * 86400>>{0}};
83 
84   sstr << // expected-error@*:* {{invalid operands to binary expression}}
85       std::chrono::local_time<std::chrono::months>{std::chrono::months{0}};
86 
87   sstr << // expected-error@*:* {{invalid operands to binary expression}}
88       std::chrono::local_time<std::chrono::years>{std::chrono::years{0}};
89 }
90