Mercurial > minori
comparison dep/fmt/test/chrono-test.cc @ 343:1faa72660932
*: transfer back to cmake from autotools
autotools just made lots of things more complicated than
they should have and many things broke (i.e. translations)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 20 Jun 2024 05:56:06 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
342:adb79bdde329 | 343:1faa72660932 |
---|---|
1 // Formatting library for C++ - time formatting tests | |
2 // | |
3 // Copyright (c) 2012 - present, Victor Zverovich | |
4 // All rights reserved. | |
5 // | |
6 // For the license information refer to format.h. | |
7 | |
8 #include "fmt/chrono.h" | |
9 | |
10 #include <algorithm> | |
11 #include <ctime> | |
12 #include <vector> | |
13 | |
14 #include "gtest-extra.h" // EXPECT_THROW_MSG | |
15 #include "util.h" // get_locale | |
16 | |
17 using fmt::runtime; | |
18 using testing::Contains; | |
19 | |
20 template <typename Duration> | |
21 using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>; | |
22 | |
23 #if defined(__MINGW32__) && !defined(_UCRT) | |
24 // Only C89 conversion specifiers when using MSVCRT instead of UCRT | |
25 # define FMT_HAS_C99_STRFTIME 0 | |
26 #else | |
27 # define FMT_HAS_C99_STRFTIME 1 | |
28 #endif | |
29 | |
30 #if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L | |
31 using days = std::chrono::days; | |
32 #else | |
33 using days = std::chrono::duration<std::chrono::hours::rep, std::ratio<86400>>; | |
34 #endif | |
35 | |
36 auto make_tm() -> std::tm { | |
37 auto time = std::tm(); | |
38 time.tm_mday = 1; | |
39 return time; | |
40 } | |
41 | |
42 auto make_hour(int h) -> std::tm { | |
43 auto time = make_tm(); | |
44 time.tm_hour = h; | |
45 return time; | |
46 } | |
47 | |
48 auto make_minute(int m) -> std::tm { | |
49 auto time = make_tm(); | |
50 time.tm_min = m; | |
51 return time; | |
52 } | |
53 | |
54 auto make_second(int s) -> std::tm { | |
55 auto time = make_tm(); | |
56 time.tm_sec = s; | |
57 return time; | |
58 } | |
59 | |
60 std::string system_strftime(const std::string& format, const std::tm* timeptr, | |
61 std::locale* locptr = nullptr) { | |
62 auto loc = locptr ? *locptr : std::locale::classic(); | |
63 auto& facet = std::use_facet<std::time_put<char>>(loc); | |
64 std::ostringstream os; | |
65 os.imbue(loc); | |
66 facet.put(os, os, ' ', timeptr, format.c_str(), | |
67 format.c_str() + format.size()); | |
68 #ifdef _WIN32 | |
69 // Workaround a bug in older versions of Universal CRT. | |
70 auto str = os.str(); | |
71 if (str == "-0000") str = "+0000"; | |
72 return str; | |
73 #else | |
74 return os.str(); | |
75 #endif | |
76 } | |
77 | |
78 FMT_CONSTEXPR std::tm make_tm(int year, int mon, int mday, int hour, int min, | |
79 int sec) { | |
80 auto tm = std::tm(); | |
81 tm.tm_sec = sec; | |
82 tm.tm_min = min; | |
83 tm.tm_hour = hour; | |
84 tm.tm_mday = mday; | |
85 tm.tm_mon = mon - 1; | |
86 tm.tm_year = year - 1900; | |
87 return tm; | |
88 } | |
89 | |
90 TEST(chrono_test, format_tm) { | |
91 auto tm = std::tm(); | |
92 tm.tm_year = 116; | |
93 tm.tm_mon = 3; | |
94 tm.tm_mday = 25; | |
95 tm.tm_hour = 11; | |
96 tm.tm_min = 22; | |
97 tm.tm_sec = 33; | |
98 EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm), | |
99 "The date is 2016-04-25 11:22:33."); | |
100 EXPECT_EQ(fmt::format("{:%Y}", tm), "2016"); | |
101 EXPECT_EQ(fmt::format("{:%C}", tm), "20"); | |
102 EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm)); | |
103 EXPECT_EQ(fmt::format("{:%e}", tm), "25"); | |
104 EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/16"); | |
105 EXPECT_EQ(fmt::format("{:%F}", tm), "2016-04-25"); | |
106 EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33"); | |
107 | |
108 // Short year | |
109 tm.tm_year = 999 - 1900; | |
110 tm.tm_mon = 0; // for %G | |
111 tm.tm_mday = 2; // for %G | |
112 tm.tm_wday = 3; // for %G | |
113 tm.tm_yday = 1; // for %G | |
114 EXPECT_EQ(fmt::format("{:%Y}", tm), "0999"); | |
115 EXPECT_EQ(fmt::format("{:%C%y}", tm), "0999"); | |
116 EXPECT_EQ(fmt::format("{:%G}", tm), "0999"); | |
117 | |
118 tm.tm_year = 27 - 1900; | |
119 EXPECT_EQ(fmt::format("{:%Y}", tm), "0027"); | |
120 EXPECT_EQ(fmt::format("{:%C%y}", tm), "0027"); | |
121 | |
122 // Overflow year | |
123 tm.tm_year = 2147483647; | |
124 EXPECT_EQ(fmt::format("{:%Y}", tm), "2147485547"); | |
125 | |
126 tm.tm_year = -2147483648; | |
127 EXPECT_EQ(fmt::format("{:%Y}", tm), "-2147481748"); | |
128 | |
129 // for week on the year | |
130 // https://www.cl.cam.ac.uk/~mgk25/iso-time.html | |
131 std::vector<std::tm> tm_list = { | |
132 make_tm(1975, 12, 29, 12, 14, 16), // W01 | |
133 make_tm(1977, 1, 2, 12, 14, 16), // W53 | |
134 make_tm(1999, 12, 27, 12, 14, 16), // W52 | |
135 make_tm(1999, 12, 31, 12, 14, 16), // W52 | |
136 make_tm(2000, 1, 1, 12, 14, 16), // W52 | |
137 make_tm(2000, 1, 2, 12, 14, 16), // W52 | |
138 make_tm(2000, 1, 3, 12, 14, 16) // W1 | |
139 }; | |
140 | |
141 #if !FMT_HAS_C99_STRFTIME | |
142 GTEST_SKIP() << "Skip the rest of this test because it relies on strftime() " | |
143 "conforming to C99, but on this platform, MINGW + MSVCRT, " | |
144 "the function conforms only to C89."; | |
145 #endif | |
146 | |
147 const std::string iso_week_spec = "%Y-%m-%d: %G %g %V"; | |
148 for (auto ctm : tm_list) { | |
149 // Calculate tm_yday, tm_wday, etc. | |
150 std::time_t t = std::mktime(&ctm); | |
151 tm = *std::localtime(&t); | |
152 | |
153 auto fmt_spec = fmt::format("{{:{}}}", iso_week_spec); | |
154 EXPECT_EQ(system_strftime(iso_week_spec, &tm), | |
155 fmt::format(fmt::runtime(fmt_spec), tm)); | |
156 } | |
157 | |
158 // Every day from 1970-01-01 | |
159 std::time_t time_now = std::time(nullptr); | |
160 for (std::time_t t = 6 * 3600; t < time_now; t += 86400) { | |
161 tm = *std::localtime(&t); | |
162 | |
163 auto fmt_spec = fmt::format("{{:{}}}", iso_week_spec); | |
164 EXPECT_EQ(system_strftime(iso_week_spec, &tm), | |
165 fmt::format(fmt::runtime(fmt_spec), tm)); | |
166 } | |
167 } | |
168 | |
169 // MSVC: | |
170 // minkernel\crts\ucrt\src\appcrt\time\wcsftime.cpp(971) : Assertion failed: | |
171 // timeptr->tm_year >= -1900 && timeptr->tm_year <= 8099 | |
172 #ifndef _WIN32 | |
173 TEST(chrono_test, format_tm_future) { | |
174 auto tm = std::tm(); | |
175 tm.tm_year = 10445; // 10000+ years | |
176 tm.tm_mon = 3; | |
177 tm.tm_mday = 25; | |
178 tm.tm_hour = 11; | |
179 tm.tm_min = 22; | |
180 tm.tm_sec = 33; | |
181 EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm), | |
182 "The date is 12345-04-25 11:22:33."); | |
183 EXPECT_EQ(fmt::format("{:%Y}", tm), "12345"); | |
184 EXPECT_EQ(fmt::format("{:%C}", tm), "123"); | |
185 EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm)); | |
186 EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/45"); | |
187 EXPECT_EQ(fmt::format("{:%F}", tm), "12345-04-25"); | |
188 EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33"); | |
189 } | |
190 | |
191 TEST(chrono_test, format_tm_past) { | |
192 auto tm = std::tm(); | |
193 tm.tm_year = -2001; | |
194 tm.tm_mon = 3; | |
195 tm.tm_mday = 25; | |
196 tm.tm_hour = 11; | |
197 tm.tm_min = 22; | |
198 tm.tm_sec = 33; | |
199 EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm), | |
200 "The date is -101-04-25 11:22:33."); | |
201 EXPECT_EQ(fmt::format("{:%Y}", tm), "-101"); | |
202 | |
203 // macOS %C - "-1" | |
204 // Linux %C - "-2" | |
205 // fmt %C - "-1" | |
206 EXPECT_EQ(fmt::format("{:%C}", tm), "-1"); | |
207 EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm)); | |
208 | |
209 // macOS %D - "04/25/01" (%y) | |
210 // Linux %D - "04/25/99" (%y) | |
211 // fmt %D - "04/25/01" (%y) | |
212 EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/01"); | |
213 | |
214 EXPECT_EQ(fmt::format("{:%F}", tm), "-101-04-25"); | |
215 EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33"); | |
216 | |
217 tm.tm_year = -1901; // -1 | |
218 EXPECT_EQ(fmt::format("{:%Y}", tm), "-001"); | |
219 EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm)); | |
220 | |
221 tm.tm_year = -1911; // -11 | |
222 EXPECT_EQ(fmt::format("{:%Y}", tm), "-011"); | |
223 EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm)); | |
224 } | |
225 #endif | |
226 | |
227 TEST(chrono_test, grow_buffer) { | |
228 auto s = std::string("{:"); | |
229 for (int i = 0; i < 30; ++i) s += "%c"; | |
230 s += "}\n"; | |
231 auto t = std::time(nullptr); | |
232 (void)fmt::format(fmt::runtime(s), *std::localtime(&t)); | |
233 } | |
234 | |
235 TEST(chrono_test, format_to_empty_container) { | |
236 auto time = std::tm(); | |
237 time.tm_sec = 42; | |
238 auto s = std::string(); | |
239 fmt::format_to(std::back_inserter(s), "{:%S}", time); | |
240 EXPECT_EQ(s, "42"); | |
241 } | |
242 | |
243 TEST(chrono_test, empty_result) { EXPECT_EQ(fmt::format("{}", std::tm()), ""); } | |
244 | |
245 auto equal(const std::tm& lhs, const std::tm& rhs) -> bool { | |
246 return lhs.tm_sec == rhs.tm_sec && lhs.tm_min == rhs.tm_min && | |
247 lhs.tm_hour == rhs.tm_hour && lhs.tm_mday == rhs.tm_mday && | |
248 lhs.tm_mon == rhs.tm_mon && lhs.tm_year == rhs.tm_year && | |
249 lhs.tm_wday == rhs.tm_wday && lhs.tm_yday == rhs.tm_yday && | |
250 lhs.tm_isdst == rhs.tm_isdst; | |
251 } | |
252 | |
253 TEST(chrono_test, gmtime) { | |
254 auto t = std::time(nullptr); | |
255 auto tm = *std::gmtime(&t); | |
256 EXPECT_TRUE(equal(tm, fmt::gmtime(t))); | |
257 } | |
258 | |
259 template <typename TimePoint> | |
260 auto strftime_full_utc(TimePoint tp) -> std::string { | |
261 auto t = std::chrono::system_clock::to_time_t(tp); | |
262 auto tm = *std::gmtime(&t); | |
263 return system_strftime("%Y-%m-%d %H:%M:%S", &tm); | |
264 } | |
265 | |
266 TEST(chrono_test, system_clock_time_point) { | |
267 auto t1 = std::chrono::time_point_cast<std::chrono::seconds>( | |
268 std::chrono::system_clock::now()); | |
269 EXPECT_EQ(strftime_full_utc(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1)); | |
270 EXPECT_EQ(strftime_full_utc(t1), fmt::format("{}", t1)); | |
271 EXPECT_EQ(strftime_full_utc(t1), fmt::format("{:}", t1)); | |
272 | |
273 auto t2 = sys_time<std::chrono::seconds>(std::chrono::seconds(42)); | |
274 EXPECT_EQ(strftime_full_utc(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2)); | |
275 | |
276 std::vector<std::string> spec_list = { | |
277 "%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C", | |
278 "%EC", "%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U", | |
279 "%OU", "%W", "%OW", "%V", "%OV", "%j", "%d", "%Od", "%e", | |
280 "%Oe", "%a", "%A", "%w", "%Ow", "%u", "%Ou", "%H", "%OH", | |
281 "%I", "%OI", "%M", "%OM", "%S", "%OS", "%x", "%Ex", "%X", | |
282 "%EX", "%D", "%F", "%R", "%T", "%p"}; | |
283 #ifndef _WIN32 | |
284 // Disabled on Windows because these formats are not consistent among | |
285 // platforms. | |
286 spec_list.insert(spec_list.end(), {"%c", "%Ec", "%r"}); | |
287 #elif !FMT_HAS_C99_STRFTIME | |
288 // Only C89 conversion specifiers when using MSVCRT instead of UCRT | |
289 spec_list = {"%%", "%Y", "%y", "%b", "%B", "%m", "%U", "%W", "%j", "%d", | |
290 "%a", "%A", "%w", "%H", "%I", "%M", "%S", "%x", "%X", "%p"}; | |
291 #endif | |
292 spec_list.push_back("%Y-%m-%d %H:%M:%S"); | |
293 | |
294 for (const auto& spec : spec_list) { | |
295 auto t = std::chrono::system_clock::to_time_t(t1); | |
296 auto tm = *std::gmtime(&t); | |
297 | |
298 auto sys_output = system_strftime(spec, &tm); | |
299 | |
300 auto fmt_spec = fmt::format("{{:{}}}", spec); | |
301 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1)); | |
302 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm)); | |
303 } | |
304 | |
305 // Timezone formatters tests makes sense for localtime. | |
306 #if FMT_HAS_C99_STRFTIME | |
307 spec_list = {"%z", "%Z"}; | |
308 #else | |
309 spec_list = {"%Z"}; | |
310 #endif | |
311 for (const auto& spec : spec_list) { | |
312 auto t = std::chrono::system_clock::to_time_t(t1); | |
313 auto tm = *std::localtime(&t); | |
314 | |
315 auto sys_output = system_strftime(spec, &tm); | |
316 | |
317 auto fmt_spec = fmt::format("{{:{}}}", spec); | |
318 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm)); | |
319 | |
320 if (spec == "%z") { | |
321 sys_output.insert(sys_output.end() - 2, 1, ':'); | |
322 EXPECT_EQ(sys_output, fmt::format("{:%Ez}", tm)); | |
323 EXPECT_EQ(sys_output, fmt::format("{:%Oz}", tm)); | |
324 } | |
325 } | |
326 | |
327 // Separate tests for UTC, since std::time_put can use local time and ignoring | |
328 // the timezone in std::tm (if it presents on platform). | |
329 if (fmt::detail::has_member_data_tm_zone<std::tm>::value) { | |
330 auto t = std::chrono::system_clock::to_time_t(t1); | |
331 auto tm = *std::gmtime(&t); | |
332 | |
333 std::vector<std::string> tz_names = {"GMT", "UTC"}; | |
334 EXPECT_THAT(tz_names, Contains(fmt::format("{:%Z}", t1))); | |
335 EXPECT_THAT(tz_names, Contains(fmt::format("{:%Z}", tm))); | |
336 } | |
337 | |
338 if (fmt::detail::has_member_data_tm_gmtoff<std::tm>::value) { | |
339 auto t = std::chrono::system_clock::to_time_t(t1); | |
340 auto tm = *std::gmtime(&t); | |
341 | |
342 EXPECT_EQ(fmt::format("{:%z}", t1), "+0000"); | |
343 EXPECT_EQ(fmt::format("{:%z}", tm), "+0000"); | |
344 | |
345 EXPECT_EQ(fmt::format("{:%Ez}", t1), "+00:00"); | |
346 EXPECT_EQ(fmt::format("{:%Ez}", tm), "+00:00"); | |
347 | |
348 EXPECT_EQ(fmt::format("{:%Oz}", t1), "+00:00"); | |
349 EXPECT_EQ(fmt::format("{:%Oz}", tm), "+00:00"); | |
350 } | |
351 } | |
352 | |
353 #if FMT_USE_LOCAL_TIME | |
354 | |
355 TEST(chrono_test, localtime) { | |
356 auto t = std::time(nullptr); | |
357 auto tm = *std::localtime(&t); | |
358 EXPECT_TRUE(equal(tm, fmt::localtime(t))); | |
359 } | |
360 | |
361 template <typename Duration> | |
362 auto strftime_full_local(std::chrono::local_time<Duration> tp) -> std::string { | |
363 auto t = std::chrono::system_clock::to_time_t( | |
364 std::chrono::current_zone()->to_sys(tp)); | |
365 auto tm = *std::localtime(&t); | |
366 return system_strftime("%Y-%m-%d %H:%M:%S", &tm); | |
367 } | |
368 | |
369 TEST(chrono_test, local_system_clock_time_point) { | |
370 # ifdef _WIN32 | |
371 return; // Not supported on Windows. | |
372 # endif | |
373 auto t1 = std::chrono::time_point_cast<std::chrono::seconds>( | |
374 std::chrono::current_zone()->to_local(std::chrono::system_clock::now())); | |
375 EXPECT_EQ(strftime_full_local(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1)); | |
376 EXPECT_EQ(strftime_full_local(t1), fmt::format("{}", t1)); | |
377 EXPECT_EQ(strftime_full_local(t1), fmt::format("{:}", t1)); | |
378 using time_point = std::chrono::local_time<std::chrono::seconds>; | |
379 auto t2 = time_point(std::chrono::seconds(86400 + 42)); | |
380 EXPECT_EQ(strftime_full_local(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2)); | |
381 | |
382 std::vector<std::string> spec_list = { | |
383 "%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C", | |
384 "%EC", "%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U", | |
385 "%OU", "%W", "%OW", "%V", "%OV", "%j", "%d", "%Od", "%e", | |
386 "%Oe", "%a", "%A", "%w", "%Ow", "%u", "%Ou", "%H", "%OH", | |
387 "%I", "%OI", "%M", "%OM", "%S", "%OS", "%x", "%Ex", "%X", | |
388 "%EX", "%D", "%F", "%R", "%T", "%p", "%z", "%Z"}; | |
389 # ifndef _WIN32 | |
390 // Disabled on Windows because these formats are not consistent among | |
391 // platforms. | |
392 spec_list.insert(spec_list.end(), {"%c", "%Ec", "%r"}); | |
393 # elif !FMT_HAS_C99_STRFTIME | |
394 // Only C89 conversion specifiers when using MSVCRT instead of UCRT | |
395 spec_list = {"%%", "%Y", "%y", "%b", "%B", "%m", "%U", "%W", "%j", "%d", "%a", | |
396 "%A", "%w", "%H", "%I", "%M", "%S", "%x", "%X", "%p", "%Z"}; | |
397 # endif | |
398 spec_list.push_back("%Y-%m-%d %H:%M:%S"); | |
399 | |
400 for (const auto& spec : spec_list) { | |
401 auto t = std::chrono::system_clock::to_time_t( | |
402 std::chrono::current_zone()->to_sys(t1)); | |
403 auto tm = *std::localtime(&t); | |
404 | |
405 auto sys_output = system_strftime(spec, &tm); | |
406 | |
407 auto fmt_spec = fmt::format("{{:{}}}", spec); | |
408 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1)); | |
409 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm)); | |
410 } | |
411 | |
412 if (std::find(spec_list.cbegin(), spec_list.cend(), "%z") != | |
413 spec_list.cend()) { | |
414 auto t = std::chrono::system_clock::to_time_t( | |
415 std::chrono::current_zone()->to_sys(t1)); | |
416 auto tm = *std::localtime(&t); | |
417 | |
418 auto sys_output = system_strftime("%z", &tm); | |
419 sys_output.insert(sys_output.end() - 2, 1, ':'); | |
420 | |
421 EXPECT_EQ(sys_output, fmt::format("{:%Ez}", t1)); | |
422 EXPECT_EQ(sys_output, fmt::format("{:%Ez}", tm)); | |
423 | |
424 EXPECT_EQ(sys_output, fmt::format("{:%Oz}", t1)); | |
425 EXPECT_EQ(sys_output, fmt::format("{:%Oz}", tm)); | |
426 } | |
427 } | |
428 | |
429 #endif // FMT_USE_LOCAL_TIME | |
430 | |
431 #ifndef FMT_STATIC_THOUSANDS_SEPARATOR | |
432 | |
433 TEST(chrono_test, format_default) { | |
434 EXPECT_EQ(fmt::format("{}", std::chrono::seconds(42)), "42s"); | |
435 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::atto>(42)), | |
436 "42as"); | |
437 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::femto>(42)), | |
438 "42fs"); | |
439 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::pico>(42)), | |
440 "42ps"); | |
441 EXPECT_EQ(fmt::format("{}", std::chrono::nanoseconds(42)), "42ns"); | |
442 EXPECT_EQ(fmt::format("{}", std::chrono::microseconds(42)), "42µs"); | |
443 EXPECT_EQ(fmt::format("{}", std::chrono::milliseconds(42)), "42ms"); | |
444 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::centi>(42)), | |
445 "42cs"); | |
446 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::deci>(42)), | |
447 "42ds"); | |
448 EXPECT_EQ(fmt::format("{}", std::chrono::seconds(42)), "42s"); | |
449 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::deca>(42)), | |
450 "42das"); | |
451 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::hecto>(42)), | |
452 "42hs"); | |
453 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::kilo>(42)), | |
454 "42ks"); | |
455 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::mega>(42)), | |
456 "42Ms"); | |
457 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::giga>(42)), | |
458 "42Gs"); | |
459 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::tera>(42)), | |
460 "42Ts"); | |
461 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::peta>(42)), | |
462 "42Ps"); | |
463 EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::exa>(42)), | |
464 "42Es"); | |
465 EXPECT_EQ(fmt::format("{}", std::chrono::minutes(42)), "42min"); | |
466 EXPECT_EQ(fmt::format("{}", std::chrono::hours(42)), "42h"); | |
467 EXPECT_EQ(fmt::format("{}", days(42)), "42d"); | |
468 EXPECT_EQ( | |
469 fmt::format("{}", std::chrono::duration<int, std::ratio<15, 1>>(42)), | |
470 "42[15]s"); | |
471 EXPECT_EQ( | |
472 fmt::format("{}", std::chrono::duration<int, std::ratio<15, 4>>(42)), | |
473 "42[15/4]s"); | |
474 } | |
475 | |
476 TEST(chrono_test, duration_align) { | |
477 auto s = std::chrono::seconds(42); | |
478 EXPECT_EQ(fmt::format("{:5}", s), "42s "); | |
479 EXPECT_EQ(fmt::format("{:{}}", s, 5), "42s "); | |
480 EXPECT_EQ(fmt::format("{:>5}", s), " 42s"); | |
481 EXPECT_EQ(fmt::format("{:*^7}", s), "**42s**"); | |
482 EXPECT_EQ(fmt::format("{:12%H:%M:%S}", std::chrono::seconds(12345)), | |
483 "03:25:45 "); | |
484 EXPECT_EQ(fmt::format("{:>12%H:%M:%S}", std::chrono::seconds(12345)), | |
485 " 03:25:45"); | |
486 EXPECT_EQ(fmt::format("{:~^12%H:%M:%S}", std::chrono::seconds(12345)), | |
487 "~~03:25:45~~"); | |
488 EXPECT_EQ(fmt::format("{:{}%H:%M:%S}", std::chrono::seconds(12345), 12), | |
489 "03:25:45 "); | |
490 } | |
491 | |
492 TEST(chrono_test, tm_align) { | |
493 auto t = make_tm(1975, 12, 29, 12, 14, 16); | |
494 EXPECT_EQ(fmt::format("{:%F %T}", t), "1975-12-29 12:14:16"); | |
495 EXPECT_EQ(fmt::format("{:30%F %T}", t), "1975-12-29 12:14:16 "); | |
496 EXPECT_EQ(fmt::format("{:{}%F %T}", t, 30), "1975-12-29 12:14:16 "); | |
497 EXPECT_EQ(fmt::format("{:<30%F %T}", t), "1975-12-29 12:14:16 "); | |
498 EXPECT_EQ(fmt::format("{:^30%F %T}", t), " 1975-12-29 12:14:16 "); | |
499 EXPECT_EQ(fmt::format("{:>30%F %T}", t), " 1975-12-29 12:14:16"); | |
500 | |
501 EXPECT_EQ(fmt::format("{:*<30%F %T}", t), "1975-12-29 12:14:16***********"); | |
502 EXPECT_EQ(fmt::format("{:*^30%F %T}", t), "*****1975-12-29 12:14:16******"); | |
503 EXPECT_EQ(fmt::format("{:*>30%F %T}", t), "***********1975-12-29 12:14:16"); | |
504 } | |
505 | |
506 TEST(chrono_test, tp_align) { | |
507 auto tp = std::chrono::time_point_cast<std::chrono::microseconds>( | |
508 std::chrono::system_clock::from_time_t(0)); | |
509 EXPECT_EQ(fmt::format("{:%M:%S}", tp), "00:00.000000"); | |
510 EXPECT_EQ(fmt::format("{:15%M:%S}", tp), "00:00.000000 "); | |
511 EXPECT_EQ(fmt::format("{:{}%M:%S}", tp, 15), "00:00.000000 "); | |
512 EXPECT_EQ(fmt::format("{:<15%M:%S}", tp), "00:00.000000 "); | |
513 EXPECT_EQ(fmt::format("{:^15%M:%S}", tp), " 00:00.000000 "); | |
514 EXPECT_EQ(fmt::format("{:>15%M:%S}", tp), " 00:00.000000"); | |
515 | |
516 EXPECT_EQ(fmt::format("{:*<15%M:%S}", tp), "00:00.000000***"); | |
517 EXPECT_EQ(fmt::format("{:*^15%M:%S}", tp), "*00:00.000000**"); | |
518 EXPECT_EQ(fmt::format("{:*>15%M:%S}", tp), "***00:00.000000"); | |
519 } | |
520 | |
521 TEST(chrono_test, format_specs) { | |
522 EXPECT_EQ(fmt::format("{:%%}", std::chrono::seconds(0)), "%"); | |
523 EXPECT_EQ(fmt::format("{:%n}", std::chrono::seconds(0)), "\n"); | |
524 EXPECT_EQ(fmt::format("{:%t}", std::chrono::seconds(0)), "\t"); | |
525 EXPECT_EQ(fmt::format("{:%S}", std::chrono::seconds(0)), "00"); | |
526 EXPECT_EQ(fmt::format("{:%S}", std::chrono::seconds(60)), "00"); | |
527 EXPECT_EQ(fmt::format("{:%S}", std::chrono::seconds(42)), "42"); | |
528 EXPECT_EQ(fmt::format("{:%S}", std::chrono::milliseconds(1234)), "01.234"); | |
529 EXPECT_EQ(fmt::format("{:%M}", std::chrono::minutes(0)), "00"); | |
530 EXPECT_EQ(fmt::format("{:%M}", std::chrono::minutes(60)), "00"); | |
531 EXPECT_EQ(fmt::format("{:%M}", std::chrono::minutes(42)), "42"); | |
532 EXPECT_EQ(fmt::format("{:%M}", std::chrono::seconds(61)), "01"); | |
533 EXPECT_EQ(fmt::format("{:%H}", std::chrono::hours(0)), "00"); | |
534 EXPECT_EQ(fmt::format("{:%H}", std::chrono::hours(24)), "00"); | |
535 EXPECT_EQ(fmt::format("{:%H}", std::chrono::hours(14)), "14"); | |
536 EXPECT_EQ(fmt::format("{:%H}", std::chrono::minutes(61)), "01"); | |
537 EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(0)), "12"); | |
538 EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(12)), "12"); | |
539 EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(24)), "12"); | |
540 EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(4)), "04"); | |
541 EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(14)), "02"); | |
542 EXPECT_EQ(fmt::format("{:%j}", days(12345)), "12345"); | |
543 EXPECT_EQ(fmt::format("{:%j}", std::chrono::hours(12345 * 24 + 12)), "12345"); | |
544 EXPECT_EQ(fmt::format("{:%H:%M:%S}", std::chrono::seconds(12345)), | |
545 "03:25:45"); | |
546 EXPECT_EQ(fmt::format("{:%R}", std::chrono::seconds(12345)), "03:25"); | |
547 EXPECT_EQ(fmt::format("{:%T}", std::chrono::seconds(12345)), "03:25:45"); | |
548 EXPECT_EQ(fmt::format("{:%Q}", std::chrono::seconds(12345)), "12345"); | |
549 EXPECT_EQ(fmt::format("{:%q}", std::chrono::seconds(12345)), "s"); | |
550 } | |
551 | |
552 TEST(chrono_test, invalid_specs) { | |
553 auto sec = std::chrono::seconds(0); | |
554 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%a}"), sec), fmt::format_error, | |
555 "no date"); | |
556 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%A}"), sec), fmt::format_error, | |
557 "no date"); | |
558 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%c}"), sec), fmt::format_error, | |
559 "no date"); | |
560 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%x}"), sec), fmt::format_error, | |
561 "no date"); | |
562 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Ex}"), sec), fmt::format_error, | |
563 "no date"); | |
564 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%X}"), sec), fmt::format_error, | |
565 "no date"); | |
566 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%EX}"), sec), fmt::format_error, | |
567 "no date"); | |
568 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%D}"), sec), fmt::format_error, | |
569 "no date"); | |
570 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%F}"), sec), fmt::format_error, | |
571 "no date"); | |
572 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Ec}"), sec), fmt::format_error, | |
573 "no date"); | |
574 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%w}"), sec), fmt::format_error, | |
575 "no date"); | |
576 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%u}"), sec), fmt::format_error, | |
577 "no date"); | |
578 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%b}"), sec), fmt::format_error, | |
579 "no date"); | |
580 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%B}"), sec), fmt::format_error, | |
581 "no date"); | |
582 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%z}"), sec), fmt::format_error, | |
583 "no date"); | |
584 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Z}"), sec), fmt::format_error, | |
585 "no date"); | |
586 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Eq}"), sec), fmt::format_error, | |
587 "invalid format"); | |
588 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Oq}"), sec), fmt::format_error, | |
589 "invalid format"); | |
590 EXPECT_THROW_MSG((void)fmt::format(runtime("{:abc}"), sec), fmt::format_error, | |
591 "invalid format"); | |
592 EXPECT_THROW_MSG((void)fmt::format(runtime("{:.2f}"), sec), fmt::format_error, | |
593 "invalid format"); | |
594 } | |
595 | |
596 auto format_tm(const std::tm& time, fmt::string_view spec, | |
597 const std::locale& loc) -> std::string { | |
598 auto& facet = std::use_facet<std::time_put<char>>(loc); | |
599 std::ostringstream os; | |
600 os.imbue(loc); | |
601 facet.put(os, os, ' ', &time, spec.begin(), spec.end()); | |
602 return os.str(); | |
603 } | |
604 | |
605 TEST(chrono_test, locale) { | |
606 auto loc = get_locale("ja_JP.utf8"); | |
607 if (loc == std::locale::classic()) return; | |
608 # define EXPECT_TIME(spec, time, duration) \ | |
609 { \ | |
610 auto jp_loc = std::locale("ja_JP.utf8"); \ | |
611 EXPECT_EQ(format_tm(time, spec, jp_loc), \ | |
612 fmt::format(jp_loc, "{:L" spec "}", duration)); \ | |
613 } | |
614 EXPECT_TIME("%OH", make_hour(14), std::chrono::hours(14)); | |
615 EXPECT_TIME("%OI", make_hour(14), std::chrono::hours(14)); | |
616 EXPECT_TIME("%OM", make_minute(42), std::chrono::minutes(42)); | |
617 EXPECT_TIME("%OS", make_second(42), std::chrono::seconds(42)); | |
618 auto time = make_tm(); | |
619 time.tm_hour = 3; | |
620 time.tm_min = 25; | |
621 time.tm_sec = 45; | |
622 auto sec = std::chrono::seconds(12345); | |
623 EXPECT_TIME("%r", time, sec); | |
624 EXPECT_TIME("%p", time, sec); | |
625 } | |
626 | |
627 using dms = std::chrono::duration<double, std::milli>; | |
628 | |
629 TEST(chrono_test, format_default_fp) { | |
630 EXPECT_EQ(fmt::format("{}", std::chrono::duration<float>(1.234)), "1.234s"); | |
631 EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::milli>(1.234)), | |
632 "1.234ms"); | |
633 EXPECT_EQ(fmt::format("{}", std::chrono::duration<double>(1.234)), "1.234s"); | |
634 EXPECT_EQ(fmt::format("{}", dms(1.234)), "1.234ms"); | |
635 } | |
636 | |
637 TEST(chrono_test, format_precision) { | |
638 EXPECT_THROW_MSG( | |
639 (void)fmt::format(runtime("{:.2%Q}"), std::chrono::seconds(42)), | |
640 fmt::format_error, "precision not allowed for this argument type"); | |
641 EXPECT_EQ(fmt::format("{:.0}", dms(1.234)), "1ms"); | |
642 EXPECT_EQ(fmt::format("{:.1}", dms(1.234)), "1.2ms"); | |
643 EXPECT_EQ(fmt::format("{:.{}}", dms(1.234), 2), "1.23ms"); | |
644 | |
645 EXPECT_EQ(fmt::format("{:.0}", dms(12.56)), "13ms"); | |
646 EXPECT_EQ(fmt::format("{:.1}", dms(12.56)), "12.6ms"); | |
647 EXPECT_EQ(fmt::format("{:.2}", dms(12.56)), "12.56ms"); | |
648 } | |
649 | |
650 TEST(chrono_test, format_full_specs) { | |
651 EXPECT_EQ(fmt::format("{:6.0}", dms(1.234)), "1ms "); | |
652 EXPECT_EQ(fmt::format("{:6.1}", dms(1.234)), "1.2ms "); | |
653 EXPECT_EQ(fmt::format("{:>8.{}}", dms(1.234), 2), " 1.23ms"); | |
654 EXPECT_EQ(fmt::format("{:^{}.{}}", dms(1.234), 7, 1), " 1.2ms "); | |
655 EXPECT_EQ(fmt::format("{0:^{2}.{1}}", dms(1.234), 2, 8), " 1.23ms "); | |
656 EXPECT_EQ(fmt::format("{:=^{}.{}}", dms(1.234), 9, 3), "=1.234ms="); | |
657 EXPECT_EQ(fmt::format("{:*^10.4}", dms(1.234)), "*1.2340ms*"); | |
658 | |
659 EXPECT_EQ(fmt::format("{:6.0}", dms(12.56)), "13ms "); | |
660 EXPECT_EQ(fmt::format("{:>8.{}}", dms(12.56), 0), " 13ms"); | |
661 EXPECT_EQ(fmt::format("{:^{}.{}}", dms(12.56), 6, 0), " 13ms "); | |
662 EXPECT_EQ(fmt::format("{0:^{2}.{1}}", dms(12.56), 0, 8), " 13ms "); | |
663 EXPECT_EQ(fmt::format("{:=^{}.{}}", dms(12.56), 9, 0), "==13ms==="); | |
664 EXPECT_EQ(fmt::format("{:*^10.0}", dms(12.56)), "***13ms***"); | |
665 } | |
666 | |
667 TEST(chrono_test, format_simple_q) { | |
668 EXPECT_EQ(fmt::format("{:%Q %q}", std::chrono::duration<float>(1.234)), | |
669 "1.234 s"); | |
670 EXPECT_EQ( | |
671 fmt::format("{:%Q %q}", std::chrono::duration<float, std::milli>(1.234)), | |
672 "1.234 ms"); | |
673 EXPECT_EQ(fmt::format("{:%Q %q}", std::chrono::duration<double>(1.234)), | |
674 "1.234 s"); | |
675 EXPECT_EQ(fmt::format("{:%Q %q}", dms(1.234)), "1.234 ms"); | |
676 } | |
677 | |
678 TEST(chrono_test, format_precision_q) { | |
679 EXPECT_THROW_MSG( | |
680 (void)fmt::format(runtime("{:.2%Q %q}"), std::chrono::seconds(42)), | |
681 fmt::format_error, "precision not allowed for this argument type"); | |
682 EXPECT_EQ(fmt::format("{:.1%Q %q}", dms(1.234)), "1.2 ms"); | |
683 EXPECT_EQ(fmt::format("{:.{}%Q %q}", dms(1.234), 2), "1.23 ms"); | |
684 } | |
685 | |
686 TEST(chrono_test, format_full_specs_q) { | |
687 EXPECT_EQ(fmt::format("{:7.0%Q %q}", dms(1.234)), "1 ms "); | |
688 EXPECT_EQ(fmt::format("{:7.1%Q %q}", dms(1.234)), "1.2 ms "); | |
689 EXPECT_EQ(fmt::format("{:>8.{}%Q %q}", dms(1.234), 2), " 1.23 ms"); | |
690 EXPECT_EQ(fmt::format("{:^{}.{}%Q %q}", dms(1.234), 8, 1), " 1.2 ms "); | |
691 EXPECT_EQ(fmt::format("{0:^{2}.{1}%Q %q}", dms(1.234), 2, 9), " 1.23 ms "); | |
692 EXPECT_EQ(fmt::format("{:=^{}.{}%Q %q}", dms(1.234), 10, 3), "=1.234 ms="); | |
693 EXPECT_EQ(fmt::format("{:*^11.4%Q %q}", dms(1.234)), "*1.2340 ms*"); | |
694 | |
695 EXPECT_EQ(fmt::format("{:7.0%Q %q}", dms(12.56)), "13 ms "); | |
696 EXPECT_EQ(fmt::format("{:>8.{}%Q %q}", dms(12.56), 0), " 13 ms"); | |
697 EXPECT_EQ(fmt::format("{:^{}.{}%Q %q}", dms(12.56), 8, 0), " 13 ms "); | |
698 EXPECT_EQ(fmt::format("{0:^{2}.{1}%Q %q}", dms(12.56), 0, 9), " 13 ms "); | |
699 EXPECT_EQ(fmt::format("{:=^{}.{}%Q %q}", dms(12.56), 9, 0), "==13 ms=="); | |
700 EXPECT_EQ(fmt::format("{:*^11.0%Q %q}", dms(12.56)), "***13 ms***"); | |
701 } | |
702 | |
703 TEST(chrono_test, invalid_width_id) { | |
704 EXPECT_THROW((void)fmt::format(runtime("{:{o}"), std::chrono::seconds(0)), | |
705 fmt::format_error); | |
706 } | |
707 | |
708 TEST(chrono_test, invalid_colons) { | |
709 EXPECT_THROW((void)fmt::format(runtime("{0}=:{0::"), std::chrono::seconds(0)), | |
710 fmt::format_error); | |
711 } | |
712 | |
713 TEST(chrono_test, negative_durations) { | |
714 EXPECT_EQ(fmt::format("{:%Q}", std::chrono::seconds(-12345)), "-12345"); | |
715 EXPECT_EQ(fmt::format("{:%H:%M:%S}", std::chrono::seconds(-12345)), | |
716 "-03:25:45"); | |
717 EXPECT_EQ(fmt::format("{:%M:%S}", std::chrono::duration<double>(-1)), | |
718 "-00:01"); | |
719 EXPECT_EQ(fmt::format("{:%q}", std::chrono::seconds(-12345)), "s"); | |
720 EXPECT_EQ(fmt::format("{:%S}", | |
721 std::chrono::duration<signed char, std::milli>(-127)), | |
722 "-00.127"); | |
723 auto min = std::numeric_limits<int>::min(); | |
724 EXPECT_EQ(fmt::format("{}", min), | |
725 fmt::format("{:%Q}", std::chrono::duration<int>(min))); | |
726 } | |
727 | |
728 TEST(chrono_test, special_durations) { | |
729 EXPECT_EQ(fmt::format("{:%S}", std::chrono::duration<double>(1e20)), "40"); | |
730 auto nan = std::numeric_limits<double>::quiet_NaN(); | |
731 EXPECT_EQ( | |
732 fmt::format("{:%I %H %M %S %R %r}", std::chrono::duration<double>(nan)), | |
733 "nan nan nan nan nan:nan nan"); | |
734 EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::exa>(1)), | |
735 "1Es"); | |
736 EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::atto>(1)), | |
737 "1as"); | |
738 EXPECT_EQ(fmt::format("{:%R}", std::chrono::duration<char, std::mega>{2}), | |
739 "03:33"); | |
740 EXPECT_EQ(fmt::format("{:%T}", std::chrono::duration<char, std::mega>{2}), | |
741 "03:33:20"); | |
742 EXPECT_EQ( | |
743 fmt::format("{:.3%S}", std::chrono::duration<float, std::pico>(1.234e12)), | |
744 "01.234"); | |
745 } | |
746 | |
747 TEST(chrono_test, unsigned_duration) { | |
748 EXPECT_EQ(fmt::format("{}", std::chrono::duration<unsigned>(42)), "42s"); | |
749 } | |
750 | |
751 TEST(chrono_test, weekday) { | |
752 auto loc = get_locale("es_ES.UTF-8"); | |
753 std::locale::global(loc); | |
754 auto sat = fmt::weekday(6); | |
755 | |
756 auto tm = std::tm(); | |
757 tm.tm_wday = static_cast<int>(sat.c_encoding()); | |
758 | |
759 EXPECT_EQ(fmt::format("{}", sat), "Sat"); | |
760 EXPECT_EQ(fmt::format("{:%a}", tm), "Sat"); | |
761 | |
762 if (loc != std::locale::classic()) { | |
763 auto saturdays = std::vector<std::string>{"sáb", "sá."}; | |
764 EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L}", sat))); | |
765 EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", tm))); | |
766 } | |
767 } | |
768 | |
769 TEST(chrono_test, cpp20_duration_subsecond_support) { | |
770 using attoseconds = std::chrono::duration<long long, std::atto>; | |
771 // Check that 18 digits of subsecond precision are supported. | |
772 EXPECT_EQ(fmt::format("{:%S}", attoseconds{999999999999999999}), | |
773 "00.999999999999999999"); | |
774 EXPECT_EQ(fmt::format("{:%S}", attoseconds{673231113420148734}), | |
775 "00.673231113420148734"); | |
776 EXPECT_EQ(fmt::format("{:%S}", attoseconds{-673231113420148734}), | |
777 "-00.673231113420148734"); | |
778 EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds{13420148734}), | |
779 "13.420148734"); | |
780 EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds{-13420148734}), | |
781 "-13.420148734"); | |
782 EXPECT_EQ(fmt::format("{:%S}", std::chrono::milliseconds{1234}), "01.234"); | |
783 // Check subsecond presision modifier. | |
784 EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::nanoseconds{1234}), | |
785 "00.000001"); | |
786 EXPECT_EQ(fmt::format("{:.18%S}", std::chrono::nanoseconds{1234}), | |
787 "00.000001234000000000"); | |
788 EXPECT_EQ(fmt::format("{:.{}%S}", std::chrono::nanoseconds{1234}, 6), | |
789 "00.000001"); | |
790 EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::milliseconds{1234}), | |
791 "01.234000"); | |
792 EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::milliseconds{-1234}), | |
793 "-01.234000"); | |
794 EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::seconds{1234}), "34.000"); | |
795 EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::hours{1234}), "00.000"); | |
796 EXPECT_EQ(fmt::format("{:.5%S}", dms(1.234)), "00.00123"); | |
797 EXPECT_EQ(fmt::format("{:.8%S}", dms(1.234)), "00.00123400"); | |
798 { | |
799 // Check that {:%H:%M:%S} is equivalent to {:%T}. | |
800 auto dur = std::chrono::milliseconds{3601234}; | |
801 auto formatted_dur = fmt::format("{:%T}", dur); | |
802 EXPECT_EQ(formatted_dur, "01:00:01.234"); | |
803 EXPECT_EQ(fmt::format("{:%H:%M:%S}", dur), formatted_dur); | |
804 EXPECT_EQ(fmt::format("{:.6%H:%M:%S}", dur), "01:00:01.234000"); | |
805 } | |
806 using nanoseconds_dbl = std::chrono::duration<double, std::nano>; | |
807 EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{-123456789}), "-00.123456789"); | |
808 EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{9123456789}), "09.123456789"); | |
809 // Verify that only the seconds part is extracted and printed. | |
810 EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{99123456789}), "39.123456789"); | |
811 EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{99123000000}), "39.123000000"); | |
812 { | |
813 // Now the hour is printed, and we also test if negative doubles work. | |
814 auto dur = nanoseconds_dbl{-99123456789}; | |
815 auto formatted_dur = fmt::format("{:%T}", dur); | |
816 EXPECT_EQ(formatted_dur, "-00:01:39.123456789"); | |
817 EXPECT_EQ(fmt::format("{:%H:%M:%S}", dur), formatted_dur); | |
818 EXPECT_EQ(fmt::format("{:.3%H:%M:%S}", dur), "-00:01:39.123"); | |
819 } | |
820 // Check that durations with precision greater than std::chrono::seconds have | |
821 // fixed precision, and print zeros even if there is no fractional part. | |
822 EXPECT_EQ(fmt::format("{:%S}", std::chrono::microseconds{7000000}), | |
823 "07.000000"); | |
824 EXPECT_EQ(fmt::format("{:%S}", | |
825 std::chrono::duration<long long, std::ratio<1, 3>>(1)), | |
826 "00.333333"); | |
827 EXPECT_EQ(fmt::format("{:%S}", | |
828 std::chrono::duration<long long, std::ratio<1, 7>>(1)), | |
829 "00.142857"); | |
830 | |
831 EXPECT_EQ( | |
832 fmt::format("{:%S}", | |
833 std::chrono::duration<signed char, std::ratio<1, 100>>(0x80)), | |
834 "-01.28"); | |
835 | |
836 EXPECT_EQ( | |
837 fmt::format("{:%M:%S}", | |
838 std::chrono::duration<short, std::ratio<1, 100>>(0x8000)), | |
839 "-05:27.68"); | |
840 | |
841 // Check that floating point seconds with ratio<1,1> are printed. | |
842 EXPECT_EQ(fmt::format("{:%S}", std::chrono::duration<double>{1.5}), | |
843 "01.500000"); | |
844 EXPECT_EQ(fmt::format("{:%M:%S}", std::chrono::duration<double>{-61.25}), | |
845 "-01:01.250000"); | |
846 } | |
847 | |
848 #endif // FMT_STATIC_THOUSANDS_SEPARATOR | |
849 | |
850 // Disable the utc_clock test for windows, as the icu.dll used for tzdb | |
851 // (time zone database) is not shipped with many windows versions. | |
852 #if FMT_USE_UTC_TIME && !defined(_WIN32) | |
853 TEST(chrono_test, utc_clock) { | |
854 auto t1 = std::chrono::system_clock::now(); | |
855 auto t1_utc = std::chrono::utc_clock::from_sys(t1); | |
856 EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%S}", t1), | |
857 fmt::format("{:%Y-%m-%d %H:%M:%S}", t1_utc)); | |
858 } | |
859 #endif | |
860 | |
861 TEST(chrono_test, timestamp_ratios) { | |
862 auto t1 = | |
863 sys_time<std::chrono::milliseconds>(std::chrono::milliseconds(67890)); | |
864 EXPECT_EQ(fmt::format("{:%M:%S}", t1), "01:07.890"); | |
865 | |
866 auto t2 = sys_time<std::chrono::minutes>(std::chrono::minutes(7)); | |
867 EXPECT_EQ(fmt::format("{:%M:%S}", t2), "07:00"); | |
868 | |
869 auto t3 = sys_time<std::chrono::duration<int, std::ratio<9>>>( | |
870 std::chrono::duration<int, std::ratio<9>>(7)); | |
871 EXPECT_EQ(fmt::format("{:%M:%S}", t3), "01:03"); | |
872 | |
873 auto t4 = sys_time<std::chrono::duration<int, std::ratio<63>>>( | |
874 std::chrono::duration<int, std::ratio<63>>(1)); | |
875 EXPECT_EQ(fmt::format("{:%M:%S}", t4), "01:03"); | |
876 | |
877 if (sizeof(time_t) > 4) { | |
878 auto tp = | |
879 sys_time<std::chrono::milliseconds>(std::chrono::seconds(32503680000)); | |
880 EXPECT_EQ(fmt::format("{:%Y-%m-%d}", tp), "3000-01-01"); | |
881 } | |
882 | |
883 if (FMT_SAFE_DURATION_CAST) { | |
884 using years = std::chrono::duration<std::int64_t, std::ratio<31556952>>; | |
885 auto tp = sys_time<years>(years(std::numeric_limits<std::int64_t>::max())); | |
886 EXPECT_THROW_MSG((void)fmt::format("{:%Y-%m-%d}", tp), fmt::format_error, | |
887 "cannot format duration"); | |
888 } | |
889 } | |
890 | |
891 TEST(chrono_test, timestamp_sub_seconds) { | |
892 auto t1 = sys_time<std::chrono::duration<long long, std::ratio<1, 3>>>( | |
893 std::chrono::duration<long long, std::ratio<1, 3>>(4)); | |
894 EXPECT_EQ(fmt::format("{:%S}", t1), "01.333333"); | |
895 | |
896 auto t2 = sys_time<std::chrono::duration<double, std::ratio<1, 3>>>( | |
897 std::chrono::duration<double, std::ratio<1, 3>>(4)); | |
898 EXPECT_EQ(fmt::format("{:%S}", t2), "01.333333"); | |
899 | |
900 auto t3 = sys_time<std::chrono::seconds>(std::chrono::seconds(2)); | |
901 EXPECT_EQ(fmt::format("{:%S}", t3), "02"); | |
902 | |
903 auto t4 = sys_time<std::chrono::duration<double>>( | |
904 std::chrono::duration<double, std::ratio<1, 1>>(9.5)); | |
905 EXPECT_EQ(fmt::format("{:%S}", t4), "09.500000"); | |
906 | |
907 auto t5 = sys_time<std::chrono::duration<double>>( | |
908 std::chrono::duration<double, std::ratio<1, 1>>(9)); | |
909 EXPECT_EQ(fmt::format("{:%S}", t5), "09"); | |
910 | |
911 auto t6 = sys_time<std::chrono::milliseconds>(std::chrono::seconds(1) + | |
912 std::chrono::milliseconds(120)); | |
913 EXPECT_EQ(fmt::format("{:%S}", t6), "01.120"); | |
914 | |
915 auto t7 = | |
916 sys_time<std::chrono::microseconds>(std::chrono::microseconds(1234567)); | |
917 EXPECT_EQ(fmt::format("{:%S}", t7), "01.234567"); | |
918 | |
919 auto t8 = | |
920 sys_time<std::chrono::nanoseconds>(std::chrono::nanoseconds(123456789)); | |
921 EXPECT_EQ(fmt::format("{:%S}", t8), "00.123456789"); | |
922 | |
923 auto t9 = std::chrono::time_point_cast<std::chrono::nanoseconds>( | |
924 std::chrono::system_clock::now()); | |
925 auto t9_sec = std::chrono::time_point_cast<std::chrono::seconds>(t9); | |
926 auto t9_sub_sec_part = fmt::format("{0:09}", (t9 - t9_sec).count()); | |
927 EXPECT_EQ(fmt::format("{}.{}", strftime_full_utc(t9_sec), t9_sub_sec_part), | |
928 fmt::format("{:%Y-%m-%d %H:%M:%S}", t9)); | |
929 EXPECT_EQ(fmt::format("{}.{}", strftime_full_utc(t9_sec), t9_sub_sec_part), | |
930 fmt::format("{:%Y-%m-%d %T}", t9)); | |
931 | |
932 auto t10 = | |
933 sys_time<std::chrono::milliseconds>(std::chrono::milliseconds(2000)); | |
934 EXPECT_EQ(fmt::format("{:%S}", t10), "02.000"); | |
935 | |
936 auto epoch = sys_time<std::chrono::milliseconds>(); | |
937 auto d = std::chrono::milliseconds(250); | |
938 EXPECT_EQ(fmt::format("{:%S}", epoch - d), "59.750"); | |
939 EXPECT_EQ(fmt::format("{:%S}", epoch), "00.000"); | |
940 EXPECT_EQ(fmt::format("{:%S}", epoch + d), "00.250"); | |
941 } | |
942 | |
943 TEST(chrono_test, glibc_extensions) { | |
944 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%0}"), std::chrono::seconds()), | |
945 fmt::format_error, "invalid format"); | |
946 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%_}"), std::chrono::seconds()), | |
947 fmt::format_error, "invalid format"); | |
948 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%-}"), std::chrono::seconds()), | |
949 fmt::format_error, "invalid format"); | |
950 | |
951 { | |
952 const auto d = std::chrono::hours(1) + std::chrono::minutes(2) + | |
953 std::chrono::seconds(3); | |
954 | |
955 EXPECT_EQ(fmt::format("{:%I,%H,%M,%S}", d), "01,01,02,03"); | |
956 EXPECT_EQ(fmt::format("{:%0I,%0H,%0M,%0S}", d), "01,01,02,03"); | |
957 EXPECT_EQ(fmt::format("{:%_I,%_H,%_M,%_S}", d), " 1, 1, 2, 3"); | |
958 EXPECT_EQ(fmt::format("{:%-I,%-H,%-M,%-S}", d), "1,1,2,3"); | |
959 | |
960 EXPECT_EQ(fmt::format("{:%OI,%OH,%OM,%OS}", d), "01,01,02,03"); | |
961 EXPECT_EQ(fmt::format("{:%0OI,%0OH,%0OM,%0OS}", d), "01,01,02,03"); | |
962 EXPECT_EQ(fmt::format("{:%_OI,%_OH,%_OM,%_OS}", d), " 1, 1, 2, 3"); | |
963 EXPECT_EQ(fmt::format("{:%-OI,%-OH,%-OM,%-OS}", d), "1,1,2,3"); | |
964 } | |
965 | |
966 { | |
967 const auto tm = make_tm(1970, 1, 1, 1, 2, 3); | |
968 EXPECT_EQ(fmt::format("{:%I,%H,%M,%S}", tm), "01,01,02,03"); | |
969 EXPECT_EQ(fmt::format("{:%0I,%0H,%0M,%0S}", tm), "01,01,02,03"); | |
970 EXPECT_EQ(fmt::format("{:%_I,%_H,%_M,%_S}", tm), " 1, 1, 2, 3"); | |
971 EXPECT_EQ(fmt::format("{:%-I,%-H,%-M,%-S}", tm), "1,1,2,3"); | |
972 | |
973 EXPECT_EQ(fmt::format("{:%OI,%OH,%OM,%OS}", tm), "01,01,02,03"); | |
974 EXPECT_EQ(fmt::format("{:%0OI,%0OH,%0OM,%0OS}", tm), "01,01,02,03"); | |
975 EXPECT_EQ(fmt::format("{:%_OI,%_OH,%_OM,%_OS}", tm), " 1, 1, 2, 3"); | |
976 EXPECT_EQ(fmt::format("{:%-OI,%-OH,%-OM,%-OS}", tm), "1,1,2,3"); | |
977 } | |
978 | |
979 { | |
980 const auto d = std::chrono::seconds(3) + std::chrono::milliseconds(140); | |
981 EXPECT_EQ(fmt::format("{:%S}", d), "03.140"); | |
982 EXPECT_EQ(fmt::format("{:%0S}", d), "03.140"); | |
983 EXPECT_EQ(fmt::format("{:%_S}", d), " 3.140"); | |
984 EXPECT_EQ(fmt::format("{:%-S}", d), "3.140"); | |
985 } | |
986 | |
987 { | |
988 const auto d = std::chrono::duration<double>(3.14); | |
989 EXPECT_EQ(fmt::format("{:%S}", d), "03.140000"); | |
990 EXPECT_EQ(fmt::format("{:%0S}", d), "03.140000"); | |
991 EXPECT_EQ(fmt::format("{:%_S}", d), " 3.140000"); | |
992 EXPECT_EQ(fmt::format("{:%-S}", d), "3.140000"); | |
993 } | |
994 } | |
995 | |
996 TEST(chrono_test, out_of_range) { | |
997 auto d = std::chrono::duration<unsigned long, std::giga>(538976288); | |
998 EXPECT_THROW((void)fmt::format("{:%j}", d), fmt::format_error); | |
999 } |