1 // (C) Copyright Howard Hinnant
2 // (C) Copyright 2011 Vicente J. Botet Escriba
3 // Use, modification and distribution are subject to the Boost Software License,
4 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt).
9 * Duration formatting facet for output.
11 #ifndef BOOST_CHRONO_IO_TIME_POINT_PUT_HPP
12 #define BOOST_CHRONO_IO_TIME_POINT_PUT_HPP
14 #include <boost/chrono/config.hpp>
15 #include <boost/chrono/io/time_point_units.hpp>
16 #include <boost/chrono/io/duration_put.hpp>
17 #include <boost/assert.hpp>
26 * @tparam ChatT a character type
27 * @tparam OutputIterator a model of @c OutputIterator
29 * The @c time_point_put facet provides facilities for formatted output of @c time_point values.
30 * The member function of @c time_point_put take a @c time_point and format it into character string representation.
33 template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
34 class time_point_put: public std::locale::facet
38 * Type of character the facet is instantiated on.
40 typedef CharT char_type;
42 * Type of character string passed to member functions.
44 typedef std::basic_string<CharT> string_type;
46 * Type of iterator used to write in the character buffer.
48 typedef OutputIterator iter_type;
51 * Construct a time_point_put facet.
53 * @Effects Construct a time_point_put facet.
54 * If the @c refs argument is @c 0 then destruction of the object is
55 * delegated to the @c locale, or locales, containing it. This allows
56 * the user to ignore lifetime management issues. On the other had,
57 * if @c refs is @c 1 then the object must be explicitly deleted;
58 * the @c locale will not do so. In this case, the object can be
59 * maintained across the lifetime of multiple locales.
61 explicit time_point_put(size_t refs = 0) :
62 std::locale::facet(refs)
67 * @param i an output stream iterator
68 * @param ios a reference to a ios_base
69 * @param fill the character used as filler
70 * @param tp the @c time_point
71 * @param pattern begin of the formatting pattern
72 * @param pat_end end of the formatting pattern
74 * @Effects Steps through the sequence from @c pattern to @c pat_end,
75 * identifying characters that are part of a pattern sequence. Each character
76 * that is not part of a pattern sequence is written to @c s immediately, and
77 * each pattern sequence, as it is identified, results in a call to
78 * @c put_duration or @c put_epoch;
79 * thus, pattern elements and other characters are interleaved in the output
80 * in the order in which they appear in the pattern. Pattern sequences are
81 * identified by converting each character @c c to a @c char value as if by
82 * @c ct.narrow(c,0), where @c ct is a reference to @c ctype<charT> obtained from
83 * @c ios.getloc(). The first character of each sequence is equal to @c '%',
84 * followed by a pattern specifier character @c spec, which can be @c 'd' for
85 * the duration value or @c 'e' for the epoch.
86 * For each valid pattern sequence identified, calls
87 * <c>put_duration(s, ios, fill, tp.time_since_epoch())</c> or <c>put_epoch(s, ios)</c>.
89 * @Returns An iterator pointing immediately after the last character produced.
92 template <class Clock, class Duration>
93 iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp, const CharT* pattern,
94 const CharT* pat_end) const
96 if (std::has_facet<time_point_units<CharT> >(ios.getloc()))
98 time_point_units<CharT> const &facet =
99 std::use_facet<time_point_units<CharT> >(ios.getloc());
100 return put(facet, i, ios, fill, tp, pattern, pat_end);
104 time_point_units_default<CharT> facet;
105 return put(facet, i, ios, fill, tp, pattern, pat_end);
109 template <class Clock, class Duration>
110 iter_type put(time_point_units<CharT> const& units_facet, iter_type s, std::ios_base& ios, char_type fill,
111 time_point<Clock, Duration> const& tp, const CharT* pattern, const CharT* pat_end) const
114 const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
115 for (; pattern != pat_end; ++pattern)
117 if (ct.narrow(*pattern, 0) == '%')
119 if (++pattern == pat_end)
124 char fmt = ct.narrow(*pattern, 0);
129 s = put_duration(s, ios, fill, tp.time_since_epoch());
134 s = put_epoch<Clock> (units_facet, s, ios);
138 BOOST_ASSERT(false && "Boost::Chrono internal error.");
149 * @param i an output stream iterator
150 * @param ios a reference to a ios_base
151 * @param fill the character used as filler
152 * @param tp the @c time_point
153 * @param pattern begin of the formatting pattern
154 * @param pat_end end of the formatting pattern
156 * @Effects Stores the time_point pattern from the @c time_point_unit facet in let say @c str. Last as if
158 * return put(s, ios, dill, tp, str.data(), str.data() + str.size());
160 * @Returns An iterator pointing immediately after the last character produced.
162 template <class Clock, class Duration>
163 iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp) const
165 if (std::has_facet<time_point_units<CharT> >(ios.getloc()))
167 time_point_units<CharT> const &facet =
168 std::use_facet<time_point_units<CharT> >(ios.getloc());
169 std::basic_string<CharT> str = facet.get_pattern();
170 return put(facet, i, ios, fill, tp, str.data(), str.data() + str.size());
174 time_point_units_default<CharT> facet;
175 std::basic_string<CharT> str = facet.get_pattern();
176 return put(facet, i, ios, fill, tp, str.data(), str.data() + str.size());
181 * @param i an output stream iterator
182 * @param ios a reference to a ios_base
183 * @param fill the character used as filler
184 * @param d the @c duration
185 * @Effects As if <c>facet.put(s, ios, fill, d)</c> where facet is the @c duration_put<CharT> facet associated
186 * to the @c ios or a new instance of @c duration_put<CharT>.
187 * @Returns An iterator pointing immediately after the last character produced.
189 template <typename Rep, typename Period>
190 iter_type put_duration(iter_type i, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
192 if (std::has_facet<duration_put<CharT> >(ios.getloc()))
194 duration_put<CharT> const &facet = std::use_facet<duration_put<CharT> >(ios.getloc());
195 return facet.put(i, ios, fill, d);
199 duration_put<CharT> facet;
200 return facet.put(i, ios, fill, d);
206 * @param i an output stream iterator
207 * @param ios a reference to a ios_base
210 * string_type str = facet.template get_epoch<Clock>();
211 * s=std::copy(str.begin(), str.end(), s);
213 * where facet is the @c time_point_units<CharT> facet associated
214 * to the @c ios or a new instance of @c time_point_units_default<CharT>.
215 * @Returns s, iterator pointing immediately after the last character produced.
218 template <typename Clock>
219 iter_type put_epoch(iter_type i, std::ios_base& os) const
221 if (std::has_facet<time_point_units<CharT> >(os.getloc()))
223 time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(os.getloc());
224 return put_epoch<Clock> (facet, i, os);
228 time_point_units_default<CharT> facet;
229 return put_epoch<Clock> (facet, i, os);
233 template <typename Clock>
234 iter_type put_epoch(time_point_units<CharT> const& facet, iter_type s, std::ios_base&) const
236 string_type str = facet.template get_epoch<Clock>();
237 s= std::copy(str.begin(), str.end(), s);
242 * Unique identifier for this type of facet.
244 static std::locale::id id;
247 * @Effects Destroy the facet
255 template <class CharT, class OutputIterator>
256 std::locale::id time_point_put<CharT, OutputIterator>::id;