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