ostream.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_OSTREAM_H
  10. #define _LIBCPP___CHRONO_OSTREAM_H
  11. #include <__chrono/day.h>
  12. #include <__chrono/duration.h>
  13. #include <__chrono/month.h>
  14. #include <__chrono/month_weekday.h>
  15. #include <__chrono/monthday.h>
  16. #include <__chrono/statically_widen.h>
  17. #include <__chrono/weekday.h>
  18. #include <__chrono/year.h>
  19. #include <__chrono/year_month.h>
  20. #include <__chrono/year_month_day.h>
  21. #include <__chrono/year_month_weekday.h>
  22. #include <__concepts/same_as.h>
  23. #include <__config>
  24. #include <__format/format_functions.h>
  25. #include <ostream>
  26. #include <ratio>
  27. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  28. # pragma GCC system_header
  29. #endif
  30. _LIBCPP_BEGIN_NAMESPACE_STD
  31. #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
  32. namespace chrono {
  33. // Depending on the type the return is a const _CharT* or a basic_string<_CharT>
  34. template <class _CharT, class _Period>
  35. _LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
  36. // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
  37. if constexpr (same_as<typename _Period::type, atto>)
  38. return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
  39. else if constexpr (same_as<typename _Period::type, femto>)
  40. return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
  41. else if constexpr (same_as<typename _Period::type, pico>)
  42. return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
  43. else if constexpr (same_as<typename _Period::type, nano>)
  44. return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
  45. else if constexpr (same_as<typename _Period::type, micro>)
  46. # ifndef _LIBCPP_HAS_NO_UNICODE
  47. return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
  48. # else
  49. return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
  50. # endif
  51. else if constexpr (same_as<typename _Period::type, milli>)
  52. return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
  53. else if constexpr (same_as<typename _Period::type, centi>)
  54. return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
  55. else if constexpr (same_as<typename _Period::type, deci>)
  56. return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
  57. else if constexpr (same_as<typename _Period::type, ratio<1>>)
  58. return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
  59. else if constexpr (same_as<typename _Period::type, deca>)
  60. return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
  61. else if constexpr (same_as<typename _Period::type, hecto>)
  62. return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
  63. else if constexpr (same_as<typename _Period::type, kilo>)
  64. return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
  65. else if constexpr (same_as<typename _Period::type, mega>)
  66. return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
  67. else if constexpr (same_as<typename _Period::type, giga>)
  68. return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
  69. else if constexpr (same_as<typename _Period::type, tera>)
  70. return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
  71. else if constexpr (same_as<typename _Period::type, peta>)
  72. return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
  73. else if constexpr (same_as<typename _Period::type, exa>)
  74. return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
  75. else if constexpr (same_as<typename _Period::type, ratio<60>>)
  76. return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
  77. else if constexpr (same_as<typename _Period::type, ratio<3600>>)
  78. return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
  79. else if constexpr (same_as<typename _Period::type, ratio<86400>>)
  80. return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
  81. else if constexpr (_Period::den == 1)
  82. return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
  83. else
  84. return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
  85. }
  86. template <class _CharT, class _Traits, class _Rep, class _Period>
  87. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  88. operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
  89. basic_ostringstream<_CharT, _Traits> __s;
  90. __s.flags(__os.flags());
  91. __s.imbue(__os.getloc());
  92. __s.precision(__os.precision());
  93. __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
  94. return __os << __s.str();
  95. }
  96. template <class _CharT, class _Traits>
  97. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  98. operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
  99. return __os
  100. << (__d.ok()
  101. ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
  102. // Note this error differs from the wording of the Standard. The
  103. // Standard wording doesn't work well on AIX or Windows. There
  104. // the formatted day seems to be either modulo 100 or completely
  105. // omitted. Judging by the wording this is valid.
  106. // TODO FMT Write a paper of file an LWG issue.
  107. : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"), static_cast<unsigned>(__d)));
  108. }
  109. template <class _CharT, class _Traits>
  110. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  111. operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
  112. return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
  113. : std::format(__os.getloc(),
  114. _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
  115. static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
  116. }
  117. template <class _CharT, class _Traits>
  118. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  119. operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
  120. return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
  121. : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
  122. }
  123. template <class _CharT, class _Traits>
  124. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  125. operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
  126. return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
  127. : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
  128. _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
  129. static_cast<unsigned>(__wd.c_encoding())));
  130. }
  131. template <class _CharT, class _Traits>
  132. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  133. operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
  134. auto __i = __wdi.index();
  135. return __os << (__i >= 1 && __i <= 5
  136. ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
  137. : std::format(__os.getloc(),
  138. _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
  139. __wdi.weekday(),
  140. __i));
  141. }
  142. template <class _CharT, class _Traits>
  143. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  144. operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
  145. return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
  146. }
  147. template <class _CharT, class _Traits>
  148. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  149. operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
  150. // TODO FMT The Standard allows 30th of February to be printed.
  151. // It would be nice to show an error message instead.
  152. return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
  153. }
  154. template <class _CharT, class _Traits>
  155. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  156. operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
  157. return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
  158. }
  159. template <class _CharT, class _Traits>
  160. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  161. operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
  162. return __os << std::format(
  163. __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
  164. }
  165. template <class _CharT, class _Traits>
  166. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  167. operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
  168. return __os << std::format(
  169. __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
  170. }
  171. template <class _CharT, class _Traits>
  172. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  173. operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
  174. return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
  175. }
  176. template <class _CharT, class _Traits>
  177. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  178. operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
  179. return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
  180. : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
  181. }
  182. template <class _CharT, class _Traits>
  183. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  184. operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
  185. return __os << std::format(
  186. __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
  187. }
  188. template <class _CharT, class _Traits>
  189. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  190. operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
  191. return __os << std::format(
  192. __os.getloc(),
  193. _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
  194. __ymwd.year(),
  195. __ymwd.month(),
  196. __ymwd.weekday_indexed());
  197. }
  198. template <class _CharT, class _Traits>
  199. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
  200. operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
  201. return __os << std::format(
  202. __os.getloc(),
  203. _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
  204. __ymwdl.year(),
  205. __ymwdl.month(),
  206. __ymwdl.weekday_last());
  207. }
  208. } // namespace chrono
  209. #endif //if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
  210. _LIBCPP_END_NAMESPACE_STD
  211. #endif // _LIBCPP___CHRONO_OSTREAM_H