blob: cb17dbea58bee3606014379ac0c69596f92b0945 [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 Weverbc2cf422023-04-28 06:13:0113#include <__chrono/calendar.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0214#include <__chrono/day.h>
Mark de Wever719c3dc2022-03-20 12:40:0215#include <__chrono/duration.h>
Mark de Wever96f30332023-04-20 19:40:3616#include <__chrono/file_clock.h>
Mark de Wever7f5d1302022-03-20 12:40:0217#include <__chrono/hh_mm_ss.h>
Mark de Wever8a21d592024-04-18 15:23:0718#include <__chrono/local_info.h>
Mark de Wever1522f192022-03-20 12:40:0219#include <__chrono/month.h>
Mark de Wever105fef52022-03-20 12:40:0220#include <__chrono/month_weekday.h>
21#include <__chrono/monthday.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0222#include <__chrono/statically_widen.h>
Mark de Wever6f7976c2024-04-17 18:55:5123#include <__chrono/sys_info.h>
Mark de Wever2c1d7952022-03-20 12:40:0224#include <__chrono/system_clock.h>
Mark de Wever566868c2022-03-20 12:40:0225#include <__chrono/weekday.h>
Mark de Wever3eb4f162022-03-20 12:40:0226#include <__chrono/year.h>
Mark de Wever105fef52022-03-20 12:40:0227#include <__chrono/year_month.h>
28#include <__chrono/year_month_day.h>
29#include <__chrono/year_month_weekday.h>
Mark de Wever719c3dc2022-03-20 12:40:0230#include <__concepts/same_as.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0231#include <__config>
Mark de Weverf2a26352022-09-13 18:10:2632#include <__format/format_functions.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0233#include <ostream>
Mark de Wever719c3dc2022-03-20 12:40:0234#include <ratio>
Mark de Wevere5d2d3e2022-03-20 12:40:0235
36#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
37# pragma GCC system_header
38#endif
39
40_LIBCPP_BEGIN_NAMESPACE_STD
41
Mark de Weverdff62f52023-05-17 17:17:5242#if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:0243
44namespace chrono {
45
Mark de Wever2c1d7952022-03-20 12:40:0246template <class _CharT, class _Traits, class _Duration>
Mark de Wever042a6a12024-01-22 18:06:1547 requires(!treat_as_floating_point_v<typename _Duration::rep> && _Duration{1} < days{1})
Mark de Wever2c1d7952022-03-20 12:40:0248_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever042a6a12024-01-22 18:06:1549operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration>& __tp) {
Mark de Wever2c1d7952022-03-20 12:40:0250 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
51}
52
Mark de Wever042a6a12024-01-22 18:06:1553template <class _CharT, class _Traits>
54_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
55operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) {
56 return __os << year_month_day{__dp};
57}
58
Mark de Wever96f30332023-04-20 19:40:3659template <class _CharT, class _Traits, class _Duration>
60_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
61operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {
62 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
63}
64
Mark de Weverbc2cf422023-04-28 06:13:0165template <class _CharT, class _Traits, class _Duration>
66_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
67operator<<(basic_ostream<_CharT, _Traits>& __os, const local_time<_Duration> __tp) {
68 return __os << sys_time<_Duration>{__tp.time_since_epoch()};
69}
70
Mark de Wever719c3dc2022-03-20 12:40:0271// Depending on the type the return is a const _CharT* or a basic_string<_CharT>
72template <class _CharT, class _Period>
73_LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
74 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
75 if constexpr (same_as<typename _Period::type, atto>)
76 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
77 else if constexpr (same_as<typename _Period::type, femto>)
78 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
79 else if constexpr (same_as<typename _Period::type, pico>)
80 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
81 else if constexpr (same_as<typename _Period::type, nano>)
82 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
83 else if constexpr (same_as<typename _Period::type, micro>)
84# ifndef _LIBCPP_HAS_NO_UNICODE
85 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
86# else
87 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
88# endif
89 else if constexpr (same_as<typename _Period::type, milli>)
90 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
91 else if constexpr (same_as<typename _Period::type, centi>)
92 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
93 else if constexpr (same_as<typename _Period::type, deci>)
94 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
95 else if constexpr (same_as<typename _Period::type, ratio<1>>)
96 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
97 else if constexpr (same_as<typename _Period::type, deca>)
98 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
99 else if constexpr (same_as<typename _Period::type, hecto>)
100 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
101 else if constexpr (same_as<typename _Period::type, kilo>)
102 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
103 else if constexpr (same_as<typename _Period::type, mega>)
104 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
105 else if constexpr (same_as<typename _Period::type, giga>)
106 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
107 else if constexpr (same_as<typename _Period::type, tera>)
108 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
109 else if constexpr (same_as<typename _Period::type, peta>)
110 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
111 else if constexpr (same_as<typename _Period::type, exa>)
112 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
113 else if constexpr (same_as<typename _Period::type, ratio<60>>)
114 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
115 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
116 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
117 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
118 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
119 else if constexpr (_Period::den == 1)
120 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
121 else
122 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
123}
124
125template <class _CharT, class _Traits, class _Rep, class _Period>
Louis Dionne3d334df2023-03-16 17:09:44126_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever719c3dc2022-03-20 12:40:02127operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
128 basic_ostringstream<_CharT, _Traits> __s;
129 __s.flags(__os.flags());
130 __s.imbue(__os.getloc());
131 __s.precision(__os.precision());
132 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
133 return __os << __s.str();
134}
135
Mark de Wevere5d2d3e2022-03-20 12:40:02136template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44137_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
Mark de Weverde6827b2023-02-24 20:35:41138 return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
139 // Note this error differs from the wording of the Standard. The
140 // Standard wording doesn't work well on AIX or Windows. There
141 // the formatted day seems to be either modulo 100 or completely
142 // omitted. Judging by the wording this is valid.
143 // TODO FMT Write a paper of file an LWG issue.
144 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
145 static_cast<unsigned>(__d)));
Mark de Wevere5d2d3e2022-03-20 12:40:02146}
147
Mark de Wever3eb4f162022-03-20 12:40:02148template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44149_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever1522f192022-03-20 12:40:02150operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
151 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
152 : std::format(__os.getloc(),
153 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
154 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
155}
156
157template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44158_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever3eb4f162022-03-20 12:40:02159operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
160 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
161 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
162}
163
Mark de Wever566868c2022-03-20 12:40:02164template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44165_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever566868c2022-03-20 12:40:02166operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
167 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
168 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
169 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
170 static_cast<unsigned>(__wd.c_encoding())));
171}
172
Mark de Wever105fef52022-03-20 12:40:02173template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44174_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02175operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
176 auto __i = __wdi.index();
177 return __os << (__i >= 1 && __i <= 5
178 ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
179 : std::format(__os.getloc(),
180 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
181 __wdi.weekday(),
182 __i));
183}
184
185template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44186_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02187operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
188 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
189}
190
191template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44192_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02193operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
194 // TODO FMT The Standard allows 30th of February to be printed.
195 // It would be nice to show an error message instead.
196 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
197}
198
199template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44200_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02201operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
202 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
203}
204
205template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44206_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02207operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
208 return __os << std::format(
209 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
210}
211
212template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44213_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02214operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
215 return __os << std::format(
216 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
217}
218
219template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44220_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02221operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
222 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
223}
224
225template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44226_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02227operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
228 return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
229 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
230}
231
232template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44233_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02234operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
235 return __os << std::format(
236 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
237}
238
239template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44240_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02241operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
242 return __os << std::format(
243 __os.getloc(),
244 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
245 __ymwd.year(),
246 __ymwd.month(),
247 __ymwd.weekday_indexed());
248}
249
250template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44251_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02252operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
253 return __os << std::format(
254 __os.getloc(),
255 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
256 __ymwdl.year(),
257 __ymwdl.month(),
258 __ymwdl.weekday_last());
259}
260
Mark de Wever7f5d1302022-03-20 12:40:02261template <class _CharT, class _Traits, class _Duration>
Louis Dionne3d334df2023-03-16 17:09:44262_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever7f5d1302022-03-20 12:40:02263operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
264 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
265}
266
Mark de Wever6f7976c2024-04-17 18:55:51267# if !defined(_LIBCPP_HAS_NO_INCOMPLETE_TZDB)
268
269template <class _CharT, class _Traits>
270_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
271operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __info) {
272 // __info.abbrev is always std::basic_string<char>.
273 // Since these strings typically are short the conversion should be cheap.
274 std::basic_string<_CharT> __abbrev{__info.abbrev.begin(), __info.abbrev.end()};
275 return __os << std::format(
276 _LIBCPP_STATICALLY_WIDEN(_CharT, "[{:%F %T}, {:%F %T}) {:%T} {:%Q%q} \"{}\""),
277 __info.begin,
278 __info.end,
279 hh_mm_ss{__info.offset},
280 __info.save,
281 __abbrev);
282}
283
Mark de Wever8a21d592024-04-18 15:23:07284template <class _CharT, class _Traits>
285_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
286operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __info) {
287 auto __result = [&]() -> basic_string<_CharT> {
288 switch (__info.result) {
289 case local_info::unique:
290 return _LIBCPP_STATICALLY_WIDEN(_CharT, "unique");
291 case local_info::nonexistent:
292 return _LIBCPP_STATICALLY_WIDEN(_CharT, "non-existent");
293 case local_info::ambiguous:
294 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ambiguous");
295
296 default:
297 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "unspecified result ({})"), __info.result);
298 };
299 };
300
301 return __os << std::format(
302 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}: {{{}, {}}}"), __result(), __info.first, __info.second);
303}
304
Mark de Wever6f7976c2024-04-17 18:55:51305# endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_TZDB)
306
Mark de Wevere5d2d3e2022-03-20 12:40:02307} // namespace chrono
308
Mark de Weverdff62f52023-05-17 17:17:52309#endif // if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:02310
311_LIBCPP_END_NAMESPACE_STD
312
313#endif // _LIBCPP___CHRONO_OSTREAM_H