blob: c48bd6a8b307d0f0bfe3a32bc48088f9d4218b1b [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 Wever1522f192022-03-20 12:40:0215#include <__chrono/month.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0216#include <__chrono/statically_widen.h>
Mark de Wever566868c2022-03-20 12:40:0217#include <__chrono/weekday.h>
Mark de Wever3eb4f162022-03-20 12:40:0218#include <__chrono/year.h>
Mark de Wever719c3dc2022-03-20 12:40:0219#include <__concepts/same_as.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0220#include <__config>
Mark de Weverf2a26352022-09-13 18:10:2621#include <__format/format_functions.h>
Mark de Wevere5d2d3e2022-03-20 12:40:0222#include <ostream>
Mark de Wever719c3dc2022-03-20 12:40:0223#include <ratio>
Mark de Wevere5d2d3e2022-03-20 12:40:0224
25#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26# pragma GCC system_header
27#endif
28
29_LIBCPP_BEGIN_NAMESPACE_STD
30
31#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
32
33namespace chrono {
34
Mark de Wever719c3dc2022-03-20 12:40:0235// Depending on the type the return is a const _CharT* or a basic_string<_CharT>
36template <class _CharT, class _Period>
37_LIBCPP_HIDE_FROM_ABI auto __units_suffix() {
38 // TODO FMT LWG issue the suffixes are always char and not STATICALLY-WIDEN'ed.
39 if constexpr (same_as<typename _Period::type, atto>)
40 return _LIBCPP_STATICALLY_WIDEN(_CharT, "as");
41 else if constexpr (same_as<typename _Period::type, femto>)
42 return _LIBCPP_STATICALLY_WIDEN(_CharT, "fs");
43 else if constexpr (same_as<typename _Period::type, pico>)
44 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ps");
45 else if constexpr (same_as<typename _Period::type, nano>)
46 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ns");
47 else if constexpr (same_as<typename _Period::type, micro>)
48# ifndef _LIBCPP_HAS_NO_UNICODE
49 return _LIBCPP_STATICALLY_WIDEN(_CharT, "\u00b5s");
50# else
51 return _LIBCPP_STATICALLY_WIDEN(_CharT, "us");
52# endif
53 else if constexpr (same_as<typename _Period::type, milli>)
54 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ms");
55 else if constexpr (same_as<typename _Period::type, centi>)
56 return _LIBCPP_STATICALLY_WIDEN(_CharT, "cs");
57 else if constexpr (same_as<typename _Period::type, deci>)
58 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ds");
59 else if constexpr (same_as<typename _Period::type, ratio<1>>)
60 return _LIBCPP_STATICALLY_WIDEN(_CharT, "s");
61 else if constexpr (same_as<typename _Period::type, deca>)
62 return _LIBCPP_STATICALLY_WIDEN(_CharT, "das");
63 else if constexpr (same_as<typename _Period::type, hecto>)
64 return _LIBCPP_STATICALLY_WIDEN(_CharT, "hs");
65 else if constexpr (same_as<typename _Period::type, kilo>)
66 return _LIBCPP_STATICALLY_WIDEN(_CharT, "ks");
67 else if constexpr (same_as<typename _Period::type, mega>)
68 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ms");
69 else if constexpr (same_as<typename _Period::type, giga>)
70 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Gs");
71 else if constexpr (same_as<typename _Period::type, tera>)
72 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ts");
73 else if constexpr (same_as<typename _Period::type, peta>)
74 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Ps");
75 else if constexpr (same_as<typename _Period::type, exa>)
76 return _LIBCPP_STATICALLY_WIDEN(_CharT, "Es");
77 else if constexpr (same_as<typename _Period::type, ratio<60>>)
78 return _LIBCPP_STATICALLY_WIDEN(_CharT, "min");
79 else if constexpr (same_as<typename _Period::type, ratio<3600>>)
80 return _LIBCPP_STATICALLY_WIDEN(_CharT, "h");
81 else if constexpr (same_as<typename _Period::type, ratio<86400>>)
82 return _LIBCPP_STATICALLY_WIDEN(_CharT, "d");
83 else if constexpr (_Period::den == 1)
84 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}]s"), _Period::num);
85 else
86 return std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "[{}/{}]s"), _Period::num, _Period::den);
87}
88
89template <class _CharT, class _Traits, class _Rep, class _Period>
90_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
91operator<<(basic_ostream<_CharT, _Traits>& __os, const duration<_Rep, _Period>& __d) {
92 basic_ostringstream<_CharT, _Traits> __s;
93 __s.flags(__os.flags());
94 __s.imbue(__os.getloc());
95 __s.precision(__os.precision());
96 __s << __d.count() << chrono::__units_suffix<_CharT, _Period>();
97 return __os << __s.str();
98}
99
Mark de Wevere5d2d3e2022-03-20 12:40:02100template <class _CharT, class _Traits>
101_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
102operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
103 return __os
104 << (__d.ok()
105 ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%d}"), __d)
106 // Note this error differs from the wording of the Standard. The
107 // Standard wording doesn't work well on AIX or Windows. There
108 // the formatted day seems to be either modulo 100 or completely
109 // omitted. Judging by the wording this is valid.
110 // TODO FMT Write a paper of file an LWG issue.
111 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"), static_cast<unsigned>(__d)));
112}
113
Mark de Wever3eb4f162022-03-20 12:40:02114template <class _CharT, class _Traits>
115_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
Mark de Wever1522f192022-03-20 12:40:02116operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
117 return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
118 : std::format(__os.getloc(),
119 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
120 static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
121}
122
123template <class _CharT, class _Traits>
124_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
Mark de Wever3eb4f162022-03-20 12:40:02125operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
126 return __os << (__y.ok() ? std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y}"), __y)
127 : std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:%Y} is not a valid year"), __y));
128}
129
Mark de Wever566868c2022-03-20 12:40:02130template <class _CharT, class _Traits>
131_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
132operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) {
133 return __os << (__wd.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%a}"), __wd)
134 : std::format(__os.getloc(), // TODO FMT Standard mandated locale isn't used.
135 _LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid weekday"),
136 static_cast<unsigned>(__wd.c_encoding())));
137}
138
Mark de Wevere5d2d3e2022-03-20 12:40:02139} // namespace chrono
140
141#endif //if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
142
143_LIBCPP_END_NAMESPACE_STD
144
145#endif // _LIBCPP___CHRONO_OSTREAM_H