1 // (C) Copyright Howard Hinnant
2 // (C) Copyright 2011 Vicente J. Botet Escriba
3 // Copyright (c) Microsoft Corporation 2014
4 // Use, modification and distribution are subject to the Boost Software License,
5 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt).
9 #ifndef BOOST_CHRONO_IO_TIME_POINT_UNITS_HPP
10 #define BOOST_CHRONO_IO_TIME_POINT_UNITS_HPP
12 #include <boost/chrono/config.hpp>
13 #include <boost/chrono/process_cpu_clocks.hpp>
14 #include <boost/chrono/system_clocks.hpp>
15 #include <boost/chrono/thread_clock.hpp>
16 #include <boost/chrono/io/ios_base_state.hpp>
28 * customization point to the epoch associated to the clock @c Clock
29 * The default calls @c f.do_get_epoch(Clock()). The user can overload this function.
30 * @return the string epoch associated to the @c Clock
32 template <typename CharT, typename Clock, typename TPUFacet>
33 std::basic_string<CharT> get_epoch_custom(Clock, TPUFacet& f)
35 return f.do_get_epoch(Clock());
39 * @c time_point_units facet gives useful information about the time_point pattern,
40 * the text associated to a time_point's epoch,
42 template <typename CharT=char>
43 class time_point_units: public std::locale::facet
47 * Type of character the facet is instantiated on.
49 typedef CharT char_type;
51 * Type of character string used by member functions.
53 typedef std::basic_string<char_type> string_type;
56 * Unique identifier for this type of facet.
58 static std::locale::id id;
61 * Construct a @c time_point_units facet.
63 * @Effects Construct a @c time_point_units facet.
64 * If the @c refs argument is @c 0 then destruction of the object is
65 * delegated to the @c locale, or locales, containing it. This allows
66 * the user to ignore lifetime management issues. On the other had,
67 * if @c refs is @c 1 then the object must be explicitly deleted;
68 * the @c locale will not do so. In this case, the object can be
69 * maintained across the lifetime of multiple locales.
71 explicit time_point_units(size_t refs = 0) :
72 std::locale::facet(refs)
77 * @return the pattern to be used by default.
79 virtual string_type get_pattern() const =0;
82 * @return the epoch associated to the clock @c Clock calling @c do_get_epoch(Clock())
84 template <typename Clock>
85 string_type get_epoch() const
87 return get_epoch_custom<CharT>(Clock(), *this);
94 virtual ~time_point_units() {}
100 * @param c a dummy instance of @c system_clock.
101 * @return The epoch string associated to the @c system_clock.
103 virtual string_type do_get_epoch(system_clock) const=0;
107 * @param c a dummy instance of @c steady_clock.
108 * @return The epoch string associated to the @c steady_clock.
110 virtual string_type do_get_epoch(steady_clock) const=0;
112 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
115 * @param c a dummy instance of @c process_real_cpu_clock.
116 * @return The epoch string associated to the @c process_real_cpu_clock.
118 virtual string_type do_get_epoch(process_real_cpu_clock) const=0;
119 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
122 * @param c a dummy instance of @c process_user_cpu_clock.
123 * @return The epoch string associated to the @c process_user_cpu_clock.
125 virtual string_type do_get_epoch(process_user_cpu_clock) const=0;
128 * @param c a dummy instance of @c process_system_cpu_clock.
129 * @return The epoch string associated to the @c process_system_cpu_clock.
131 virtual string_type do_get_epoch(process_system_cpu_clock) const=0;
134 * @param c a dummy instance of @c process_cpu_clock.
135 * @return The epoch string associated to the @c process_cpu_clock.
137 virtual string_type do_get_epoch(process_cpu_clock) const=0;
140 #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
143 * @param c a dummy instance of @c thread_clock.
144 * @return The epoch string associated to the @c thread_clock.
146 virtual string_type do_get_epoch(thread_clock) const=0;
151 template <typename CharT>
152 std::locale::id time_point_units<CharT>::id;
155 // This class is used to define the strings for the default English
156 template <typename CharT=char>
157 class time_point_units_default: public time_point_units<CharT>
161 * Type of character the facet is instantiated on.
163 typedef CharT char_type;
165 * Type of character string returned by member functions.
167 typedef std::basic_string<char_type> string_type;
169 explicit time_point_units_default(size_t refs = 0) :
170 time_point_units<CharT> (refs)
173 ~time_point_units_default() {}
176 * @return the default pattern "%d%e".
178 string_type get_pattern() const
180 static const CharT t[] =
181 { '%', 'd', '%', 'e' };
182 static const string_type pattern(t, t + sizeof (t) / sizeof (t[0]));
189 * @param c a dummy instance of @c system_clock.
190 * @return The epoch string returned by @c clock_string<system_clock,CharT>::since().
192 string_type do_get_epoch(system_clock ) const
194 return clock_string<system_clock,CharT>::since();
197 * @param c a dummy instance of @c steady_clock.
198 * @return The epoch string returned by @c clock_string<steady_clock,CharT>::since().
200 string_type do_get_epoch(steady_clock ) const
202 return clock_string<steady_clock,CharT>::since();
205 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
207 * @param c a dummy instance of @c process_real_cpu_clock.
208 * @return The epoch string returned by @c clock_string<process_real_cpu_clock,CharT>::since().
210 string_type do_get_epoch(process_real_cpu_clock ) const
212 return clock_string<process_real_cpu_clock,CharT>::since();
214 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
216 * @param c a dummy instance of @c process_user_cpu_clock.
217 * @return The epoch string returned by @c clock_string<process_user_cpu_clock,CharT>::since().
219 string_type do_get_epoch(process_user_cpu_clock ) const
221 return clock_string<process_user_cpu_clock,CharT>::since();
224 * @param c a dummy instance of @c process_system_cpu_clock.
225 * @return The epoch string returned by @c clock_string<process_system_cpu_clock,CharT>::since().
227 string_type do_get_epoch(process_system_cpu_clock ) const
229 return clock_string<process_system_cpu_clock,CharT>::since();
232 * @param c a dummy instance of @c process_cpu_clock.
233 * @return The epoch string returned by @c clock_string<process_cpu_clock,CharT>::since().
235 string_type do_get_epoch(process_cpu_clock ) const
237 return clock_string<process_cpu_clock,CharT>::since();
242 #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
244 * @param c a dummy instance of @c thread_clock.
245 * @return The epoch string returned by @c clock_string<thread_clock,CharT>::since().
247 string_type do_get_epoch(thread_clock ) const
249 return clock_string<thread_clock,CharT>::since();