Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file support/date_time.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 07.11.2012 Chris@16: * Chris@16: * This header enables Boost.DateTime support for Boost.Log. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace expressions { Chris@16: Chris@16: namespace aux { Chris@16: Chris@16: namespace date_time_support { Chris@16: Chris@16: template< typename DateT, typename ValueT > Chris@16: inline void decompose_date(DateT const& d, boost::log::aux::decomposed_time_wrapper< ValueT >& v) Chris@16: { Chris@16: typedef typename DateT::ymd_type ymd_type; Chris@16: ymd_type ymd = d.year_month_day(); Chris@16: v.year = ymd.year; Chris@16: v.month = ymd.month; Chris@16: v.day = ymd.day; Chris@16: } Chris@16: Chris@16: template< typename TimeDurationT, typename ValueT > Chris@16: inline void decompose_time_of_day(TimeDurationT const& tod, boost::log::aux::decomposed_time_wrapper< ValueT >& v) Chris@16: { Chris@16: v.hours = tod.hours(); Chris@16: v.minutes = tod.minutes(); Chris@16: v.seconds = tod.seconds(); Chris@16: Chris@16: typedef typename TimeDurationT::traits_type traits_type; Chris@16: enum Chris@16: { Chris@16: adjustment_ratio = (traits_type::ticks_per_second > boost::log::aux::decomposed_time::subseconds_per_second ? Chris@16: traits_type::ticks_per_second / boost::log::aux::decomposed_time::subseconds_per_second : Chris@16: boost::log::aux::decomposed_time::subseconds_per_second / traits_type::ticks_per_second) Chris@16: }; Chris@16: uint64_t frac = tod.fractional_seconds(); Chris@16: 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); Chris@16: } Chris@16: Chris@16: template< typename TimeDurationT, typename ValueT > Chris@16: inline void decompose_time_duration(TimeDurationT const& dur, boost::log::aux::decomposed_time_wrapper< ValueT >& v) Chris@16: { Chris@16: if (dur.is_negative()) Chris@16: { Chris@16: v.negative = true; Chris@16: (decompose_time_of_day)(-dur, v); Chris@16: } Chris@16: else Chris@16: (decompose_time_of_day)(dur, v); Chris@16: } Chris@16: Chris@16: template< typename DateDurationT, typename ValueT > Chris@16: inline void decompose_date_duration(DateDurationT const& dur, boost::log::aux::decomposed_time_wrapper< ValueT >& v) Chris@16: { Chris@16: if (dur.is_negative()) Chris@16: { Chris@16: v.negative = true; Chris@16: v.day = (-dur).days(); Chris@16: } Chris@16: else Chris@16: v.day = dur.days(); Chris@16: } Chris@16: Chris@16: template< typename TimeT, typename ValueT > Chris@16: inline void decompose_time(TimeT const& t, boost::log::aux::decomposed_time_wrapper< ValueT >& v) Chris@16: { Chris@16: (decompose_date)(t.date(), v); Chris@16: (decompose_time_of_day)(t.time_of_day(), v); Chris@16: } Chris@16: Chris@16: } // namespace date_time_support Chris@16: Chris@16: template< typename TimeT, typename CharT > Chris@16: struct date_time_formatter_generator_traits_impl Chris@16: { Chris@16: //! Character type Chris@16: typedef CharT char_type; Chris@16: //! String type Chris@16: typedef std::basic_string< char_type > string_type; Chris@16: //! Formatting stream type Chris@16: typedef basic_formatting_ostream< char_type > stream_type; Chris@16: //! Value type Chris@16: typedef TimeT value_type; Chris@16: Chris@16: //! Formatter function Chris@16: typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type; Chris@16: Chris@16: //! Formatter implementation Chris@16: class formatter : Chris@16: public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type > Chris@16: { Chris@16: BOOST_COPYABLE_AND_MOVABLE_ALT(formatter) Chris@16: Chris@16: private: Chris@16: // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type. Chris@16: typedef typename formatter::date_time_formatter_ base_type; Chris@16: Chris@16: public: Chris@16: typedef typename base_type::result_type result_type; Chris@16: // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead. Chris@16: typedef typename date_time_formatter_generator_traits_impl< TimeT, CharT >::value_type value_type; Chris@16: Chris@16: public: Chris@16: BOOST_DEFAULTED_FUNCTION(formatter(), {}) Chris@16: formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {} Chris@16: formatter(BOOST_RV_REF(formatter) that) { this->swap(that); } Chris@16: Chris@16: formatter& operator= (formatter that) Chris@16: { Chris@16: this->swap(that); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: result_type operator() (stream_type& strm, value_type const& value) const Chris@16: { Chris@16: if (value.is_not_a_date_time()) Chris@16: strm << "not-a-date-time"; Chris@16: else if (value.is_pos_infinity()) Chris@16: strm << "+infinity"; Chris@16: else if (value.is_neg_infinity()) Chris@16: strm << "-infinity"; Chris@16: else Chris@16: { Chris@16: boost::log::aux::decomposed_time_wrapper< value_type > val(value); Chris@16: date_time_support::decompose_time(value, val); Chris@16: base_type::operator() (strm, val); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: //! The function parses format string and constructs formatter function Chris@16: static formatter_function_type parse(string_type const& format) Chris@16: { Chris@16: formatter fmt; Chris@16: boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > builder(fmt); Chris@16: boost::log::aux::parse_date_time_format(format, builder); Chris@16: return formatter_function_type(boost::move(fmt)); Chris@16: } Chris@16: }; Chris@16: Chris@16: template< typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< posix_time::ptime, CharT, VoidT > : Chris@16: public date_time_formatter_generator_traits_impl< posix_time::ptime, CharT > Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename TimeT, typename TimeZoneT, typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< local_time::local_date_time_base< TimeT, TimeZoneT >, CharT, VoidT > Chris@16: { Chris@16: //! Character type Chris@16: typedef CharT char_type; Chris@16: //! String type Chris@16: typedef std::basic_string< char_type > string_type; Chris@16: //! Formatting stream type Chris@16: typedef basic_formatting_ostream< char_type > stream_type; Chris@16: //! Value type Chris@16: typedef local_time::local_date_time_base< TimeT, TimeZoneT > value_type; Chris@16: Chris@16: //! Formatter function Chris@16: typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type; Chris@16: Chris@16: //! Formatter implementation Chris@16: class formatter : Chris@16: public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type > Chris@16: { Chris@16: BOOST_COPYABLE_AND_MOVABLE_ALT(formatter) Chris@16: Chris@16: private: Chris@16: // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type. Chris@16: typedef typename formatter::date_time_formatter_ base_type; Chris@16: Chris@16: public: Chris@16: typedef typename base_type::result_type result_type; Chris@16: typedef typename base_type::context context; Chris@16: // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead. Chris@16: typedef typename date_time_formatter_generator_traits< local_time::local_date_time_base< TimeT, TimeZoneT >, CharT, VoidT >::value_type value_type; Chris@16: Chris@16: public: Chris@16: BOOST_DEFAULTED_FUNCTION(formatter(), {}) Chris@16: formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {} Chris@16: formatter(BOOST_RV_REF(formatter) that) { this->swap(that); } Chris@16: Chris@16: formatter& operator= (formatter that) Chris@16: { Chris@16: this->swap(that); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: result_type operator() (stream_type& strm, value_type const& value) const Chris@16: { Chris@16: if (value.is_not_a_date_time()) Chris@16: strm << "not-a-date-time"; Chris@16: else if (value.is_pos_infinity()) Chris@16: strm << "+infinity"; Chris@16: else if (value.is_neg_infinity()) Chris@16: strm << "-infinity"; Chris@16: else Chris@16: { Chris@16: boost::log::aux::decomposed_time_wrapper< value_type > val(value); Chris@16: date_time_support::decompose_time(value.local_time(), val); Chris@16: base_type::operator() (strm, val); Chris@16: } Chris@16: } Chris@16: Chris@16: public: Chris@16: static void format_iso_time_zone(context& ctx) Chris@16: { Chris@16: ctx.strm << ctx.value.m_time.zone_abbrev(true); Chris@16: ctx.strm.flush(); Chris@16: } Chris@16: Chris@16: static void format_extended_iso_time_zone(context& ctx) Chris@16: { Chris@16: ctx.strm << ctx.value.m_time.zone_name(true); Chris@16: ctx.strm.flush(); Chris@16: } Chris@16: }; Chris@16: Chris@16: class formatter_builder : Chris@16: public boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > Chris@16: { Chris@16: private: Chris@16: typedef boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > base_type; Chris@16: Chris@16: public: Chris@16: explicit formatter_builder(formatter& fmt) : base_type(fmt) Chris@16: { Chris@16: } Chris@16: Chris@16: void on_iso_time_zone() Chris@16: { Chris@16: this->m_formatter.add_formatter(&formatter::format_iso_time_zone); Chris@16: } Chris@16: Chris@16: void on_extended_iso_time_zone() Chris@16: { Chris@16: this->m_formatter.add_formatter(&formatter::format_extended_iso_time_zone); Chris@16: } Chris@16: }; Chris@16: Chris@16: //! The function parses format string and constructs formatter function Chris@16: static formatter_function_type parse(string_type const& format) Chris@16: { Chris@16: formatter fmt; Chris@16: formatter_builder builder(fmt); Chris@16: boost::log::aux::parse_date_time_format(format, builder); Chris@16: return formatter_function_type(boost::move(fmt)); Chris@16: } Chris@16: }; Chris@16: Chris@16: template< typename DateT, typename CharT > Chris@16: struct date_formatter_generator_traits_impl Chris@16: { Chris@16: //! Character type Chris@16: typedef CharT char_type; Chris@16: //! String type Chris@16: typedef std::basic_string< char_type > string_type; Chris@16: //! Formatting stream type Chris@16: typedef basic_formatting_ostream< char_type > stream_type; Chris@16: //! Value type Chris@16: typedef DateT value_type; Chris@16: Chris@16: //! Formatter function Chris@16: typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type; Chris@16: Chris@16: //! Formatter implementation Chris@16: class formatter : Chris@16: public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type > Chris@16: { Chris@16: BOOST_COPYABLE_AND_MOVABLE_ALT(formatter) Chris@16: Chris@16: private: Chris@16: // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type. Chris@16: typedef typename formatter::date_time_formatter_ base_type; Chris@16: Chris@16: public: Chris@16: typedef typename base_type::result_type result_type; Chris@16: // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead. Chris@16: typedef typename date_formatter_generator_traits_impl< DateT, CharT >::value_type value_type; Chris@16: Chris@16: public: Chris@16: BOOST_DEFAULTED_FUNCTION(formatter(), {}) Chris@16: formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {} Chris@16: formatter(BOOST_RV_REF(formatter) that) { this->swap(that); } Chris@16: Chris@16: formatter& operator= (formatter that) Chris@16: { Chris@16: this->swap(that); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: result_type operator() (stream_type& strm, value_type const& value) const Chris@16: { Chris@16: if (value.is_not_a_date()) Chris@16: strm << "not-a-date-time"; Chris@16: else if (value.is_pos_infinity()) Chris@16: strm << "+infinity"; Chris@16: else if (value.is_neg_infinity()) Chris@16: strm << "-infinity"; Chris@16: else Chris@16: { Chris@16: boost::log::aux::decomposed_time_wrapper< value_type > val(value); Chris@16: date_time_support::decompose_date(value, val); Chris@16: base_type::operator() (strm, val); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: //! The function parses format string and constructs formatter function Chris@16: static formatter_function_type parse(string_type const& format) Chris@16: { Chris@16: formatter fmt; Chris@16: boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > builder(fmt); Chris@16: boost::log::aux::parse_date_format(format, builder); Chris@16: return formatter_function_type(boost::move(fmt)); Chris@16: } Chris@16: }; Chris@16: Chris@16: template< typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< gregorian::date, CharT, VoidT > : Chris@16: public date_formatter_generator_traits_impl< gregorian::date, CharT > Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename TimeDurationT, typename CharT > Chris@16: struct time_duration_formatter_generator_traits_impl Chris@16: { Chris@16: //! Character type Chris@16: typedef CharT char_type; Chris@16: //! String type Chris@16: typedef std::basic_string< char_type > string_type; Chris@16: //! Formatting stream type Chris@16: typedef basic_formatting_ostream< char_type > stream_type; Chris@16: //! Value type Chris@16: typedef TimeDurationT value_type; Chris@16: Chris@16: //! Formatter function Chris@16: typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type; Chris@16: Chris@16: //! Formatter implementation Chris@16: class formatter : Chris@16: public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type > Chris@16: { Chris@16: BOOST_COPYABLE_AND_MOVABLE_ALT(formatter) Chris@16: Chris@16: private: Chris@16: // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type. Chris@16: typedef typename formatter::date_time_formatter_ base_type; Chris@16: Chris@16: public: Chris@16: typedef typename base_type::result_type result_type; Chris@16: // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead. Chris@16: typedef typename time_duration_formatter_generator_traits_impl< TimeDurationT, CharT >::value_type value_type; Chris@16: Chris@16: public: Chris@16: BOOST_DEFAULTED_FUNCTION(formatter(), {}) Chris@16: formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {} Chris@16: formatter(BOOST_RV_REF(formatter) that) { this->swap(that); } Chris@16: Chris@16: formatter& operator= (formatter that) Chris@16: { Chris@16: this->swap(that); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: result_type operator() (stream_type& strm, value_type const& value) const Chris@16: { Chris@16: if (value.is_not_a_date_time()) Chris@16: strm << "not-a-date-time"; Chris@16: else if (value.is_pos_infinity()) Chris@16: strm << "+infinity"; Chris@16: else if (value.is_neg_infinity()) Chris@16: strm << "-infinity"; Chris@16: else Chris@16: { Chris@16: boost::log::aux::decomposed_time_wrapper< value_type > val(value); Chris@16: date_time_support::decompose_time_duration(value, val); Chris@16: base_type::operator() (strm, val); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: //! The function parses format string and constructs formatter function Chris@16: static formatter_function_type parse(string_type const& format) Chris@16: { Chris@16: formatter fmt; Chris@16: boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > builder(fmt); Chris@16: boost::log::aux::parse_time_format(format, builder); Chris@16: return formatter_function_type(boost::move(fmt)); Chris@16: } Chris@16: }; Chris@16: Chris@16: template< typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< posix_time::time_duration, CharT, VoidT > : Chris@16: public time_duration_formatter_generator_traits_impl< posix_time::time_duration, CharT > Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< posix_time::hours, CharT, VoidT > : Chris@16: public time_duration_formatter_generator_traits_impl< posix_time::hours, CharT > Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< posix_time::minutes, CharT, VoidT > : Chris@16: public time_duration_formatter_generator_traits_impl< posix_time::minutes, CharT > Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< posix_time::seconds, CharT, VoidT > : Chris@16: public time_duration_formatter_generator_traits_impl< posix_time::seconds, CharT > Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename BaseDurationT, uint64_t FracOfSecondV, typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< date_time::subsecond_duration< BaseDurationT, FracOfSecondV >, CharT, VoidT > : Chris@16: public time_duration_formatter_generator_traits_impl< date_time::subsecond_duration< BaseDurationT, FracOfSecondV >, CharT > Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename DateDurationT, typename CharT > Chris@16: struct date_duration_formatter_generator_traits_impl Chris@16: { Chris@16: //! Character type Chris@16: typedef CharT char_type; Chris@16: //! String type Chris@16: typedef std::basic_string< char_type > string_type; Chris@16: //! Formatting stream type Chris@16: typedef basic_formatting_ostream< char_type > stream_type; Chris@16: //! Value type Chris@16: typedef DateDurationT value_type; Chris@16: Chris@16: //! Formatter function Chris@16: typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type; Chris@16: Chris@16: //! Formatter implementation Chris@16: class formatter : Chris@16: public boost::log::aux::date_time_formatter< boost::log::aux::decomposed_time_wrapper< value_type >, char_type > Chris@16: { Chris@16: BOOST_COPYABLE_AND_MOVABLE_ALT(formatter) Chris@16: Chris@16: private: Chris@16: // Do not change this typedef, copy-pasting the inherited class from above will break compilation with MSVC 2012 because it incorrectly binds value_type. Chris@16: typedef typename formatter::date_time_formatter_ base_type; Chris@16: Chris@16: public: Chris@16: typedef typename base_type::result_type result_type; Chris@16: // This typedef is needed to work around MSVC 2012 crappy name lookup. Otherwise base_type::value_type is bound instead. Chris@16: typedef typename date_duration_formatter_generator_traits_impl< DateDurationT, CharT >::value_type value_type; Chris@16: Chris@16: public: Chris@16: BOOST_DEFAULTED_FUNCTION(formatter(), {}) Chris@16: formatter(formatter const& that) : base_type(static_cast< base_type const& >(that)) {} Chris@16: formatter(BOOST_RV_REF(formatter) that) { this->swap(that); } Chris@16: Chris@16: formatter& operator= (formatter that) Chris@16: { Chris@16: this->swap(that); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: result_type operator() (stream_type& strm, value_type const& value) const Chris@16: { Chris@16: if (value.is_not_a_date()) Chris@16: strm << "not-a-date-time"; Chris@16: else if (value.is_pos_infinity()) Chris@16: strm << "+infinity"; Chris@16: else if (value.is_neg_infinity()) Chris@16: strm << "-infinity"; Chris@16: else Chris@16: { Chris@16: boost::log::aux::decomposed_time_wrapper< value_type > val(value); Chris@16: date_time_support::decompose_date_duration(value, val); Chris@16: base_type::operator() (strm, val); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: //! The function parses format string and constructs formatter function Chris@16: static formatter_function_type parse(string_type const& format) Chris@16: { Chris@16: formatter fmt; Chris@16: boost::log::aux::decomposed_time_formatter_builder< formatter, char_type > builder(fmt); Chris@16: boost::log::aux::parse_date_format(format, builder); Chris@16: return formatter_function_type(boost::move(fmt)); Chris@16: } Chris@16: }; Chris@16: Chris@16: template< typename CharT, typename VoidT > Chris@16: struct date_time_formatter_generator_traits< gregorian::date_duration, CharT, VoidT > : Chris@16: public date_formatter_generator_traits_impl< gregorian::date_duration, CharT > Chris@16: { Chris@16: }; Chris@16: Chris@16: } // namespace aux Chris@16: Chris@16: } // namespace expressions Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_SUPPORT_DATE_TIME_HPP_INCLUDED_