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 support/date_time.hpp
9 * \author Andrey Semashev
12 * This header enables Boost.DateTime support for Boost.Log.
15 #ifndef BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_
16 #define BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_
23 #include <boost/cstdint.hpp>
24 #include <boost/move/core.hpp>
25 #include <boost/move/utility_core.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>
39 #ifdef BOOST_HAS_PRAGMA_ONCE
45 BOOST_LOG_OPEN_NAMESPACE
47 namespace expressions {
51 namespace date_time_support {
53 template< typename DateT, typename ValueT >
54 inline void decompose_date(DateT const& d, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
56 typedef typename DateT::ymd_type ymd_type;
57 ymd_type ymd = d.year_month_day();
63 template< typename TimeDurationT, typename ValueT >
64 inline void decompose_time_of_day(TimeDurationT const& tod, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
66 v.hours = tod.hours();
67 v.minutes = tod.minutes();
68 v.seconds = tod.seconds();
70 typedef typename TimeDurationT::traits_type traits_type;
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)
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);
81 template< typename TimeDurationT, typename ValueT >
82 inline void decompose_time_duration(TimeDurationT const& dur, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
84 if (dur.is_negative())
87 (decompose_time_of_day)(-dur, v);
90 (decompose_time_of_day)(dur, v);
93 template< typename DateDurationT, typename ValueT >
94 inline void decompose_date_duration(DateDurationT const& dur, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
96 if (dur.is_negative())
99 v.day = (-dur).days();
105 template< typename TimeT, typename ValueT >
106 inline void decompose_time(TimeT const& t, boost::log::aux::decomposed_time_wrapper< ValueT >& v)
108 (decompose_date)(t.date(), v);
109 (decompose_time_of_day)(t.time_of_day(), v);
112 } // namespace date_time_support
114 template< typename TimeT, typename CharT >
115 struct date_time_formatter_generator_traits_impl
118 typedef CharT char_type;
120 typedef std::basic_string< char_type > string_type;
121 //! Formatting stream type
122 typedef basic_formatting_ostream< char_type > stream_type;
124 typedef TimeT value_type;
126 //! Formatter function
127 typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
129 //! Formatter implementation
131 public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
133 BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
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;
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;
145 BOOST_DEFAULTED_FUNCTION(formatter(), {})
146 formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
147 formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
149 formatter& operator= (formatter that)
155 result_type operator() (stream_type& strm, value_type const& value) const
157 if (value.is_not_a_date_time())
158 strm << "not-a-date-time";
159 else if (value.is_pos_infinity())
161 else if (value.is_neg_infinity())
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);
172 //! The function parses format string and constructs formatter function
173 static formatter_function_type parse(string_type const& format)
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));
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 >
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 >
192 typedef CharT char_type;
194 typedef std::basic_string< char_type > string_type;
195 //! Formatting stream type
196 typedef basic_formatting_ostream< char_type > stream_type;
198 typedef local_time::local_date_time_base< TimeT, TimeZoneT > value_type;
200 //! Formatter function
201 typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
203 //! Formatter implementation
205 public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
207 BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
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;
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;
220 BOOST_DEFAULTED_FUNCTION(formatter(), {})
221 formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
222 formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
224 formatter& operator= (formatter that)
230 result_type operator() (stream_type& strm, value_type const& value) const
232 if (value.is_not_a_date_time())
233 strm << "not-a-date-time";
234 else if (value.is_pos_infinity())
236 else if (value.is_neg_infinity())
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);
247 static void format_iso_time_zone(context& ctx)
249 ctx.strm << ctx.value.m_time.zone_abbrev(true);
253 static void format_extended_iso_time_zone(context& ctx)
255 ctx.strm << ctx.value.m_time.zone_name(true);
260 class formatter_builder :
261 public boost::log::aux::decomposed_time_formatter_builder< formatter, char_type >
264 typedef boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > base_type;
267 explicit formatter_builder(formatter& fmt) : base_type(fmt)
271 void on_iso_time_zone()
273 this->m_formatter.add_formatter(&formatter::format_iso_time_zone);
276 void on_extended_iso_time_zone()
278 this->m_formatter.add_formatter(&formatter::format_extended_iso_time_zone);
282 //! The function parses format string and constructs formatter function
283 static formatter_function_type parse(string_type const& format)
286 formatter_builder builder(fmt);
287 boost::log::aux::parse_date_time_format(format, builder);
288 return formatter_function_type(boost::move(fmt));
292 template< typename DateT, typename CharT >
293 struct date_formatter_generator_traits_impl
296 typedef CharT char_type;
298 typedef std::basic_string< char_type > string_type;
299 //! Formatting stream type
300 typedef basic_formatting_ostream< char_type > stream_type;
302 typedef DateT value_type;
304 //! Formatter function
305 typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
307 //! Formatter implementation
309 public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
311 BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
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;
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;
323 BOOST_DEFAULTED_FUNCTION(formatter(), {})
324 formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
325 formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
327 formatter& operator= (formatter that)
333 result_type operator() (stream_type& strm, value_type const& value) const
335 if (value.is_not_a_date())
336 strm << "not-a-date-time";
337 else if (value.is_pos_infinity())
339 else if (value.is_neg_infinity())
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);
350 //! The function parses format string and constructs formatter function
351 static formatter_function_type parse(string_type const& format)
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));
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 >
366 template< typename TimeDurationT, typename CharT >
367 struct time_duration_formatter_generator_traits_impl
370 typedef CharT char_type;
372 typedef std::basic_string< char_type > string_type;
373 //! Formatting stream type
374 typedef basic_formatting_ostream< char_type > stream_type;
376 typedef TimeDurationT value_type;
378 //! Formatter function
379 typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
381 //! Formatter implementation
383 public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
385 BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
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;
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;
397 BOOST_DEFAULTED_FUNCTION(formatter(), {})
398 formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
399 formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
401 formatter& operator= (formatter that)
407 result_type operator() (stream_type& strm, value_type const& value) const
409 if (value.is_not_a_date_time())
410 strm << "not-a-date-time";
411 else if (value.is_pos_infinity())
413 else if (value.is_neg_infinity())
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);
424 //! The function parses format string and constructs formatter function
425 static formatter_function_type parse(string_type const& format)
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));
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 >
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 >
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 >
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 >
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 >
464 template< typename DateDurationT, typename CharT >
465 struct date_duration_formatter_generator_traits_impl
468 typedef CharT char_type;
470 typedef std::basic_string< char_type > string_type;
471 //! Formatting stream type
472 typedef basic_formatting_ostream< char_type > stream_type;
474 typedef DateDurationT value_type;
476 //! Formatter function
477 typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
479 //! Formatter implementation
481 public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type >
483 BOOST_COPYABLE_AND_MOVABLE_ALT(formatter)
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;
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;
495 BOOST_DEFAULTED_FUNCTION(formatter(), {})
496 formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {}
497 formatter(BOOST_RV_REF(formatter) that) { this->swap(that); }
499 formatter& operator= (formatter that)
505 result_type operator() (stream_type& strm, value_type const& value) const
507 if (value.is_not_a_date())
508 strm << "not-a-date-time";
509 else if (value.is_pos_infinity())
511 else if (value.is_neg_infinity())
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);
522 //! The function parses format string and constructs formatter function
523 static formatter_function_type parse(string_type const& format)
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));
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 >
540 } // namespace expressions
542 BOOST_LOG_CLOSE_NAMESPACE // namespace log
546 #include <boost/log/detail/footer.hpp>
548 #endif // BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_