1 // Formatting library for C++ - time formatting tests
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
8 #include "fmt/chrono.h"
14 #include "gtest-extra.h" // EXPECT_THROW_MSG
15 #include "util.h" // get_locale
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
27 # define FMT_HAS_C99_STRFTIME 1
30 #if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L
31 using days
= std::chrono::days
;
33 using days
= std::chrono::duration
<std::chrono::hours::rep
, std::ratio
<86400>>;
36 auto make_tm() -> std::tm
{
37 auto time
= std::tm();
42 auto make_hour(int h
) -> std::tm
{
43 auto time
= make_tm();
48 auto make_minute(int m
) -> std::tm
{
49 auto time
= make_tm();
54 auto make_second(int s
) -> std::tm
{
55 auto time
= make_tm();
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
;
66 facet
.put(os
, os
, ' ', timeptr
, format
.c_str(),
67 format
.c_str() + format
.size());
69 // Workaround a bug in older versions of Universal CRT.
71 if (str
== "-0000") str
= "+0000";
78 FMT_CONSTEXPR
std::tm
make_tm(int year
, int mon
, int mday
, int hour
, int min
,
86 tm
.tm_year
= year
- 1900;
90 TEST(chrono_test
, format_tm
) {
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");
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");
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.";
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
));
170 // minkernel\crts\ucrt\src\appcrt\time\wcsftime.cpp(971) : Assertion failed:
171 // timeptr->tm_year >= -1900 && timeptr->tm_year <= 8099
173 TEST(chrono_test
, format_tm_future
) {
175 tm
.tm_year
= 10445; // 10000+ years
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
) {
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");
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
));
227 TEST(chrono_test
, grow_buffer
) {
228 auto s
= std::string("{:");
229 for (int i
= 0; i
< 30; ++i
) s
+= "%c";
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();
238 auto s
= std::string();
239 fmt::format_to(std::back_inserter(s
), "{:%S}", time
);
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"};
284 // Disabled on Windows because these formats are not consistent among
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"};
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"};
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
));
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
) {
371 return; // Not supported on Windows.
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"};
390 // Disabled on Windows because these formats are not consistent among
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"};
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") !=
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)),
437 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::femto
>(42)),
439 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::pico
>(42)),
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)),
446 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::deci
>(42)),
448 EXPECT_EQ(fmt::format("{}", std::chrono::seconds(42)), "42s");
449 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::deca
>(42)),
451 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::hecto
>(42)),
453 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::kilo
>(42)),
455 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::mega
>(42)),
457 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::giga
>(42)),
459 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::tera
>(42)),
461 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::peta
>(42)),
463 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<int, std::exa
>(42)),
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");
469 fmt::format("{}", std::chrono::duration
<int, std::ratio
<15, 1>>(42)),
472 fmt::format("{}", std::chrono::duration
<int, std::ratio
<15, 4>>(42)),
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)),
484 EXPECT_EQ(fmt::format("{:>12%H:%M:%S}", std::chrono::seconds(12345)),
486 EXPECT_EQ(fmt::format("{:~^12%H:%M:%S}", std::chrono::seconds(12345)),
488 EXPECT_EQ(fmt::format("{:{}%H:%M:%S}", std::chrono::seconds(12345), 12),
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)),
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
,
556 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%A}"), sec
), fmt::format_error
,
558 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%c}"), sec
), fmt::format_error
,
560 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%x}"), sec
), fmt::format_error
,
562 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Ex}"), sec
), fmt::format_error
,
564 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%X}"), sec
), fmt::format_error
,
566 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%EX}"), sec
), fmt::format_error
,
568 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%D}"), sec
), fmt::format_error
,
570 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%F}"), sec
), fmt::format_error
,
572 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Ec}"), sec
), fmt::format_error
,
574 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%w}"), sec
), fmt::format_error
,
576 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%u}"), sec
), fmt::format_error
,
578 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%b}"), sec
), fmt::format_error
,
580 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%B}"), sec
), fmt::format_error
,
582 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%z}"), sec
), fmt::format_error
,
584 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Z}"), sec
), fmt::format_error
,
586 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Eq}"), sec
), fmt::format_error
,
588 EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Oq}"), sec
), fmt::format_error
,
590 EXPECT_THROW_MSG((void)fmt::format(runtime("{:abc}"), sec
), fmt::format_error
,
592 EXPECT_THROW_MSG((void)fmt::format(runtime("{:.2f}"), sec
), fmt::format_error
,
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
;
601 facet
.put(os
, os
, ' ', &time
, spec
.begin(), spec
.end());
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();
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)),
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
) {
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)),
671 fmt::format("{:%Q %q}", std::chrono::duration
<float, std::milli
>(1.234)),
673 EXPECT_EQ(fmt::format("{:%Q %q}", std::chrono::duration
<double>(1.234)),
675 EXPECT_EQ(fmt::format("{:%Q %q}", dms(1.234)), "1.234 ms");
678 TEST(chrono_test
, format_precision_q
) {
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)),
708 TEST(chrono_test
, invalid_colons
) {
709 EXPECT_THROW((void)fmt::format(runtime("{0}=:{0::"), std::chrono::seconds(0)),
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)),
717 EXPECT_EQ(fmt::format("{:%M:%S}", std::chrono::duration
<double>(-1)),
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)),
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();
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)),
736 EXPECT_EQ(fmt::format("{}", std::chrono::duration
<float, std::atto
>(1)),
738 EXPECT_EQ(fmt::format("{:%R}", std::chrono::duration
<char, std::mega
>{2}),
740 EXPECT_EQ(fmt::format("{:%T}", std::chrono::duration
<char, std::mega
>{2}),
743 fmt::format("{:.3%S}", std::chrono::duration
<float, std::pico
>(1.234e12
)),
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);
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}),
784 EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds
{-13420148734}),
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}),
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),
794 EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::milliseconds
{1234}),
796 EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::milliseconds
{-1234}),
798 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds
{12345}),
800 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds
{12375}),
802 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds
{-12375}),
804 EXPECT_EQ(fmt::format("{:.0%S}", std::chrono::milliseconds
{12054}),
806 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds
{99999}),
808 EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds
{1000}),
810 EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::milliseconds
{1}),
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}),
842 EXPECT_EQ(fmt::format("{:%S}",
843 std::chrono::duration
<long long, std::ratio
<1, 3>>(1)),
845 EXPECT_EQ(fmt::format("{:%S}",
846 std::chrono::duration
<long long, std::ratio
<1, 7>>(1)),
851 std::chrono::duration
<signed char, std::ratio
<1, 100>>(0x80)),
855 fmt::format("{:%M:%S}",
856 std::chrono::duration
<short, std::ratio
<1, 100>>(0x8000)),
859 // Check that floating point seconds with ratio<1,1> are printed.
860 EXPECT_EQ(fmt::format("{:%S}", std::chrono::duration
<double>{1.5}),
862 EXPECT_EQ(fmt::format("{:%M:%S}", std::chrono::duration
<double>{-61.25}),
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
));
879 TEST(chrono_test
, timestamp_ratios
) {
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) {
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");
934 sys_time
<std::chrono::microseconds
>(std::chrono::microseconds(1234567));
935 EXPECT_EQ(fmt::format("{:%S}", t7
), "01.234567");
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
));
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");
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");
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
)));