1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
13 // TODO FMT This test should not require std::to_chars(floating-point)
14 // XFAIL: availability-fp_to_chars-missing
16 // REQUIRES: locale.fr_FR.UTF-8
17 // REQUIRES: locale.ja_JP.UTF-8
21 // class system_clock;
23 // template<class charT, class traits, class Duration>
24 // basic_ostream<charT, traits>&
25 // operator<<(basic_ostream<charT, traits>& os, const sys_time<Duration>& tp);
32 #include "make_string.h"
33 #include "platform_support.h" // locale name macros
34 #include "test_macros.h"
36 #define SV(S) MAKE_STRING_VIEW(CharT, S)
38 // Modeled after the system clock's usings, this make adapating the
40 template <class Duration
>
41 using file_time
= std::chrono::time_point
<std::chrono::file_clock
, Duration
>;
42 using file_seconds
= file_time
<std::chrono::seconds
>;
43 using file_days
= file_time
<std::chrono::days
>;
45 template <class CharT
, class Duration
>
46 static std::basic_string
<CharT
> stream_c_locale(file_time
<Duration
> time_point
) {
47 std::basic_stringstream
<CharT
> sstr
;
48 sstr
<< std::fixed
<< time_point
;
52 template <class CharT
, class Duration
>
53 static std::basic_string
<CharT
> stream_fr_FR_locale(file_time
<Duration
> time_point
) {
54 std::basic_stringstream
<CharT
> sstr
;
55 const std::locale
locale(LOCALE_fr_FR_UTF_8
);
57 sstr
<< std::fixed
<< time_point
;
61 template <class CharT
, class Duration
>
62 static std::basic_string
<CharT
> stream_ja_JP_locale(file_time
<Duration
> time_point
) {
63 std::basic_stringstream
<CharT
> sstr
;
64 const std::locale
locale(LOCALE_ja_JP_UTF_8
);
66 sstr
<< std::fixed
<< time_point
;
70 template <class CharT
>
71 static void test_c() {
72 using namespace std::literals::chrono_literals
;
74 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-946'688'523'123'456'789ns
}) ==
75 SV("1940-01-01 22:57:56.876543211"));
77 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{-946'688'523'123'456us
}) ==
78 SV("1940-01-01 22:57:56.876544"));
80 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{-946'688'523'123ms
}) ==
81 SV("1940-01-01 22:57:56.877"));
83 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-1000000000ns
}) ==
84 SV("1969-12-31 23:59:59.000000000"));
86 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{-1000000us
}) ==
87 SV("1969-12-31 23:59:59.000000"));
89 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{-1000ms
}) ==
90 SV("1969-12-31 23:59:59.000"));
92 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-1ns
}) ==
93 SV("1969-12-31 23:59:59.999999999"));
95 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{0ns
}) ==
96 SV("1970-01-01 00:00:00.000000000"));
98 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{1ns
}) ==
99 SV("1970-01-01 00:00:00.000000001"));
101 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{1000000000ns
}) ==
102 SV("1970-01-01 00:00:01.000000000"));
104 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{1000000us
}) ==
105 SV("1970-01-01 00:00:01.000000"));
107 assert(stream_c_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{1000ms
}) ==
108 SV("1970-01-01 00:00:01.000"));
110 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::nanoseconds
>{946'688'523'123'456'789ns
}) ==
111 SV("2000-01-01 01:02:03.123456789"));
113 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::microseconds
>{946'688'523'123'456us
}) ==
114 SV("2000-01-01 01:02:03.123456"));
116 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::milliseconds
>{946'684'800'123ms
}) ==
117 SV("2000-01-01 00:00:00.123"));
118 assert(stream_c_locale
<CharT
>(file_seconds
{1'234'567'890s
}) == SV("2009-02-13 23:31:30"));
119 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::minutes
>{20'576'131min
}) == SV("2009-02-13 23:31:00"));
120 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::hours
>{342'935h
}) == SV("2009-02-13 23:00:00"));
121 assert(stream_c_locale
<CharT
>(file_days
{std::chrono::days
{14'288}}) == SV("2009-02-13 00:00:00"));
122 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::weeks
>{std::chrono::weeks
{2041}}) == SV("2009-02-12 00:00:00"));
124 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::duration
<signed char, std::ratio
<2, 1>>>{
125 std::chrono::duration
<signed char, std::ratio
<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));
126 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::duration
<short, std::ratio
<1, 2>>>{
127 std::chrono::duration
<short, std::ratio
<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0"));
128 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::duration
<int, std::ratio
<1, 4>>>{
129 std::chrono::duration
<int, std::ratio
<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00"));
130 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::duration
<long, std::ratio
<1, 10>>>{
131 std::chrono::duration
<long, std::ratio
<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1"));
132 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::duration
<long long, std::ratio
<1, 100>>>{
133 std::chrono::duration
<long long, std::ratio
<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30.10"));
135 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::duration
<float, std::ratio
<1, 1>>>{
136 std::chrono::duration
<float, std::ratio
<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));
137 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::duration
<double, std::ratio
<1, 10>>>{
138 std::chrono::duration
<double, std::ratio
<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12.3"));
139 assert(stream_c_locale
<CharT
>(file_time
<std::chrono::duration
<long double, std::ratio
<1, 100>>>{
140 std::chrono::duration
<long double, std::ratio
<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01.23"));
143 template <class CharT
>
144 static void test_fr_FR() {
145 using namespace std::literals::chrono_literals
;
147 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-946'688'523'123'456'789ns
}) ==
148 SV("1940-01-01 22:57:56,876543211"));
150 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{-946'688'523'123'456us
}) ==
151 SV("1940-01-01 22:57:56,876544"));
153 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{-946'688'523'123ms
}) ==
154 SV("1940-01-01 22:57:56,877"));
156 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-1000000000ns
}) ==
157 SV("1969-12-31 23:59:59,000000000"));
159 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{-1000000us
}) ==
160 SV("1969-12-31 23:59:59,000000"));
162 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{-1000ms
}) ==
163 SV("1969-12-31 23:59:59,000"));
165 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-1ns
}) ==
166 SV("1969-12-31 23:59:59,999999999"));
168 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{0ns
}) ==
169 SV("1970-01-01 00:00:00,000000000"));
171 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{1ns
}) ==
172 SV("1970-01-01 00:00:00,000000001"));
174 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{1000000000ns
}) ==
175 SV("1970-01-01 00:00:01,000000000"));
177 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{1000000us
}) ==
178 SV("1970-01-01 00:00:01,000000"));
180 assert(stream_fr_FR_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{1000ms
}) ==
181 SV("1970-01-01 00:00:01,000"));
183 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::nanoseconds
>{946'688'523'123'456'789ns
}) ==
184 SV("2000-01-01 01:02:03,123456789"));
185 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::microseconds
>{946'688'523'123'456us
}) ==
186 SV("2000-01-01 01:02:03,123456"));
188 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::milliseconds
>{946'684'800'123ms
}) ==
189 SV("2000-01-01 00:00:00,123"));
190 assert(stream_fr_FR_locale
<CharT
>(file_seconds
{1'234'567'890s
}) == SV("2009-02-13 23:31:30"));
191 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::minutes
>{20'576'131min
}) == SV("2009-02-13 23:31:00"));
192 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::hours
>{342'935h
}) == SV("2009-02-13 23:00:00"));
193 assert(stream_fr_FR_locale
<CharT
>(file_days
{std::chrono::days
{14'288}}) == SV("2009-02-13 00:00:00"));
194 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::weeks
>{std::chrono::weeks
{2041}}) ==
195 SV("2009-02-12 00:00:00"));
197 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::duration
<signed char, std::ratio
<2, 1>>>{
198 std::chrono::duration
<signed char, std::ratio
<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));
199 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::duration
<short, std::ratio
<1, 2>>>{
200 std::chrono::duration
<short, std::ratio
<1, 2>>{3600}}) == SV("1970-01-01 00:30:00,0"));
201 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::duration
<int, std::ratio
<1, 4>>>{
202 std::chrono::duration
<int, std::ratio
<1, 4>>{3600}}) == SV("1970-01-01 00:15:00,00"));
203 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::duration
<long, std::ratio
<1, 10>>>{
204 std::chrono::duration
<long, std::ratio
<1, 10>>{36611}}) == SV("1970-01-01 01:01:01,1"));
205 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::duration
<long long, std::ratio
<1, 100>>>{
206 std::chrono::duration
<long long, std::ratio
<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30,10"));
208 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::duration
<float, std::ratio
<1, 1>>>{
209 std::chrono::duration
<float, std::ratio
<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));
210 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::duration
<double, std::ratio
<1, 10>>>{
211 std::chrono::duration
<double, std::ratio
<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12,3"));
212 assert(stream_fr_FR_locale
<CharT
>(file_time
<std::chrono::duration
<long double, std::ratio
<1, 100>>>{
213 std::chrono::duration
<long double, std::ratio
<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01,23"));
216 template <class CharT
>
217 static void test_ja_JP() {
218 using namespace std::literals::chrono_literals
;
220 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-946'688'523'123'456'789ns
}) ==
221 SV("1940-01-01 22:57:56.876543211"));
223 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{-946'688'523'123'456us
}) ==
224 SV("1940-01-01 22:57:56.876544"));
226 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{-946'688'523'123ms
}) ==
227 SV("1940-01-01 22:57:56.877"));
229 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-1000000000ns
}) ==
230 SV("1969-12-31 23:59:59.000000000"));
232 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{-1000000us
}) ==
233 SV("1969-12-31 23:59:59.000000"));
235 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{-1000ms
}) ==
236 SV("1969-12-31 23:59:59.000"));
238 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{-1ns
}) ==
239 SV("1969-12-31 23:59:59.999999999"));
241 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{0ns
}) ==
242 SV("1970-01-01 00:00:00.000000000"));
244 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{1ns
}) ==
245 SV("1970-01-01 00:00:00.000000001"));
247 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::nanoseconds
>{1000000000ns
}) ==
248 SV("1970-01-01 00:00:01.000000000"));
250 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::microseconds
>{1000000us
}) ==
251 SV("1970-01-01 00:00:01.000000"));
253 assert(stream_ja_JP_locale
<CharT
>(std::chrono::file_time
<std::chrono::milliseconds
>{1000ms
}) ==
254 SV("1970-01-01 00:00:01.000"));
256 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::nanoseconds
>{946'688'523'123'456'789ns
}) ==
257 SV("2000-01-01 01:02:03.123456789"));
258 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::microseconds
>{946'688'523'123'456us
}) ==
259 SV("2000-01-01 01:02:03.123456"));
261 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::milliseconds
>{946'684'800'123ms
}) ==
262 SV("2000-01-01 00:00:00.123"));
263 assert(stream_ja_JP_locale
<CharT
>(file_seconds
{1'234'567'890s
}) == SV("2009-02-13 23:31:30"));
264 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::minutes
>{20'576'131min
}) == SV("2009-02-13 23:31:00"));
265 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::hours
>{342'935h
}) == SV("2009-02-13 23:00:00"));
266 assert(stream_ja_JP_locale
<CharT
>(file_days
{std::chrono::days
{14'288}}) == SV("2009-02-13 00:00:00"));
267 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::weeks
>{std::chrono::weeks
{2041}}) ==
268 SV("2009-02-12 00:00:00"));
270 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::duration
<signed char, std::ratio
<2, 1>>>{
271 std::chrono::duration
<signed char, std::ratio
<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));
272 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::duration
<short, std::ratio
<1, 2>>>{
273 std::chrono::duration
<short, std::ratio
<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0"));
274 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::duration
<int, std::ratio
<1, 4>>>{
275 std::chrono::duration
<int, std::ratio
<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00"));
276 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::duration
<long, std::ratio
<1, 10>>>{
277 std::chrono::duration
<long, std::ratio
<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1"));
278 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::duration
<long long, std::ratio
<1, 100>>>{
279 std::chrono::duration
<long long, std::ratio
<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30.10"));
281 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::duration
<float, std::ratio
<1, 1>>>{
282 std::chrono::duration
<float, std::ratio
<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));
283 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::duration
<double, std::ratio
<1, 10>>>{
284 std::chrono::duration
<double, std::ratio
<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12.3"));
285 assert(stream_ja_JP_locale
<CharT
>(file_time
<std::chrono::duration
<long double, std::ratio
<1, 100>>>{
286 std::chrono::duration
<long double, std::ratio
<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01.23"));
289 template <class CharT
>
296 int main(int, char**) {
299 #ifndef TEST_HAS_NO_WIDE_CHARACTERS