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   support/date_time.hpp
9  * \author Andrey Semashev
10  * \date   07.11.2012
11  *
12  * This header enables Boost.DateTime support for Boost.Log.
13  */
14 
15 #ifndef BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_
16 #define BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_
17 
18 #include <ctime>
19 #include <string>
20 #include <locale>
21 #include <ostream>
22 #include <iterator>
23 #include <boost/cstdint.hpp>
24 #include <boost/move/core.hpp>
25 #include <boost/move/utility.hpp>
26 #include <boost/date_time/time.hpp>
27 #include <boost/date_time/date.hpp>
28 #include <boost/date_time/gregorian/gregorian_types.hpp>
29 #include <boost/date_time/local_time/local_time_types.hpp>
30 #include <boost/date_time/posix_time/posix_time_types.hpp>
31 #include <boost/log/detail/config.hpp>
32 #include <boost/log/detail/date_time_format_parser.hpp>
33 #include <boost/log/detail/light_function.hpp>
34 #include <boost/log/detail/decomposed_time.hpp>
35 #include <boost/log/detail/date_time_fmt_gen_traits_fwd.hpp>
36 #include <boost/log/utility/formatting_ostream.hpp>
37 #include <boost/log/detail/header.hpp>
38 
39 #ifdef BOOST_HAS_PRAGMA_ONCE
40 #pragma once
41 #endif
42 
43 namespace boost {
44 
45 BOOST_LOG_OPEN_NAMESPACE
46 
47 namespace expressions {
48 
49 namespace aux {
50 
51 namespace date_time_support {
52 
53 template< typename DateT, typename ValueT >
decompose_date(DateT const & d,boost::log::aux::decomposed_time_wrapper<ValueT> & v)54 inline void decompose_date(DateT const& d, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
55 {
56     typedef typename DateT::ymd_type ymd_type;
57     ymd_type ymd = d.year_month_day();
58     v.year = ymd.year;
59     v.month = ymd.month;
60     v.day = ymd.day;
61 }
62 
63 template< typename TimeDurationT, typename ValueT >
decompose_time_of_day(TimeDurationT const & tod,boost::log::aux::decomposed_time_wrapper<ValueT> & v)64 inline void decompose_time_of_day(TimeDurationT const& tod, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
65 {
66     v.hours = tod.hours();
67     v.minutes = tod.minutes();
68     v.seconds = tod.seconds();
69 
70     typedef typename TimeDurationT::traits_type traits_type;
71     enum
72     {
73         adjustment_ratio = (traits_type::ticks_per_second > boost::log::aux::decomposed_time::subseconds_per_second ?
74             traits_type::ticks_per_second / boost::log::aux::decomposed_time::subseconds_per_second :
75             boost::log::aux::decomposed_time::subseconds_per_second / traits_type::ticks_per_second)
76     };
77     uint64_t frac = tod.fractional_seconds();
78     v.subseconds = static_cast< uint32_t >(traits_type::ticks_per_second > boost::log::aux::decomposed_time::subseconds_per_second ? frac / adjustment_ratio : frac * adjustment_ratio);
79 }
80 
81 template< typename TimeDurationT, typename ValueT >
decompose_time_duration(TimeDurationT const & dur,boost::log::aux::decomposed_time_wrapper<ValueT> & v)82 inline void decompose_time_duration(TimeDurationT const& dur, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
83 {
84     if (dur.is_negative())
85     {
86         v.negative = true;
87         (decompose_time_of_day)(-dur, v);
88     }
89     else
90         (decompose_time_of_day)(dur, v);
91 }
92 
93 template< typename DateDurationT, typename ValueT >
decompose_date_duration(DateDurationT const & dur,boost::log::aux::decomposed_time_wrapper<ValueT> & v)94 inline void decompose_date_duration(DateDurationT const& dur, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
95 {
96     if (dur.is_negative())
97     {
98         v.negative = true;
99         v.day = (-dur).days();
100     }
101     else
102         v.day = dur.days();
103 }
104 
105 template< typename TimeT, typename ValueT >
decompose_time(TimeT const & t,boost::log::aux::decomposed_time_wrapper<ValueT> & v)106 inline void decompose_time(TimeT const& t, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
107 {
108     (decompose_date)(t.date(), v);
109     (decompose_time_of_day)(t.time_of_day(), v);
110 }
111 
112 } // namespace date_time_support
113 
114 template< typename TimeT, typename CharT >
115 struct date_time_formatter_generator_traits_impl
116 {
117     //! Character type
118     typedef CharT char_type;
119     //! String type
120     typedef std::basic_string< char_type > string_type;
121     //! Formatting stream type
122     typedef basic_formatting_ostream< char_type > stream_type;
123     //! Value type
124     typedef TimeT value_type;
125 
126     //! Formatter function
127     typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
128 
129     //! Formatter implementation
130     class formatter :
131         public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
132     {
133         BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
134 
135     private:
136         // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type.
137         typedef typename formatter::date_time_formatter_ base_type;
138 
139     public:
140         typedef typename base_type::result_type result_type;
141         // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead.
142         typedef typename date_time_formatter_generator_traits_impl< TimeT, CharT >::value_type value_type;
143 
144     public:
formatter()145         BOOST_DEFAULTED_FUNCTION(formatter(), {})
146         formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
formatter(BOOST_RV_REF (formatter)that)147         formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
148 
operator =(formatter that)149         formatter& operator= (formatter that)
150         {
151             this->swap(that);
152             return *this;
153         }
154 
operator ()(stream_type & strm,value_type const & value) const155         result_type operator() (stream_type& strm, value_type const& value) const
156         {
157             if (value.is_not_a_date_time())
158                 strm << "not-a-date-time";
159             else if (value.is_pos_infinity())
160                 strm << "+infinity";
161             else if (value.is_neg_infinity())
162                 strm << "-infinity";
163             else
164             {
165                 boost::log::aux::decomposed_time_wrapper< value_type > val(value);
166                 date_time_support::decompose_time(value, val);
167                 base_type::operator() (strm, val);
168             }
169         }
170     };
171 
172     //! The function parses format string and constructs formatter function
parseboost::expressions::aux::date_time_formatter_generator_traits_impl173     static formatter_function_type parse(string_type const& format)
174     {
175         formatter fmt;
176         boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > builder(fmt);
177         boost::log::aux::parse_date_time_format(format, builder);
178         return formatter_function_type(boost::move(fmt));
179     }
180 };
181 
182 template< typename CharT, typename VoidT >
183 struct date_time_formatter_generator_traits< posix_time::ptime, CharT, VoidT > :
184     public date_time_formatter_generator_traits_impl< posix_time::ptime, CharT >
185 {
186 };
187 
188 template< typename TimeT, typename TimeZoneT, typename CharT, typename VoidT >
189 struct date_time_formatter_generator_traits< local_time::local_date_time_base< TimeT, TimeZoneT >, CharT, VoidT >
190 {
191     //! Character type
192     typedef CharT char_type;
193     //! String type
194     typedef std::basic_string< char_type > string_type;
195     //! Formatting stream type
196     typedef basic_formatting_ostream< char_type > stream_type;
197     //! Value type
198     typedef local_time::local_date_time_base< TimeT, TimeZoneT > value_type;
199 
200     //! Formatter function
201     typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
202 
203     //! Formatter implementation
204     class formatter :
205         public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
206     {
207         BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
208 
209     private:
210         // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type.
211         typedef typename formatter::date_time_formatter_ base_type;
212 
213     public:
214         typedef typename base_type::result_type result_type;
215         typedef typename base_type::context context;
216         // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead.
217         typedef typename date_time_formatter_generator_traits< local_time::local_date_time_base< TimeT, TimeZoneT >, CharT, VoidT >::value_type value_type;
218 
219     public:
formatter()220         BOOST_DEFAULTED_FUNCTION(formatter(), {})
221         formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
formatter(BOOST_RV_REF (formatter)that)222         formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
223 
operator =(formatter that)224         formatter& operator= (formatter that)
225         {
226             this->swap(that);
227             return *this;
228         }
229 
operator ()(stream_type & strm,value_type const & value) const230         result_type operator() (stream_type& strm, value_type const& value) const
231         {
232             if (value.is_not_a_date_time())
233                 strm << "not-a-date-time";
234             else if (value.is_pos_infinity())
235                 strm << "+infinity";
236             else if (value.is_neg_infinity())
237                 strm << "-infinity";
238             else
239             {
240                 boost::log::aux::decomposed_time_wrapper< value_type > val(value);
241                 date_time_support::decompose_time(value.local_time(), val);
242                 base_type::operator() (strm, val);
243             }
244         }
245 
246     public:
format_iso_time_zone(context & ctx)247         static void format_iso_time_zone(context& ctx)
248         {
249             ctx.strm << ctx.value.m_time.zone_abbrev(true);
250             ctx.strm.flush();
251         }
252 
format_extended_iso_time_zone(context & ctx)253         static void format_extended_iso_time_zone(context& ctx)
254         {
255             ctx.strm << ctx.value.m_time.zone_name(true);
256             ctx.strm.flush();
257         }
258     };
259 
260     class formatter_builder :
261         public boost::log::aux::decomposed_time_formatter_builder< formatter, char_type >
262     {
263     private:
264         typedef boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > base_type;
265 
266     public:
formatter_builder(formatter & fmt)267         explicit formatter_builder(formatter& fmt) : base_type(fmt)
268         {
269         }
270 
on_iso_time_zone()271         void on_iso_time_zone()
272         {
273             this->m_formatter.add_formatter(&formatter::format_iso_time_zone);
274         }
275 
on_extended_iso_time_zone()276         void on_extended_iso_time_zone()
277         {
278             this->m_formatter.add_formatter(&formatter::format_extended_iso_time_zone);
279         }
280     };
281 
282     //! The function parses format string and constructs formatter function
parseboost::expressions::aux::date_time_formatter_generator_traits283     static formatter_function_type parse(string_type const& format)
284     {
285         formatter fmt;
286         formatter_builder builder(fmt);
287         boost::log::aux::parse_date_time_format(format, builder);
288         return formatter_function_type(boost::move(fmt));
289     }
290 };
291 
292 template< typename DateT, typename CharT >
293 struct date_formatter_generator_traits_impl
294 {
295     //! Character type
296     typedef CharT char_type;
297     //! String type
298     typedef std::basic_string< char_type > string_type;
299     //! Formatting stream type
300     typedef basic_formatting_ostream< char_type > stream_type;
301     //! Value type
302     typedef DateT value_type;
303 
304     //! Formatter function
305     typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
306 
307     //! Formatter implementation
308     class formatter :
309         public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
310     {
311         BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
312 
313     private:
314         // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type.
315         typedef typename formatter::date_time_formatter_ base_type;
316 
317     public:
318         typedef typename base_type::result_type result_type;
319         // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead.
320         typedef typename date_formatter_generator_traits_impl< DateT, CharT >::value_type value_type;
321 
322     public:
formatter()323         BOOST_DEFAULTED_FUNCTION(formatter(), {})
324         formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
formatter(BOOST_RV_REF (formatter)that)325         formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
326 
operator =(formatter that)327         formatter& operator= (formatter that)
328         {
329             this->swap(that);
330             return *this;
331         }
332 
operator ()(stream_type & strm,value_type const & value) const333         result_type operator() (stream_type& strm, value_type const& value) const
334         {
335             if (value.is_not_a_date())
336                 strm << "not-a-date-time";
337             else if (value.is_pos_infinity())
338                 strm << "+infinity";
339             else if (value.is_neg_infinity())
340                 strm << "-infinity";
341             else
342             {
343                 boost::log::aux::decomposed_time_wrapper< value_type > val(value);
344                 date_time_support::decompose_date(value, val);
345                 base_type::operator() (strm, val);
346             }
347         }
348     };
349 
350     //! The function parses format string and constructs formatter function
parseboost::expressions::aux::date_formatter_generator_traits_impl351     static formatter_function_type parse(string_type const& format)
352     {
353         formatter fmt;
354         boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > builder(fmt);
355         boost::log::aux::parse_date_format(format, builder);
356         return formatter_function_type(boost::move(fmt));
357     }
358 };
359 
360 template< typename CharT, typename VoidT >
361 struct date_time_formatter_generator_traits< gregorian::date, CharT, VoidT > :
362     public date_formatter_generator_traits_impl< gregorian::date, CharT >
363 {
364 };
365 
366 template< typename TimeDurationT, typename CharT >
367 struct time_duration_formatter_generator_traits_impl
368 {
369     //! Character type
370     typedef CharT char_type;
371     //! String type
372     typedef std::basic_string< char_type > string_type;
373     //! Formatting stream type
374     typedef basic_formatting_ostream< char_type > stream_type;
375     //! Value type
376     typedef TimeDurationT value_type;
377 
378     //! Formatter function
379     typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
380 
381     //! Formatter implementation
382     class formatter :
383         public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
384     {
385         BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
386 
387     private:
388         // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type.
389         typedef typename formatter::date_time_formatter_ base_type;
390 
391     public:
392         typedef typename base_type::result_type result_type;
393         // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead.
394         typedef typename time_duration_formatter_generator_traits_impl< TimeDurationT, CharT >::value_type value_type;
395 
396     public:
formatter()397         BOOST_DEFAULTED_FUNCTION(formatter(), {})
398         formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
formatter(BOOST_RV_REF (formatter)that)399         formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
400 
operator =(formatter that)401         formatter& operator= (formatter that)
402         {
403             this->swap(that);
404             return *this;
405         }
406 
operator ()(stream_type & strm,value_type const & value) const407         result_type operator() (stream_type& strm, value_type const& value) const
408         {
409             if (value.is_not_a_date_time())
410                 strm << "not-a-date-time";
411             else if (value.is_pos_infinity())
412                 strm << "+infinity";
413             else if (value.is_neg_infinity())
414                 strm << "-infinity";
415             else
416             {
417                 boost::log::aux::decomposed_time_wrapper< value_type > val(value);
418                 date_time_support::decompose_time_duration(value, val);
419                 base_type::operator() (strm, val);
420             }
421         }
422     };
423 
424     //! The function parses format string and constructs formatter function
parseboost::expressions::aux::time_duration_formatter_generator_traits_impl425     static formatter_function_type parse(string_type const& format)
426     {
427         formatter fmt;
428         boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > builder(fmt);
429         boost::log::aux::parse_time_format(format, builder);
430         return formatter_function_type(boost::move(fmt));
431     }
432 };
433 
434 template< typename CharT, typename VoidT >
435 struct date_time_formatter_generator_traits< posix_time::time_duration, CharT, VoidT > :
436     public time_duration_formatter_generator_traits_impl< posix_time::time_duration, CharT >
437 {
438 };
439 
440 template< typename CharT, typename VoidT >
441 struct date_time_formatter_generator_traits< posix_time::hours, CharT, VoidT > :
442     public time_duration_formatter_generator_traits_impl< posix_time::hours, CharT >
443 {
444 };
445 
446 template< typename CharT, typename VoidT >
447 struct date_time_formatter_generator_traits< posix_time::minutes, CharT, VoidT > :
448     public time_duration_formatter_generator_traits_impl< posix_time::minutes, CharT >
449 {
450 };
451 
452 template< typename CharT, typename VoidT >
453 struct date_time_formatter_generator_traits< posix_time::seconds, CharT, VoidT > :
454     public time_duration_formatter_generator_traits_impl< posix_time::seconds, CharT >
455 {
456 };
457 
458 template< typename BaseDurationT, uint64_t FracOfSecondV, typename CharT, typename VoidT >
459 struct date_time_formatter_generator_traits< date_time::subsecond_duration< BaseDurationT, FracOfSecondV >, CharT, VoidT > :
460     public time_duration_formatter_generator_traits_impl< date_time::subsecond_duration< BaseDurationT, FracOfSecondV >, CharT >
461 {
462 };
463 
464 template< typename DateDurationT, typename CharT >
465 struct date_duration_formatter_generator_traits_impl
466 {
467     //! Character type
468     typedef CharT char_type;
469     //! String type
470     typedef std::basic_string< char_type > string_type;
471     //! Formatting stream type
472     typedef basic_formatting_ostream< char_type > stream_type;
473     //! Value type
474     typedef DateDurationT value_type;
475 
476     //! Formatter function
477     typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
478 
479     //! Formatter implementation
480     class formatter :
481         public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
482     {
483         BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
484 
485     private:
486         // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type.
487         typedef typename formatter::date_time_formatter_ base_type;
488 
489     public:
490         typedef typename base_type::result_type result_type;
491         // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead.
492         typedef typename date_duration_formatter_generator_traits_impl< DateDurationT, CharT >::value_type value_type;
493 
494     public:
formatter()495         BOOST_DEFAULTED_FUNCTION(formatter(), {})
496         formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
formatter(BOOST_RV_REF (formatter)that)497         formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
498 
operator =(formatter that)499         formatter& operator= (formatter that)
500         {
501             this->swap(that);
502             return *this;
503         }
504 
operator ()(stream_type & strm,value_type const & value) const505         result_type operator() (stream_type& strm, value_type const& value) const
506         {
507             if (value.is_not_a_date())
508                 strm << "not-a-date-time";
509             else if (value.is_pos_infinity())
510                 strm << "+infinity";
511             else if (value.is_neg_infinity())
512                 strm << "-infinity";
513             else
514             {
515                 boost::log::aux::decomposed_time_wrapper< value_type > val(value);
516                 date_time_support::decompose_date_duration(value, val);
517                 base_type::operator() (strm, val);
518             }
519         }
520     };
521 
522     //! The function parses format string and constructs formatter function
parseboost::expressions::aux::date_duration_formatter_generator_traits_impl523     static formatter_function_type parse(string_type const& format)
524     {
525         formatter fmt;
526         boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > builder(fmt);
527         boost::log::aux::parse_date_format(format, builder);
528         return formatter_function_type(boost::move(fmt));
529     }
530 };
531 
532 template< typename CharT, typename VoidT >
533 struct date_time_formatter_generator_traits< gregorian::date_duration, CharT, VoidT > :
534     public date_formatter_generator_traits_impl< gregorian::date_duration, CharT >
535 {
536 };
537 
538 } // namespace aux
539 
540 } // namespace expressions
541 
542 BOOST_LOG_CLOSE_NAMESPACE // namespace log
543 
544 } // namespace boost
545 
546 #include <boost/log/detail/footer.hpp>
547 
548 #endif // BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_
549