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 decomposed_time.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 07.11.2012 Chris@16: * Chris@16: * \brief This header is the Boost.Log library implementation, see the library documentation Chris@16: * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_DETAIL_DECOMPOSED_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: 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 aux { Chris@16: Chris@16: //! Date and time suitable for formatting Chris@16: struct decomposed_time Chris@16: { Chris@16: // Subseconds are microseconds Chris@16: enum _ Chris@16: { Chris@16: subseconds_per_second = 1000000, Chris@16: subseconds_digits10 = 6 Chris@16: }; Chris@16: Chris@16: uint32_t year, month, day, hours, minutes, seconds, subseconds; Chris@16: bool negative; Chris@16: Chris@16: decomposed_time() : year(0), month(1), day(1), hours(0), minutes(0), seconds(0), subseconds(0), negative(false) Chris@16: { Chris@16: } Chris@16: Chris@16: 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) : Chris@16: year(y), month(mo), day(d), hours(h), minutes(mi), seconds(s), subseconds(ss), negative(neg) Chris@16: { Chris@16: } Chris@16: Chris@16: unsigned int week_day() const Chris@16: { Chris@16: unsigned int a = (14u - month) / 12u; Chris@16: unsigned int y = year - a; Chris@16: unsigned int m = month + 12u * a - 2u; Chris@16: return (day + y + (y / 4u) - (y / 100u) + (y / 400u) + (31u * m) / 12u) % 7u; Chris@16: } Chris@16: Chris@16: unsigned int year_day() const Chris@16: { Chris@16: bool is_leap_year = (!(year % 4u)) && ((year % 100u) || (!(year % 400u))); Chris@16: static const unsigned int first_day_offset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; Chris@16: return first_day_offset[month - 1] + day + (month > 2 && is_leap_year); Chris@16: } Chris@16: }; Chris@16: Chris@16: inline std::tm to_tm(decomposed_time const& t) Chris@16: { Chris@16: std::tm res = {}; Chris@16: res.tm_year = static_cast< int >(t.year) - 1900; Chris@16: res.tm_mon = t.month - 1; Chris@16: res.tm_mday = t.day; Chris@16: res.tm_hour = t.hours; Chris@16: res.tm_min = t.minutes; Chris@16: res.tm_sec = t.seconds; Chris@16: res.tm_wday = t.week_day(); Chris@16: res.tm_yday = t.year_day(); Chris@16: res.tm_isdst = -1; Chris@16: Chris@16: return res; Chris@16: } Chris@16: Chris@16: template< typename T > Chris@16: struct decomposed_time_wrapper : Chris@16: public boost::log::aux::decomposed_time Chris@16: { Chris@16: typedef boost::log::aux::decomposed_time base_type; Chris@16: typedef T value_type; Chris@16: value_type m_time; Chris@16: Chris@16: BOOST_DEFAULTED_FUNCTION(decomposed_time_wrapper(), {}) Chris@16: Chris@16: explicit decomposed_time_wrapper(value_type const& time) : m_time(time) Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: template< typename CharT > Chris@16: BOOST_LOG_API void put_integer(std::basic_string< CharT >& str, uint32_t value, unsigned int width, CharT fill_char); Chris@16: Chris@16: template< typename T, typename CharT > Chris@16: class date_time_formatter Chris@16: { Chris@16: BOOST_COPYABLE_AND_MOVABLE_ALT(date_time_formatter) Chris@16: Chris@16: protected: Chris@16: // Note: This typedef is needed to work around MSVC 2012 crappy name lookup in the derived classes Chris@16: typedef date_time_formatter date_time_formatter_; Chris@16: Chris@16: public: Chris@16: typedef void result_type; Chris@16: typedef T value_type; Chris@16: typedef CharT char_type; Chris@16: typedef std::basic_string< char_type > string_type; Chris@16: typedef basic_formatting_ostream< char_type > stream_type; Chris@16: Chris@16: struct context Chris@16: { Chris@16: date_time_formatter const& self; Chris@16: stream_type& strm; Chris@16: string_type& str; Chris@16: value_type const& value; Chris@16: unsigned int literal_index, literal_pos; Chris@16: Chris@16: context(date_time_formatter const& self_, stream_type& strm_, value_type const& value_) : Chris@16: self(self_), Chris@16: strm(strm_), Chris@16: str(*strm_.rdbuf()->storage()), Chris@16: value(value_), Chris@16: literal_index(0), Chris@16: literal_pos(0) Chris@16: { Chris@16: } Chris@16: Chris@16: BOOST_DELETED_FUNCTION(context(context const&)) Chris@16: BOOST_DELETED_FUNCTION(context& operator=(context const&)) Chris@16: }; Chris@16: Chris@16: private: Chris@16: typedef void (*formatter_type)(context&); Chris@16: typedef std::vector< formatter_type > formatters; Chris@16: typedef std::vector< unsigned int > literal_lens; Chris@16: Chris@16: protected: Chris@16: formatters m_formatters; Chris@16: literal_lens m_literal_lens; Chris@16: string_type m_literal_chars; Chris@16: Chris@16: public: Chris@16: BOOST_DEFAULTED_FUNCTION(date_time_formatter(), {}) Chris@16: date_time_formatter(date_time_formatter const& that) : Chris@16: m_formatters(that.m_formatters), Chris@16: m_literal_lens(that.m_literal_lens), Chris@16: m_literal_chars(that.m_literal_chars) Chris@16: { Chris@16: } Chris@16: date_time_formatter(BOOST_RV_REF(date_time_formatter) that) Chris@16: { Chris@16: this->swap(static_cast< date_time_formatter& >(that)); Chris@16: } Chris@16: Chris@16: date_time_formatter& operator= (date_time_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: // Some formatters will put characters directly to the underlying string, so we have to flush stream buffers before formatting Chris@16: strm.flush(); Chris@16: context ctx(*this, strm, value); Chris@16: for (typename formatters::const_iterator it = m_formatters.begin(), end = m_formatters.end(); strm.good() && it != end; ++it) Chris@16: { Chris@16: (*it)(ctx); Chris@16: } Chris@16: } Chris@16: Chris@16: void add_formatter(formatter_type fun) Chris@16: { Chris@16: m_formatters.push_back(fun); Chris@16: } Chris@16: Chris@16: void add_literal(iterator_range< const char_type* > const& lit) Chris@16: { Chris@16: m_literal_chars.append(lit.begin(), lit.end()); Chris@16: m_literal_lens.push_back(static_cast< unsigned int >(lit.size())); Chris@16: m_formatters.push_back(&date_time_formatter_::format_literal); Chris@16: } Chris@16: Chris@16: void swap(date_time_formatter& that) Chris@16: { Chris@16: m_formatters.swap(that.m_formatters); Chris@101: m_literal_lens.swap(that.m_literal_lens); Chris@16: m_literal_chars.swap(that.m_literal_chars); Chris@16: } Chris@16: Chris@16: public: Chris@16: template< char FormatCharV > Chris@16: static void format_through_locale(context& ctx) Chris@16: { Chris@16: typedef std::time_put< char_type > facet_type; Chris@16: typedef typename facet_type::iter_type iter_type; Chris@16: std::tm t = to_tm(static_cast< decomposed_time const& >(ctx.value)); Chris@16: std::use_facet< facet_type >(ctx.strm.getloc()).put(iter_type(ctx.strm.stream()), ctx.strm.stream(), ' ', &t, FormatCharV); Chris@16: ctx.strm.flush(); Chris@16: } Chris@16: Chris@16: static void format_full_year(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.year, 4, static_cast< char_type >('0')); Chris@16: } Chris@16: Chris@16: static void format_short_year(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.year % 100u, 2, static_cast< char_type >('0')); Chris@16: } Chris@16: Chris@16: static void format_numeric_month(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.month, 2, static_cast< char_type >('0')); Chris@16: } Chris@16: Chris@16: template< char_type FillCharV > Chris@16: static void format_month_day(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.day, 2, static_cast< char_type >(FillCharV)); Chris@16: } Chris@16: Chris@16: static void format_week_day(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, static_cast< decomposed_time const& >(ctx.value).week_day(), 1, static_cast< char_type >('0')); Chris@16: } Chris@16: Chris@16: template< char_type FillCharV > Chris@16: static void format_hours(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.hours, 2, static_cast< char_type >(FillCharV)); Chris@16: } Chris@16: Chris@16: template< char_type FillCharV > Chris@16: static void format_hours_12(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.hours % 12u + 1u, 2, static_cast< char_type >(FillCharV)); Chris@16: } Chris@16: Chris@16: static void format_minutes(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.minutes, 2, static_cast< char_type >('0')); Chris@16: } Chris@16: Chris@16: static void format_seconds(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.seconds, 2, static_cast< char_type >('0')); Chris@16: } Chris@16: Chris@16: static void format_fractional_seconds(context& ctx) Chris@16: { Chris@16: (put_integer)(ctx.str, ctx.value.subseconds, decomposed_time::subseconds_digits10, static_cast< char_type >('0')); Chris@16: } Chris@16: Chris@16: template< bool UpperCaseV > Chris@16: static void format_am_pm(context& ctx) Chris@16: { Chris@16: static const char_type am[] = { static_cast< char_type >(UpperCaseV ? 'A' : 'a'), static_cast< char_type >(UpperCaseV ? 'M' : 'm'), static_cast< char_type >(0) }; Chris@16: static const char_type pm[] = { static_cast< char_type >(UpperCaseV ? 'P' : 'p'), static_cast< char_type >(UpperCaseV ? 'M' : 'm'), static_cast< char_type >(0) }; Chris@16: Chris@16: ctx.str.append(((static_cast< decomposed_time const& >(ctx.value).hours > 11) ? pm : am), 2u); Chris@16: } Chris@16: Chris@16: template< bool DisplayPositiveV > Chris@16: static void format_sign(context& ctx) Chris@16: { Chris@16: if (static_cast< decomposed_time const& >(ctx.value).negative) Chris@16: ctx.str.push_back('-'); Chris@16: else if (DisplayPositiveV) Chris@16: ctx.str.push_back('+'); Chris@16: } Chris@16: Chris@16: private: Chris@16: static void format_literal(context& ctx) Chris@16: { Chris@16: unsigned int len = ctx.self.m_literal_lens[ctx.literal_index], pos = ctx.literal_pos; Chris@16: ++ctx.literal_index; Chris@16: ctx.literal_pos += len; Chris@16: const char_type* lit = ctx.self.m_literal_chars.c_str(); Chris@16: ctx.str.append(lit + pos, len); Chris@16: } Chris@16: }; Chris@16: Chris@16: template< typename FormatterT, typename CharT > Chris@16: class decomposed_time_formatter_builder : Chris@16: public date_time_format_parser_callback< CharT > Chris@16: { Chris@16: public: Chris@16: typedef date_time_format_parser_callback< CharT > base_type; Chris@16: typedef typename base_type::char_type char_type; Chris@16: typedef FormatterT formatter_type; Chris@16: typedef typename formatter_type::value_type value_type; Chris@16: typedef typename formatter_type::stream_type stream_type; Chris@16: typedef typename stream_type::string_type string_type; Chris@16: Chris@16: protected: Chris@16: formatter_type& m_formatter; Chris@16: Chris@16: public: Chris@16: explicit decomposed_time_formatter_builder(formatter_type& fmt) : m_formatter(fmt) Chris@16: { Chris@16: } Chris@16: Chris@16: void on_literal(iterator_range< const char_type* > const& lit) Chris@16: { Chris@16: m_formatter.add_literal(lit); Chris@16: } Chris@16: Chris@16: void on_short_year() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::format_short_year); Chris@16: } Chris@16: Chris@16: void on_full_year() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::format_full_year); Chris@16: } Chris@16: Chris@16: void on_numeric_month() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::format_numeric_month); Chris@16: } Chris@16: Chris@16: void on_short_month() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'b' >); Chris@16: } Chris@16: Chris@16: void on_full_month() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'B' >); Chris@16: } Chris@16: Chris@16: void on_month_day(bool leading_zero) Chris@16: { Chris@16: if (leading_zero) Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_month_day< '0' >); Chris@16: else Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_month_day< ' ' >); Chris@16: } Chris@16: Chris@16: void on_numeric_week_day() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::format_week_day); Chris@16: } Chris@16: Chris@16: void on_short_week_day() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'a' >); Chris@16: } Chris@16: Chris@16: void on_full_week_day() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'A' >); Chris@16: } Chris@16: Chris@16: void on_hours(bool leading_zero) Chris@16: { Chris@16: if (leading_zero) Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours< '0' >); Chris@16: else Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours< ' ' >); Chris@16: } Chris@16: Chris@16: void on_hours_12(bool leading_zero) Chris@16: { Chris@16: if (leading_zero) Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours_12< '0' >); Chris@16: else Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours_12< ' ' >); Chris@16: } Chris@16: Chris@16: void on_minutes() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::format_minutes); Chris@16: } Chris@16: Chris@16: void on_seconds() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::format_seconds); Chris@16: } Chris@16: Chris@16: void on_fractional_seconds() Chris@16: { Chris@16: m_formatter.add_formatter(&formatter_type::format_fractional_seconds); Chris@16: } Chris@16: Chris@16: void on_am_pm(bool upper_case) Chris@16: { Chris@16: if (upper_case) Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_am_pm< true >); Chris@16: else Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_am_pm< false >); Chris@16: } Chris@16: Chris@16: void on_duration_sign(bool display_positive) Chris@16: { Chris@16: if (display_positive) Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_sign< true >); Chris@16: else Chris@16: m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_sign< false >); Chris@16: } Chris@16: Chris@16: void on_iso_time_zone() Chris@16: { Chris@16: } Chris@16: Chris@16: void on_extended_iso_time_zone() Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace aux 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_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_