formatter.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___CHRONO_FORMATTER_H
  10. #define _LIBCPP___CHRONO_FORMATTER_H
  11. #include <__chrono/calendar.h>
  12. #include <__chrono/concepts.h>
  13. #include <__chrono/convert_to_tm.h>
  14. #include <__chrono/day.h>
  15. #include <__chrono/duration.h>
  16. #include <__chrono/file_clock.h>
  17. #include <__chrono/hh_mm_ss.h>
  18. #include <__chrono/month.h>
  19. #include <__chrono/month_weekday.h>
  20. #include <__chrono/monthday.h>
  21. #include <__chrono/ostream.h>
  22. #include <__chrono/parser_std_format_spec.h>
  23. #include <__chrono/statically_widen.h>
  24. #include <__chrono/system_clock.h>
  25. #include <__chrono/time_point.h>
  26. #include <__chrono/weekday.h>
  27. #include <__chrono/year.h>
  28. #include <__chrono/year_month.h>
  29. #include <__chrono/year_month_day.h>
  30. #include <__chrono/year_month_weekday.h>
  31. #include <__concepts/arithmetic.h>
  32. #include <__concepts/same_as.h>
  33. #include <__config>
  34. #include <__format/concepts.h>
  35. #include <__format/format_error.h>
  36. #include <__format/format_functions.h>
  37. #include <__format/format_parse_context.h>
  38. #include <__format/formatter.h>
  39. #include <__format/parser_std_format_spec.h>
  40. #include <__format/write_escaped.h>
  41. #include <__memory/addressof.h>
  42. #include <cmath>
  43. #include <ctime>
  44. #include <sstream>
  45. #include <string_view>
  46. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  47. # pragma GCC system_header
  48. #endif
  49. _LIBCPP_BEGIN_NAMESPACE_STD
  50. #if _LIBCPP_STD_VER >= 20
  51. namespace __formatter {
  52. /// Formats a time based on a tm struct.
  53. ///
  54. /// This formatter passes the formatting to time_put which uses strftime. When
  55. /// the value is outside the valid range it's unspecified what strftime will
  56. /// output. For example weekday 8 can print 1 when the day is processed modulo
  57. /// 7 since that handles the Sunday for 0-based weekday. It can also print 8 if
  58. /// 7 is handled as a special case.
  59. ///
  60. /// The Standard doesn't specify what to do in this case so the result depends
  61. /// on the result of the underlying code.
  62. ///
  63. /// \pre When the (abbreviated) weekday or month name are used, the caller
  64. /// validates whether the value is valid. So the caller handles that
  65. /// requirement of Table 97: Meaning of conversion specifiers
  66. /// [tab:time.format.spec].
  67. ///
  68. /// When no chrono-specs are provided it uses the stream formatter.
  69. // For tiny ratios it's not possible to convert a duration to a hh_mm_ss. This
  70. // fails compile-time due to the limited precision of the ratio (64-bit is too
  71. // small). Therefore a duration uses its own conversion.
  72. template <class _CharT, class _Rep, class _Period>
  73. _LIBCPP_HIDE_FROM_ABI void
  74. __format_sub_seconds(const chrono::duration<_Rep, _Period>& __value, basic_stringstream<_CharT>& __sstr) {
  75. __sstr << std::use_facet<numpunct<_CharT>>(__sstr.getloc()).decimal_point();
  76. using __duration = chrono::duration<_Rep, _Period>;
  77. auto __fraction = __value - chrono::duration_cast<chrono::seconds>(__value);
  78. if constexpr (chrono::treat_as_floating_point_v<_Rep>)
  79. // When the floating-point value has digits itself they are ignored based
  80. // on the wording in [tab:time.format.spec]
  81. // If the precision of the input cannot be exactly represented with
  82. // seconds, then the format is a decimal floating-point number with a
  83. // fixed format and a precision matching that of the precision of the
  84. // input (or to a microseconds precision if the conversion to
  85. // floating-point decimal seconds cannot be made within 18 fractional
  86. // digits).
  87. //
  88. // This matches the behaviour of MSVC STL, fmtlib interprets this
  89. // differently and uses 3 decimals.
  90. // https://godbolt.org/z/6dsbnW8ba
  91. std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
  92. _LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}.0f}"),
  93. chrono::duration_cast<typename chrono::hh_mm_ss<__duration>::precision>(__fraction).count(),
  94. chrono::hh_mm_ss<__duration>::fractional_width);
  95. else
  96. std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
  97. _LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}}"),
  98. chrono::duration_cast<typename chrono::hh_mm_ss<__duration>::precision>(__fraction).count(),
  99. chrono::hh_mm_ss<__duration>::fractional_width);
  100. }
  101. template <class _CharT, __is_time_point _Tp>
  102. _LIBCPP_HIDE_FROM_ABI void __format_sub_seconds(const _Tp& __value, basic_stringstream<_CharT>& __sstr) {
  103. __formatter::__format_sub_seconds(__value.time_since_epoch(), __sstr);
  104. }
  105. template <class _CharT, class _Duration>
  106. _LIBCPP_HIDE_FROM_ABI void
  107. __format_sub_seconds(const chrono::hh_mm_ss<_Duration>& __value, basic_stringstream<_CharT>& __sstr) {
  108. __sstr << std::use_facet<numpunct<_CharT>>(__sstr.getloc()).decimal_point();
  109. if constexpr (chrono::treat_as_floating_point_v<typename _Duration::rep>)
  110. std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
  111. _LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}.0f}"),
  112. __value.subseconds().count(),
  113. __value.fractional_width);
  114. else
  115. std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
  116. _LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}}"),
  117. __value.subseconds().count(),
  118. __value.fractional_width);
  119. }
  120. template <class _Tp>
  121. consteval bool __use_fraction() {
  122. if constexpr (__is_time_point<_Tp>)
  123. return chrono::hh_mm_ss<typename _Tp::duration>::fractional_width;
  124. else if constexpr (chrono::__is_duration<_Tp>::value)
  125. return chrono::hh_mm_ss<_Tp>::fractional_width;
  126. else if constexpr (__is_hh_mm_ss<_Tp>)
  127. return _Tp::fractional_width;
  128. else
  129. return false;
  130. }
  131. template <class _CharT>
  132. _LIBCPP_HIDE_FROM_ABI void __format_year(int __year, basic_stringstream<_CharT>& __sstr) {
  133. if (__year < 0) {
  134. __sstr << _CharT('-');
  135. __year = -__year;
  136. }
  137. // TODO FMT Write an issue
  138. // If the result has less than four digits it is zero-padded with 0 to two digits.
  139. // is less -> has less
  140. // left-padded -> zero-padded, otherwise the proper value would be 000-0.
  141. // Note according to the wording it should be left padded, which is odd.
  142. __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:04}"), __year);
  143. }
  144. template <class _CharT>
  145. _LIBCPP_HIDE_FROM_ABI void __format_century(int __year, basic_stringstream<_CharT>& __sstr) {
  146. // TODO FMT Write an issue
  147. // [tab:time.format.spec]
  148. // %C The year divided by 100 using floored division. If the result is a
  149. // single decimal digit, it is prefixed with 0.
  150. bool __negative = __year < 0;
  151. int __century = (__year - (99 * __negative)) / 100; // floored division
  152. __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02}"), __century);
  153. }
  154. template <class _CharT, class _Tp>
  155. _LIBCPP_HIDE_FROM_ABI void __format_chrono_using_chrono_specs(
  156. const _Tp& __value, basic_stringstream<_CharT>& __sstr, basic_string_view<_CharT> __chrono_specs) {
  157. tm __t = std::__convert_to_tm<tm>(__value);
  158. const auto& __facet = std::use_facet<time_put<_CharT>>(__sstr.getloc());
  159. for (auto __it = __chrono_specs.begin(); __it != __chrono_specs.end(); ++__it) {
  160. if (*__it == _CharT('%')) {
  161. auto __s = __it;
  162. ++__it;
  163. // We only handle the types that can't be directly handled by time_put.
  164. // (as an optimization n, t, and % are also handled directly.)
  165. switch (*__it) {
  166. case _CharT('n'):
  167. __sstr << _CharT('\n');
  168. break;
  169. case _CharT('t'):
  170. __sstr << _CharT('\t');
  171. break;
  172. case _CharT('%'):
  173. __sstr << *__it;
  174. break;
  175. case _CharT('C'): {
  176. // strftime's output is only defined in the range [00, 99].
  177. int __year = __t.tm_year + 1900;
  178. if (__year < 1000 || __year > 9999)
  179. __formatter::__format_century(__year, __sstr);
  180. else
  181. __facet.put(
  182. {__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  183. } break;
  184. case _CharT('j'):
  185. if constexpr (chrono::__is_duration<_Tp>::value)
  186. // Converting a duration where the period has a small ratio to days
  187. // may fail to compile. This due to loss of precision in the
  188. // conversion. In order to avoid that issue convert to seconds as
  189. // an intemediate step.
  190. __sstr << chrono::duration_cast<chrono::days>(chrono::duration_cast<chrono::seconds>(__value)).count();
  191. else
  192. __facet.put(
  193. {__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  194. break;
  195. case _CharT('q'):
  196. if constexpr (chrono::__is_duration<_Tp>::value) {
  197. __sstr << chrono::__units_suffix<_CharT, typename _Tp::period>();
  198. break;
  199. }
  200. __builtin_unreachable();
  201. case _CharT('Q'):
  202. // TODO FMT Determine the proper ideas
  203. // - Should it honour the precision?
  204. // - Shoult it honour the locale setting for the separators?
  205. // The wording for Q doesn't use the word locale and the effect of
  206. // precision is unspecified.
  207. //
  208. // MSVC STL ignores precision but uses separator
  209. // FMT honours precision and has a bug for separator
  210. // https://godbolt.org/z/78b7sMxns
  211. if constexpr (chrono::__is_duration<_Tp>::value) {
  212. __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{}"), __value.count());
  213. break;
  214. }
  215. __builtin_unreachable();
  216. case _CharT('S'):
  217. case _CharT('T'):
  218. __facet.put(
  219. {__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  220. if constexpr (__use_fraction<_Tp>())
  221. __formatter::__format_sub_seconds(__value, __sstr);
  222. break;
  223. // Unlike time_put and strftime the formatting library requires %Y
  224. //
  225. // [tab:time.format.spec]
  226. // The year as a decimal number. If the result is less than four digits
  227. // it is left-padded with 0 to four digits.
  228. //
  229. // This means years in the range (-1000, 1000) need manual formatting.
  230. // It's unclear whether %EY needs the same treatment. For example the
  231. // Japanese EY contains the era name and year. This is zero-padded to 2
  232. // digits in time_put (note that older glibc versions didn't do
  233. // padding.) However most eras won't reach 100 years, let alone 1000.
  234. // So padding to 4 digits seems unwanted for Japanese.
  235. //
  236. // The same applies to %Ex since that too depends on the era.
  237. //
  238. // %x the locale's date representation is currently doesn't handle the
  239. // zero-padding too.
  240. //
  241. // The 4 digits can be implemented better at a later time. On POSIX
  242. // systems the required information can be extracted by nl_langinfo
  243. // https://man7.org/linux/man-pages/man3/nl_langinfo.3.html
  244. //
  245. // Note since year < -1000 is expected to be rare it uses the more
  246. // expensive year routine.
  247. //
  248. // TODO FMT evaluate the comment above.
  249. # if defined(__GLIBC__) || defined(_AIX) || defined(_WIN32)
  250. case _CharT('y'):
  251. // Glibc fails for negative values, AIX for positive values too.
  252. __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02}"), (std::abs(__t.tm_year + 1900)) % 100);
  253. break;
  254. # endif // defined(__GLIBC__) || defined(_AIX) || defined(_WIN32)
  255. case _CharT('Y'):
  256. // Depending on the platform's libc the range of supported years is
  257. // limited. Intead of of testing all conditions use the internal
  258. // implementation unconditionally.
  259. __formatter::__format_year(__t.tm_year + 1900, __sstr);
  260. break;
  261. case _CharT('F'): {
  262. int __year = __t.tm_year + 1900;
  263. if (__year < 1000) {
  264. __formatter::__format_year(__year, __sstr);
  265. __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "-{:02}-{:02}"), __t.tm_mon + 1, __t.tm_mday);
  266. } else
  267. __facet.put(
  268. {__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  269. } break;
  270. case _CharT('Z'):
  271. // TODO FMT Add proper timezone support.
  272. __sstr << _LIBCPP_STATICALLY_WIDEN(_CharT, "UTC");
  273. break;
  274. case _CharT('O'):
  275. if constexpr (__use_fraction<_Tp>()) {
  276. // Handle OS using the normal representation for the non-fractional
  277. // part. There seems to be no locale information regarding how the
  278. // fractional part should be formatted.
  279. if (*(__it + 1) == 'S') {
  280. ++__it;
  281. __facet.put(
  282. {__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  283. __formatter::__format_sub_seconds(__value, __sstr);
  284. break;
  285. }
  286. }
  287. [[fallthrough]];
  288. case _CharT('E'):
  289. ++__it;
  290. [[fallthrough]];
  291. default:
  292. __facet.put(
  293. {__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  294. break;
  295. }
  296. } else {
  297. __sstr << *__it;
  298. }
  299. }
  300. }
  301. template <class _Tp>
  302. _LIBCPP_HIDE_FROM_ABI constexpr bool __weekday_ok(const _Tp& __value) {
  303. if constexpr (__is_time_point<_Tp>)
  304. return true;
  305. else if constexpr (same_as<_Tp, chrono::day>)
  306. return true;
  307. else if constexpr (same_as<_Tp, chrono::month>)
  308. return __value.ok();
  309. else if constexpr (same_as<_Tp, chrono::year>)
  310. return true;
  311. else if constexpr (same_as<_Tp, chrono::weekday>)
  312. return true;
  313. else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
  314. return true;
  315. else if constexpr (same_as<_Tp, chrono::weekday_last>)
  316. return true;
  317. else if constexpr (same_as<_Tp, chrono::month_day>)
  318. return true;
  319. else if constexpr (same_as<_Tp, chrono::month_day_last>)
  320. return true;
  321. else if constexpr (same_as<_Tp, chrono::month_weekday>)
  322. return true;
  323. else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
  324. return true;
  325. else if constexpr (same_as<_Tp, chrono::year_month>)
  326. return true;
  327. else if constexpr (same_as<_Tp, chrono::year_month_day>)
  328. return __value.ok();
  329. else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
  330. return __value.ok();
  331. else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
  332. return __value.weekday().ok();
  333. else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
  334. return __value.weekday().ok();
  335. else if constexpr (__is_hh_mm_ss<_Tp>)
  336. return true;
  337. else
  338. static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
  339. }
  340. template <class _Tp>
  341. _LIBCPP_HIDE_FROM_ABI constexpr bool __weekday_name_ok(const _Tp& __value) {
  342. if constexpr (__is_time_point<_Tp>)
  343. return true;
  344. else if constexpr (same_as<_Tp, chrono::day>)
  345. return true;
  346. else if constexpr (same_as<_Tp, chrono::month>)
  347. return __value.ok();
  348. else if constexpr (same_as<_Tp, chrono::year>)
  349. return true;
  350. else if constexpr (same_as<_Tp, chrono::weekday>)
  351. return __value.ok();
  352. else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
  353. return __value.weekday().ok();
  354. else if constexpr (same_as<_Tp, chrono::weekday_last>)
  355. return __value.weekday().ok();
  356. else if constexpr (same_as<_Tp, chrono::month_day>)
  357. return true;
  358. else if constexpr (same_as<_Tp, chrono::month_day_last>)
  359. return true;
  360. else if constexpr (same_as<_Tp, chrono::month_weekday>)
  361. return __value.weekday_indexed().ok();
  362. else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
  363. return __value.weekday_indexed().ok();
  364. else if constexpr (same_as<_Tp, chrono::year_month>)
  365. return true;
  366. else if constexpr (same_as<_Tp, chrono::year_month_day>)
  367. return __value.ok();
  368. else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
  369. return __value.ok();
  370. else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
  371. return __value.weekday().ok();
  372. else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
  373. return __value.weekday().ok();
  374. else if constexpr (__is_hh_mm_ss<_Tp>)
  375. return true;
  376. else
  377. static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
  378. }
  379. template <class _Tp>
  380. _LIBCPP_HIDE_FROM_ABI constexpr bool __date_ok(const _Tp& __value) {
  381. if constexpr (__is_time_point<_Tp>)
  382. return true;
  383. else if constexpr (same_as<_Tp, chrono::day>)
  384. return true;
  385. else if constexpr (same_as<_Tp, chrono::month>)
  386. return __value.ok();
  387. else if constexpr (same_as<_Tp, chrono::year>)
  388. return true;
  389. else if constexpr (same_as<_Tp, chrono::weekday>)
  390. return true;
  391. else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
  392. return true;
  393. else if constexpr (same_as<_Tp, chrono::weekday_last>)
  394. return true;
  395. else if constexpr (same_as<_Tp, chrono::month_day>)
  396. return true;
  397. else if constexpr (same_as<_Tp, chrono::month_day_last>)
  398. return true;
  399. else if constexpr (same_as<_Tp, chrono::month_weekday>)
  400. return true;
  401. else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
  402. return true;
  403. else if constexpr (same_as<_Tp, chrono::year_month>)
  404. return true;
  405. else if constexpr (same_as<_Tp, chrono::year_month_day>)
  406. return __value.ok();
  407. else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
  408. return __value.ok();
  409. else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
  410. return __value.ok();
  411. else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
  412. return __value.ok();
  413. else if constexpr (__is_hh_mm_ss<_Tp>)
  414. return true;
  415. else
  416. static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
  417. }
  418. template <class _Tp>
  419. _LIBCPP_HIDE_FROM_ABI constexpr bool __month_name_ok(const _Tp& __value) {
  420. if constexpr (__is_time_point<_Tp>)
  421. return true;
  422. else if constexpr (same_as<_Tp, chrono::day>)
  423. return true;
  424. else if constexpr (same_as<_Tp, chrono::month>)
  425. return __value.ok();
  426. else if constexpr (same_as<_Tp, chrono::year>)
  427. return true;
  428. else if constexpr (same_as<_Tp, chrono::weekday>)
  429. return true;
  430. else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
  431. return true;
  432. else if constexpr (same_as<_Tp, chrono::weekday_last>)
  433. return true;
  434. else if constexpr (same_as<_Tp, chrono::month_day>)
  435. return __value.month().ok();
  436. else if constexpr (same_as<_Tp, chrono::month_day_last>)
  437. return __value.month().ok();
  438. else if constexpr (same_as<_Tp, chrono::month_weekday>)
  439. return __value.month().ok();
  440. else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
  441. return __value.month().ok();
  442. else if constexpr (same_as<_Tp, chrono::year_month>)
  443. return __value.month().ok();
  444. else if constexpr (same_as<_Tp, chrono::year_month_day>)
  445. return __value.month().ok();
  446. else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
  447. return __value.month().ok();
  448. else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
  449. return __value.month().ok();
  450. else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
  451. return __value.month().ok();
  452. else if constexpr (__is_hh_mm_ss<_Tp>)
  453. return true;
  454. else
  455. static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
  456. }
  457. template <class _CharT, class _Tp, class _FormatContext>
  458. _LIBCPP_HIDE_FROM_ABI auto
  459. __format_chrono(const _Tp& __value,
  460. _FormatContext& __ctx,
  461. __format_spec::__parsed_specifications<_CharT> __specs,
  462. basic_string_view<_CharT> __chrono_specs) {
  463. basic_stringstream<_CharT> __sstr;
  464. // [time.format]/2
  465. // 2.1 - the "C" locale if the L option is not present in chrono-format-spec, otherwise
  466. // 2.2 - the locale passed to the formatting function if any, otherwise
  467. // 2.3 - the global locale.
  468. // Note that the __ctx's locale() call does 2.2 and 2.3.
  469. if (__specs.__chrono_.__locale_specific_form_)
  470. __sstr.imbue(__ctx.locale());
  471. else
  472. __sstr.imbue(locale::classic());
  473. if (__chrono_specs.empty())
  474. __sstr << __value;
  475. else {
  476. if constexpr (chrono::__is_duration<_Tp>::value) {
  477. if (__value < __value.zero())
  478. __sstr << _CharT('-');
  479. __formatter::__format_chrono_using_chrono_specs(chrono::abs(__value), __sstr, __chrono_specs);
  480. // TODO FMT When keeping the precision it will truncate the string.
  481. // Note that the behaviour what the precision does isn't specified.
  482. __specs.__precision_ = -1;
  483. } else {
  484. // Test __weekday_name_ before __weekday_ to give a better error.
  485. if (__specs.__chrono_.__weekday_name_ && !__formatter::__weekday_name_ok(__value))
  486. std::__throw_format_error("Formatting a weekday name needs a valid weekday");
  487. if (__specs.__chrono_.__weekday_ && !__formatter::__weekday_ok(__value))
  488. std::__throw_format_error("Formatting a weekday needs a valid weekday");
  489. if (__specs.__chrono_.__day_of_year_ && !__formatter::__date_ok(__value))
  490. std::__throw_format_error("Formatting a day of year needs a valid date");
  491. if (__specs.__chrono_.__week_of_year_ && !__formatter::__date_ok(__value))
  492. std::__throw_format_error("Formatting a week of year needs a valid date");
  493. if (__specs.__chrono_.__month_name_ && !__formatter::__month_name_ok(__value))
  494. std::__throw_format_error("Formatting a month name from an invalid month number");
  495. if constexpr (__is_hh_mm_ss<_Tp>) {
  496. // Note this is a pedantic intepretation of the Standard. A hh_mm_ss
  497. // is no longer a time_of_day and can store an arbitrary number of
  498. // hours. A number of hours in a 12 or 24 hour clock can't represent
  499. // 24 hours or more. The functions std::chrono::make12 and
  500. // std::chrono::make24 reaffirm this view point.
  501. //
  502. // Interestingly this will be the only output stream function that
  503. // throws.
  504. //
  505. // TODO FMT The wording probably needs to be adapted to
  506. // - The displayed hours is hh_mm_ss.hours() % 24
  507. // - It should probably allow %j in the same fashion as duration.
  508. // - The stream formatter should change its output when hours >= 24
  509. // - Write it as not valid,
  510. // - or write the number of days.
  511. if (__specs.__chrono_.__hour_ && __value.hours().count() > 23)
  512. std::__throw_format_error("Formatting a hour needs a valid value");
  513. if (__value.is_negative())
  514. __sstr << _CharT('-');
  515. }
  516. __formatter::__format_chrono_using_chrono_specs(__value, __sstr, __chrono_specs);
  517. }
  518. }
  519. return __formatter::__write_string(__sstr.view(), __ctx.out(), __specs);
  520. }
  521. } // namespace __formatter
  522. template <__fmt_char_type _CharT>
  523. struct _LIBCPP_TEMPLATE_VIS __formatter_chrono {
  524. public:
  525. template <class _ParseContext>
  526. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator
  527. __parse(_ParseContext& __ctx, __format_spec::__fields __fields, __format_spec::__flags __flags) {
  528. return __parser_.__parse(__ctx, __fields, __flags);
  529. }
  530. template <class _Tp, class _FormatContext>
  531. _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _Tp& __value, _FormatContext& __ctx) const {
  532. return __formatter::__format_chrono(
  533. __value, __ctx, __parser_.__parser_.__get_parsed_chrono_specifications(__ctx), __parser_.__chrono_specs_);
  534. }
  535. __format_spec::__parser_chrono<_CharT> __parser_;
  536. };
  537. template <class _Duration, __fmt_char_type _CharT>
  538. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::sys_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
  539. public:
  540. using _Base = __formatter_chrono<_CharT>;
  541. template <class _ParseContext>
  542. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  543. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__clock);
  544. }
  545. };
  546. template <class _Duration, __fmt_char_type _CharT>
  547. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::file_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
  548. public:
  549. using _Base = __formatter_chrono<_CharT>;
  550. template <class _ParseContext>
  551. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  552. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__clock);
  553. }
  554. };
  555. template <class _Duration, __fmt_char_type _CharT>
  556. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::local_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
  557. public:
  558. using _Base = __formatter_chrono<_CharT>;
  559. template <class _ParseContext>
  560. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  561. // The flags are not __clock since there is no associated time-zone.
  562. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date_time);
  563. }
  564. };
  565. template <class _Rep, class _Period, __fmt_char_type _CharT>
  566. struct formatter<chrono::duration<_Rep, _Period>, _CharT> : public __formatter_chrono<_CharT> {
  567. public:
  568. using _Base = __formatter_chrono<_CharT>;
  569. template <class _ParseContext>
  570. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  571. // [time.format]/1
  572. // Giving a precision specification in the chrono-format-spec is valid only
  573. // for std::chrono::duration types where the representation type Rep is a
  574. // floating-point type. For all other Rep types, an exception of type
  575. // format_error is thrown if the chrono-format-spec contains a precision
  576. // specification.
  577. //
  578. // Note this doesn't refer to chrono::treat_as_floating_point_v<_Rep>.
  579. if constexpr (std::floating_point<_Rep>)
  580. return _Base::__parse(__ctx, __format_spec::__fields_chrono_fractional, __format_spec::__flags::__duration);
  581. else
  582. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__duration);
  583. }
  584. };
  585. template <__fmt_char_type _CharT>
  586. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::day, _CharT> : public __formatter_chrono<_CharT> {
  587. public:
  588. using _Base = __formatter_chrono<_CharT>;
  589. template <class _ParseContext>
  590. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  591. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__day);
  592. }
  593. };
  594. template <__fmt_char_type _CharT>
  595. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month, _CharT> : public __formatter_chrono<_CharT> {
  596. public:
  597. using _Base = __formatter_chrono<_CharT>;
  598. template <class _ParseContext>
  599. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  600. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month);
  601. }
  602. };
  603. template <__fmt_char_type _CharT>
  604. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year, _CharT> : public __formatter_chrono<_CharT> {
  605. public:
  606. using _Base = __formatter_chrono<_CharT>;
  607. template <class _ParseContext>
  608. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  609. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__year);
  610. }
  611. };
  612. template <__fmt_char_type _CharT>
  613. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday, _CharT> : public __formatter_chrono<_CharT> {
  614. public:
  615. using _Base = __formatter_chrono<_CharT>;
  616. template <class _ParseContext>
  617. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  618. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__weekday);
  619. }
  620. };
  621. template <__fmt_char_type _CharT>
  622. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday_indexed, _CharT> : public __formatter_chrono<_CharT> {
  623. public:
  624. using _Base = __formatter_chrono<_CharT>;
  625. template <class _ParseContext>
  626. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  627. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__weekday);
  628. }
  629. };
  630. template <__fmt_char_type _CharT>
  631. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday_last, _CharT> : public __formatter_chrono<_CharT> {
  632. public:
  633. using _Base = __formatter_chrono<_CharT>;
  634. template <class _ParseContext>
  635. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  636. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__weekday);
  637. }
  638. };
  639. template <__fmt_char_type _CharT>
  640. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_day, _CharT> : public __formatter_chrono<_CharT> {
  641. public:
  642. using _Base = __formatter_chrono<_CharT>;
  643. template <class _ParseContext>
  644. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  645. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_day);
  646. }
  647. };
  648. template <__fmt_char_type _CharT>
  649. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_day_last, _CharT> : public __formatter_chrono<_CharT> {
  650. public:
  651. using _Base = __formatter_chrono<_CharT>;
  652. template <class _ParseContext>
  653. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  654. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month);
  655. }
  656. };
  657. template <__fmt_char_type _CharT>
  658. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_weekday, _CharT> : public __formatter_chrono<_CharT> {
  659. public:
  660. using _Base = __formatter_chrono<_CharT>;
  661. template <class _ParseContext>
  662. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  663. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_weekday);
  664. }
  665. };
  666. template <__fmt_char_type _CharT>
  667. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_weekday_last, _CharT> : public __formatter_chrono<_CharT> {
  668. public:
  669. using _Base = __formatter_chrono<_CharT>;
  670. template <class _ParseContext>
  671. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  672. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_weekday);
  673. }
  674. };
  675. template <__fmt_char_type _CharT>
  676. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month, _CharT> : public __formatter_chrono<_CharT> {
  677. public:
  678. using _Base = __formatter_chrono<_CharT>;
  679. template <class _ParseContext>
  680. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  681. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__year_month);
  682. }
  683. };
  684. template <__fmt_char_type _CharT>
  685. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_day, _CharT> : public __formatter_chrono<_CharT> {
  686. public:
  687. using _Base = __formatter_chrono<_CharT>;
  688. template <class _ParseContext>
  689. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  690. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
  691. }
  692. };
  693. template <__fmt_char_type _CharT>
  694. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_day_last, _CharT> : public __formatter_chrono<_CharT> {
  695. public:
  696. using _Base = __formatter_chrono<_CharT>;
  697. template <class _ParseContext>
  698. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  699. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
  700. }
  701. };
  702. template <__fmt_char_type _CharT>
  703. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_weekday, _CharT> : public __formatter_chrono<_CharT> {
  704. public:
  705. using _Base = __formatter_chrono<_CharT>;
  706. template <class _ParseContext>
  707. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  708. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
  709. }
  710. };
  711. template <__fmt_char_type _CharT>
  712. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_weekday_last, _CharT> : public __formatter_chrono<_CharT> {
  713. public:
  714. using _Base = __formatter_chrono<_CharT>;
  715. template <class _ParseContext>
  716. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  717. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
  718. }
  719. };
  720. template <class _Duration, __fmt_char_type _CharT>
  721. struct formatter<chrono::hh_mm_ss<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
  722. public:
  723. using _Base = __formatter_chrono<_CharT>;
  724. template <class _ParseContext>
  725. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  726. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__time);
  727. }
  728. };
  729. #endif // if _LIBCPP_STD_VER >= 20
  730. _LIBCPP_END_NAMESPACE_STD
  731. #endif // _LIBCPP___CHRONO_FORMATTER_H