Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:25

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___CHRONO_OSTREAM_H
0011 #define _LIBCPP___CXX03___CHRONO_OSTREAM_H
0012 
0013 #include <__cxx03/__chrono/calendar.h>
0014 #include <__cxx03/__chrono/day.h>
0015 #include <__cxx03/__chrono/duration.h>
0016 #include <__cxx03/__chrono/file_clock.h>
0017 #include <__cxx03/__chrono/hh_mm_ss.h>
0018 #include <__cxx03/__chrono/local_info.h>
0019 #include <__cxx03/__chrono/month.h>
0020 #include <__cxx03/__chrono/month_weekday.h>
0021 #include <__cxx03/__chrono/monthday.h>
0022 #include <__cxx03/__chrono/statically_widen.h>
0023 #include <__cxx03/__chrono/sys_info.h>
0024 #include <__cxx03/__chrono/system_clock.h>
0025 #include <__cxx03/__chrono/weekday.h>
0026 #include <__cxx03/__chrono/year.h>
0027 #include <__cxx03/__chrono/year_month.h>
0028 #include <__cxx03/__chrono/year_month_day.h>
0029 #include <__cxx03/__chrono/year_month_weekday.h>
0030 #include <__cxx03/__chrono/zoned_time.h>
0031 #include <__cxx03/__concepts/same_as.h>
0032 #include <__cxx03/__config>
0033 #include <__cxx03/__format/format_functions.h>
0034 #include <__cxx03/__fwd/ostream.h>
0035 #include <__cxx03/ratio>
0036 
0037 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0038 #  pragma GCC system_header
0039 #endif
0040 
0041 _LIBCPP_BEGIN_NAMESPACE_STD
0042 
0043 #if _LIBCPP_STD_VER >= 20
0044 
0045 namespace chrono {
0046 
0047 template <class _CharT, class _Traits, class _Duration>
0048   requires(!treat_as_floating_point_v<typename _Duration::rep> && _Duration{1} < days{1})
0049 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0050 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration>& __tp) {
0051   return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
0052 }
0053 
0054 template <class _CharT, class _Traits>
0055 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0056 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) {
0057   return __os << year_month_day{__dp};
0058 }
0059 
0060 template <class _CharT, class _Traits, class _Duration>
0061 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0062 operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {
0063   return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
0064 }
0065 
0066 template <class _CharT, class _Traits, class _Duration>
0067 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0068 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_time<_Duration> __tp) {
0069   return __os << sys_time<_Duration>{__tp.time_since_epoch()};
0070 }
0071 
0072 // Depending on the type the return is a const _CharT* or a basic_string<_CharT>
0073 template <class _CharT, class _Period>
0074 _LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
0075   // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
0076   if constexpr (same_as<typename _Period::type, atto>)
0077     return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
0078   else if constexpr (same_as<typename _Period::type, femto>)
0079     return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
0080   else if constexpr (same_as<typename _Period::type, pico>)
0081     return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
0082   else if constexpr (same_as<typename _Period::type, nano>)
0083     return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
0084   else if constexpr (same_as<typename _Period::type, micro>)
0085 #  ifndef _LIBCPP_HAS_NO_UNICODE
0086     return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
0087 #  else
0088     return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
0089 #  endif
0090   else if constexpr (same_as<typename _Period::type, milli>)
0091     return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
0092   else if constexpr (same_as<typename _Period::type, centi>)
0093     return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
0094   else if constexpr (same_as<typename _Period::type, deci>)
0095     return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
0096   else if constexpr (same_as<typename _Period::type, ratio<1>>)
0097     return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
0098   else if constexpr (same_as<typename _Period::type, deca>)
0099     return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
0100   else if constexpr (same_as<typename _Period::type, hecto>)
0101     return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
0102   else if constexpr (same_as<typename _Period::type, kilo>)
0103     return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
0104   else if constexpr (same_as<typename _Period::type, mega>)
0105     return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
0106   else if constexpr (same_as<typename _Period::type, giga>)
0107     return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
0108   else if constexpr (same_as<typename _Period::type, tera>)
0109     return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
0110   else if constexpr (same_as<typename _Period::type, peta>)
0111     return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
0112   else if constexpr (same_as<typename _Period::type, exa>)
0113     return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
0114   else if constexpr (same_as<typename _Period::type, ratio<60>>)
0115     return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
0116   else if constexpr (same_as<typename _Period::type, ratio<3600>>)
0117     return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
0118   else if constexpr (same_as<typename _Period::type, ratio<86400>>)
0119     return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
0120   else if constexpr (_Period::den == 1)
0121     return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
0122   else
0123     return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
0124 }
0125 
0126 template <class _CharT, class _Traits, class _Rep, class _Period>
0127 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0128 operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
0129   basic_ostringstream<_CharT, _Traits> __s;
0130   __s.flags(__os.flags());
0131   __s.imbue(__os.getloc());
0132   __s.precision(__os.precision());
0133   __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
0134   return __os << __s.str();
0135 }
0136 
0137 template <class _CharT, class _Traits>
0138 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
0139   return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
0140                            // Note this error differs from the wording of the Standard. The
0141                            // Standard wording doesn't work well on AIX or Windows. There
0142                            // the formatted day seems to be either modulo 100 or completely
0143                            // omitted. Judging by the wording this is valid.
0144                            // TODO FMT Write a paper of file an LWG issue.
0145                            : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
0146                                          static_cast<unsigned>(__d)));
0147 }
0148 
0149 template <class _CharT, class _Traits>
0150 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0151 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
0152   return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
0153                            : std::format(__os.getloc(),
0154                                          _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
0155                                          static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
0156 }
0157 
0158 template <class _CharT, class _Traits>
0159 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0160 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
0161   return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
0162                            : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
0163 }
0164 
0165 template <class _CharT, class _Traits>
0166 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0167 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
0168   return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
0169                             : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
0170                                           _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
0171                                           static_cast<unsigned>(__wd.c_encoding())));
0172 }
0173 
0174 template <class _CharT, class _Traits>
0175 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0176 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
0177   auto __i = __wdi.index();
0178   return __os << (__i >= 1 && __i <= 5
0179                       ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
0180                       : std::format(__os.getloc(),
0181                                     _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
0182                                     __wdi.weekday(),
0183                                     __i));
0184 }
0185 
0186 template <class _CharT, class _Traits>
0187 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0188 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
0189   return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
0190 }
0191 
0192 template <class _CharT, class _Traits>
0193 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0194 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
0195   // TODO FMT The Standard allows 30th of February to be printed.
0196   // It would be nice to show an error message instead.
0197   return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
0198 }
0199 
0200 template <class _CharT, class _Traits>
0201 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0202 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
0203   return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
0204 }
0205 
0206 template <class _CharT, class _Traits>
0207 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0208 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
0209   return __os << std::format(
0210              __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
0211 }
0212 
0213 template <class _CharT, class _Traits>
0214 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0215 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
0216   return __os << std::format(
0217              __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
0218 }
0219 
0220 template <class _CharT, class _Traits>
0221 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0222 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
0223   return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
0224 }
0225 
0226 template <class _CharT, class _Traits>
0227 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0228 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
0229   return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
0230                              : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
0231 }
0232 
0233 template <class _CharT, class _Traits>
0234 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0235 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
0236   return __os << std::format(
0237              __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
0238 }
0239 
0240 template <class _CharT, class _Traits>
0241 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0242 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
0243   return __os << std::format(
0244              __os.getloc(),
0245              _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
0246              __ymwd.year(),
0247              __ymwd.month(),
0248              __ymwd.weekday_indexed());
0249 }
0250 
0251 template <class _CharT, class _Traits>
0252 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0253 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
0254   return __os << std::format(
0255              __os.getloc(),
0256              _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
0257              __ymwdl.year(),
0258              __ymwdl.month(),
0259              __ymwdl.weekday_last());
0260 }
0261 
0262 template <class _CharT, class _Traits, class _Duration>
0263 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0264 operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
0265   return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
0266 }
0267 
0268 #  if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
0269 
0270 template <class _CharT, class _Traits>
0271 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0272 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __info) {
0273   // __info.abbrev is always std::basic_string<char>.
0274   // Since these strings typically are short the conversion should be cheap.
0275   std::basic_string<_CharT> __abbrev{__info.abbrev.begin(), __info.abbrev.end()};
0276   return __os << std::format(
0277              _LIBCPP_STATICALLY_WIDEN(_CharT, "[{:%F %T}, {:%F %T}) {:%T} {:%Q%q} \"{}\""),
0278              __info.begin,
0279              __info.end,
0280              hh_mm_ss{__info.offset},
0281              __info.save,
0282              __abbrev);
0283 }
0284 
0285 template <class _CharT, class _Traits>
0286 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0287 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __info) {
0288   auto __result = [&]() -> basic_string<_CharT> {
0289     switch (__info.result) {
0290     case local_info::unique:
0291       return _LIBCPP_STATICALLY_WIDEN(_CharT, "unique");
0292     case local_info::nonexistent:
0293       return _LIBCPP_STATICALLY_WIDEN(_CharT, "non-existent");
0294     case local_info::ambiguous:
0295       return _LIBCPP_STATICALLY_WIDEN(_CharT, "ambiguous");
0296 
0297     default:
0298       return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "unspecified result ({})"), __info.result);
0299     };
0300   };
0301 
0302   return __os << std::format(
0303              _LIBCPP_STATICALLY_WIDEN(_CharT, "{}: {{{}, {}}}"), __result(), __info.first, __info.second);
0304 }
0305 
0306 #    if !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) &&                          \
0307         !defined(_LIBCPP_HAS_NO_LOCALIZATION)
0308 template <class _CharT, class _Traits, class _Duration, class _TimeZonePtr>
0309 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0310 operator<<(basic_ostream<_CharT, _Traits>& __os, const zoned_time<_Duration, _TimeZonePtr>& __tp) {
0311   return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T %Z}"), __tp);
0312 }
0313 #    endif
0314 #  endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
0315 
0316 } // namespace chrono
0317 
0318 #endif // if _LIBCPP_STD_VER >= 20
0319 
0320 _LIBCPP_END_NAMESPACE_STD
0321 
0322 #endif // _LIBCPP___CXX03___CHRONO_OSTREAM_H