blob: e6c43254eea15ee7272d1f4a046d1cdfca196b3f [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 Weverafbfb162024-07-20 17:24:4130#include <__chrono/zoned_time.h>
Mark de Wever719c3dc2022-03-20 12:40:0231#include <__concepts/same_as.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0232#include <__config>
Mark de Weverf2a26352022-09-13 18:10:2633#include <__format/format_functions.h>
Nikolas Klauser622d8b02024-06-20 19:40:2334#include <__fwd/ostream.h>
Mark de Wever719c3dc2022-03-20 12:40:0235#include <ratio>
Mark de Wevere5d2d3e2022-03-20 12:40:0236
37#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
38# pragma GCC system_header
39#endif
40
41_LIBCPP_BEGIN_NAMESPACE_STD
42
Mark de Weverdff62f52023-05-17 17:17:5243#if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:0244
45namespace chrono {
46
Mark de Wever2c1d7952022-03-20 12:40:0247template <class _CharT, class _Traits, class _Duration>
Mark de Wever042a6a12024-01-22 18:06:1548 requires(!treat_as_floating_point_v<typename _Duration::rep> && _Duration{1} < days{1})
Mark de Wever2c1d7952022-03-20 12:40:0249_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever042a6a12024-01-22 18:06:1550operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration>& __tp) {
Mark de Wever2c1d7952022-03-20 12:40:0251 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
52}
53
Mark de Wever042a6a12024-01-22 18:06:1554template <class _CharT, class _Traits>
55_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
56operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) {
57 return __os << year_month_day{__dp};
58}
59
Mark de Wever96f30332023-04-20 19:40:3660template <class _CharT, class _Traits, class _Duration>
61_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
62operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {
63 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
64}
65
Mark de Weverbc2cf422023-04-28 06:13:0166template <class _CharT, class _Traits, class _Duration>
67_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
68operator<<(basic_ostream<_CharT, _Traits>& __os, const local_time<_Duration> __tp) {
69 return __os << sys_time<_Duration>{__tp.time_since_epoch()};
70}
71
Mark de Wever719c3dc2022-03-20 12:40:0272// Depending on the type the return is a const _CharT* or a basic_string<_CharT>
73template <class _CharT, class _Period>
74_LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
75 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
76 if constexpr (same_as<typename _Period::type, atto>)
77 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
78 else if constexpr (same_as<typename _Period::type, femto>)
79 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
80 else if constexpr (same_as<typename _Period::type, pico>)
81 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
82 else if constexpr (same_as<typename _Period::type, nano>)
83 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
84 else if constexpr (same_as<typename _Period::type, micro>)
85# ifndef _LIBCPP_HAS_NO_UNICODE
86 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
87# else
88 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
89# endif
90 else if constexpr (same_as<typename _Period::type, milli>)
91 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
92 else if constexpr (same_as<typename _Period::type, centi>)
93 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
94 else if constexpr (same_as<typename _Period::type, deci>)
95 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
96 else if constexpr (same_as<typename _Period::type, ratio<1>>)
97 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
98 else if constexpr (same_as<typename _Period::type, deca>)
99 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
100 else if constexpr (same_as<typename _Period::type, hecto>)
101 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
102 else if constexpr (same_as<typename _Period::type, kilo>)
103 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
104 else if constexpr (same_as<typename _Period::type, mega>)
105 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
106 else if constexpr (same_as<typename _Period::type, giga>)
107 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
108 else if constexpr (same_as<typename _Period::type, tera>)
109 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
110 else if constexpr (same_as<typename _Period::type, peta>)
111 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
112 else if constexpr (same_as<typename _Period::type, exa>)
113 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
114 else if constexpr (same_as<typename _Period::type, ratio<60>>)
115 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
116 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
117 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
118 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
119 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
120 else if constexpr (_Period::den == 1)
121 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
122 else
123 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
124}
125
126template <class _CharT, class _Traits, class _Rep, class _Period>
Louis Dionne3d334df2023-03-16 17:09:44127_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever719c3dc2022-03-20 12:40:02128operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
129 basic_ostringstream<_CharT, _Traits> __s;
130 __s.flags(__os.flags());
131 __s.imbue(__os.getloc());
132 __s.precision(__os.precision());
133 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
134 return __os << __s.str();
135}
136
Mark de Wevere5d2d3e2022-03-20 12:40:02137template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44138_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
Mark de Weverde6827b2023-02-24 20:35:41139 return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
140 // Note this error differs from the wording of the Standard. The
141 // Standard wording doesn't work well on AIX or Windows. There
142 // the formatted day seems to be either modulo 100 or completely
143 // omitted. Judging by the wording this is valid.
144 // TODO FMT Write a paper of file an LWG issue.
145 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
146 static_cast<unsigned>(__d)));
Mark de Wevere5d2d3e2022-03-20 12:40:02147}
148
Mark de Wever3eb4f162022-03-20 12:40:02149template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44150_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever1522f192022-03-20 12:40:02151operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
152 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
153 : std::format(__os.getloc(),
154 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
155 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
156}
157
158template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44159_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever3eb4f162022-03-20 12:40:02160operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
161 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
162 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
163}
164
Mark de Wever566868c2022-03-20 12:40:02165template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44166_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever566868c2022-03-20 12:40:02167operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
168 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
169 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
170 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
171 static_cast<unsigned>(__wd.c_encoding())));
172}
173
Mark de Wever105fef52022-03-20 12:40:02174template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44175_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02176operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
177 auto __i = __wdi.index();
178 return __os << (__i >= 1 && __i <= 5
179 ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
180 : std::format(__os.getloc(),
181 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
182 __wdi.weekday(),
183 __i));
184}
185
186template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44187_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02188operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
189 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
190}
191
192template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44193_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02194operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
195 // TODO FMT The Standard allows 30th of February to be printed.
196 // It would be nice to show an error message instead.
197 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
198}
199
200template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44201_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02202operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
203 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
204}
205
206template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44207_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02208operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
209 return __os << std::format(
210 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
211}
212
213template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44214_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02215operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
216 return __os << std::format(
217 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
218}
219
220template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44221_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02222operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
223 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
224}
225
226template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44227_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02228operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
229 return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
230 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
231}
232
233template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44234_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02235operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
236 return __os << std::format(
237 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
238}
239
240template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44241_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02242operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
243 return __os << std::format(
244 __os.getloc(),
245 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
246 __ymwd.year(),
247 __ymwd.month(),
248 __ymwd.weekday_indexed());
249}
250
251template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44252_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02253operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
254 return __os << std::format(
255 __os.getloc(),
256 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
257 __ymwdl.year(),
258 __ymwdl.month(),
259 __ymwdl.weekday_last());
260}
261
Mark de Wever7f5d1302022-03-20 12:40:02262template <class _CharT, class _Traits, class _Duration>
Louis Dionne3d334df2023-03-16 17:09:44263_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever7f5d1302022-03-20 12:40:02264operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
265 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
266}
267
Mark de Wevera4422a52024-04-20 10:09:13268# if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
Mark de Wever6f7976c2024-04-17 18:55:51269
270template <class _CharT, class _Traits>
271_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
272operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __info) {
273 // __info.abbrev is always std::basic_string<char>.
274 // Since these strings typically are short the conversion should be cheap.
275 std::basic_string<_CharT> __abbrev{__info.abbrev.begin(), __info.abbrev.end()};
276 return __os << std::format(
277 _LIBCPP_STATICALLY_WIDEN(_CharT, "[{:%F %T}, {:%F %T}) {:%T} {:%Q%q} \"{}\""),
278 __info.begin,
279 __info.end,
280 hh_mm_ss{__info.offset},
281 __info.save,
282 __abbrev);
283}
284
Mark de Wever8a21d592024-04-18 15:23:07285template <class _CharT, class _Traits>
286_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
287operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __info) {
288 auto __result = [&]() -> basic_string<_CharT> {
289 switch (__info.result) {
290 case local_info::unique:
291 return _LIBCPP_STATICALLY_WIDEN(_CharT, "unique");
292 case local_info::nonexistent:
293 return _LIBCPP_STATICALLY_WIDEN(_CharT, "non-existent");
294 case local_info::ambiguous:
295 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ambiguous");
296
297 default:
298 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "unspecified result ({})"), __info.result);
299 };
300 };
301
302 return __os << std::format(
303 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}: {{{}, {}}}"), __result(), __info.first, __info.second);
304}
305
Mark de Weverafbfb162024-07-20 17:24:41306# if !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \
307 !defined(_LIBCPP_HAS_NO_LOCALIZATION)
308template <class _CharT, class _Traits, class _Duration, class _TimeZonePtr>
309_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
310operator<<(basic_ostream<_CharT, _Traits>& __os, const zoned_time<_Duration, _TimeZonePtr>& __tp) {
311 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T %Z}"), __tp);
312}
313# endif
Mark de Wevera4422a52024-04-20 10:09:13314# endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
Mark de Wever6f7976c2024-04-17 18:55:51315
Mark de Wevere5d2d3e2022-03-20 12:40:02316} // namespace chrono
317
Mark de Weverdff62f52023-05-17 17:17:52318#endif // if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:02319
320_LIBCPP_END_NAMESPACE_STD
321
322#endif // _LIBCPP___CHRONO_OSTREAM_H