2 * Copyright Andrey Semashev 2007 - 2015.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file decomposed_time.hpp
9 * \author Andrey Semashev
12 * \brief This header is the Boost.Log library implementation, see the library documentation
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
16 #ifndef BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_
23 #include <boost/cstdint.hpp>
24 #include <boost/move/core.hpp>
25 #include <boost/range/iterator_range_core.hpp>
26 #include <boost/log/detail/config.hpp>
27 #include <boost/log/detail/date_time_format_parser.hpp>
28 #include <boost/log/detail/attachable_sstream_buf.hpp>
29 #include <boost/log/utility/formatting_ostream.hpp>
30 #include <boost/log/detail/header.hpp>
32 #ifdef BOOST_HAS_PRAGMA_ONCE
38 BOOST_LOG_OPEN_NAMESPACE
42 //! Date and time suitable for formatting
43 struct decomposed_time
45 // Subseconds are microseconds
48 subseconds_per_second = 1000000,
49 subseconds_digits10 = 6
52 uint32_t year, month, day, hours, minutes, seconds, subseconds;
55 decomposed_time() : year(0), month(1), day(1), hours(0), minutes(0), seconds(0), subseconds(0), negative(false)
59 decomposed_time(uint32_t y, uint32_t mo, uint32_t d, uint32_t h, uint32_t mi, uint32_t s, uint32_t ss = 0, bool neg = false) :
60 year(y), month(mo), day(d), hours(h), minutes(mi), seconds(s), subseconds(ss), negative(neg)
64 unsigned int week_day() const
66 unsigned int a = (14u - month) / 12u;
67 unsigned int y = year - a;
68 unsigned int m = month + 12u * a - 2u;
69 return (day + y + (y / 4u) - (y / 100u) + (y / 400u) + (31u * m) / 12u) % 7u;
72 unsigned int year_day() const
74 bool is_leap_year = (!(year % 4u)) && ((year % 100u) || (!(year % 400u)));
75 static const unsigned int first_day_offset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
76 return first_day_offset[month - 1] + day + (month > 2 && is_leap_year);
80 inline std::tm to_tm(decomposed_time const& t)
83 res.tm_year = static_cast< int >(t.year) - 1900;
84 res.tm_mon = t.month - 1;
86 res.tm_hour = t.hours;
87 res.tm_min = t.minutes;
88 res.tm_sec = t.seconds;
89 res.tm_wday = t.week_day();
90 res.tm_yday = t.year_day();
96 template< typename T >
97 struct decomposed_time_wrapper :
98 public boost::log::aux::decomposed_time
100 typedef boost::log::aux::decomposed_time base_type;
101 typedef T value_type;
104 BOOST_DEFAULTED_FUNCTION(decomposed_time_wrapper(), {})
106 explicit decomposed_time_wrapper(value_type const& time) : m_time(time)
111 template< typename CharT >
112 BOOST_LOG_API void put_integer(boost::log::aux::basic_ostringstreambuf< CharT >& strbuf, uint32_t value, unsigned int width, CharT fill_char);
114 template< typename T, typename CharT >
115 class date_time_formatter
117 BOOST_COPYABLE_AND_MOVABLE_ALT(date_time_formatter)
120 // Note: This typedef is needed to work around MSVC 2012 crappy name lookup in the derived classes
121 typedef date_time_formatter date_time_formatter_;
124 typedef void result_type;
125 typedef T value_type;
126 typedef CharT char_type;
127 typedef std::basic_string< char_type > string_type;
128 typedef basic_formatting_ostream< char_type > stream_type;
132 date_time_formatter const& self;
134 value_type const& value;
135 unsigned int literal_index, literal_pos;
137 context(date_time_formatter const& self_, stream_type& strm_, value_type const& value_) :
146 BOOST_DELETED_FUNCTION(context(context const&))
147 BOOST_DELETED_FUNCTION(context& operator=(context const&))
151 typedef void (*formatter_type)(context&);
152 typedef std::vector< formatter_type > formatters;
153 typedef std::vector< unsigned int > literal_lens;
156 formatters m_formatters;
157 literal_lens m_literal_lens;
158 string_type m_literal_chars;
161 BOOST_DEFAULTED_FUNCTION(date_time_formatter(), {})
162 date_time_formatter(date_time_formatter const& that) :
163 m_formatters(that.m_formatters),
164 m_literal_lens(that.m_literal_lens),
165 m_literal_chars(that.m_literal_chars)
168 date_time_formatter(BOOST_RV_REF(date_time_formatter) that) BOOST_NOEXCEPT
170 this->swap(static_cast< date_time_formatter& >(that));
173 date_time_formatter& operator= (date_time_formatter that) BOOST_NOEXCEPT
179 result_type operator() (stream_type& strm, value_type const& value) const
181 // Some formatters will put characters directly to the underlying string, so we have to flush stream buffers before formatting
183 context ctx(*this, strm, value);
184 for (typename formatters::const_iterator it = m_formatters.begin(), end = m_formatters.end(); strm.good() && it != end; ++it)
190 void add_formatter(formatter_type fun)
192 m_formatters.push_back(fun);
195 void add_literal(iterator_range< const char_type* > const& lit)
197 m_literal_chars.append(lit.begin(), lit.end());
198 m_literal_lens.push_back(static_cast< unsigned int >(lit.size()));
199 m_formatters.push_back(&date_time_formatter_::format_literal);
202 void swap(date_time_formatter& that) BOOST_NOEXCEPT
204 m_formatters.swap(that.m_formatters);
205 m_literal_lens.swap(that.m_literal_lens);
206 m_literal_chars.swap(that.m_literal_chars);
210 template< char FormatCharV >
211 static void format_through_locale(context& ctx)
213 typedef std::time_put< char_type > facet_type;
214 typedef typename facet_type::iter_type iter_type;
215 std::tm t = to_tm(static_cast< decomposed_time const& >(ctx.value));
216 std::use_facet< facet_type >(ctx.strm.getloc()).put(iter_type(ctx.strm.stream()), ctx.strm.stream(), ' ', &t, FormatCharV);
220 static void format_full_year(context& ctx)
222 (put_integer)(*ctx.strm.rdbuf(), ctx.value.year, 4, static_cast< char_type >('0'));
225 static void format_short_year(context& ctx)
227 (put_integer)(*ctx.strm.rdbuf(), ctx.value.year % 100u, 2, static_cast< char_type >('0'));
230 static void format_numeric_month(context& ctx)
232 (put_integer)(*ctx.strm.rdbuf(), ctx.value.month, 2, static_cast< char_type >('0'));
235 template< char_type FillCharV >
236 static void format_month_day(context& ctx)
238 (put_integer)(*ctx.strm.rdbuf(), ctx.value.day, 2, static_cast< char_type >(FillCharV));
241 static void format_week_day(context& ctx)
243 (put_integer)(*ctx.strm.rdbuf(), static_cast< decomposed_time const& >(ctx.value).week_day(), 1, static_cast< char_type >('0'));
246 template< char_type FillCharV >
247 static void format_hours(context& ctx)
249 (put_integer)(*ctx.strm.rdbuf(), ctx.value.hours, 2, static_cast< char_type >(FillCharV));
252 template< char_type FillCharV >
253 static void format_hours_12(context& ctx)
255 (put_integer)(*ctx.strm.rdbuf(), ctx.value.hours % 12u + 1u, 2, static_cast< char_type >(FillCharV));
258 static void format_minutes(context& ctx)
260 (put_integer)(*ctx.strm.rdbuf(), ctx.value.minutes, 2, static_cast< char_type >('0'));
263 static void format_seconds(context& ctx)
265 (put_integer)(*ctx.strm.rdbuf(), ctx.value.seconds, 2, static_cast< char_type >('0'));
268 static void format_fractional_seconds(context& ctx)
270 (put_integer)(*ctx.strm.rdbuf(), ctx.value.subseconds, decomposed_time::subseconds_digits10, static_cast< char_type >('0'));
273 template< bool UpperCaseV >
274 static void format_am_pm(context& ctx)
276 static const char_type am[] = { static_cast< char_type >(UpperCaseV ? 'A' : 'a'), static_cast< char_type >(UpperCaseV ? 'M' : 'm'), static_cast< char_type >(0) };
277 static const char_type pm[] = { static_cast< char_type >(UpperCaseV ? 'P' : 'p'), static_cast< char_type >(UpperCaseV ? 'M' : 'm'), static_cast< char_type >(0) };
279 ctx.strm.rdbuf()->append(((static_cast< decomposed_time const& >(ctx.value).hours > 11) ? pm : am), 2u);
282 template< bool DisplayPositiveV >
283 static void format_sign(context& ctx)
285 if (static_cast< decomposed_time const& >(ctx.value).negative)
286 ctx.strm.rdbuf()->push_back('-');
287 else if (DisplayPositiveV)
288 ctx.strm.rdbuf()->push_back('+');
292 static void format_literal(context& ctx)
294 unsigned int len = ctx.self.m_literal_lens[ctx.literal_index], pos = ctx.literal_pos;
296 ctx.literal_pos += len;
297 const char_type* lit = ctx.self.m_literal_chars.c_str();
298 ctx.strm.rdbuf()->append(lit + pos, len);
302 template< typename FormatterT, typename CharT >
303 class decomposed_time_formatter_builder :
304 public date_time_format_parser_callback< CharT >
307 typedef date_time_format_parser_callback< CharT > base_type;
308 typedef typename base_type::char_type char_type;
309 typedef FormatterT formatter_type;
310 typedef typename formatter_type::value_type value_type;
311 typedef typename formatter_type::stream_type stream_type;
312 typedef typename stream_type::string_type string_type;
315 formatter_type& m_formatter;
318 explicit decomposed_time_formatter_builder(formatter_type& fmt) : m_formatter(fmt)
322 void on_literal(iterator_range< const char_type* > const& lit)
324 m_formatter.add_literal(lit);
329 m_formatter.add_formatter(&formatter_type::format_short_year);
334 m_formatter.add_formatter(&formatter_type::format_full_year);
337 void on_numeric_month()
339 m_formatter.add_formatter(&formatter_type::format_numeric_month);
342 void on_short_month()
344 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'b' >);
349 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'B' >);
352 void on_month_day(bool leading_zero)
355 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_month_day< '0' >);
357 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_month_day< ' ' >);
360 void on_numeric_week_day()
362 m_formatter.add_formatter(&formatter_type::format_week_day);
365 void on_short_week_day()
367 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'a' >);
370 void on_full_week_day()
372 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'A' >);
375 void on_hours(bool leading_zero)
378 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours< '0' >);
380 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours< ' ' >);
383 void on_hours_12(bool leading_zero)
386 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours_12< '0' >);
388 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours_12< ' ' >);
393 m_formatter.add_formatter(&formatter_type::format_minutes);
398 m_formatter.add_formatter(&formatter_type::format_seconds);
401 void on_fractional_seconds()
403 m_formatter.add_formatter(&formatter_type::format_fractional_seconds);
406 void on_am_pm(bool upper_case)
409 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_am_pm< true >);
411 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_am_pm< false >);
414 void on_duration_sign(bool display_positive)
416 if (display_positive)
417 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_sign< true >);
419 m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_sign< false >);
422 void on_iso_time_zone()
426 void on_extended_iso_time_zone()
433 BOOST_LOG_CLOSE_NAMESPACE // namespace log
437 #include <boost/log/detail/footer.hpp>
439 #endif // BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_