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 | 1522f19 | 2022-03-20 12:40:02 | [diff] [blame] | 15 | #include <__chrono/month.h> |
Mark de Wever | e5d2d3e | 2022-03-20 12:40:02 | [diff] [blame] | 16 | #include <__chrono/statically_widen.h> |
Mark de Wever | 566868c | 2022-03-20 12:40:02 | [diff] [blame^] | 17 | #include <__chrono/weekday.h> |
Mark de Wever | 3eb4f16 | 2022-03-20 12:40:02 | [diff] [blame] | 18 | #include <__chrono/year.h> |
Mark de Wever | 719c3dc | 2022-03-20 12:40:02 | [diff] [blame] | 19 | #include <__concepts/same_as.h> |
Mark de Wever | e5d2d3e | 2022-03-20 12:40:02 | [diff] [blame] | 20 | #include <__config> |
Mark de Wever | f2a2635 | 2022-09-13 18:10:26 | [diff] [blame] | 21 | #include <__format/format_functions.h> |
Mark de Wever | e5d2d3e | 2022-03-20 12:40:02 | [diff] [blame] | 22 | #include <ostream> |
Mark de Wever | 719c3dc | 2022-03-20 12:40:02 | [diff] [blame] | 23 | #include <ratio> |
Mark de Wever | e5d2d3e | 2022-03-20 12:40:02 | [diff] [blame] | 24 | |
| 25 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 26 | # pragma GCC system_header |
| 27 | #endif |
| 28 | |
| 29 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 30 | |
| 31 | #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT) |
| 32 | |
| 33 | namespace chrono { |
| 34 | |
Mark de Wever | 719c3dc | 2022-03-20 12:40:02 | [diff] [blame] | 35 | // Depending on the type the return is a const _CharT* or a basic_string<_CharT> |
| 36 | template <class _CharT, class _Period> |
| 37 | _LIBCPP_HIDE_FROM_ABI auto __units_suffix() { |
| 38 | // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed. |
| 39 | if constexpr (same_as<typename _Period::type, atto>) |
| 40 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "as"); |
| 41 | else if constexpr (same_as<typename _Period::type, femto>) |
| 42 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs"); |
| 43 | else if constexpr (same_as<typename _Period::type, pico>) |
| 44 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps"); |
| 45 | else if constexpr (same_as<typename _Period::type, nano>) |
| 46 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns"); |
| 47 | else if constexpr (same_as<typename _Period::type, micro>) |
| 48 | # ifndef _LIBCPP_HAS_NO_UNICODE |
| 49 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s"); |
| 50 | # else |
| 51 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "us"); |
| 52 | # endif |
| 53 | else if constexpr (same_as<typename _Period::type, milli>) |
| 54 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms"); |
| 55 | else if constexpr (same_as<typename _Period::type, centi>) |
| 56 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs"); |
| 57 | else if constexpr (same_as<typename _Period::type, deci>) |
| 58 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds"); |
| 59 | else if constexpr (same_as<typename _Period::type, ratio<1>>) |
| 60 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "s"); |
| 61 | else if constexpr (same_as<typename _Period::type, deca>) |
| 62 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "das"); |
| 63 | else if constexpr (same_as<typename _Period::type, hecto>) |
| 64 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs"); |
| 65 | else if constexpr (same_as<typename _Period::type, kilo>) |
| 66 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks"); |
| 67 | else if constexpr (same_as<typename _Period::type, mega>) |
| 68 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms"); |
| 69 | else if constexpr (same_as<typename _Period::type, giga>) |
| 70 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs"); |
| 71 | else if constexpr (same_as<typename _Period::type, tera>) |
| 72 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts"); |
| 73 | else if constexpr (same_as<typename _Period::type, peta>) |
| 74 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps"); |
| 75 | else if constexpr (same_as<typename _Period::type, exa>) |
| 76 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es"); |
| 77 | else if constexpr (same_as<typename _Period::type, ratio<60>>) |
| 78 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "min"); |
| 79 | else if constexpr (same_as<typename _Period::type, ratio<3600>>) |
| 80 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "h"); |
| 81 | else if constexpr (same_as<typename _Period::type, ratio<86400>>) |
| 82 | return _LIBCPP_STATICALLY_WIDEN(_CharT, "d"); |
| 83 | else if constexpr (_Period::den == 1) |
| 84 | return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num); |
| 85 | else |
| 86 | return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den); |
| 87 | } |
| 88 | |
| 89 | template <class _CharT, class _Traits, class _Rep, class _Period> |
| 90 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>& |
| 91 | operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) { |
| 92 | basic_ostringstream<_CharT, _Traits> __s; |
| 93 | __s.flags(__os.flags()); |
| 94 | __s.imbue(__os.getloc()); |
| 95 | __s.precision(__os.precision()); |
| 96 | __s << __d.count() << chrono::__units_suffix<_CharT, _Period>(); |
| 97 | return __os << __s.str(); |
| 98 | } |
| 99 | |
Mark de Wever | e5d2d3e | 2022-03-20 12:40:02 | [diff] [blame] | 100 | template <class _CharT, class _Traits> |
| 101 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>& |
| 102 | operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) { |
| 103 | return __os |
| 104 | << (__d.ok() |
| 105 | ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d) |
| 106 | // Note this error differs from the wording of the Standard. The |
| 107 | // Standard wording doesn't work well on AIX or Windows. There |
| 108 | // the formatted day seems to be either modulo 100 or completely |
| 109 | // omitted. Judging by the wording this is valid. |
| 110 | // TODO FMT Write a paper of file an LWG issue. |
| 111 | : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"), static_cast<unsigned>(__d))); |
| 112 | } |
| 113 | |
Mark de Wever | 3eb4f16 | 2022-03-20 12:40:02 | [diff] [blame] | 114 | template <class _CharT, class _Traits> |
| 115 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>& |
Mark de Wever | 1522f19 | 2022-03-20 12:40:02 | [diff] [blame] | 116 | operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) { |
| 117 | return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m) |
| 118 | : std::format(__os.getloc(), |
| 119 | _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"), |
| 120 | static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used. |
| 121 | } |
| 122 | |
| 123 | template <class _CharT, class _Traits> |
| 124 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>& |
Mark de Wever | 3eb4f16 | 2022-03-20 12:40:02 | [diff] [blame] | 125 | operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) { |
| 126 | return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y) |
| 127 | : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y)); |
| 128 | } |
| 129 | |
Mark de Wever | 566868c | 2022-03-20 12:40:02 | [diff] [blame^] | 130 | template <class _CharT, class _Traits> |
| 131 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>& |
| 132 | operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) { |
| 133 | return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd) |
| 134 | : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used. |
| 135 | _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"), |
| 136 | static_cast<unsigned>(__wd.c_encoding()))); |
| 137 | } |
| 138 | |
Mark de Wever | e5d2d3e | 2022-03-20 12:40:02 | [diff] [blame] | 139 | } // namespace chrono |
| 140 | |
| 141 | #endif //if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT) |
| 142 | |
| 143 | _LIBCPP_END_NAMESPACE_STD |
| 144 | |
| 145 | #endif // _LIBCPP___CHRONO_OSTREAM_H |