blob: b1a2fb1f3263804d135c9a00b60e21b2e29a9913 [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
13#include <__chrono/day.h>
Mark de Wever719c3dc2022-03-20 12:40:0214#include <__chrono/duration.h>
Mark de Wever96f30332023-04-20 19:40:3615#include <__chrono/file_clock.h>
Mark de Wever7f5d1302022-03-20 12:40:0216#include <__chrono/hh_mm_ss.h>
Mark de Wever1522f192022-03-20 12:40:0217#include <__chrono/month.h>
Mark de Wever105fef52022-03-20 12:40:0218#include <__chrono/month_weekday.h>
19#include <__chrono/monthday.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0220#include <__chrono/statically_widen.h>
Mark de Wever2c1d7952022-03-20 12:40:0221#include <__chrono/system_clock.h>
Mark de Wever566868c2022-03-20 12:40:0222#include <__chrono/weekday.h>
Mark de Wever3eb4f162022-03-20 12:40:0223#include <__chrono/year.h>
Mark de Wever105fef52022-03-20 12:40:0224#include <__chrono/year_month.h>
25#include <__chrono/year_month_day.h>
26#include <__chrono/year_month_weekday.h>
Mark de Wever719c3dc2022-03-20 12:40:0227#include <__concepts/same_as.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0228#include <__config>
Mark de Weverf2a26352022-09-13 18:10:2629#include <__format/format_functions.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0230#include <ostream>
Mark de Wever719c3dc2022-03-20 12:40:0231#include <ratio>
Mark de Wevere5d2d3e2022-03-20 12:40:0232
33#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
34# pragma GCC system_header
35#endif
36
37_LIBCPP_BEGIN_NAMESPACE_STD
38
Nikolas Klauser4f152672023-02-13 23:56:0939#if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
Mark de Wevere5d2d3e2022-03-20 12:40:0240
41namespace chrono {
42
Mark de Wever2c1d7952022-03-20 12:40:0243template <class _CharT, class _Traits, class _Duration>
44_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
45operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_time<_Duration> __tp) {
46 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
47}
48
Mark de Wever96f30332023-04-20 19:40:3649template <class _CharT, class _Traits, class _Duration>
50_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
51operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {
52 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
53}
54
Mark de Wever719c3dc2022-03-20 12:40:0255// Depending on the type the return is a const _CharT* or a basic_string<_CharT>
56template <class _CharT, class _Period>
57_LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
58 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
59 if constexpr (same_as<typename _Period::type, atto>)
60 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
61 else if constexpr (same_as<typename _Period::type, femto>)
62 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
63 else if constexpr (same_as<typename _Period::type, pico>)
64 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
65 else if constexpr (same_as<typename _Period::type, nano>)
66 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
67 else if constexpr (same_as<typename _Period::type, micro>)
68# ifndef _LIBCPP_HAS_NO_UNICODE
69 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
70# else
71 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
72# endif
73 else if constexpr (same_as<typename _Period::type, milli>)
74 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
75 else if constexpr (same_as<typename _Period::type, centi>)
76 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
77 else if constexpr (same_as<typename _Period::type, deci>)
78 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
79 else if constexpr (same_as<typename _Period::type, ratio<1>>)
80 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
81 else if constexpr (same_as<typename _Period::type, deca>)
82 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
83 else if constexpr (same_as<typename _Period::type, hecto>)
84 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
85 else if constexpr (same_as<typename _Period::type, kilo>)
86 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
87 else if constexpr (same_as<typename _Period::type, mega>)
88 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
89 else if constexpr (same_as<typename _Period::type, giga>)
90 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
91 else if constexpr (same_as<typename _Period::type, tera>)
92 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
93 else if constexpr (same_as<typename _Period::type, peta>)
94 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
95 else if constexpr (same_as<typename _Period::type, exa>)
96 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
97 else if constexpr (same_as<typename _Period::type, ratio<60>>)
98 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
99 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
100 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
101 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
102 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
103 else if constexpr (_Period::den == 1)
104 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
105 else
106 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
107}
108
109template <class _CharT, class _Traits, class _Rep, class _Period>
Louis Dionne3d334df2023-03-16 17:09:44110_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever719c3dc2022-03-20 12:40:02111operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
112 basic_ostringstream<_CharT, _Traits> __s;
113 __s.flags(__os.flags());
114 __s.imbue(__os.getloc());
115 __s.precision(__os.precision());
116 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
117 return __os << __s.str();
118}
119
Mark de Wevere5d2d3e2022-03-20 12:40:02120template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44121_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
Mark de Weverde6827b2023-02-24 20:35:41122 return __os << (__d.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
123 // Note this error differs from the wording of the Standard. The
124 // Standard wording doesn't work well on AIX or Windows. There
125 // the formatted day seems to be either modulo 100 or completely
126 // omitted. Judging by the wording this is valid.
127 // TODO FMT Write a paper of file an LWG issue.
128 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"),
129 static_cast<unsigned>(__d)));
Mark de Wevere5d2d3e2022-03-20 12:40:02130}
131
Mark de Wever3eb4f162022-03-20 12:40:02132template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44133_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever1522f192022-03-20 12:40:02134operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
135 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
136 : std::format(__os.getloc(),
137 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
138 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
139}
140
141template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44142_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever3eb4f162022-03-20 12:40:02143operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
144 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
145 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
146}
147
Mark de Wever566868c2022-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 Wever566868c2022-03-20 12:40:02150operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
151 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
152 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
153 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
154 static_cast<unsigned>(__wd.c_encoding())));
155}
156
Mark de Wever105fef52022-03-20 12:40:02157template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44158_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02159operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_indexed& __wdi) {
160 auto __i = __wdi.index();
161 return __os << (__i >= 1 && __i <= 5
162 ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{}]"), __wdi.weekday(), __i)
163 : std::format(__os.getloc(),
164 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[{} is not a valid index]"),
165 __wdi.weekday(),
166 __i));
167}
168
169template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44170_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02171operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday_last& __wdl) {
172 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}[last]"), __wdl.weekday());
173}
174
175template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44176_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02177operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) {
178 // TODO FMT The Standard allows 30th of February to be printed.
179 // It would be nice to show an error message instead.
180 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{}"), __md.month(), __md.day());
181}
182
183template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44184_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02185operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day_last& __mdl) {
186 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/last"), __mdl.month());
187}
188
189template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44190_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02191operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday& __mwd) {
192 return __os << std::format(
193 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwd.month(), __mwd.weekday_indexed());
194}
195
196template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44197_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02198operator<<(basic_ostream<_CharT, _Traits>& __os, const month_weekday_last& __mwdl) {
199 return __os << std::format(
200 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L}/{:L}"), __mwdl.month(), __mwdl.weekday_last());
201}
202
203template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44204_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02205operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) {
206 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ym.year(), __ym.month());
207}
208
209template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44210_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02211operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day& __ymd) {
212 return __os << (__ymd.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F}"), __ymd)
213 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%F} is not a valid date"), __ymd));
214}
215
216template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44217_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02218operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_day_last& __ymdl) {
219 return __os << std::format(
220 __os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}"), __ymdl.year(), __ymdl.month_day_last());
221}
222
223template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44224_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02225operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday& __ymwd) {
226 return __os << std::format(
227 __os.getloc(),
228 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
229 __ymwd.year(),
230 __ymwd.month(),
231 __ymwd.weekday_indexed());
232}
233
234template <class _CharT, class _Traits>
Louis Dionne3d334df2023-03-16 17:09:44235_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever105fef52022-03-20 12:40:02236operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month_weekday_last& __ymwdl) {
237 return __os << std::format(
238 __os.getloc(),
239 _LIBCPP_STATICALLY_WIDEN(_CharT, "{}/{:L}/{:L}"),
240 __ymwdl.year(),
241 __ymwdl.month(),
242 __ymwdl.weekday_last());
243}
244
Mark de Wever7f5d1302022-03-20 12:40:02245template <class _CharT, class _Traits, class _Duration>
Louis Dionne3d334df2023-03-16 17:09:44246_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Mark de Wever7f5d1302022-03-20 12:40:02247operator<<(basic_ostream<_CharT, _Traits>& __os, const hh_mm_ss<_Duration> __hms) {
248 return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%T}"), __hms);
249}
250
Mark de Wevere5d2d3e2022-03-20 12:40:02251} // namespace chrono
252
Mark de Weverde6827b2023-02-24 20:35:41253#endif // if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
Mark de Wevere5d2d3e2022-03-20 12:40:02254
255_LIBCPP_END_NAMESPACE_STD
256
257#endif // _LIBCPP___CHRONO_OSTREAM_H