ostream.h 12 KB

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