blob: 196ebd518541c71aca356853b13727774cf8aa10 [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>
39
40# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
41# pragma GCC system_header
42# endif
Mark de Wevere5d2d3e2022-03-20 12:40:0243
44_LIBCPP_BEGIN_NAMESPACE_STD
45
Louis Dionne87d56c52024-09-16 12:15:3846# if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:0247
48namespace chrono {
49
Mark de Wever2c1d7952022-03-20 12:40:0250template <class _CharT, class _Traits, class _Duration>
Mark de Wever042a6a12024-01-22 18:06:1551 requires(!treat_as_floating_point_v<typename _Duration::rep> && _Duration{1} < days{1})
Mark de Wever2c1d7952022-03-20 12:40:0252_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever042a6a12024-01-22 18:06:1553operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration>& __tp) {
Mark de Wever2c1d7952022-03-20 12:40:0254 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
55}
56
Mark de Wever042a6a12024-01-22 18:06:1557template <class _CharT, class _Traits>
58_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
59operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) {
60 return __os << year_month_day{__dp};
61}
62
Mark de Wever96f30332023-04-20 19:40:3663template <class _CharT, class _Traits, class _Duration>
64_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
65operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {
66 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
67}
68
Mark de Weverbc2cf422023-04-28 06:13:0169template <class _CharT, class _Traits, class _Duration>
70_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
71operator<<(basic_ostream<_CharT, _Traits>& __os, const local_time<_Duration> __tp) {
72 return __os << sys_time<_Duration>{__tp.time_since_epoch()};
73}
74
Mark de Wever719c3dc2022-03-20 12:40:0275// Depending on the type the return is a const _CharT* or a basic_string<_CharT>
76template <class _CharT, class _Period>
77_LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
78 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
79 if constexpr (same_as<typename _Period::type, atto>)
80 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
81 else if constexpr (same_as<typename _Period::type, femto>)
82 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
83 else if constexpr (same_as<typename _Period::type, pico>)
84 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
85 else if constexpr (same_as<typename _Period::type, nano>)
86 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
87 else if constexpr (same_as<typename _Period::type, micro>)
Louis Dionne87d56c52024-09-16 12:15:3888# ifndef _LIBCPP_HAS_NO_UNICODE
Mark de Wever719c3dc2022-03-20 12:40:0289 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
Louis Dionne87d56c52024-09-16 12:15:3890# else
Mark de Wever719c3dc2022-03-20 12:40:0291 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
Louis Dionne87d56c52024-09-16 12:15:3892# endif
Mark de Wever719c3dc2022-03-20 12:40:0293 else if constexpr (same_as<typename _Period::type, milli>)
94 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
95 else if constexpr (same_as<typename _Period::type, centi>)
96 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
97 else if constexpr (same_as<typename _Period::type, deci>)
98 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
99 else if constexpr (same_as<typename _Period::type, ratio<1>>)
100 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
101 else if constexpr (same_as<typename _Period::type, deca>)
102 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
103 else if constexpr (same_as<typename _Period::type, hecto>)
104 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
105 else if constexpr (same_as<typename _Period::type, kilo>)
106 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
107 else if constexpr (same_as<typename _Period::type, mega>)
108 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
109 else if constexpr (same_as<typename _Period::type, giga>)
110 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
111 else if constexpr (same_as<typename _Period::type, tera>)
112 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
113 else if constexpr (same_as<typename _Period::type, peta>)
114 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
115 else if constexpr (same_as<typename _Period::type, exa>)
116 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
117 else if constexpr (same_as<typename _Period::type, ratio<60>>)
118 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
119 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
120 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
121 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
122 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
123 else if constexpr (_Period::den == 1)
124 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
125 else
126 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
127}
128
129template <class _CharT, class _Traits, class _Rep, class _Period>
Louis Dionne3d334df2023-03-16 17:09:44130_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever719c3dc2022-03-20 12:40:02131operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
132 basic_ostringstream<_CharT, _Traits> __s;
133 __s.flags(__os.flags());
134 __s.imbue(__os.getloc());
135 __s.precision(__os.precision());
136 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
137 return __os << __s.str();
138}
139
Mark de Wevere5d2d3e2022-03-20 12:40:02140template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44141_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
Mark de Weverde6827b2023-02-24 20:35:41142 return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
143 // Note this error differs from the wording of the Standard. The
144 // Standard wording doesn't work well on AIX or Windows. There
145 // the formatted day seems to be either modulo 100 or completely
146 // omitted. Judging by the wording this is valid.
147 // TODO FMT Write a paper of file an LWG issue.
148 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
149 static_cast<unsigned>(__d)));
Mark de Wevere5d2d3e2022-03-20 12:40:02150}
151
Mark de Wever3eb4f162022-03-20 12:40:02152template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44153_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever1522f192022-03-20 12:40:02154operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
155 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
156 : std::format(__os.getloc(),
157 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
158 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
159}
160
161template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44162_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever3eb4f162022-03-20 12:40:02163operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
164 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
165 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
166}
167
Mark de Wever566868c2022-03-20 12:40:02168template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44169_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever566868c2022-03-20 12:40:02170operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
171 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
172 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
173 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
174 static_cast<unsigned>(__wd.c_encoding())));
175}
176
Mark de Wever105fef52022-03-20 12:40:02177template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44178_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02179operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
180 auto __i = __wdi.index();
181 return __os << (__i >= 1 && __i <= 5
182 ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
183 : std::format(__os.getloc(),
184 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
185 __wdi.weekday(),
186 __i));
187}
188
189template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44190_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02191operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
192 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
193}
194
195template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44196_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02197operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
198 // TODO FMT The Standard allows 30th of February to be printed.
199 // It would be nice to show an error message instead.
200 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
201}
202
203template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44204_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02205operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
206 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
207}
208
209template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44210_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02211operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
212 return __os << std::format(
213 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
214}
215
216template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44217_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02218operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
219 return __os << std::format(
220 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
221}
222
223template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44224_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02225operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
226 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
227}
228
229template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44230_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02231operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
232 return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
233 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
234}
235
236template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44237_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02238operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
239 return __os << std::format(
240 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
241}
242
243template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44244_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02245operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
246 return __os << std::format(
247 __os.getloc(),
248 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
249 __ymwd.year(),
250 __ymwd.month(),
251 __ymwd.weekday_indexed());
252}
253
254template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44255_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02256operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
257 return __os << std::format(
258 __os.getloc(),
259 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
260 __ymwdl.year(),
261 __ymwdl.month(),
262 __ymwdl.weekday_last());
263}
264
Mark de Wever7f5d1302022-03-20 12:40:02265template <class _CharT, class _Traits, class _Duration>
Louis Dionne3d334df2023-03-16 17:09:44266_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever7f5d1302022-03-20 12:40:02267operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
268 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
269}
270
Louis Dionne87d56c52024-09-16 12:15:38271# if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
Mark de Wever6f7976c2024-04-17 18:55:51272
273template <class _CharT, class _Traits>
274_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
275operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __info) {
276 // __info.abbrev is always std::basic_string<char>.
277 // Since these strings typically are short the conversion should be cheap.
278 std::basic_string<_CharT> __abbrev{__info.abbrev.begin(), __info.abbrev.end()};
279 return __os << std::format(
280 _LIBCPP_STATICALLY_WIDEN(_CharT, "[{:%F %T}, {:%F %T}) {:%T} {:%Q%q} \"{}\""),
281 __info.begin,
282 __info.end,
283 hh_mm_ss{__info.offset},
284 __info.save,
285 __abbrev);
286}
287
Mark de Wever8a21d592024-04-18 15:23:07288template <class _CharT, class _Traits>
289_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
290operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __info) {
291 auto __result = [&]() -> basic_string<_CharT> {
292 switch (__info.result) {
293 case local_info::unique:
294 return _LIBCPP_STATICALLY_WIDEN(_CharT, "unique");
295 case local_info::nonexistent:
296 return _LIBCPP_STATICALLY_WIDEN(_CharT, "non-existent");
297 case local_info::ambiguous:
298 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ambiguous");
299
300 default:
301 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "unspecified result ({})"), __info.result);
302 };
303 };
304
305 return __os << std::format(
306 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}: {{{}, {}}}"), __result(), __info.first, __info.second);
307}
308
Louis Dionne87d56c52024-09-16 12:15:38309# if !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
Mark de Weverafbfb162024-07-20 17:24:41310template <class _CharT, class _Traits, class _Duration, class _TimeZonePtr>
311_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
312operator<<(basic_ostream<_CharT, _Traits>& __os, const zoned_time<_Duration, _TimeZonePtr>& __tp) {
313 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T %Z}"), __tp);
314}
Louis Dionne87d56c52024-09-16 12:15:38315# endif
316# endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
Mark de Wever6f7976c2024-04-17 18:55:51317
Mark de Wevere5d2d3e2022-03-20 12:40:02318} // namespace chrono
319
Louis Dionne87d56c52024-09-16 12:15:38320# endif // if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:02321
322_LIBCPP_END_NAMESPACE_STD
323
Louis Dionne87d56c52024-09-16 12:15:38324#endif // !_LIBCPP_HAS_NO_LOCALIZATION
325
Mark de Wevere5d2d3e2022-03-20 12:40:02326#endif // _LIBCPP___CHRONO_OSTREAM_H