formatter.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  182. } break;
  183. case _CharT('j'):
  184. if constexpr (chrono::__is_duration<_Tp>::value)
  185. // Converting a duration where the period has a small ratio to days
  186. // may fail to compile. This due to loss of precision in the
  187. // conversion. In order to avoid that issue convert to seconds as
  188. // an intemediate step.
  189. __sstr << chrono::duration_cast<chrono::days>(chrono::duration_cast<chrono::seconds>(__value)).count();
  190. else
  191. __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  192. break;
  193. case _CharT('q'):
  194. if constexpr (chrono::__is_duration<_Tp>::value) {
  195. __sstr << chrono::__units_suffix<_CharT, typename _Tp::period>();
  196. break;
  197. }
  198. __builtin_unreachable();
  199. case _CharT('Q'):
  200. // TODO FMT Determine the proper ideas
  201. // - Should it honour the precision?
  202. // - Shoult it honour the locale setting for the separators?
  203. // The wording for Q doesn't use the word locale and the effect of
  204. // precision is unspecified.
  205. //
  206. // MSVC STL ignores precision but uses separator
  207. // FMT honours precision and has a bug for separator
  208. // https://godbolt.org/z/78b7sMxns
  209. if constexpr (chrono::__is_duration<_Tp>::value) {
  210. __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{}"), __value.count());
  211. break;
  212. }
  213. __builtin_unreachable();
  214. case _CharT('S'):
  215. case _CharT('T'):
  216. __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  217. if constexpr (__use_fraction<_Tp>())
  218. __formatter::__format_sub_seconds(__value, __sstr);
  219. break;
  220. // Unlike time_put and strftime the formatting library requires %Y
  221. //
  222. // [tab:time.format.spec]
  223. // The year as a decimal number. If the result is less than four digits
  224. // it is left-padded with 0 to four digits.
  225. //
  226. // This means years in the range (-1000, 1000) need manual formatting.
  227. // It's unclear whether %EY needs the same treatment. For example the
  228. // Japanese EY contains the era name and year. This is zero-padded to 2
  229. // digits in time_put (note that older glibc versions didn't do
  230. // padding.) However most eras won't reach 100 years, let alone 1000.
  231. // So padding to 4 digits seems unwanted for Japanese.
  232. //
  233. // The same applies to %Ex since that too depends on the era.
  234. //
  235. // %x the locale's date representation is currently doesn't handle the
  236. // zero-padding too.
  237. //
  238. // The 4 digits can be implemented better at a later time. On POSIX
  239. // systems the required information can be extracted by nl_langinfo
  240. // https://man7.org/linux/man-pages/man3/nl_langinfo.3.html
  241. //
  242. // Note since year < -1000 is expected to be rare it uses the more
  243. // expensive year routine.
  244. //
  245. // TODO FMT evaluate the comment above.
  246. # if defined(__GLIBC__) || defined(_AIX) || defined(_WIN32)
  247. case _CharT('y'):
  248. // Glibc fails for negative values, AIX for positive values too.
  249. __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02}"), (std::abs(__t.tm_year + 1900)) % 100);
  250. break;
  251. # endif // defined(__GLIBC__) || defined(_AIX) || defined(_WIN32)
  252. case _CharT('Y'):
  253. // Depending on the platform's libc the range of supported years is
  254. // limited. Intead of of testing all conditions use the internal
  255. // implementation unconditionally.
  256. __formatter::__format_year(__t.tm_year + 1900, __sstr);
  257. break;
  258. case _CharT('F'): {
  259. int __year = __t.tm_year + 1900;
  260. if (__year < 1000) {
  261. __formatter::__format_year(__year, __sstr);
  262. __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "-{:02}-{:02}"), __t.tm_mon + 1, __t.tm_mday);
  263. } else
  264. __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  265. } break;
  266. case _CharT('Z'):
  267. // TODO FMT Add proper timezone support.
  268. __sstr << _LIBCPP_STATICALLY_WIDEN(_CharT, "UTC");
  269. break;
  270. case _CharT('O'):
  271. if constexpr (__use_fraction<_Tp>()) {
  272. // Handle OS using the normal representation for the non-fractional
  273. // part. There seems to be no locale information regarding how the
  274. // fractional part should be formatted.
  275. if (*(__it + 1) == 'S') {
  276. ++__it;
  277. __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  278. __formatter::__format_sub_seconds(__value, __sstr);
  279. break;
  280. }
  281. }
  282. [[fallthrough]];
  283. case _CharT('E'):
  284. ++__it;
  285. [[fallthrough]];
  286. default:
  287. __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
  288. break;
  289. }
  290. } else {
  291. __sstr << *__it;
  292. }
  293. }
  294. }
  295. template <class _Tp>
  296. _LIBCPP_HIDE_FROM_ABI constexpr bool __weekday_ok(const _Tp& __value) {
  297. if constexpr (__is_time_point<_Tp>)
  298. return true;
  299. else if constexpr (same_as<_Tp, chrono::day>)
  300. return true;
  301. else if constexpr (same_as<_Tp, chrono::month>)
  302. return __value.ok();
  303. else if constexpr (same_as<_Tp, chrono::year>)
  304. return true;
  305. else if constexpr (same_as<_Tp, chrono::weekday>)
  306. return true;
  307. else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
  308. return true;
  309. else if constexpr (same_as<_Tp, chrono::weekday_last>)
  310. return true;
  311. else if constexpr (same_as<_Tp, chrono::month_day>)
  312. return true;
  313. else if constexpr (same_as<_Tp, chrono::month_day_last>)
  314. return true;
  315. else if constexpr (same_as<_Tp, chrono::month_weekday>)
  316. return true;
  317. else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
  318. return true;
  319. else if constexpr (same_as<_Tp, chrono::year_month>)
  320. return true;
  321. else if constexpr (same_as<_Tp, chrono::year_month_day>)
  322. return __value.ok();
  323. else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
  324. return __value.ok();
  325. else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
  326. return __value.weekday().ok();
  327. else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
  328. return __value.weekday().ok();
  329. else if constexpr (__is_hh_mm_ss<_Tp>)
  330. return true;
  331. else
  332. static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
  333. }
  334. template <class _Tp>
  335. _LIBCPP_HIDE_FROM_ABI constexpr bool __weekday_name_ok(const _Tp& __value) {
  336. if constexpr (__is_time_point<_Tp>)
  337. return true;
  338. else if constexpr (same_as<_Tp, chrono::day>)
  339. return true;
  340. else if constexpr (same_as<_Tp, chrono::month>)
  341. return __value.ok();
  342. else if constexpr (same_as<_Tp, chrono::year>)
  343. return true;
  344. else if constexpr (same_as<_Tp, chrono::weekday>)
  345. return __value.ok();
  346. else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
  347. return __value.weekday().ok();
  348. else if constexpr (same_as<_Tp, chrono::weekday_last>)
  349. return __value.weekday().ok();
  350. else if constexpr (same_as<_Tp, chrono::month_day>)
  351. return true;
  352. else if constexpr (same_as<_Tp, chrono::month_day_last>)
  353. return true;
  354. else if constexpr (same_as<_Tp, chrono::month_weekday>)
  355. return __value.weekday_indexed().ok();
  356. else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
  357. return __value.weekday_indexed().ok();
  358. else if constexpr (same_as<_Tp, chrono::year_month>)
  359. return true;
  360. else if constexpr (same_as<_Tp, chrono::year_month_day>)
  361. return __value.ok();
  362. else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
  363. return __value.ok();
  364. else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
  365. return __value.weekday().ok();
  366. else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
  367. return __value.weekday().ok();
  368. else if constexpr (__is_hh_mm_ss<_Tp>)
  369. return true;
  370. else
  371. static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
  372. }
  373. template <class _Tp>
  374. _LIBCPP_HIDE_FROM_ABI constexpr bool __date_ok(const _Tp& __value) {
  375. if constexpr (__is_time_point<_Tp>)
  376. return true;
  377. else if constexpr (same_as<_Tp, chrono::day>)
  378. return true;
  379. else if constexpr (same_as<_Tp, chrono::month>)
  380. return __value.ok();
  381. else if constexpr (same_as<_Tp, chrono::year>)
  382. return true;
  383. else if constexpr (same_as<_Tp, chrono::weekday>)
  384. return true;
  385. else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
  386. return true;
  387. else if constexpr (same_as<_Tp, chrono::weekday_last>)
  388. return true;
  389. else if constexpr (same_as<_Tp, chrono::month_day>)
  390. return true;
  391. else if constexpr (same_as<_Tp, chrono::month_day_last>)
  392. return true;
  393. else if constexpr (same_as<_Tp, chrono::month_weekday>)
  394. return true;
  395. else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
  396. return true;
  397. else if constexpr (same_as<_Tp, chrono::year_month>)
  398. return true;
  399. else if constexpr (same_as<_Tp, chrono::year_month_day>)
  400. return __value.ok();
  401. else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
  402. return __value.ok();
  403. else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
  404. return __value.ok();
  405. else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
  406. return __value.ok();
  407. else if constexpr (__is_hh_mm_ss<_Tp>)
  408. return true;
  409. else
  410. static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
  411. }
  412. template <class _Tp>
  413. _LIBCPP_HIDE_FROM_ABI constexpr bool __month_name_ok(const _Tp& __value) {
  414. if constexpr (__is_time_point<_Tp>)
  415. return true;
  416. else if constexpr (same_as<_Tp, chrono::day>)
  417. return true;
  418. else if constexpr (same_as<_Tp, chrono::month>)
  419. return __value.ok();
  420. else if constexpr (same_as<_Tp, chrono::year>)
  421. return true;
  422. else if constexpr (same_as<_Tp, chrono::weekday>)
  423. return true;
  424. else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
  425. return true;
  426. else if constexpr (same_as<_Tp, chrono::weekday_last>)
  427. return true;
  428. else if constexpr (same_as<_Tp, chrono::month_day>)
  429. return __value.month().ok();
  430. else if constexpr (same_as<_Tp, chrono::month_day_last>)
  431. return __value.month().ok();
  432. else if constexpr (same_as<_Tp, chrono::month_weekday>)
  433. return __value.month().ok();
  434. else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
  435. return __value.month().ok();
  436. else if constexpr (same_as<_Tp, chrono::year_month>)
  437. return __value.month().ok();
  438. else if constexpr (same_as<_Tp, chrono::year_month_day>)
  439. return __value.month().ok();
  440. else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
  441. return __value.month().ok();
  442. else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
  443. return __value.month().ok();
  444. else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
  445. return __value.month().ok();
  446. else if constexpr (__is_hh_mm_ss<_Tp>)
  447. return true;
  448. else
  449. static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
  450. }
  451. template <class _CharT, class _Tp, class _FormatContext>
  452. _LIBCPP_HIDE_FROM_ABI auto
  453. __format_chrono(const _Tp& __value,
  454. _FormatContext& __ctx,
  455. __format_spec::__parsed_specifications<_CharT> __specs,
  456. basic_string_view<_CharT> __chrono_specs) {
  457. basic_stringstream<_CharT> __sstr;
  458. // [time.format]/2
  459. // 2.1 - the "C" locale if the L option is not present in chrono-format-spec, otherwise
  460. // 2.2 - the locale passed to the formatting function if any, otherwise
  461. // 2.3 - the global locale.
  462. // Note that the __ctx's locale() call does 2.2 and 2.3.
  463. if (__specs.__chrono_.__locale_specific_form_)
  464. __sstr.imbue(__ctx.locale());
  465. else
  466. __sstr.imbue(locale::classic());
  467. if (__chrono_specs.empty())
  468. __sstr << __value;
  469. else {
  470. if constexpr (chrono::__is_duration<_Tp>::value) {
  471. if (__value < __value.zero())
  472. __sstr << _CharT('-');
  473. __formatter::__format_chrono_using_chrono_specs(chrono::abs(__value), __sstr, __chrono_specs);
  474. // TODO FMT When keeping the precision it will truncate the string.
  475. // Note that the behaviour what the precision does isn't specified.
  476. __specs.__precision_ = -1;
  477. } else {
  478. // Test __weekday_name_ before __weekday_ to give a better error.
  479. if (__specs.__chrono_.__weekday_name_ && !__formatter::__weekday_name_ok(__value))
  480. std::__throw_format_error("Formatting a weekday name needs a valid weekday");
  481. if (__specs.__chrono_.__weekday_ && !__formatter::__weekday_ok(__value))
  482. std::__throw_format_error("Formatting a weekday needs a valid weekday");
  483. if (__specs.__chrono_.__day_of_year_ && !__formatter::__date_ok(__value))
  484. std::__throw_format_error("Formatting a day of year needs a valid date");
  485. if (__specs.__chrono_.__week_of_year_ && !__formatter::__date_ok(__value))
  486. std::__throw_format_error("Formatting a week of year needs a valid date");
  487. if (__specs.__chrono_.__month_name_ && !__formatter::__month_name_ok(__value))
  488. std::__throw_format_error("Formatting a month name from an invalid month number");
  489. if constexpr (__is_hh_mm_ss<_Tp>) {
  490. // Note this is a pedantic intepretation of the Standard. A hh_mm_ss
  491. // is no longer a time_of_day and can store an arbitrary number of
  492. // hours. A number of hours in a 12 or 24 hour clock can't represent
  493. // 24 hours or more. The functions std::chrono::make12 and
  494. // std::chrono::make24 reaffirm this view point.
  495. //
  496. // Interestingly this will be the only output stream function that
  497. // throws.
  498. //
  499. // TODO FMT The wording probably needs to be adapted to
  500. // - The displayed hours is hh_mm_ss.hours() % 24
  501. // - It should probably allow %j in the same fashion as duration.
  502. // - The stream formatter should change its output when hours >= 24
  503. // - Write it as not valid,
  504. // - or write the number of days.
  505. if (__specs.__chrono_.__hour_ && __value.hours().count() > 23)
  506. std::__throw_format_error("Formatting a hour needs a valid value");
  507. if (__value.is_negative())
  508. __sstr << _CharT('-');
  509. }
  510. __formatter::__format_chrono_using_chrono_specs(__value, __sstr, __chrono_specs);
  511. }
  512. }
  513. return __formatter::__write_string(__sstr.view(), __ctx.out(), __specs);
  514. }
  515. } // namespace __formatter
  516. template <__fmt_char_type _CharT>
  517. struct _LIBCPP_TEMPLATE_VIS __formatter_chrono {
  518. public:
  519. template <class _ParseContext>
  520. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator
  521. __parse(_ParseContext& __ctx, __format_spec::__fields __fields, __format_spec::__flags __flags) {
  522. return __parser_.__parse(__ctx, __fields, __flags);
  523. }
  524. template <class _Tp, class _FormatContext>
  525. _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _Tp& __value, _FormatContext& __ctx) const {
  526. return __formatter::__format_chrono(
  527. __value, __ctx, __parser_.__parser_.__get_parsed_chrono_specifications(__ctx), __parser_.__chrono_specs_);
  528. }
  529. __format_spec::__parser_chrono<_CharT> __parser_;
  530. };
  531. template <class _Duration, __fmt_char_type _CharT>
  532. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::sys_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
  533. public:
  534. using _Base = __formatter_chrono<_CharT>;
  535. template <class _ParseContext>
  536. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  537. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__clock);
  538. }
  539. };
  540. template <class _Duration, __fmt_char_type _CharT>
  541. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::file_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
  542. public:
  543. using _Base = __formatter_chrono<_CharT>;
  544. template <class _ParseContext>
  545. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  546. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__clock);
  547. }
  548. };
  549. template <class _Duration, __fmt_char_type _CharT>
  550. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::local_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
  551. public:
  552. using _Base = __formatter_chrono<_CharT>;
  553. template <class _ParseContext>
  554. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  555. // The flags are not __clock since there is no associated time-zone.
  556. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date_time);
  557. }
  558. };
  559. template <class _Rep, class _Period, __fmt_char_type _CharT>
  560. struct formatter<chrono::duration<_Rep, _Period>, _CharT> : public __formatter_chrono<_CharT> {
  561. public:
  562. using _Base = __formatter_chrono<_CharT>;
  563. template <class _ParseContext>
  564. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  565. // [time.format]/1
  566. // Giving a precision specification in the chrono-format-spec is valid only
  567. // for std::chrono::duration types where the representation type Rep is a
  568. // floating-point type. For all other Rep types, an exception of type
  569. // format_error is thrown if the chrono-format-spec contains a precision
  570. // specification.
  571. //
  572. // Note this doesn't refer to chrono::treat_as_floating_point_v<_Rep>.
  573. if constexpr (std::floating_point<_Rep>)
  574. return _Base::__parse(__ctx, __format_spec::__fields_chrono_fractional, __format_spec::__flags::__duration);
  575. else
  576. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__duration);
  577. }
  578. };
  579. template <__fmt_char_type _CharT>
  580. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::day, _CharT>
  581. : public __formatter_chrono<_CharT> {
  582. public:
  583. using _Base = __formatter_chrono<_CharT>;
  584. template <class _ParseContext>
  585. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  586. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__day);
  587. }
  588. };
  589. template <__fmt_char_type _CharT>
  590. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month, _CharT>
  591. : public __formatter_chrono<_CharT> {
  592. public:
  593. using _Base = __formatter_chrono<_CharT>;
  594. template <class _ParseContext>
  595. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  596. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month);
  597. }
  598. };
  599. template <__fmt_char_type _CharT>
  600. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year, _CharT>
  601. : public __formatter_chrono<_CharT> {
  602. public:
  603. using _Base = __formatter_chrono<_CharT>;
  604. template <class _ParseContext>
  605. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  606. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__year);
  607. }
  608. };
  609. template <__fmt_char_type _CharT>
  610. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday, _CharT>
  611. : public __formatter_chrono<_CharT> {
  612. public:
  613. using _Base = __formatter_chrono<_CharT>;
  614. template <class _ParseContext>
  615. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  616. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__weekday);
  617. }
  618. };
  619. template <__fmt_char_type _CharT>
  620. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday_indexed, _CharT>
  621. : public __formatter_chrono<_CharT> {
  622. public:
  623. using _Base = __formatter_chrono<_CharT>;
  624. template <class _ParseContext>
  625. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  626. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__weekday);
  627. }
  628. };
  629. template <__fmt_char_type _CharT>
  630. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday_last, _CharT>
  631. : 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>
  641. : public __formatter_chrono<_CharT> {
  642. public:
  643. using _Base = __formatter_chrono<_CharT>;
  644. template <class _ParseContext>
  645. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  646. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_day);
  647. }
  648. };
  649. template <__fmt_char_type _CharT>
  650. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_day_last, _CharT>
  651. : public __formatter_chrono<_CharT> {
  652. public:
  653. using _Base = __formatter_chrono<_CharT>;
  654. template <class _ParseContext>
  655. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  656. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month);
  657. }
  658. };
  659. template <__fmt_char_type _CharT>
  660. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_weekday, _CharT>
  661. : public __formatter_chrono<_CharT> {
  662. public:
  663. using _Base = __formatter_chrono<_CharT>;
  664. template <class _ParseContext>
  665. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  666. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_weekday);
  667. }
  668. };
  669. template <__fmt_char_type _CharT>
  670. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_weekday_last, _CharT>
  671. : public __formatter_chrono<_CharT> {
  672. public:
  673. using _Base = __formatter_chrono<_CharT>;
  674. template <class _ParseContext>
  675. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  676. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_weekday);
  677. }
  678. };
  679. template <__fmt_char_type _CharT>
  680. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month, _CharT>
  681. : public __formatter_chrono<_CharT> {
  682. public:
  683. using _Base = __formatter_chrono<_CharT>;
  684. template <class _ParseContext>
  685. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  686. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__year_month);
  687. }
  688. };
  689. template <__fmt_char_type _CharT>
  690. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_day, _CharT>
  691. : public __formatter_chrono<_CharT> {
  692. public:
  693. using _Base = __formatter_chrono<_CharT>;
  694. template <class _ParseContext>
  695. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  696. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
  697. }
  698. };
  699. template <__fmt_char_type _CharT>
  700. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_day_last, _CharT>
  701. : public __formatter_chrono<_CharT> {
  702. public:
  703. using _Base = __formatter_chrono<_CharT>;
  704. template <class _ParseContext>
  705. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  706. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
  707. }
  708. };
  709. template <__fmt_char_type _CharT>
  710. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_weekday, _CharT>
  711. : public __formatter_chrono<_CharT> {
  712. public:
  713. using _Base = __formatter_chrono<_CharT>;
  714. template <class _ParseContext>
  715. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  716. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
  717. }
  718. };
  719. template <__fmt_char_type _CharT>
  720. struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_weekday_last, _CharT>
  721. : 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::__date);
  727. }
  728. };
  729. template <class _Duration, __fmt_char_type _CharT>
  730. struct formatter<chrono::hh_mm_ss<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
  731. public:
  732. using _Base = __formatter_chrono<_CharT>;
  733. template <class _ParseContext>
  734. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  735. return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__time);
  736. }
  737. };
  738. #endif // if _LIBCPP_STD_VER >= 20
  739. _LIBCPP_END_NAMESPACE_STD
  740. #endif // _LIBCPP___CHRONO_FORMATTER_H