• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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)
6  */
7 /*!
8  * \file   decomposed_time.hpp
9  * \author Andrey Semashev
10  * \date   07.11.2012
11  *
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.
14  */
15 
16 #ifndef BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_
18 
19 #include <ctime>
20 #include <string>
21 #include <vector>
22 #include <locale>
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>
31 
32 #ifdef BOOST_HAS_PRAGMA_ONCE
33 #pragma once
34 #endif
35 
36 namespace boost {
37 
38 BOOST_LOG_OPEN_NAMESPACE
39 
40 namespace aux {
41 
42 //! Date and time suitable for formatting
43 struct decomposed_time
44 {
45     // Subseconds are microseconds
46     enum _
47     {
48         subseconds_per_second = 1000000,
49         subseconds_digits10 = 6
50     };
51 
52     uint32_t year, month, day, hours, minutes, seconds, subseconds;
53     bool negative;
54 
decomposed_timeboost::aux::decomposed_time55     decomposed_time() : year(0), month(1), day(1), hours(0), minutes(0), seconds(0), subseconds(0), negative(false)
56     {
57     }
58 
decomposed_timeboost::aux::decomposed_time59     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)
61     {
62     }
63 
week_dayboost::aux::decomposed_time64     unsigned int week_day() const
65     {
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;
70     }
71 
year_dayboost::aux::decomposed_time72     unsigned int year_day() const
73     {
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);
77     }
78 };
79 
to_tm(decomposed_time const & t)80 inline std::tm to_tm(decomposed_time const& t)
81 {
82     std::tm res = {};
83     res.tm_year = static_cast< int >(t.year) - 1900;
84     res.tm_mon = t.month - 1;
85     res.tm_mday = t.day;
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();
91     res.tm_isdst = -1;
92 
93     return res;
94 }
95 
96 template< typename T >
97 struct decomposed_time_wrapper :
98     public boost::log::aux::decomposed_time
99 {
100     typedef boost::log::aux::decomposed_time base_type;
101     typedef T value_type;
102     value_type m_time;
103 
decomposed_time_wrapperboost::aux::decomposed_time_wrapper104     BOOST_DEFAULTED_FUNCTION(decomposed_time_wrapper(), {})
105 
106     explicit decomposed_time_wrapper(value_type const& time) : m_time(time)
107     {
108     }
109 };
110 
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);
113 
114 template< typename T, typename CharT >
115 class date_time_formatter
116 {
117     BOOST_COPYABLE_AND_MOVABLE_ALT(date_time_formatter)
118 
119 protected:
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_;
122 
123 public:
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;
129 
130     struct context
131     {
132         date_time_formatter const& self;
133         stream_type& strm;
134         value_type const& value;
135         unsigned int literal_index, literal_pos;
136 
contextboost::aux::date_time_formatter::context137         context(date_time_formatter const& self_, stream_type& strm_, value_type const& value_) :
138             self(self_),
139             strm(strm_),
140             value(value_),
141             literal_index(0),
142             literal_pos(0)
143         {
144         }
145 
146         BOOST_DELETED_FUNCTION(context(context const&))
147         BOOST_DELETED_FUNCTION(context& operator=(context const&))
148     };
149 
150 private:
151     typedef void (*formatter_type)(context&);
152     typedef std::vector< formatter_type > formatters;
153     typedef std::vector< unsigned int > literal_lens;
154 
155 protected:
156     formatters m_formatters;
157     literal_lens m_literal_lens;
158     string_type m_literal_chars;
159 
160 public:
date_time_formatter()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)
166     {
167     }
date_time_formatter(BOOST_RV_REF (date_time_formatter)that)168     date_time_formatter(BOOST_RV_REF(date_time_formatter) that) BOOST_NOEXCEPT
169     {
170         this->swap(static_cast< date_time_formatter& >(that));
171     }
172 
operator =(date_time_formatter that)173     date_time_formatter& operator= (date_time_formatter that) BOOST_NOEXCEPT
174     {
175         this->swap(that);
176         return *this;
177     }
178 
operator ()(stream_type & strm,value_type const & value) const179     result_type operator() (stream_type& strm, value_type const& value) const
180     {
181         // Some formatters will put characters directly to the underlying string, so we have to flush stream buffers before formatting
182         strm.flush();
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)
185         {
186             (*it)(ctx);
187         }
188     }
189 
add_formatter(formatter_type fun)190     void add_formatter(formatter_type fun)
191     {
192         m_formatters.push_back(fun);
193     }
194 
add_literal(iterator_range<const char_type * > const & lit)195     void add_literal(iterator_range< const char_type* > const& lit)
196     {
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);
200     }
201 
swap(date_time_formatter & that)202     void swap(date_time_formatter& that) BOOST_NOEXCEPT
203     {
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);
207     }
208 
209 public:
210     template< char FormatCharV >
format_through_locale(context & ctx)211     static void format_through_locale(context& ctx)
212     {
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);
217         ctx.strm.flush();
218     }
219 
format_full_year(context & ctx)220     static void format_full_year(context& ctx)
221     {
222         (put_integer)(*ctx.strm.rdbuf(), ctx.value.year, 4, static_cast< char_type >('0'));
223     }
224 
format_short_year(context & ctx)225     static void format_short_year(context& ctx)
226     {
227         (put_integer)(*ctx.strm.rdbuf(), ctx.value.year % 100u, 2, static_cast< char_type >('0'));
228     }
229 
format_numeric_month(context & ctx)230     static void format_numeric_month(context& ctx)
231     {
232         (put_integer)(*ctx.strm.rdbuf(), ctx.value.month, 2, static_cast< char_type >('0'));
233     }
234 
235     template< char_type FillCharV >
format_month_day(context & ctx)236     static void format_month_day(context& ctx)
237     {
238         (put_integer)(*ctx.strm.rdbuf(), ctx.value.day, 2, static_cast< char_type >(FillCharV));
239     }
240 
format_week_day(context & ctx)241     static void format_week_day(context& ctx)
242     {
243         (put_integer)(*ctx.strm.rdbuf(), static_cast< decomposed_time const& >(ctx.value).week_day(), 1, static_cast< char_type >('0'));
244     }
245 
246     template< char_type FillCharV >
format_hours(context & ctx)247     static void format_hours(context& ctx)
248     {
249         (put_integer)(*ctx.strm.rdbuf(), ctx.value.hours, 2, static_cast< char_type >(FillCharV));
250     }
251 
252     template< char_type FillCharV >
format_hours_12(context & ctx)253     static void format_hours_12(context& ctx)
254     {
255         (put_integer)(*ctx.strm.rdbuf(), ctx.value.hours % 12u + 1u, 2, static_cast< char_type >(FillCharV));
256     }
257 
format_minutes(context & ctx)258     static void format_minutes(context& ctx)
259     {
260         (put_integer)(*ctx.strm.rdbuf(), ctx.value.minutes, 2, static_cast< char_type >('0'));
261     }
262 
format_seconds(context & ctx)263     static void format_seconds(context& ctx)
264     {
265         (put_integer)(*ctx.strm.rdbuf(), ctx.value.seconds, 2, static_cast< char_type >('0'));
266     }
267 
format_fractional_seconds(context & ctx)268     static void format_fractional_seconds(context& ctx)
269     {
270         (put_integer)(*ctx.strm.rdbuf(), ctx.value.subseconds, decomposed_time::subseconds_digits10, static_cast< char_type >('0'));
271     }
272 
273     template< bool UpperCaseV >
format_am_pm(context & ctx)274     static void format_am_pm(context& ctx)
275     {
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) };
278 
279         ctx.strm.rdbuf()->append(((static_cast< decomposed_time const& >(ctx.value).hours > 11) ? pm : am), 2u);
280     }
281 
282     template< bool DisplayPositiveV >
format_sign(context & ctx)283     static void format_sign(context& ctx)
284     {
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('+');
289     }
290 
291 private:
format_literal(context & ctx)292     static void format_literal(context& ctx)
293     {
294         unsigned int len = ctx.self.m_literal_lens[ctx.literal_index], pos = ctx.literal_pos;
295         ++ctx.literal_index;
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);
299     }
300 };
301 
302 template< typename FormatterT, typename CharT >
303 class decomposed_time_formatter_builder :
304     public date_time_format_parser_callback< CharT >
305 {
306 public:
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;
313 
314 protected:
315     formatter_type& m_formatter;
316 
317 public:
decomposed_time_formatter_builder(formatter_type & fmt)318     explicit decomposed_time_formatter_builder(formatter_type& fmt) : m_formatter(fmt)
319     {
320     }
321 
on_literal(iterator_range<const char_type * > const & lit)322     void on_literal(iterator_range< const char_type* > const& lit)
323     {
324         m_formatter.add_literal(lit);
325     }
326 
on_short_year()327     void on_short_year()
328     {
329         m_formatter.add_formatter(&formatter_type::format_short_year);
330     }
331 
on_full_year()332     void on_full_year()
333     {
334         m_formatter.add_formatter(&formatter_type::format_full_year);
335     }
336 
on_numeric_month()337     void on_numeric_month()
338     {
339         m_formatter.add_formatter(&formatter_type::format_numeric_month);
340     }
341 
on_short_month()342     void on_short_month()
343     {
344         m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'b' >);
345     }
346 
on_full_month()347     void on_full_month()
348     {
349         m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'B' >);
350     }
351 
on_month_day(bool leading_zero)352     void on_month_day(bool leading_zero)
353     {
354         if (leading_zero)
355             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_month_day< '0' >);
356         else
357             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_month_day< ' ' >);
358     }
359 
on_numeric_week_day()360     void on_numeric_week_day()
361     {
362         m_formatter.add_formatter(&formatter_type::format_week_day);
363     }
364 
on_short_week_day()365     void on_short_week_day()
366     {
367         m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'a' >);
368     }
369 
on_full_week_day()370     void on_full_week_day()
371     {
372         m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'A' >);
373     }
374 
on_hours(bool leading_zero)375     void on_hours(bool leading_zero)
376     {
377         if (leading_zero)
378             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours< '0' >);
379         else
380             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours< ' ' >);
381     }
382 
on_hours_12(bool leading_zero)383     void on_hours_12(bool leading_zero)
384     {
385         if (leading_zero)
386             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours_12< '0' >);
387         else
388             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours_12< ' ' >);
389     }
390 
on_minutes()391     void on_minutes()
392     {
393         m_formatter.add_formatter(&formatter_type::format_minutes);
394     }
395 
on_seconds()396     void on_seconds()
397     {
398         m_formatter.add_formatter(&formatter_type::format_seconds);
399     }
400 
on_fractional_seconds()401     void on_fractional_seconds()
402     {
403         m_formatter.add_formatter(&formatter_type::format_fractional_seconds);
404     }
405 
on_am_pm(bool upper_case)406     void on_am_pm(bool upper_case)
407     {
408         if (upper_case)
409             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_am_pm< true >);
410         else
411             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_am_pm< false >);
412     }
413 
on_duration_sign(bool display_positive)414     void on_duration_sign(bool display_positive)
415     {
416         if (display_positive)
417             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_sign< true >);
418         else
419             m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_sign< false >);
420     }
421 
on_iso_time_zone()422     void on_iso_time_zone()
423     {
424     }
425 
on_extended_iso_time_zone()426     void on_extended_iso_time_zone()
427     {
428     }
429 };
430 
431 } // namespace aux
432 
433 BOOST_LOG_CLOSE_NAMESPACE // namespace log
434 
435 } // namespace boost
436 
437 #include <boost/log/detail/footer.hpp>
438 
439 #endif // BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_
440