Include fmt 11.0.2
[openal-soft.git] / fmt-11.0.2 / test / chrono-test.cc
blob5befcab6c58099d46b452eb8e31bd3b058406e1d
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.
8 #include "fmt/chrono.h"
10 #include <algorithm>
11 #include <ctime>
12 #include <vector>
14 #include "gtest-extra.h" // EXPECT_THROW_MSG
15 #include "util.h" // get_locale
17 using fmt::runtime;
18 using testing::Contains;
20 template <typename Duration>
21 using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>;
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
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
36 auto make_tm() -> std::tm {
37 auto time = std::tm();
38 time.tm_mday = 1;
39 return time;
42 auto make_hour(int h) -> std::tm {
43 auto time = make_tm();
44 time.tm_hour = h;
45 return time;
48 auto make_minute(int m) -> std::tm {
49 auto time = make_tm();
50 time.tm_min = m;
51 return time;
54 auto make_second(int s) -> std::tm {
55 auto time = make_tm();
56 time.tm_sec = s;
57 return time;
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
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;
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");
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");
118 tm.tm_year = 27 - 1900;
119 EXPECT_EQ(fmt::format("{:%Y}", tm), "0027");
120 EXPECT_EQ(fmt::format("{:%C%y}", tm), "0027");
122 // Overflow year
123 tm.tm_year = 2147483647;
124 EXPECT_EQ(fmt::format("{:%Y}", tm), "2147485547");
126 tm.tm_year = -2147483648;
127 EXPECT_EQ(fmt::format("{:%Y}", tm), "-2147481748");
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
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
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);
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));
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);
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));
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");
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");
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));
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");
214 EXPECT_EQ(fmt::format("{:%F}", tm), "-101-04-25");
215 EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33");
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));
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));
225 #endif
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));
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");
243 TEST(chrono_test, empty_result) { EXPECT_EQ(fmt::format("{}", std::tm()), ""); }
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;
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)));
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);
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));
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));
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");
294 for (const auto& spec : spec_list) {
295 auto t = std::chrono::system_clock::to_time_t(t1);
296 auto tm = *std::gmtime(&t);
298 auto sys_output = system_strftime(spec, &tm);
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));
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);
315 auto sys_output = system_strftime(spec, &tm);
317 auto fmt_spec = fmt::format("{{:{}}}", spec);
318 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
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));
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);
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)));
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);
342 EXPECT_EQ(fmt::format("{:%z}", t1), "+0000");
343 EXPECT_EQ(fmt::format("{:%z}", tm), "+0000");
345 EXPECT_EQ(fmt::format("{:%Ez}", t1), "+00:00");
346 EXPECT_EQ(fmt::format("{:%Ez}", tm), "+00:00");
348 EXPECT_EQ(fmt::format("{:%Oz}", t1), "+00:00");
349 EXPECT_EQ(fmt::format("{:%Oz}", tm), "+00:00");
353 #if FMT_USE_LOCAL_TIME
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)));
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);
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));
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");
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);
405 auto sys_output = system_strftime(spec, &tm);
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));
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);
418 auto sys_output = system_strftime("%z", &tm);
419 sys_output.insert(sys_output.end() - 2, 1, ':');
421 EXPECT_EQ(sys_output, fmt::format("{:%Ez}", t1));
422 EXPECT_EQ(sys_output, fmt::format("{:%Ez}", tm));
424 EXPECT_EQ(sys_output, fmt::format("{:%Oz}", t1));
425 EXPECT_EQ(sys_output, fmt::format("{:%Oz}", tm));
429 #endif // FMT_USE_LOCAL_TIME
431 #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
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");
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 ");
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");
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");
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");
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");
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");
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");
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();
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) \
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)); \
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);
627 using dms = std::chrono::duration<double, std::milli>;
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");
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");
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");
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*");
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***");
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");
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");
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*");
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***");
703 TEST(chrono_test, invalid_width_id) {
704 EXPECT_THROW((void)fmt::format(runtime("{:{o}"), std::chrono::seconds(0)),
705 fmt::format_error);
708 TEST(chrono_test, invalid_colons) {
709 EXPECT_THROW((void)fmt::format(runtime("{0}=:{0::"), std::chrono::seconds(0)),
710 fmt::format_error);
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)));
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");
747 TEST(chrono_test, unsigned_duration) {
748 EXPECT_EQ(fmt::format("{}", std::chrono::duration<unsigned>(42)), "42s");
751 TEST(chrono_test, weekday) {
752 auto loc = get_locale("es_ES.UTF-8");
753 std::locale::global(loc);
755 auto sat = fmt::weekday(6);
757 auto tm = std::tm();
758 tm.tm_wday = static_cast<int>(sat.c_encoding());
760 EXPECT_EQ(fmt::format("{}", sat), "Sat");
761 EXPECT_EQ(fmt::format("{:%a}", sat), "Sat");
762 EXPECT_EQ(fmt::format("{:%A}", sat), "Saturday");
763 EXPECT_EQ(fmt::format("{:%a}", tm), "Sat");
765 if (loc != std::locale::classic()) {
766 auto saturdays = std::vector<std::string>{"sáb", "sá."};
767 EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L}", sat)));
768 EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", sat)));
769 EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", tm)));
773 TEST(chrono_test, cpp20_duration_subsecond_support) {
774 using attoseconds = std::chrono::duration<long long, std::atto>;
775 // Check that 18 digits of subsecond precision are supported.
776 EXPECT_EQ(fmt::format("{:%S}", attoseconds{999999999999999999}),
777 "00.999999999999999999");
778 EXPECT_EQ(fmt::format("{:%S}", attoseconds{673231113420148734}),
779 "00.673231113420148734");
780 EXPECT_EQ(fmt::format("{:%S}", attoseconds{-673231113420148734}),
781 "-00.673231113420148734");
782 EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds{13420148734}),
783 "13.420148734");
784 EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds{-13420148734}),
785 "-13.420148734");
786 EXPECT_EQ(fmt::format("{:%S}", std::chrono::milliseconds{1234}), "01.234");
787 // Check subsecond presision modifier.
788 EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::nanoseconds{1234}),
789 "00.000001");
790 EXPECT_EQ(fmt::format("{:.18%S}", std::chrono::nanoseconds{1234}),
791 "00.000001234000000000");
792 EXPECT_EQ(fmt::format("{:.{}%S}", std::chrono::nanoseconds{1234}, 6),
793 "00.000001");
794 EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::milliseconds{1234}),
795 "01.234000");
796 EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::milliseconds{-1234}),
797 "-01.234000");
798 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{12345}),
799 "12.34");
800 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{12375}),
801 "12.37");
802 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{-12375}),
803 "-12.37");
804 EXPECT_EQ(fmt::format("{:.0%S}", std::chrono::milliseconds{12054}),
805 "12");
806 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{99999}),
807 "39.99");
808 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{1000}),
809 "01.00");
810 EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::milliseconds{1}),
811 "00.001");
812 EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::seconds{1234}), "34.000");
813 EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::hours{1234}), "00.000");
814 EXPECT_EQ(fmt::format("{:.5%S}", dms(1.234)), "00.00123");
815 EXPECT_EQ(fmt::format("{:.8%S}", dms(1.234)), "00.00123400");
817 // Check that {:%H:%M:%S} is equivalent to {:%T}.
818 auto dur = std::chrono::milliseconds{3601234};
819 auto formatted_dur = fmt::format("{:%T}", dur);
820 EXPECT_EQ(formatted_dur, "01:00:01.234");
821 EXPECT_EQ(fmt::format("{:%H:%M:%S}", dur), formatted_dur);
822 EXPECT_EQ(fmt::format("{:.6%H:%M:%S}", dur), "01:00:01.234000");
824 using nanoseconds_dbl = std::chrono::duration<double, std::nano>;
825 EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{-123456789}), "-00.123456789");
826 EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{9123456789}), "09.123456789");
827 // Verify that only the seconds part is extracted and printed.
828 EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{99123456789}), "39.123456789");
829 EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{99123000000}), "39.123000000");
831 // Now the hour is printed, and we also test if negative doubles work.
832 auto dur = nanoseconds_dbl{-99123456789};
833 auto formatted_dur = fmt::format("{:%T}", dur);
834 EXPECT_EQ(formatted_dur, "-00:01:39.123456789");
835 EXPECT_EQ(fmt::format("{:%H:%M:%S}", dur), formatted_dur);
836 EXPECT_EQ(fmt::format("{:.3%H:%M:%S}", dur), "-00:01:39.123");
838 // Check that durations with precision greater than std::chrono::seconds have
839 // fixed precision, and print zeros even if there is no fractional part.
840 EXPECT_EQ(fmt::format("{:%S}", std::chrono::microseconds{7000000}),
841 "07.000000");
842 EXPECT_EQ(fmt::format("{:%S}",
843 std::chrono::duration<long long, std::ratio<1, 3>>(1)),
844 "00.333333");
845 EXPECT_EQ(fmt::format("{:%S}",
846 std::chrono::duration<long long, std::ratio<1, 7>>(1)),
847 "00.142857");
849 EXPECT_EQ(
850 fmt::format("{:%S}",
851 std::chrono::duration<signed char, std::ratio<1, 100>>(0x80)),
852 "-01.28");
854 EXPECT_EQ(
855 fmt::format("{:%M:%S}",
856 std::chrono::duration<short, std::ratio<1, 100>>(0x8000)),
857 "-05:27.68");
859 // Check that floating point seconds with ratio<1,1> are printed.
860 EXPECT_EQ(fmt::format("{:%S}", std::chrono::duration<double>{1.5}),
861 "01.500000");
862 EXPECT_EQ(fmt::format("{:%M:%S}", std::chrono::duration<double>{-61.25}),
863 "-01:01.250000");
866 #endif // FMT_STATIC_THOUSANDS_SEPARATOR
868 // Disable the utc_clock test for windows, as the icu.dll used for tzdb
869 // (time zone database) is not shipped with many windows versions.
870 #if FMT_USE_UTC_TIME && !defined(_WIN32)
871 TEST(chrono_test, utc_clock) {
872 auto t1 = std::chrono::system_clock::now();
873 auto t1_utc = std::chrono::utc_clock::from_sys(t1);
874 EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%S}", t1),
875 fmt::format("{:%Y-%m-%d %H:%M:%S}", t1_utc));
877 #endif
879 TEST(chrono_test, timestamp_ratios) {
880 auto t1 =
881 sys_time<std::chrono::milliseconds>(std::chrono::milliseconds(67890));
882 EXPECT_EQ(fmt::format("{:%M:%S}", t1), "01:07.890");
884 auto t2 = sys_time<std::chrono::minutes>(std::chrono::minutes(7));
885 EXPECT_EQ(fmt::format("{:%M:%S}", t2), "07:00");
887 auto t3 = sys_time<std::chrono::duration<int, std::ratio<9>>>(
888 std::chrono::duration<int, std::ratio<9>>(7));
889 EXPECT_EQ(fmt::format("{:%M:%S}", t3), "01:03");
891 auto t4 = sys_time<std::chrono::duration<int, std::ratio<63>>>(
892 std::chrono::duration<int, std::ratio<63>>(1));
893 EXPECT_EQ(fmt::format("{:%M:%S}", t4), "01:03");
895 if (sizeof(time_t) > 4) {
896 auto tp =
897 sys_time<std::chrono::milliseconds>(std::chrono::seconds(32503680000));
898 EXPECT_EQ(fmt::format("{:%Y-%m-%d}", tp), "3000-01-01");
901 if (FMT_SAFE_DURATION_CAST) {
902 using years = std::chrono::duration<std::int64_t, std::ratio<31556952>>;
903 auto tp = sys_time<years>(years(std::numeric_limits<std::int64_t>::max()));
904 EXPECT_THROW_MSG((void)fmt::format("{:%Y-%m-%d}", tp), fmt::format_error,
905 "cannot format duration");
909 TEST(chrono_test, timestamp_sub_seconds) {
910 auto t1 = sys_time<std::chrono::duration<long long, std::ratio<1, 3>>>(
911 std::chrono::duration<long long, std::ratio<1, 3>>(4));
912 EXPECT_EQ(fmt::format("{:%S}", t1), "01.333333");
914 auto t2 = sys_time<std::chrono::duration<double, std::ratio<1, 3>>>(
915 std::chrono::duration<double, std::ratio<1, 3>>(4));
916 EXPECT_EQ(fmt::format("{:%S}", t2), "01.333333");
918 auto t3 = sys_time<std::chrono::seconds>(std::chrono::seconds(2));
919 EXPECT_EQ(fmt::format("{:%S}", t3), "02");
921 auto t4 = sys_time<std::chrono::duration<double>>(
922 std::chrono::duration<double, std::ratio<1, 1>>(9.5));
923 EXPECT_EQ(fmt::format("{:%S}", t4), "09.500000");
925 auto t5 = sys_time<std::chrono::duration<double>>(
926 std::chrono::duration<double, std::ratio<1, 1>>(9));
927 EXPECT_EQ(fmt::format("{:%S}", t5), "09");
929 auto t6 = sys_time<std::chrono::milliseconds>(std::chrono::seconds(1) +
930 std::chrono::milliseconds(120));
931 EXPECT_EQ(fmt::format("{:%S}", t6), "01.120");
933 auto t7 =
934 sys_time<std::chrono::microseconds>(std::chrono::microseconds(1234567));
935 EXPECT_EQ(fmt::format("{:%S}", t7), "01.234567");
937 auto t8 =
938 sys_time<std::chrono::nanoseconds>(std::chrono::nanoseconds(123456789));
939 EXPECT_EQ(fmt::format("{:%S}", t8), "00.123456789");
941 auto t9 = std::chrono::time_point_cast<std::chrono::nanoseconds>(
942 std::chrono::system_clock::now());
943 auto t9_sec = std::chrono::time_point_cast<std::chrono::seconds>(t9);
944 auto t9_sub_sec_part = fmt::format("{0:09}", (t9 - t9_sec).count());
945 EXPECT_EQ(fmt::format("{}.{}", strftime_full_utc(t9_sec), t9_sub_sec_part),
946 fmt::format("{:%Y-%m-%d %H:%M:%S}", t9));
947 EXPECT_EQ(fmt::format("{}.{}", strftime_full_utc(t9_sec), t9_sub_sec_part),
948 fmt::format("{:%Y-%m-%d %T}", t9));
950 auto t10 =
951 sys_time<std::chrono::milliseconds>(std::chrono::milliseconds(2000));
952 EXPECT_EQ(fmt::format("{:%S}", t10), "02.000");
954 auto epoch = sys_time<std::chrono::milliseconds>();
955 auto d = std::chrono::milliseconds(250);
956 EXPECT_EQ(fmt::format("{:%S}", epoch - d), "59.750");
957 EXPECT_EQ(fmt::format("{:%S}", epoch), "00.000");
958 EXPECT_EQ(fmt::format("{:%S}", epoch + d), "00.250");
961 TEST(chrono_test, glibc_extensions) {
962 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%0}"), std::chrono::seconds()),
963 fmt::format_error, "invalid format");
964 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%_}"), std::chrono::seconds()),
965 fmt::format_error, "invalid format");
966 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%-}"), std::chrono::seconds()),
967 fmt::format_error, "invalid format");
970 const auto d = std::chrono::hours(1) + std::chrono::minutes(2) +
971 std::chrono::seconds(3);
973 EXPECT_EQ(fmt::format("{:%I,%H,%M,%S}", d), "01,01,02,03");
974 EXPECT_EQ(fmt::format("{:%_I,%_H,%_M,%_S}", d), " 1, 1, 2, 3");
975 EXPECT_EQ(fmt::format("{:%-I,%-H,%-M,%-S}", d), "1,1,2,3");
976 EXPECT_EQ(fmt::format("{:%-I,%H,%M,%S}", d), "1,01,02,03");
978 EXPECT_EQ(fmt::format("{:%OI,%OH,%OM,%OS}", d), "01,01,02,03");
979 EXPECT_EQ(fmt::format("{:%_OI,%_OH,%_OM,%_OS}", d), " 1, 1, 2, 3");
980 EXPECT_EQ(fmt::format("{:%-OI,%-OH,%-OM,%-OS}", d), "1,1,2,3");
984 const auto tm = make_tm(1970, 1, 1, 1, 2, 3);
985 EXPECT_EQ(fmt::format("{:%I,%H,%M,%S}", tm), "01,01,02,03");
986 EXPECT_EQ(fmt::format("{:%_I,%_H,%_M,%_S}", tm), " 1, 1, 2, 3");
987 EXPECT_EQ(fmt::format("{:%-I,%-H,%-M,%-S}", tm), "1,1,2,3");
989 EXPECT_EQ(fmt::format("{:%OI,%OH,%OM,%OS}", tm), "01,01,02,03");
990 EXPECT_EQ(fmt::format("{:%_OI,%_OH,%_OM,%_OS}", tm), " 1, 1, 2, 3");
991 EXPECT_EQ(fmt::format("{:%-OI,%-OH,%-OM,%-OS}", tm), "1,1,2,3");
995 const auto d = std::chrono::seconds(3) + std::chrono::milliseconds(140);
996 EXPECT_EQ(fmt::format("{:%S}", d), "03.140");
997 EXPECT_EQ(fmt::format("{:%_S}", d), " 3.140");
998 EXPECT_EQ(fmt::format("{:%-S}", d), "3.140");
1002 auto d = std::chrono::duration<double>(3.14);
1003 EXPECT_EQ(fmt::format("{:%S}", d), "03.140000");
1004 EXPECT_EQ(fmt::format("{:%_S}", d), " 3.140000");
1005 EXPECT_EQ(fmt::format("{:%-S}", d), "3.140000");
1009 auto t = std::tm();
1010 t.tm_yday = 7;
1011 EXPECT_EQ(fmt::format("{:%U,%W,%V}", t), "02,01,01");
1012 EXPECT_EQ(fmt::format("{:%_U,%_W,%_V}", t), " 2, 1, 1");
1013 EXPECT_EQ(fmt::format("{:%-U,%-W,%-V}", t), "2,1,1");
1017 auto t = std::tm();
1018 t.tm_mday = 7;
1019 EXPECT_EQ(fmt::format("{:%d}", t), "07");
1020 EXPECT_EQ(fmt::format("{:%_d}", t), " 7");
1021 EXPECT_EQ(fmt::format("{:%-d}", t), "7");
1023 EXPECT_EQ(fmt::format("{:%e}", t), " 7");
1027 TEST(chrono_test, out_of_range) {
1028 auto d = std::chrono::duration<unsigned long, std::giga>(538976288);
1029 EXPECT_THROW((void)fmt::format("{:%j}", d), fmt::format_error);
1032 TEST(chrono_test, year_month_day) {
1033 auto loc = get_locale("es_ES.UTF-8");
1034 std::locale::global(loc);
1036 auto year = fmt::year(2024);
1037 auto month = fmt::month(1);
1038 auto day = fmt::day(1);
1039 auto ymd = fmt::year_month_day(year, month, day);
1041 EXPECT_EQ(fmt::format("{}", year), "2024");
1042 EXPECT_EQ(fmt::format("{:%Y}", year), "2024");
1043 EXPECT_EQ(fmt::format("{:%y}", year), "24");
1045 EXPECT_EQ(fmt::format("{}", month), "Jan");
1046 EXPECT_EQ(fmt::format("{:%m}", month), "01");
1047 EXPECT_EQ(fmt::format("{:%b}", month), "Jan");
1048 EXPECT_EQ(fmt::format("{:%B}", month), "January");
1050 EXPECT_EQ(fmt::format("{}", day), "01");
1051 EXPECT_EQ(fmt::format("{:%d}", day), "01");
1053 EXPECT_EQ(fmt::format("{}", ymd), "2024-01-01");
1054 EXPECT_EQ(fmt::format("{:%Y-%m-%d}", ymd), "2024-01-01");
1055 EXPECT_EQ(fmt::format("{:%Y-%b-%d}", ymd), "2024-Jan-01");
1056 EXPECT_EQ(fmt::format("{:%Y-%B-%d}", ymd), "2024-January-01");
1058 if (loc != std::locale::classic()) {
1059 auto months = std::vector<std::string>{"ene.", "ene"};
1060 EXPECT_THAT(months, Contains(fmt::format(loc, "{:L}", month)));
1061 EXPECT_THAT(months, Contains(fmt::format(loc, "{:%b}", month)));