1// -*- 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
13#include <__config>
14
15#if _LIBCPP_HAS_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/gps_clock.h>
22# include <__chrono/hh_mm_ss.h>
23# include <__chrono/local_info.h>
24# include <__chrono/month.h>
25# include <__chrono/month_weekday.h>
26# include <__chrono/monthday.h>
27# include <__chrono/statically_widen.h>
28# include <__chrono/sys_info.h>
29# include <__chrono/system_clock.h>
30# include <__chrono/tai_clock.h>
31# include <__chrono/utc_clock.h>
32# include <__chrono/weekday.h>
33# include <__chrono/year.h>
34# include <__chrono/year_month.h>
35# include <__chrono/year_month_day.h>
36# include <__chrono/year_month_weekday.h>
37# include <__chrono/zoned_time.h>
38# include <__concepts/same_as.h>
39# include <__format/format_functions.h>
40# include <__fwd/ostream.h>
41# include <ratio>
42# include <sstream>
43
44# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45# pragma GCC system_header
46# endif
47
48_LIBCPP_BEGIN_NAMESPACE_STD
49
50# if _LIBCPP_STD_VER >= 20
51
52namespace chrono {
53
54template <class _CharT, class _Traits, class _Duration>
55 requires(!treat_as_floating_point_v<typename _Duration::rep> && _Duration{1} < days{1})
56basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration>& __tp) {
57 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
58}
59
60template <class _CharT, class _Traits>
61basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) {
62 return __os << year_month_day{__dp};
63}
64
65# if _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM
66# if _LIBCPP_HAS_EXPERIMENTAL_TZDB
67
68template <class _CharT, class _Traits, class _Duration>
69basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const utc_time<_Duration>& __tp) {
70 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
71}
72
73template <class _CharT, class _Traits, class _Duration>
74basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const tai_time<_Duration>& __tp) {
75 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
76}
77
78template <class _CharT, class _Traits, class _Duration>
79basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const gps_time<_Duration>& __tp) {
80 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
81}
82
83# endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
84# endif // _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM
85
86template <class _CharT, class _Traits, class _Duration>
87basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {
88 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
89}
90
91template <class _CharT, class _Traits, class _Duration>
92basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const local_time<_Duration> __tp) {
93 return __os << sys_time<_Duration>{__tp.time_since_epoch()};
94}
95
96// Depending on the type the return is a const _CharT* or a basic_string<_CharT>
97template <class _CharT, class _Period>
98auto __units_suffix() {
99 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
100 if constexpr (same_as<typename _Period::type, atto>)
101 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
102 else if constexpr (same_as<typename _Period::type, femto>)
103 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
104 else if constexpr (same_as<typename _Period::type, pico>)
105 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
106 else if constexpr (same_as<typename _Period::type, nano>)
107 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
108 else if constexpr (same_as<typename _Period::type, micro>)
109# if _LIBCPP_HAS_UNICODE
110 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
111# else
112 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
113# endif
114 else if constexpr (same_as<typename _Period::type, milli>)
115 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
116 else if constexpr (same_as<typename _Period::type, centi>)
117 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
118 else if constexpr (same_as<typename _Period::type, deci>)
119 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
120 else if constexpr (same_as<typename _Period::type, ratio<1>>)
121 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
122 else if constexpr (same_as<typename _Period::type, deca>)
123 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
124 else if constexpr (same_as<typename _Period::type, hecto>)
125 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
126 else if constexpr (same_as<typename _Period::type, kilo>)
127 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
128 else if constexpr (same_as<typename _Period::type, mega>)
129 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
130 else if constexpr (same_as<typename _Period::type, giga>)
131 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
132 else if constexpr (same_as<typename _Period::type, tera>)
133 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
134 else if constexpr (same_as<typename _Period::type, peta>)
135 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
136 else if constexpr (same_as<typename _Period::type, exa>)
137 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
138 else if constexpr (same_as<typename _Period::type, ratio<60>>)
139 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
140 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
141 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
142 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
143 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
144 else if constexpr (_Period::den == 1)
145 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
146 else
147 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
148}
149
150template <class _CharT, class _Traits, class _Rep, class _Period>
151basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
152 basic_ostringstream<_CharT, _Traits> __s;
153 __s.flags(__os.flags());
154 __s.imbue(__os.getloc());
155 __s.precision(__os.precision());
156 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
157 return __os << __s.str();
158}
159
160template <class _CharT, class _Traits>
161basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
162 return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
163 // Note this error differs from the wording of the Standard. The
164 // Standard wording doesn't work well on AIX or Windows. There
165 // the formatted day seems to be either modulo 100 or completely
166 // omitted. Judging by the wording this is valid.
167 // TODO FMT Write a paper of file an LWG issue.
168 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
169 static_cast<unsigned>(__d)));
170}
171
172template <class _CharT, class _Traits>
173basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
174 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
175 : std::format(__os.getloc(),
176 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
177 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
178}
179
180template <class _CharT, class _Traits>
181basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
182 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
183 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
184}
185
186template <class _CharT, class _Traits>
187basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
188 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
189 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
190 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
191 static_cast<unsigned>(__wd.c_encoding())));
192}
193
194template <class _CharT, class _Traits>
195basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
196 auto __i = __wdi.index();
197 return __os << (__i >= 1 && __i <= 5
198 ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
199 : std::format(__os.getloc(),
200 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
201 __wdi.weekday(),
202 __i));
203}
204
205template <class _CharT, class _Traits>
206basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
207 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
208}
209
210template <class _CharT, class _Traits>
211basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
212 // TODO FMT The Standard allows 30th of February to be printed.
213 // It would be nice to show an error message instead.
214 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
215}
216
217template <class _CharT, class _Traits>
218basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
219 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
220}
221
222template <class _CharT, class _Traits>
223basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
224 return __os << std::format(
225 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
226}
227
228template <class _CharT, class _Traits>
229basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
230 return __os << std::format(
231 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
232}
233
234template <class _CharT, class _Traits>
235basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
236 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
237}
238
239template <class _CharT, class _Traits>
240basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
241 return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
242 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
243}
244
245template <class _CharT, class _Traits>
246basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
247 return __os << std::format(
248 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
249}
250
251template <class _CharT, class _Traits>
252basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
253 return __os << std::format(
254 __os.getloc(),
255 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
256 __ymwd.year(),
257 __ymwd.month(),
258 __ymwd.weekday_indexed());
259}
260
261template <class _CharT, class _Traits>
262basic_ostream<_CharT, _Traits>&
263operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
264 return __os << std::format(
265 __os.getloc(),
266 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
267 __ymwdl.year(),
268 __ymwdl.month(),
269 __ymwdl.weekday_last());
270}
271
272template <class _CharT, class _Traits, class _Duration>
273basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
274 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
275}
276
277# if _LIBCPP_HAS_EXPERIMENTAL_TZDB
278
279template <class _CharT, class _Traits>
280basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __info) {
281 // __info.abbrev is always std::basic_string<char>.
282 // Since these strings typically are short the conversion should be cheap.
283 std::basic_string<_CharT> __abbrev{__info.abbrev.begin(), __info.abbrev.end()};
284 return __os << std::format(
285 _LIBCPP_STATICALLY_WIDEN(_CharT, "[{:%F %T}, {:%F %T}) {:%T} {:%Q%q} \"{}\""),
286 __info.begin,
287 __info.end,
288 hh_mm_ss{__info.offset},
289 __info.save,
290 __abbrev);
291}
292
293template <class _CharT, class _Traits>
294basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __info) {
295 auto __result = [&]() -> basic_string<_CharT> {
296 switch (__info.result) {
297 case local_info::unique:
298 return _LIBCPP_STATICALLY_WIDEN(_CharT, "unique");
299 case local_info::nonexistent:
300 return _LIBCPP_STATICALLY_WIDEN(_CharT, "non-existent");
301 case local_info::ambiguous:
302 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ambiguous");
303
304 default:
305 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "unspecified result ({})"), __info.result);
306 };
307 };
308
309 return __os << std::format(
310 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}: {{{}, {}}}"), __result(), __info.first, __info.second);
311}
312
313# if _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM
314template <class _CharT, class _Traits, class _Duration, class _TimeZonePtr>
315basic_ostream<_CharT, _Traits>&
316operator<<(basic_ostream<_CharT, _Traits>& __os, const zoned_time<_Duration, _TimeZonePtr>& __tp) {
317 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T %Z}"), __tp);
318}
319# endif
320# endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
321
322} // namespace chrono
323
324# endif // if _LIBCPP_STD_VER >= 20
325
326_LIBCPP_END_NAMESPACE_STD
327
328#endif // _LIBCPP_HAS_LOCALIZATION
329
330#endif // _LIBCPP___CHRONO_OSTREAM_H
331