chrono-test.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. #include "fmt/chrono.h"
  8. #include <ctime>
  9. #include <vector>
  10. #include "gtest-extra.h" // EXPECT_THROW_MSG
  11. #include "util.h" // get_locale
  12. using fmt::runtime;
  13. using testing::Contains;
  14. auto make_tm() -> std::tm {
  15. auto time = std::tm();
  16. time.tm_mday = 1;
  17. return time;
  18. }
  19. auto make_hour(int h) -> std::tm {
  20. auto time = make_tm();
  21. time.tm_hour = h;
  22. return time;
  23. }
  24. auto make_minute(int m) -> std::tm {
  25. auto time = make_tm();
  26. time.tm_min = m;
  27. return time;
  28. }
  29. auto make_second(int s) -> std::tm {
  30. auto time = make_tm();
  31. time.tm_sec = s;
  32. return time;
  33. }
  34. std::string system_strftime(const std::string& format, const std::tm* timeptr,
  35. std::locale* locptr = nullptr) {
  36. auto loc = locptr ? *locptr : std::locale::classic();
  37. auto& facet = std::use_facet<std::time_put<char>>(loc);
  38. std::ostringstream os;
  39. os.imbue(loc);
  40. facet.put(os, os, ' ', timeptr, format.c_str(),
  41. format.c_str() + format.size());
  42. #ifdef _WIN32
  43. // Workaround a bug in older versions of Universal CRT.
  44. auto str = os.str();
  45. if (str == "-0000") str = "+0000";
  46. return str;
  47. #else
  48. return os.str();
  49. #endif
  50. }
  51. FMT_CONSTEXPR std::tm make_tm(int year, int mon, int mday, int hour, int min,
  52. int sec) {
  53. auto tm = std::tm();
  54. tm.tm_sec = sec;
  55. tm.tm_min = min;
  56. tm.tm_hour = hour;
  57. tm.tm_mday = mday;
  58. tm.tm_mon = mon - 1;
  59. tm.tm_year = year - 1900;
  60. return tm;
  61. }
  62. TEST(chrono_test, format_tm) {
  63. auto tm = std::tm();
  64. tm.tm_year = 116;
  65. tm.tm_mon = 3;
  66. tm.tm_mday = 25;
  67. tm.tm_hour = 11;
  68. tm.tm_min = 22;
  69. tm.tm_sec = 33;
  70. EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
  71. "The date is 2016-04-25 11:22:33.");
  72. EXPECT_EQ(fmt::format("{:%Y}", tm), "2016");
  73. EXPECT_EQ(fmt::format("{:%C}", tm), "20");
  74. EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
  75. EXPECT_EQ(fmt::format("{:%e}", tm), "25");
  76. EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/16");
  77. EXPECT_EQ(fmt::format("{:%F}", tm), "2016-04-25");
  78. EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33");
  79. // Short year
  80. tm.tm_year = 999 - 1900;
  81. tm.tm_mon = 0; // for %G
  82. tm.tm_mday = 2; // for %G
  83. tm.tm_wday = 3; // for %G
  84. tm.tm_yday = 1; // for %G
  85. EXPECT_EQ(fmt::format("{:%Y}", tm), "0999");
  86. EXPECT_EQ(fmt::format("{:%C%y}", tm), "0999");
  87. EXPECT_EQ(fmt::format("{:%G}", tm), "0999");
  88. tm.tm_year = 27 - 1900;
  89. EXPECT_EQ(fmt::format("{:%Y}", tm), "0027");
  90. EXPECT_EQ(fmt::format("{:%C%y}", tm), "0027");
  91. // Overflow year
  92. tm.tm_year = 2147483647;
  93. EXPECT_EQ(fmt::format("{:%Y}", tm), "2147485547");
  94. tm.tm_year = -2147483648;
  95. EXPECT_EQ(fmt::format("{:%Y}", tm), "-2147481748");
  96. // for week on the year
  97. // https://www.cl.cam.ac.uk/~mgk25/iso-time.html
  98. std::vector<std::tm> tm_list = {
  99. make_tm(1975, 12, 29, 12, 14, 16), // W01
  100. make_tm(1977, 1, 2, 12, 14, 16), // W53
  101. make_tm(1999, 12, 27, 12, 14, 16), // W52
  102. make_tm(1999, 12, 31, 12, 14, 16), // W52
  103. make_tm(2000, 1, 1, 12, 14, 16), // W52
  104. make_tm(2000, 1, 2, 12, 14, 16), // W52
  105. make_tm(2000, 1, 3, 12, 14, 16) // W1
  106. };
  107. #if defined(__MINGW32__) && !defined(_UCRT)
  108. GTEST_SKIP() << "Skip the rest of this test because it relies on strftime() "
  109. "conforming to C99, but on this platform, MINGW + MSVCRT, "
  110. "the function conforms only to C89.";
  111. #endif
  112. const std::string iso_week_spec = "%Y-%m-%d: %G %g %V";
  113. for (auto ctm : tm_list) {
  114. // Calculate tm_yday, tm_wday, etc.
  115. std::time_t t = std::mktime(&ctm);
  116. tm = *std::localtime(&t);
  117. auto fmt_spec = fmt::format("{{:{}}}", iso_week_spec);
  118. EXPECT_EQ(system_strftime(iso_week_spec, &tm),
  119. fmt::format(fmt::runtime(fmt_spec), tm));
  120. }
  121. // Every day from 1970-01-01
  122. std::time_t time_now = std::time(nullptr);
  123. for (std::time_t t = 6 * 3600; t < time_now; t += 86400) {
  124. tm = *std::localtime(&t);
  125. auto fmt_spec = fmt::format("{{:{}}}", iso_week_spec);
  126. EXPECT_EQ(system_strftime(iso_week_spec, &tm),
  127. fmt::format(fmt::runtime(fmt_spec), tm));
  128. }
  129. }
  130. // MSVC:
  131. // minkernel\crts\ucrt\src\appcrt\time\wcsftime.cpp(971) : Assertion failed:
  132. // timeptr->tm_year >= -1900 && timeptr->tm_year <= 8099
  133. #ifndef _WIN32
  134. TEST(chrono_test, format_tm_future) {
  135. auto tm = std::tm();
  136. tm.tm_year = 10445; // 10000+ years
  137. tm.tm_mon = 3;
  138. tm.tm_mday = 25;
  139. tm.tm_hour = 11;
  140. tm.tm_min = 22;
  141. tm.tm_sec = 33;
  142. EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
  143. "The date is 12345-04-25 11:22:33.");
  144. EXPECT_EQ(fmt::format("{:%Y}", tm), "12345");
  145. EXPECT_EQ(fmt::format("{:%C}", tm), "123");
  146. EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
  147. EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/45");
  148. EXPECT_EQ(fmt::format("{:%F}", tm), "12345-04-25");
  149. EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33");
  150. }
  151. TEST(chrono_test, format_tm_past) {
  152. auto tm = std::tm();
  153. tm.tm_year = -2001;
  154. tm.tm_mon = 3;
  155. tm.tm_mday = 25;
  156. tm.tm_hour = 11;
  157. tm.tm_min = 22;
  158. tm.tm_sec = 33;
  159. EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
  160. "The date is -101-04-25 11:22:33.");
  161. EXPECT_EQ(fmt::format("{:%Y}", tm), "-101");
  162. // macOS %C - "-1"
  163. // Linux %C - "-2"
  164. // fmt %C - "-1"
  165. EXPECT_EQ(fmt::format("{:%C}", tm), "-1");
  166. EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
  167. // macOS %D - "04/25/01" (%y)
  168. // Linux %D - "04/25/99" (%y)
  169. // fmt %D - "04/25/01" (%y)
  170. EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/01");
  171. EXPECT_EQ(fmt::format("{:%F}", tm), "-101-04-25");
  172. EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33");
  173. tm.tm_year = -1901; // -1
  174. EXPECT_EQ(fmt::format("{:%Y}", tm), "-001");
  175. EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
  176. tm.tm_year = -1911; // -11
  177. EXPECT_EQ(fmt::format("{:%Y}", tm), "-011");
  178. EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
  179. }
  180. #endif
  181. TEST(chrono_test, grow_buffer) {
  182. auto s = std::string("{:");
  183. for (int i = 0; i < 30; ++i) s += "%c";
  184. s += "}\n";
  185. auto t = std::time(nullptr);
  186. (void)fmt::format(fmt::runtime(s), *std::localtime(&t));
  187. }
  188. TEST(chrono_test, format_to_empty_container) {
  189. auto time = std::tm();
  190. time.tm_sec = 42;
  191. auto s = std::string();
  192. fmt::format_to(std::back_inserter(s), "{:%S}", time);
  193. EXPECT_EQ(s, "42");
  194. }
  195. TEST(chrono_test, empty_result) { EXPECT_EQ(fmt::format("{}", std::tm()), ""); }
  196. auto equal(const std::tm& lhs, const std::tm& rhs) -> bool {
  197. return lhs.tm_sec == rhs.tm_sec && lhs.tm_min == rhs.tm_min &&
  198. lhs.tm_hour == rhs.tm_hour && lhs.tm_mday == rhs.tm_mday &&
  199. lhs.tm_mon == rhs.tm_mon && lhs.tm_year == rhs.tm_year &&
  200. lhs.tm_wday == rhs.tm_wday && lhs.tm_yday == rhs.tm_yday &&
  201. lhs.tm_isdst == rhs.tm_isdst;
  202. }
  203. TEST(chrono_test, localtime) {
  204. auto t = std::time(nullptr);
  205. auto tm = *std::localtime(&t);
  206. EXPECT_TRUE(equal(tm, fmt::localtime(t)));
  207. }
  208. TEST(chrono_test, gmtime) {
  209. auto t = std::time(nullptr);
  210. auto tm = *std::gmtime(&t);
  211. EXPECT_TRUE(equal(tm, fmt::gmtime(t)));
  212. }
  213. template <typename TimePoint> auto strftime_full(TimePoint tp) -> std::string {
  214. auto t = std::chrono::system_clock::to_time_t(tp);
  215. auto tm = *std::localtime(&t);
  216. return system_strftime("%Y-%m-%d %H:%M:%S", &tm);
  217. }
  218. TEST(chrono_test, time_point) {
  219. auto t1 = std::chrono::system_clock::now();
  220. EXPECT_EQ(strftime_full(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
  221. EXPECT_EQ(strftime_full(t1), fmt::format("{}", t1));
  222. using time_point =
  223. std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
  224. auto t2 = time_point(std::chrono::seconds(42));
  225. EXPECT_EQ(strftime_full(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
  226. std::vector<std::string> spec_list = {
  227. "%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C",
  228. "%EC", "%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U",
  229. "%OU", "%W", "%OW", "%V", "%OV", "%j", "%d", "%Od", "%e",
  230. "%Oe", "%a", "%A", "%w", "%Ow", "%u", "%Ou", "%H", "%OH",
  231. "%I", "%OI", "%M", "%OM", "%S", "%OS", "%x", "%Ex", "%X",
  232. "%EX", "%D", "%F", "%R", "%T", "%p", "%z", "%Z"};
  233. #ifndef _WIN32
  234. // Disabled on Windows because these formats are not consistent among
  235. // platforms.
  236. spec_list.insert(spec_list.end(), {"%c", "%Ec", "%r"});
  237. #elif defined(__MINGW32__) && !defined(_UCRT)
  238. // Only C89 conversion specifiers when using MSVCRT instead of UCRT
  239. spec_list = {"%%", "%Y", "%y", "%b", "%B", "%m", "%U", "%W", "%j", "%d", "%a",
  240. "%A", "%w", "%H", "%I", "%M", "%S", "%x", "%X", "%p", "%Z"};
  241. #endif
  242. spec_list.push_back("%Y-%m-%d %H:%M:%S");
  243. for (const auto& spec : spec_list) {
  244. auto t = std::chrono::system_clock::to_time_t(t1);
  245. auto tm = *std::localtime(&t);
  246. auto sys_output = system_strftime(spec, &tm);
  247. auto fmt_spec = fmt::format("{{:{}}}", spec);
  248. EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1));
  249. EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
  250. }
  251. }
  252. #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
  253. TEST(chrono_test, format_default) {
  254. EXPECT_EQ("42s", fmt::format("{}", std::chrono::seconds(42)));
  255. EXPECT_EQ("42as",
  256. fmt::format("{}", std::chrono::duration<int, std::atto>(42)));
  257. EXPECT_EQ("42fs",
  258. fmt::format("{}", std::chrono::duration<int, std::femto>(42)));
  259. EXPECT_EQ("42ps",
  260. fmt::format("{}", std::chrono::duration<int, std::pico>(42)));
  261. EXPECT_EQ("42ns", fmt::format("{}", std::chrono::nanoseconds(42)));
  262. EXPECT_EQ("42µs", fmt::format("{}", std::chrono::microseconds(42)));
  263. EXPECT_EQ("42ms", fmt::format("{}", std::chrono::milliseconds(42)));
  264. EXPECT_EQ("42cs",
  265. fmt::format("{}", std::chrono::duration<int, std::centi>(42)));
  266. EXPECT_EQ("42ds",
  267. fmt::format("{}", std::chrono::duration<int, std::deci>(42)));
  268. EXPECT_EQ("42s", fmt::format("{}", std::chrono::seconds(42)));
  269. EXPECT_EQ("42das",
  270. fmt::format("{}", std::chrono::duration<int, std::deca>(42)));
  271. EXPECT_EQ("42hs",
  272. fmt::format("{}", std::chrono::duration<int, std::hecto>(42)));
  273. EXPECT_EQ("42ks",
  274. fmt::format("{}", std::chrono::duration<int, std::kilo>(42)));
  275. EXPECT_EQ("42Ms",
  276. fmt::format("{}", std::chrono::duration<int, std::mega>(42)));
  277. EXPECT_EQ("42Gs",
  278. fmt::format("{}", std::chrono::duration<int, std::giga>(42)));
  279. EXPECT_EQ("42Ts",
  280. fmt::format("{}", std::chrono::duration<int, std::tera>(42)));
  281. EXPECT_EQ("42Ps",
  282. fmt::format("{}", std::chrono::duration<int, std::peta>(42)));
  283. EXPECT_EQ("42Es",
  284. fmt::format("{}", std::chrono::duration<int, std::exa>(42)));
  285. EXPECT_EQ("42m", fmt::format("{}", std::chrono::minutes(42)));
  286. EXPECT_EQ("42h", fmt::format("{}", std::chrono::hours(42)));
  287. EXPECT_EQ(
  288. "42[15]s",
  289. fmt::format("{}", std::chrono::duration<int, std::ratio<15, 1>>(42)));
  290. EXPECT_EQ(
  291. "42[15/4]s",
  292. fmt::format("{}", std::chrono::duration<int, std::ratio<15, 4>>(42)));
  293. }
  294. TEST(chrono_test, align) {
  295. auto s = std::chrono::seconds(42);
  296. EXPECT_EQ("42s ", fmt::format("{:5}", s));
  297. EXPECT_EQ("42s ", fmt::format("{:{}}", s, 5));
  298. EXPECT_EQ(" 42s", fmt::format("{:>5}", s));
  299. EXPECT_EQ("**42s**", fmt::format("{:*^7}", s));
  300. EXPECT_EQ("03:25:45 ",
  301. fmt::format("{:12%H:%M:%S}", std::chrono::seconds(12345)));
  302. EXPECT_EQ(" 03:25:45",
  303. fmt::format("{:>12%H:%M:%S}", std::chrono::seconds(12345)));
  304. EXPECT_EQ("~~03:25:45~~",
  305. fmt::format("{:~^12%H:%M:%S}", std::chrono::seconds(12345)));
  306. EXPECT_EQ("03:25:45 ",
  307. fmt::format("{:{}%H:%M:%S}", std::chrono::seconds(12345), 12));
  308. }
  309. TEST(chrono_test, format_specs) {
  310. EXPECT_EQ("%", fmt::format("{:%%}", std::chrono::seconds(0)));
  311. EXPECT_EQ("\n", fmt::format("{:%n}", std::chrono::seconds(0)));
  312. EXPECT_EQ("\t", fmt::format("{:%t}", std::chrono::seconds(0)));
  313. EXPECT_EQ("00", fmt::format("{:%S}", std::chrono::seconds(0)));
  314. EXPECT_EQ("00", fmt::format("{:%S}", std::chrono::seconds(60)));
  315. EXPECT_EQ("42", fmt::format("{:%S}", std::chrono::seconds(42)));
  316. EXPECT_EQ("01.234", fmt::format("{:%S}", std::chrono::milliseconds(1234)));
  317. EXPECT_EQ("00", fmt::format("{:%M}", std::chrono::minutes(0)));
  318. EXPECT_EQ("00", fmt::format("{:%M}", std::chrono::minutes(60)));
  319. EXPECT_EQ("42", fmt::format("{:%M}", std::chrono::minutes(42)));
  320. EXPECT_EQ("01", fmt::format("{:%M}", std::chrono::seconds(61)));
  321. EXPECT_EQ("00", fmt::format("{:%H}", std::chrono::hours(0)));
  322. EXPECT_EQ("00", fmt::format("{:%H}", std::chrono::hours(24)));
  323. EXPECT_EQ("14", fmt::format("{:%H}", std::chrono::hours(14)));
  324. EXPECT_EQ("01", fmt::format("{:%H}", std::chrono::minutes(61)));
  325. EXPECT_EQ("12", fmt::format("{:%I}", std::chrono::hours(0)));
  326. EXPECT_EQ("12", fmt::format("{:%I}", std::chrono::hours(12)));
  327. EXPECT_EQ("12", fmt::format("{:%I}", std::chrono::hours(24)));
  328. EXPECT_EQ("04", fmt::format("{:%I}", std::chrono::hours(4)));
  329. EXPECT_EQ("02", fmt::format("{:%I}", std::chrono::hours(14)));
  330. EXPECT_EQ("03:25:45",
  331. fmt::format("{:%H:%M:%S}", std::chrono::seconds(12345)));
  332. EXPECT_EQ("03:25", fmt::format("{:%R}", std::chrono::seconds(12345)));
  333. EXPECT_EQ("03:25:45", fmt::format("{:%T}", std::chrono::seconds(12345)));
  334. EXPECT_EQ("12345", fmt::format("{:%Q}", std::chrono::seconds(12345)));
  335. EXPECT_EQ("s", fmt::format("{:%q}", std::chrono::seconds(12345)));
  336. }
  337. TEST(chrono_test, invalid_specs) {
  338. auto sec = std::chrono::seconds(0);
  339. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%a}"), sec), fmt::format_error,
  340. "no date");
  341. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%A}"), sec), fmt::format_error,
  342. "no date");
  343. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%c}"), sec), fmt::format_error,
  344. "no date");
  345. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%x}"), sec), fmt::format_error,
  346. "no date");
  347. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Ex}"), sec), fmt::format_error,
  348. "no date");
  349. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%X}"), sec), fmt::format_error,
  350. "no date");
  351. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%EX}"), sec), fmt::format_error,
  352. "no date");
  353. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%D}"), sec), fmt::format_error,
  354. "no date");
  355. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%F}"), sec), fmt::format_error,
  356. "no date");
  357. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Ec}"), sec), fmt::format_error,
  358. "no date");
  359. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%w}"), sec), fmt::format_error,
  360. "no date");
  361. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%u}"), sec), fmt::format_error,
  362. "no date");
  363. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%b}"), sec), fmt::format_error,
  364. "no date");
  365. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%B}"), sec), fmt::format_error,
  366. "no date");
  367. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%z}"), sec), fmt::format_error,
  368. "no date");
  369. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Z}"), sec), fmt::format_error,
  370. "no date");
  371. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Eq}"), sec), fmt::format_error,
  372. "invalid format");
  373. EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Oq}"), sec), fmt::format_error,
  374. "invalid format");
  375. }
  376. auto format_tm(const std::tm& time, fmt::string_view spec,
  377. const std::locale& loc) -> std::string {
  378. auto& facet = std::use_facet<std::time_put<char>>(loc);
  379. std::ostringstream os;
  380. os.imbue(loc);
  381. facet.put(os, os, ' ', &time, spec.begin(), spec.end());
  382. return os.str();
  383. }
  384. TEST(chrono_test, locale) {
  385. auto loc = get_locale("ja_JP.utf8");
  386. if (loc == std::locale::classic()) return;
  387. # define EXPECT_TIME(spec, time, duration) \
  388. { \
  389. auto jp_loc = std::locale("ja_JP.utf8"); \
  390. EXPECT_EQ(format_tm(time, spec, jp_loc), \
  391. fmt::format(jp_loc, "{:L" spec "}", duration)); \
  392. }
  393. EXPECT_TIME("%OH", make_hour(14), std::chrono::hours(14));
  394. EXPECT_TIME("%OI", make_hour(14), std::chrono::hours(14));
  395. EXPECT_TIME("%OM", make_minute(42), std::chrono::minutes(42));
  396. EXPECT_TIME("%OS", make_second(42), std::chrono::seconds(42));
  397. auto time = make_tm();
  398. time.tm_hour = 3;
  399. time.tm_min = 25;
  400. time.tm_sec = 45;
  401. auto sec = std::chrono::seconds(12345);
  402. EXPECT_TIME("%r", time, sec);
  403. EXPECT_TIME("%p", time, sec);
  404. }
  405. using dms = std::chrono::duration<double, std::milli>;
  406. TEST(chrono_test, format_default_fp) {
  407. typedef std::chrono::duration<float> fs;
  408. EXPECT_EQ("1.234s", fmt::format("{}", fs(1.234)));
  409. typedef std::chrono::duration<float, std::milli> fms;
  410. EXPECT_EQ("1.234ms", fmt::format("{}", fms(1.234)));
  411. typedef std::chrono::duration<double> ds;
  412. EXPECT_EQ("1.234s", fmt::format("{}", ds(1.234)));
  413. EXPECT_EQ("1.234ms", fmt::format("{}", dms(1.234)));
  414. }
  415. TEST(chrono_test, format_precision) {
  416. EXPECT_THROW_MSG(
  417. (void)fmt::format(runtime("{:.2}"), std::chrono::seconds(42)),
  418. fmt::format_error, "precision not allowed for this argument type");
  419. EXPECT_EQ("1ms", fmt::format("{:.0}", dms(1.234)));
  420. EXPECT_EQ("1.2ms", fmt::format("{:.1}", dms(1.234)));
  421. EXPECT_EQ("1.23ms", fmt::format("{:.{}}", dms(1.234), 2));
  422. EXPECT_EQ("13ms", fmt::format("{:.0}", dms(12.56)));
  423. EXPECT_EQ("12.6ms", fmt::format("{:.1}", dms(12.56)));
  424. EXPECT_EQ("12.56ms", fmt::format("{:.2}", dms(12.56)));
  425. }
  426. TEST(chrono_test, format_full_specs) {
  427. EXPECT_EQ("1ms ", fmt::format("{:6.0}", dms(1.234)));
  428. EXPECT_EQ("1.2ms ", fmt::format("{:6.1}", dms(1.234)));
  429. EXPECT_EQ(" 1.23ms", fmt::format("{:>8.{}}", dms(1.234), 2));
  430. EXPECT_EQ(" 1.2ms ", fmt::format("{:^{}.{}}", dms(1.234), 7, 1));
  431. EXPECT_EQ(" 1.23ms ", fmt::format("{0:^{2}.{1}}", dms(1.234), 2, 8));
  432. EXPECT_EQ("=1.234ms=", fmt::format("{:=^{}.{}}", dms(1.234), 9, 3));
  433. EXPECT_EQ("*1.2340ms*", fmt::format("{:*^10.4}", dms(1.234)));
  434. EXPECT_EQ("13ms ", fmt::format("{:6.0}", dms(12.56)));
  435. EXPECT_EQ(" 13ms", fmt::format("{:>8.{}}", dms(12.56), 0));
  436. EXPECT_EQ(" 13ms ", fmt::format("{:^{}.{}}", dms(12.56), 6, 0));
  437. EXPECT_EQ(" 13ms ", fmt::format("{0:^{2}.{1}}", dms(12.56), 0, 8));
  438. EXPECT_EQ("==13ms===", fmt::format("{:=^{}.{}}", dms(12.56), 9, 0));
  439. EXPECT_EQ("***13ms***", fmt::format("{:*^10.0}", dms(12.56)));
  440. }
  441. TEST(chrono_test, format_simple_q) {
  442. typedef std::chrono::duration<float> fs;
  443. EXPECT_EQ("1.234 s", fmt::format("{:%Q %q}", fs(1.234)));
  444. typedef std::chrono::duration<float, std::milli> fms;
  445. EXPECT_EQ("1.234 ms", fmt::format("{:%Q %q}", fms(1.234)));
  446. typedef std::chrono::duration<double> ds;
  447. EXPECT_EQ("1.234 s", fmt::format("{:%Q %q}", ds(1.234)));
  448. EXPECT_EQ("1.234 ms", fmt::format("{:%Q %q}", dms(1.234)));
  449. }
  450. TEST(chrono_test, format_precision_q) {
  451. EXPECT_THROW_MSG(
  452. (void)fmt::format(runtime("{:.2%Q %q}"), std::chrono::seconds(42)),
  453. fmt::format_error, "precision not allowed for this argument type");
  454. EXPECT_EQ("1.2 ms", fmt::format("{:.1%Q %q}", dms(1.234)));
  455. EXPECT_EQ("1.23 ms", fmt::format("{:.{}%Q %q}", dms(1.234), 2));
  456. }
  457. TEST(chrono_test, format_full_specs_q) {
  458. EXPECT_EQ("1 ms ", fmt::format("{:7.0%Q %q}", dms(1.234)));
  459. EXPECT_EQ("1.2 ms ", fmt::format("{:7.1%Q %q}", dms(1.234)));
  460. EXPECT_EQ(" 1.23 ms", fmt::format("{:>8.{}%Q %q}", dms(1.234), 2));
  461. EXPECT_EQ(" 1.2 ms ", fmt::format("{:^{}.{}%Q %q}", dms(1.234), 8, 1));
  462. EXPECT_EQ(" 1.23 ms ", fmt::format("{0:^{2}.{1}%Q %q}", dms(1.234), 2, 9));
  463. EXPECT_EQ("=1.234 ms=", fmt::format("{:=^{}.{}%Q %q}", dms(1.234), 10, 3));
  464. EXPECT_EQ("*1.2340 ms*", fmt::format("{:*^11.4%Q %q}", dms(1.234)));
  465. EXPECT_EQ("13 ms ", fmt::format("{:7.0%Q %q}", dms(12.56)));
  466. EXPECT_EQ(" 13 ms", fmt::format("{:>8.{}%Q %q}", dms(12.56), 0));
  467. EXPECT_EQ(" 13 ms ", fmt::format("{:^{}.{}%Q %q}", dms(12.56), 8, 0));
  468. EXPECT_EQ(" 13 ms ", fmt::format("{0:^{2}.{1}%Q %q}", dms(12.56), 0, 9));
  469. EXPECT_EQ("==13 ms==", fmt::format("{:=^{}.{}%Q %q}", dms(12.56), 9, 0));
  470. EXPECT_EQ("***13 ms***", fmt::format("{:*^11.0%Q %q}", dms(12.56)));
  471. }
  472. TEST(chrono_test, invalid_width_id) {
  473. EXPECT_THROW((void)fmt::format(runtime("{:{o}"), std::chrono::seconds(0)),
  474. fmt::format_error);
  475. }
  476. TEST(chrono_test, invalid_colons) {
  477. EXPECT_THROW((void)fmt::format(runtime("{0}=:{0::"), std::chrono::seconds(0)),
  478. fmt::format_error);
  479. }
  480. TEST(chrono_test, negative_durations) {
  481. EXPECT_EQ("-12345", fmt::format("{:%Q}", std::chrono::seconds(-12345)));
  482. EXPECT_EQ("-03:25:45",
  483. fmt::format("{:%H:%M:%S}", std::chrono::seconds(-12345)));
  484. EXPECT_EQ("-00:01",
  485. fmt::format("{:%M:%S}", std::chrono::duration<double>(-1)));
  486. EXPECT_EQ("s", fmt::format("{:%q}", std::chrono::seconds(-12345)));
  487. EXPECT_EQ("-00.127",
  488. fmt::format("{:%S}",
  489. std::chrono::duration<signed char, std::milli>{-127}));
  490. auto min = std::numeric_limits<int>::min();
  491. EXPECT_EQ(fmt::format("{}", min),
  492. fmt::format("{:%Q}", std::chrono::duration<int>(min)));
  493. }
  494. TEST(chrono_test, special_durations) {
  495. auto value = fmt::format("{:%S}", std::chrono::duration<double>(1e20));
  496. EXPECT_EQ(value, "40");
  497. auto nan = std::numeric_limits<double>::quiet_NaN();
  498. EXPECT_EQ(
  499. "nan nan nan nan nan:nan nan",
  500. fmt::format("{:%I %H %M %S %R %r}", std::chrono::duration<double>(nan)));
  501. EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::exa>(1)),
  502. "1Es");
  503. EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::atto>(1)),
  504. "1as");
  505. EXPECT_EQ(fmt::format("{:%R}", std::chrono::duration<char, std::mega>{2}),
  506. "03:33");
  507. EXPECT_EQ(fmt::format("{:%T}", std::chrono::duration<char, std::mega>{2}),
  508. "03:33:20");
  509. EXPECT_EQ("44.000000000000",
  510. fmt::format("{:%S}", std::chrono::duration<float, std::pico>(
  511. 1.54213895E+26)));
  512. }
  513. TEST(chrono_test, unsigned_duration) {
  514. EXPECT_EQ("42s", fmt::format("{}", std::chrono::duration<unsigned>(42)));
  515. }
  516. TEST(chrono_test, weekday) {
  517. auto loc = get_locale("ru_RU.UTF-8");
  518. std::locale::global(loc);
  519. auto mon = fmt::weekday(1);
  520. auto tm = std::tm();
  521. tm.tm_wday = static_cast<int>(mon.c_encoding());
  522. EXPECT_EQ(fmt::format("{}", mon), "Mon");
  523. EXPECT_EQ(fmt::format("{:%a}", tm), "Mon");
  524. if (loc != std::locale::classic()) {
  525. EXPECT_THAT((std::vector<std::string>{"пн", "Пн", "пнд", "Пнд"}),
  526. Contains(fmt::format(loc, "{:L}", mon)));
  527. EXPECT_THAT((std::vector<std::string>{"пн", "Пн", "пнд", "Пнд"}),
  528. Contains(fmt::format(loc, "{:%a}", tm)));
  529. }
  530. }
  531. TEST(chrono_test, cpp20_duration_subsecond_support) {
  532. using attoseconds = std::chrono::duration<long long, std::atto>;
  533. // Check that 18 digits of subsecond precision are supported.
  534. EXPECT_EQ(fmt::format("{:%S}", attoseconds{999999999999999999}),
  535. "00.999999999999999999");
  536. EXPECT_EQ(fmt::format("{:%S}", attoseconds{673231113420148734}),
  537. "00.673231113420148734");
  538. EXPECT_EQ(fmt::format("{:%S}", attoseconds{-673231113420148734}),
  539. "-00.673231113420148734");
  540. EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds{13420148734}),
  541. "13.420148734");
  542. EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds{-13420148734}),
  543. "-13.420148734");
  544. EXPECT_EQ(fmt::format("{:%S}", std::chrono::milliseconds{1234}), "01.234");
  545. {
  546. // Check that {:%H:%M:%S} is equivalent to {:%T}.
  547. auto dur = std::chrono::milliseconds{3601234};
  548. auto formatted_dur = fmt::format("{:%T}", dur);
  549. EXPECT_EQ(formatted_dur, "01:00:01.234");
  550. EXPECT_EQ(fmt::format("{:%H:%M:%S}", dur), formatted_dur);
  551. }
  552. using nanoseconds_dbl = std::chrono::duration<double, std::nano>;
  553. EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{-123456789}), "-00.123456789");
  554. EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{9123456789}), "09.123456789");
  555. // Verify that only the seconds part is extracted and printed.
  556. EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{99123456789}), "39.123456789");
  557. EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl{99123000000}), "39.123000000");
  558. {
  559. // Now the hour is printed, and we also test if negative doubles work.
  560. auto dur = nanoseconds_dbl{-99123456789};
  561. auto formatted_dur = fmt::format("{:%T}", dur);
  562. EXPECT_EQ(formatted_dur, "-00:01:39.123456789");
  563. EXPECT_EQ(fmt::format("{:%H:%M:%S}", dur), formatted_dur);
  564. }
  565. // Check that durations with precision greater than std::chrono::seconds have
  566. // fixed precision, and print zeros even if there is no fractional part.
  567. EXPECT_EQ(fmt::format("{:%S}", std::chrono::microseconds{7000000}),
  568. "07.000000");
  569. EXPECT_EQ(fmt::format("{:%S}", std::chrono::duration<long long, std::ratio<1, 3>>(1)),
  570. "00.333333");
  571. EXPECT_EQ(fmt::format("{:%S}", std::chrono::duration<long long, std::ratio<1, 7>>(1)),
  572. "00.142857");
  573. }
  574. #endif // FMT_STATIC_THOUSANDS_SEPARATOR