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 <__chrono/day.h>
14 #include <__chrono/duration.h>
15 #include <__chrono/hh_mm_ss.h>
16 #include <__chrono/month.h>
17 #include <__chrono/month_weekday.h>
18 #include <__chrono/monthday.h>
19 #include <__chrono/statically_widen.h>
20 #include <__chrono/system_clock.h>
21 #include <__chrono/weekday.h>
22 #include <__chrono/year.h>
23 #include <__chrono/year_month.h>
24 #include <__chrono/year_month_day.h>
25 #include <__chrono/year_month_weekday.h>
26 #include <__concepts/same_as.h>
27 #include <__config>
28 #include <__format/format_functions.h>
29 #include <ostream>
30 #include <ratio>
31
32 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33 # pragma GCC system_header
34 #endif
35
36 _LIBCPP_BEGIN_NAMESPACE_STD
37
38 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
39
40 namespace chrono {
41
42 template <class _CharT, class _Traits, class _Duration>
43 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
44 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration> __tp) {
45 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
46 }
47
48 // Depending on the type the return is a const _CharT* or a basic_string<_CharT>
49 template <class _CharT, class _Period>
__units_suffix()50 _LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
51 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
52 if constexpr (same_as<typename _Period::type, atto>)
53 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
54 else if constexpr (same_as<typename _Period::type, femto>)
55 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
56 else if constexpr (same_as<typename _Period::type, pico>)
57 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
58 else if constexpr (same_as<typename _Period::type, nano>)
59 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
60 else if constexpr (same_as<typename _Period::type, micro>)
61 # ifndef _LIBCPP_HAS_NO_UNICODE
62 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
63 # else
64 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
65 # endif
66 else if constexpr (same_as<typename _Period::type, milli>)
67 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
68 else if constexpr (same_as<typename _Period::type, centi>)
69 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
70 else if constexpr (same_as<typename _Period::type, deci>)
71 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
72 else if constexpr (same_as<typename _Period::type, ratio<1>>)
73 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
74 else if constexpr (same_as<typename _Period::type, deca>)
75 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
76 else if constexpr (same_as<typename _Period::type, hecto>)
77 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
78 else if constexpr (same_as<typename _Period::type, kilo>)
79 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
80 else if constexpr (same_as<typename _Period::type, mega>)
81 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
82 else if constexpr (same_as<typename _Period::type, giga>)
83 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
84 else if constexpr (same_as<typename _Period::type, tera>)
85 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
86 else if constexpr (same_as<typename _Period::type, peta>)
87 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
88 else if constexpr (same_as<typename _Period::type, exa>)
89 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
90 else if constexpr (same_as<typename _Period::type, ratio<60>>)
91 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
92 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
93 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
94 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
95 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
96 else if constexpr (_Period::den == 1)
97 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
98 else
99 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
100 }
101
102 template <class _CharT, class _Traits, class _Rep, class _Period>
103 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
104 operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
105 basic_ostringstream<_CharT, _Traits> __s;
106 __s.flags(__os.flags());
107 __s.imbue(__os.getloc());
108 __s.precision(__os.precision());
109 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
110 return __os << __s.str();
111 }
112
113 template <class _CharT, class _Traits>
114 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
115 return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
116 // Note this error differs from the wording of the Standard. The
117 // Standard wording doesn't work well on AIX or Windows. There
118 // the formatted day seems to be either modulo 100 or completely
119 // omitted. Judging by the wording this is valid.
120 // TODO FMT Write a paper of file an LWG issue.
121 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
122 static_cast<unsigned>(__d)));
123 }
124
125 template <class _CharT, class _Traits>
126 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
127 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
128 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
129 : std::format(__os.getloc(),
130 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
131 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
132 }
133
134 template <class _CharT, class _Traits>
135 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
136 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
137 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
138 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
139 }
140
141 template <class _CharT, class _Traits>
142 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
143 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
144 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
145 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
146 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
147 static_cast<unsigned>(__wd.c_encoding())));
148 }
149
150 template <class _CharT, class _Traits>
151 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
152 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
153 auto __i = __wdi.index();
154 return __os << (__i >= 1 && __i <= 5
155 ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
156 : std::format(__os.getloc(),
157 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
158 __wdi.weekday(),
159 __i));
160 }
161
162 template <class _CharT, class _Traits>
163 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
164 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
165 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
166 }
167
168 template <class _CharT, class _Traits>
169 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
170 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
171 // TODO FMT The Standard allows 30th of February to be printed.
172 // It would be nice to show an error message instead.
173 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
174 }
175
176 template <class _CharT, class _Traits>
177 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
178 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
179 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
180 }
181
182 template <class _CharT, class _Traits>
183 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
184 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
185 return __os << std::format(
186 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
187 }
188
189 template <class _CharT, class _Traits>
190 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
191 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
192 return __os << std::format(
193 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
194 }
195
196 template <class _CharT, class _Traits>
197 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
198 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
199 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
200 }
201
202 template <class _CharT, class _Traits>
203 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
204 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
205 return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
206 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
207 }
208
209 template <class _CharT, class _Traits>
210 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
211 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
212 return __os << std::format(
213 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
214 }
215
216 template <class _CharT, class _Traits>
217 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
218 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
219 return __os << std::format(
220 __os.getloc(),
221 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
222 __ymwd.year(),
223 __ymwd.month(),
224 __ymwd.weekday_indexed());
225 }
226
227 template <class _CharT, class _Traits>
228 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
229 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
230 return __os << std::format(
231 __os.getloc(),
232 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
233 __ymwdl.year(),
234 __ymwdl.month(),
235 __ymwdl.weekday_last());
236 }
237
238 template <class _CharT, class _Traits, class _Duration>
239 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
240 operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
241 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
242 }
243
244 } // namespace chrono
245
246 #endif // if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
247
248 _LIBCPP_END_NAMESPACE_STD
249
250 #endif // _LIBCPP___CHRONO_OSTREAM_H
251