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