blob: 9476406d761075ab6ee41d2a372d6d64d31a0dad [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 Wever1522f192022-03-20 12:40:0218#include <__chrono/month.h>
Mark de Wever105fef52022-03-20 12:40:0219#include <__chrono/month_weekday.h>
20#include <__chrono/monthday.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0221#include <__chrono/statically_widen.h>
Mark de Wever6f7976c2024-04-17 18:55:5122#include <__chrono/sys_info.h>
Mark de Wever2c1d7952022-03-20 12:40:0223#include <__chrono/system_clock.h>
Mark de Wever566868c2022-03-20 12:40:0224#include <__chrono/weekday.h>
Mark de Wever3eb4f162022-03-20 12:40:0225#include <__chrono/year.h>
Mark de Wever105fef52022-03-20 12:40:0226#include <__chrono/year_month.h>
27#include <__chrono/year_month_day.h>
28#include <__chrono/year_month_weekday.h>
Mark de Wever719c3dc2022-03-20 12:40:0229#include <__concepts/same_as.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0230#include <__config>
Mark de Weverf2a26352022-09-13 18:10:2631#include <__format/format_functions.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0232#include <ostream>
Mark de Wever719c3dc2022-03-20 12:40:0233#include <ratio>
Mark de Wevere5d2d3e2022-03-20 12:40:0234
35#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
36# pragma GCC system_header
37#endif
38
39_LIBCPP_BEGIN_NAMESPACE_STD
40
Mark de Weverdff62f52023-05-17 17:17:5241#if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:0242
43namespace chrono {
44
Mark de Wever2c1d7952022-03-20 12:40:0245template <class _CharT, class _Traits, class _Duration>
Mark de Wever042a6a12024-01-22 18:06:1546 requires(!treat_as_floating_point_v<typename _Duration::rep> && _Duration{1} < days{1})
Mark de Wever2c1d7952022-03-20 12:40:0247_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever042a6a12024-01-22 18:06:1548operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration>& __tp) {
Mark de Wever2c1d7952022-03-20 12:40:0249 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
50}
51
Mark de Wever042a6a12024-01-22 18:06:1552template <class _CharT, class _Traits>
53_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
54operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) {
55 return __os << year_month_day{__dp};
56}
57
Mark de Wever96f30332023-04-20 19:40:3658template <class _CharT, class _Traits, class _Duration>
59_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
60operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {
61 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
62}
63
Mark de Weverbc2cf422023-04-28 06:13:0164template <class _CharT, class _Traits, class _Duration>
65_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
66operator<<(basic_ostream<_CharT, _Traits>& __os, const local_time<_Duration> __tp) {
67 return __os << sys_time<_Duration>{__tp.time_since_epoch()};
68}
69
Mark de Wever719c3dc2022-03-20 12:40:0270// Depending on the type the return is a const _CharT* or a basic_string<_CharT>
71template <class _CharT, class _Period>
72_LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
73 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
74 if constexpr (same_as<typename _Period::type, atto>)
75 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
76 else if constexpr (same_as<typename _Period::type, femto>)
77 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
78 else if constexpr (same_as<typename _Period::type, pico>)
79 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
80 else if constexpr (same_as<typename _Period::type, nano>)
81 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
82 else if constexpr (same_as<typename _Period::type, micro>)
83# ifndef _LIBCPP_HAS_NO_UNICODE
84 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
85# else
86 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
87# endif
88 else if constexpr (same_as<typename _Period::type, milli>)
89 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
90 else if constexpr (same_as<typename _Period::type, centi>)
91 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
92 else if constexpr (same_as<typename _Period::type, deci>)
93 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
94 else if constexpr (same_as<typename _Period::type, ratio<1>>)
95 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
96 else if constexpr (same_as<typename _Period::type, deca>)
97 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
98 else if constexpr (same_as<typename _Period::type, hecto>)
99 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
100 else if constexpr (same_as<typename _Period::type, kilo>)
101 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
102 else if constexpr (same_as<typename _Period::type, mega>)
103 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
104 else if constexpr (same_as<typename _Period::type, giga>)
105 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
106 else if constexpr (same_as<typename _Period::type, tera>)
107 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
108 else if constexpr (same_as<typename _Period::type, peta>)
109 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
110 else if constexpr (same_as<typename _Period::type, exa>)
111 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
112 else if constexpr (same_as<typename _Period::type, ratio<60>>)
113 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
114 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
115 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
116 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
117 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
118 else if constexpr (_Period::den == 1)
119 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
120 else
121 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
122}
123
124template <class _CharT, class _Traits, class _Rep, class _Period>
Louis Dionne3d334df2023-03-16 17:09:44125_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever719c3dc2022-03-20 12:40:02126operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
127 basic_ostringstream<_CharT, _Traits> __s;
128 __s.flags(__os.flags());
129 __s.imbue(__os.getloc());
130 __s.precision(__os.precision());
131 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
132 return __os << __s.str();
133}
134
Mark de Wevere5d2d3e2022-03-20 12:40:02135template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44136_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
Mark de Weverde6827b2023-02-24 20:35:41137 return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
138 // Note this error differs from the wording of the Standard. The
139 // Standard wording doesn't work well on AIX or Windows. There
140 // the formatted day seems to be either modulo 100 or completely
141 // omitted. Judging by the wording this is valid.
142 // TODO FMT Write a paper of file an LWG issue.
143 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
144 static_cast<unsigned>(__d)));
Mark de Wevere5d2d3e2022-03-20 12:40:02145}
146
Mark de Wever3eb4f162022-03-20 12:40:02147template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44148_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever1522f192022-03-20 12:40:02149operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
150 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
151 : std::format(__os.getloc(),
152 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
153 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
154}
155
156template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44157_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever3eb4f162022-03-20 12:40:02158operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
159 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
160 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
161}
162
Mark de Wever566868c2022-03-20 12:40:02163template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44164_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever566868c2022-03-20 12:40:02165operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
166 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
167 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
168 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
169 static_cast<unsigned>(__wd.c_encoding())));
170}
171
Mark de Wever105fef52022-03-20 12:40:02172template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44173_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02174operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
175 auto __i = __wdi.index();
176 return __os << (__i >= 1 && __i <= 5
177 ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
178 : std::format(__os.getloc(),
179 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
180 __wdi.weekday(),
181 __i));
182}
183
184template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44185_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02186operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
187 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
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 month_day& __md) {
193 // TODO FMT The Standard allows 30th of February to be printed.
194 // It would be nice to show an error message instead.
195 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
196}
197
198template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44199_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02200operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
201 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
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_weekday& __mwd) {
207 return __os << std::format(
208 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
209}
210
211template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44212_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02213operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
214 return __os << std::format(
215 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
216}
217
218template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44219_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02220operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
221 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
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_day& __ymd) {
227 return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
228 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
229}
230
231template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44232_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02233operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
234 return __os << std::format(
235 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
236}
237
238template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44239_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02240operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
241 return __os << std::format(
242 __os.getloc(),
243 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
244 __ymwd.year(),
245 __ymwd.month(),
246 __ymwd.weekday_indexed());
247}
248
249template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44250_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02251operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
252 return __os << std::format(
253 __os.getloc(),
254 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
255 __ymwdl.year(),
256 __ymwdl.month(),
257 __ymwdl.weekday_last());
258}
259
Mark de Wever7f5d1302022-03-20 12:40:02260template <class _CharT, class _Traits, class _Duration>
Louis Dionne3d334df2023-03-16 17:09:44261_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever7f5d1302022-03-20 12:40:02262operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
263 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
264}
265
Mark de Wever6f7976c2024-04-17 18:55:51266# if !defined(_LIBCPP_HAS_NO_INCOMPLETE_TZDB)
267
268template <class _CharT, class _Traits>
269_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
270operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __info) {
271 // __info.abbrev is always std::basic_string<char>.
272 // Since these strings typically are short the conversion should be cheap.
273 std::basic_string<_CharT> __abbrev{__info.abbrev.begin(), __info.abbrev.end()};
274 return __os << std::format(
275 _LIBCPP_STATICALLY_WIDEN(_CharT, "[{:%F %T}, {:%F %T}) {:%T} {:%Q%q} \"{}\""),
276 __info.begin,
277 __info.end,
278 hh_mm_ss{__info.offset},
279 __info.save,
280 __abbrev);
281}
282
283# endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_TZDB)
284
Mark de Wevere5d2d3e2022-03-20 12:40:02285} // namespace chrono
286
Mark de Weverdff62f52023-05-17 17:17:52287#endif // if _LIBCPP_STD_VER >= 20
Mark de Wevere5d2d3e2022-03-20 12:40:02288
289_LIBCPP_END_NAMESPACE_STD
290
291#endif // _LIBCPP___CHRONO_OSTREAM_H