ostream.h 12 KB

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