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