chrono-test.cc 24 KB

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