blob: 3420fb12bcdb0b85276786c2b98ab9c13f0050a6 [file] [log] [blame]
Mark de Wevere5d2d3e2022-03-20 12:40:021// -*- 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
Mark de Wevere5d2d3e2022-03-20 12:40:0213#include <__config>
Mark de Wevere5d2d3e2022-03-20 12:40:0214
Louis Dionne87d56c52024-09-16 12:15:3815#ifndef _LIBCPP_HAS_NO_LOCALIZATION
16
17# include <__chrono/calendar.h>
18# include <__chrono/day.h>
19# include <__chrono/duration.h>
20# include <__chrono/file_clock.h>
21# include <__chrono/hh_mm_ss.h>
22# include <__chrono/local_info.h>
23# include <__chrono/month.h>
24# include <__chrono/month_weekday.h>
25# include <__chrono/monthday.h>
26# include <__chrono/statically_widen.h>
27# include <__chrono/sys_info.h>
28# include <__chrono/system_clock.h>
29# include <__chrono/weekday.h>
30# include <__chrono/year.h>
31# include <__chrono/year_month.h>
32# include <__chrono/year_month_day.h>
33# include <__chrono/year_month_weekday.h>
34# include <__chrono/zoned_time.h>
35# include <__concepts/same_as.h>
36# include <__format/format_functions.h>
37# include <__fwd/ostream.h>
38# include <ratio>
Louis Dionne09e3a362024-09-16 19:06:2039# include <sstream>
Louis Dionne87d56c52024-09-16 12:15:3840
41# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
42# pragma GCC system_header
43# endif
Mark de Wevere5d2d3e2022-03-20 12:40:0244
45_LIBCPP_BEGIN_NAMESPACE_STD
46
Louis Dionne87d56c52024-09-16 12:15:3847# if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:0248
49namespace chrono {
50
Mark de Wever2c1d7952022-03-20 12:40:0251template <class _CharT, class _Traits, class _Duration>
Mark de Wever042a6a12024-01-22 18:06:1552 requires(!treat_as_floating_point_v<typename _Duration::rep> && _Duration{1} < days{1})
Mark de Wever2c1d7952022-03-20 12:40:0253_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever042a6a12024-01-22 18:06:1554operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration>& __tp) {
Mark de Wever2c1d7952022-03-20 12:40:0255 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
56}
57
Mark de Wever042a6a12024-01-22 18:06:1558template <class _CharT, class _Traits>
59_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
60operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) {
61 return __os << year_month_day{__dp};
62}
63
Mark de Wever96f30332023-04-20 19:40:3664template <class _CharT, class _Traits, class _Duration>
65_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
66operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {
67 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
68}
69
Mark de Weverbc2cf422023-04-28 06:13:0170template <class _CharT, class _Traits, class _Duration>
71_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
72operator<<(basic_ostream<_CharT, _Traits>& __os, const local_time<_Duration> __tp) {
73 return __os << sys_time<_Duration>{__tp.time_since_epoch()};
74}
75
Mark de Wever719c3dc2022-03-20 12:40:0276// Depending on the type the return is a const _CharT* or a basic_string<_CharT>
77template <class _CharT, class _Period>
78_LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
79 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
80 if constexpr (same_as<typename _Period::type, atto>)
81 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
82 else if constexpr (same_as<typename _Period::type, femto>)
83 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
84 else if constexpr (same_as<typename _Period::type, pico>)
85 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
86 else if constexpr (same_as<typename _Period::type, nano>)
87 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
88 else if constexpr (same_as<typename _Period::type, micro>)
Louis Dionne87d56c52024-09-16 12:15:3889# ifndef _LIBCPP_HAS_NO_UNICODE
Mark de Wever719c3dc2022-03-20 12:40:0290 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
Louis Dionne87d56c52024-09-16 12:15:3891# else
Mark de Wever719c3dc2022-03-20 12:40:0292 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
Louis Dionne87d56c52024-09-16 12:15:3893# endif
Mark de Wever719c3dc2022-03-20 12:40:0294 else if constexpr (same_as<typename _Period::type, milli>)
95 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
96 else if constexpr (same_as<typename _Period::type, centi>)
97 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
98 else if constexpr (same_as<typename _Period::type, deci>)
99 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
100 else if constexpr (same_as<typename _Period::type, ratio<1>>)
101 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
102 else if constexpr (same_as<typename _Period::type, deca>)
103 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
104 else if constexpr (same_as<typename _Period::type, hecto>)
105 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
106 else if constexpr (same_as<typename _Period::type, kilo>)
107 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
108 else if constexpr (same_as<typename _Period::type, mega>)
109 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
110 else if constexpr (same_as<typename _Period::type, giga>)
111 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
112 else if constexpr (same_as<typename _Period::type, tera>)
113 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
114 else if constexpr (same_as<typename _Period::type, peta>)
115 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
116 else if constexpr (same_as<typename _Period::type, exa>)
117 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
118 else if constexpr (same_as<typename _Period::type, ratio<60>>)
119 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
120 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
121 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
122 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
123 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
124 else if constexpr (_Period::den == 1)
125 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
126 else
127 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
128}
129
130template <class _CharT, class _Traits, class _Rep, class _Period>
Louis Dionne3d334df2023-03-16 17:09:44131_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever719c3dc2022-03-20 12:40:02132operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
133 basic_ostringstream<_CharT, _Traits> __s;
134 __s.flags(__os.flags());
135 __s.imbue(__os.getloc());
136 __s.precision(__os.precision());
137 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
138 return __os << __s.str();
139}
140
Mark de Wevere5d2d3e2022-03-20 12:40:02141template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44142_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
Mark de Weverde6827b2023-02-24 20:35:41143 return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
144 // Note this error differs from the wording of the Standard. The
145 // Standard wording doesn't work well on AIX or Windows. There
146 // the formatted day seems to be either modulo 100 or completely
147 // omitted. Judging by the wording this is valid.
148 // TODO FMT Write a paper of file an LWG issue.
149 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
150 static_cast<unsigned>(__d)));
Mark de Wevere5d2d3e2022-03-20 12:40:02151}
152
Mark de Wever3eb4f162022-03-20 12:40:02153template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44154_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever1522f192022-03-20 12:40:02155operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
156 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
157 : std::format(__os.getloc(),
158 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
159 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
160}
161
162template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44163_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever3eb4f162022-03-20 12:40:02164operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
165 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
166 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
167}
168
Mark de Wever566868c2022-03-20 12:40:02169template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44170_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever566868c2022-03-20 12:40:02171operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
172 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
173 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
174 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
175 static_cast<unsigned>(__wd.c_encoding())));
176}
177
Mark de Wever105fef52022-03-20 12:40:02178template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44179_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02180operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
181 auto __i = __wdi.index();
182 return __os << (__i >= 1 && __i <= 5
183 ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
184 : std::format(__os.getloc(),
185 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
186 __wdi.weekday(),
187 __i));
188}
189
190template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44191_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02192operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
193 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
194}
195
196template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44197_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02198operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
199 // TODO FMT The Standard allows 30th of February to be printed.
200 // It would be nice to show an error message instead.
201 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
202}
203
204template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44205_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02206operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
207 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
208}
209
210template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44211_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02212operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
213 return __os << std::format(
214 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
215}
216
217template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44218_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02219operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
220 return __os << std::format(
221 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
222}
223
224template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44225_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02226operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
227 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
228}
229
230template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44231_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02232operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
233 return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
234 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
235}
236
237template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44238_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02239operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
240 return __os << std::format(
241 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
242}
243
244template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44245_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02246operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
247 return __os << std::format(
248 __os.getloc(),
249 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
250 __ymwd.year(),
251 __ymwd.month(),
252 __ymwd.weekday_indexed());
253}
254
255template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44256_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02257operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
258 return __os << std::format(
259 __os.getloc(),
260 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
261 __ymwdl.year(),
262 __ymwdl.month(),
263 __ymwdl.weekday_last());
264}
265
Mark de Wever7f5d1302022-03-20 12:40:02266template <class _CharT, class _Traits, class _Duration>
Louis Dionne3d334df2023-03-16 17:09:44267_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever7f5d1302022-03-20 12:40:02268operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
269 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
270}
271
Louis Dionne87d56c52024-09-16 12:15:38272# if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
Mark de Wever6f7976c2024-04-17 18:55:51273
274template <class _CharT, class _Traits>
275_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
276operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __info) {
277 // __info.abbrev is always std::basic_string<char>.
278 // Since these strings typically are short the conversion should be cheap.
279 std::basic_string<_CharT> __abbrev{__info.abbrev.begin(), __info.abbrev.end()};
280 return __os << std::format(
281 _LIBCPP_STATICALLY_WIDEN(_CharT, "[{:%F %T}, {:%F %T}) {:%T} {:%Q%q} \"{}\""),
282 __info.begin,
283 __info.end,
284 hh_mm_ss{__info.offset},
285 __info.save,
286 __abbrev);
287}
288
Mark de Wever8a21d592024-04-18 15:23:07289template <class _CharT, class _Traits>
290_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
291operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __info) {
292 auto __result = [&]() -> basic_string<_CharT> {
293 switch (__info.result) {
294 case local_info::unique:
295 return _LIBCPP_STATICALLY_WIDEN(_CharT, "unique");
296 case local_info::nonexistent:
297 return _LIBCPP_STATICALLY_WIDEN(_CharT, "non-existent");
298 case local_info::ambiguous:
299 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ambiguous");
300
301 default:
302 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "unspecified result ({})"), __info.result);
303 };
304 };
305
306 return __os << std::format(
307 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}: {{{}, {}}}"), __result(), __info.first, __info.second);
308}
309
Louis Dionne87d56c52024-09-16 12:15:38310# if !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
Mark de Weverafbfb162024-07-20 17:24:41311template <class _CharT, class _Traits, class _Duration, class _TimeZonePtr>
312_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
313operator<<(basic_ostream<_CharT, _Traits>& __os, const zoned_time<_Duration, _TimeZonePtr>& __tp) {
314 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T %Z}"), __tp);
315}
Louis Dionne87d56c52024-09-16 12:15:38316# endif
317# endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
Mark de Wever6f7976c2024-04-17 18:55:51318
Mark de Wevere5d2d3e2022-03-20 12:40:02319} // namespace chrono
320
Louis Dionne87d56c52024-09-16 12:15:38321# endif // if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:02322
323_LIBCPP_END_NAMESPACE_STD
324
Louis Dionne87d56c52024-09-16 12:15:38325#endif // !_LIBCPP_HAS_NO_LOCALIZATION
326
Mark de Wevere5d2d3e2022-03-20 12:40:02327#endif // _LIBCPP___CHRONO_OSTREAM_H