decomposed_time.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. #ifndef BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_
  17. #include <ctime>
  18. #include <string>
  19. #include <vector>
  20. #include <locale>
  21. #include <boost/cstdint.hpp>
  22. #include <boost/move/core.hpp>
  23. #include <boost/range/iterator_range_core.hpp>
  24. #include <boost/log/detail/config.hpp>
  25. #include <boost/log/detail/date_time_format_parser.hpp>
  26. #include <boost/log/detail/attachable_sstream_buf.hpp>
  27. #include <boost/log/utility/formatting_ostream.hpp>
  28. #include <boost/log/detail/header.hpp>
  29. #ifdef BOOST_HAS_PRAGMA_ONCE
  30. #pragma once
  31. #endif
  32. namespace boost {
  33. BOOST_LOG_OPEN_NAMESPACE
  34. namespace aux {
  35. //! Date and time suitable for formatting
  36. struct decomposed_time
  37. {
  38. // Subseconds are microseconds
  39. enum _
  40. {
  41. subseconds_per_second = 1000000,
  42. subseconds_digits10 = 6
  43. };
  44. uint32_t year, month, day, hours, minutes, seconds, subseconds;
  45. bool negative;
  46. decomposed_time() : year(0), month(1), day(1), hours(0), minutes(0), seconds(0), subseconds(0), negative(false)
  47. {
  48. }
  49. 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) :
  50. year(y), month(mo), day(d), hours(h), minutes(mi), seconds(s), subseconds(ss), negative(neg)
  51. {
  52. }
  53. unsigned int week_day() const
  54. {
  55. unsigned int a = (14u - month) / 12u;
  56. unsigned int y = year - a;
  57. unsigned int m = month + 12u * a - 2u;
  58. return (day + y + (y / 4u) - (y / 100u) + (y / 400u) + (31u * m) / 12u) % 7u;
  59. }
  60. unsigned int year_day() const
  61. {
  62. bool is_leap_year = (!(year % 4u)) && ((year % 100u) || (!(year % 400u)));
  63. static const unsigned int first_day_offset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  64. return first_day_offset[month - 1] + day + (month > 2 && is_leap_year);
  65. }
  66. };
  67. inline std::tm to_tm(decomposed_time const& t)
  68. {
  69. std::tm res = {};
  70. res.tm_year = static_cast< int >(t.year) - 1900;
  71. res.tm_mon = t.month - 1;
  72. res.tm_mday = t.day;
  73. res.tm_hour = t.hours;
  74. res.tm_min = t.minutes;
  75. res.tm_sec = t.seconds;
  76. res.tm_wday = t.week_day();
  77. res.tm_yday = t.year_day();
  78. res.tm_isdst = -1;
  79. return res;
  80. }
  81. template< typename T >
  82. struct decomposed_time_wrapper :
  83. public boost::log::aux::decomposed_time
  84. {
  85. typedef boost::log::aux::decomposed_time base_type;
  86. typedef T value_type;
  87. value_type m_time;
  88. BOOST_DEFAULTED_FUNCTION(decomposed_time_wrapper(), {})
  89. explicit decomposed_time_wrapper(value_type const& time) : m_time(time)
  90. {
  91. }
  92. };
  93. template< typename CharT >
  94. BOOST_LOG_API void put_integer(boost::log::aux::basic_ostringstreambuf< CharT >& strbuf, uint32_t value, unsigned int width, CharT fill_char);
  95. template< typename T, typename CharT >
  96. class date_time_formatter
  97. {
  98. BOOST_COPYABLE_AND_MOVABLE_ALT(date_time_formatter)
  99. protected:
  100. // Note: This typedef is needed to work around MSVC 2012 crappy name lookup in the derived classes
  101. typedef date_time_formatter date_time_formatter_;
  102. public:
  103. typedef void result_type;
  104. typedef T value_type;
  105. typedef CharT char_type;
  106. typedef std::basic_string< char_type > string_type;
  107. typedef basic_formatting_ostream< char_type > stream_type;
  108. struct context
  109. {
  110. date_time_formatter const& self;
  111. stream_type& strm;
  112. value_type const& value;
  113. unsigned int literal_index, literal_pos;
  114. context(date_time_formatter const& self_, stream_type& strm_, value_type const& value_) :
  115. self(self_),
  116. strm(strm_),
  117. value(value_),
  118. literal_index(0),
  119. literal_pos(0)
  120. {
  121. }
  122. BOOST_DELETED_FUNCTION(context(context const&))
  123. BOOST_DELETED_FUNCTION(context& operator=(context const&))
  124. };
  125. private:
  126. typedef void (*formatter_type)(context&);
  127. typedef std::vector< formatter_type > formatters;
  128. typedef std::vector< unsigned int > literal_lens;
  129. protected:
  130. formatters m_formatters;
  131. literal_lens m_literal_lens;
  132. string_type m_literal_chars;
  133. public:
  134. BOOST_DEFAULTED_FUNCTION(date_time_formatter(), {})
  135. date_time_formatter(date_time_formatter const& that) :
  136. m_formatters(that.m_formatters),
  137. m_literal_lens(that.m_literal_lens),
  138. m_literal_chars(that.m_literal_chars)
  139. {
  140. }
  141. date_time_formatter(BOOST_RV_REF(date_time_formatter) that) BOOST_NOEXCEPT
  142. {
  143. this->swap(static_cast< date_time_formatter& >(that));
  144. }
  145. date_time_formatter& operator= (date_time_formatter that) BOOST_NOEXCEPT
  146. {
  147. this->swap(that);
  148. return *this;
  149. }
  150. result_type operator() (stream_type& strm, value_type const& value) const
  151. {
  152. // Some formatters will put characters directly to the underlying string, so we have to flush stream buffers before formatting
  153. strm.flush();
  154. context ctx(*this, strm, value);
  155. for (typename formatters::const_iterator it = m_formatters.begin(), end = m_formatters.end(); strm.good() && it != end; ++it)
  156. {
  157. (*it)(ctx);
  158. }
  159. }
  160. void add_formatter(formatter_type fun)
  161. {
  162. m_formatters.push_back(fun);
  163. }
  164. void add_literal(iterator_range< const char_type* > const& lit)
  165. {
  166. m_literal_chars.append(lit.begin(), lit.end());
  167. m_literal_lens.push_back(static_cast< unsigned int >(lit.size()));
  168. m_formatters.push_back(&date_time_formatter_::format_literal);
  169. }
  170. void swap(date_time_formatter& that) BOOST_NOEXCEPT
  171. {
  172. m_formatters.swap(that.m_formatters);
  173. m_literal_lens.swap(that.m_literal_lens);
  174. m_literal_chars.swap(that.m_literal_chars);
  175. }
  176. public:
  177. template< char FormatCharV >
  178. static void format_through_locale(context& ctx)
  179. {
  180. typedef std::time_put< char_type > facet_type;
  181. typedef typename facet_type::iter_type iter_type;
  182. std::tm t = to_tm(static_cast< decomposed_time const& >(ctx.value));
  183. std::use_facet< facet_type >(ctx.strm.getloc()).put(iter_type(ctx.strm.stream()), ctx.strm.stream(), ' ', &t, FormatCharV);
  184. ctx.strm.flush();
  185. }
  186. static void format_full_year(context& ctx)
  187. {
  188. (put_integer)(*ctx.strm.rdbuf(), ctx.value.year, 4, static_cast< char_type >('0'));
  189. }
  190. static void format_short_year(context& ctx)
  191. {
  192. (put_integer)(*ctx.strm.rdbuf(), ctx.value.year % 100u, 2, static_cast< char_type >('0'));
  193. }
  194. static void format_numeric_month(context& ctx)
  195. {
  196. (put_integer)(*ctx.strm.rdbuf(), ctx.value.month, 2, static_cast< char_type >('0'));
  197. }
  198. template< char_type FillCharV >
  199. static void format_month_day(context& ctx)
  200. {
  201. (put_integer)(*ctx.strm.rdbuf(), ctx.value.day, 2, static_cast< char_type >(FillCharV));
  202. }
  203. static void format_week_day(context& ctx)
  204. {
  205. (put_integer)(*ctx.strm.rdbuf(), static_cast< decomposed_time const& >(ctx.value).week_day(), 1, static_cast< char_type >('0'));
  206. }
  207. template< char_type FillCharV >
  208. static void format_hours(context& ctx)
  209. {
  210. (put_integer)(*ctx.strm.rdbuf(), ctx.value.hours, 2, static_cast< char_type >(FillCharV));
  211. }
  212. template< char_type FillCharV >
  213. static void format_hours_12(context& ctx)
  214. {
  215. (put_integer)(*ctx.strm.rdbuf(), ctx.value.hours % 12u + 1u, 2, static_cast< char_type >(FillCharV));
  216. }
  217. static void format_minutes(context& ctx)
  218. {
  219. (put_integer)(*ctx.strm.rdbuf(), ctx.value.minutes, 2, static_cast< char_type >('0'));
  220. }
  221. static void format_seconds(context& ctx)
  222. {
  223. (put_integer)(*ctx.strm.rdbuf(), ctx.value.seconds, 2, static_cast< char_type >('0'));
  224. }
  225. static void format_fractional_seconds(context& ctx)
  226. {
  227. (put_integer)(*ctx.strm.rdbuf(), ctx.value.subseconds, decomposed_time::subseconds_digits10, static_cast< char_type >('0'));
  228. }
  229. template< bool UpperCaseV >
  230. static void format_am_pm(context& ctx)
  231. {
  232. static const char_type am[] = { static_cast< char_type >(UpperCaseV ? 'A' : 'a'), static_cast< char_type >(UpperCaseV ? 'M' : 'm'), static_cast< char_type >(0) };
  233. static const char_type pm[] = { static_cast< char_type >(UpperCaseV ? 'P' : 'p'), static_cast< char_type >(UpperCaseV ? 'M' : 'm'), static_cast< char_type >(0) };
  234. ctx.strm.rdbuf()->append(((static_cast< decomposed_time const& >(ctx.value).hours > 11) ? pm : am), 2u);
  235. }
  236. template< bool DisplayPositiveV >
  237. static void format_sign(context& ctx)
  238. {
  239. if (static_cast< decomposed_time const& >(ctx.value).negative)
  240. ctx.strm.rdbuf()->push_back('-');
  241. else if (DisplayPositiveV)
  242. ctx.strm.rdbuf()->push_back('+');
  243. }
  244. private:
  245. static void format_literal(context& ctx)
  246. {
  247. unsigned int len = ctx.self.m_literal_lens[ctx.literal_index], pos = ctx.literal_pos;
  248. ++ctx.literal_index;
  249. ctx.literal_pos += len;
  250. const char_type* lit = ctx.self.m_literal_chars.c_str();
  251. ctx.strm.rdbuf()->append(lit + pos, len);
  252. }
  253. };
  254. template< typename FormatterT, typename CharT >
  255. class decomposed_time_formatter_builder :
  256. public date_time_format_parser_callback< CharT >
  257. {
  258. public:
  259. typedef date_time_format_parser_callback< CharT > base_type;
  260. typedef typename base_type::char_type char_type;
  261. typedef FormatterT formatter_type;
  262. typedef typename formatter_type::value_type value_type;
  263. typedef typename formatter_type::stream_type stream_type;
  264. typedef typename stream_type::string_type string_type;
  265. protected:
  266. formatter_type& m_formatter;
  267. public:
  268. explicit decomposed_time_formatter_builder(formatter_type& fmt) : m_formatter(fmt)
  269. {
  270. }
  271. void on_literal(iterator_range< const char_type* > const& lit)
  272. {
  273. m_formatter.add_literal(lit);
  274. }
  275. void on_short_year()
  276. {
  277. m_formatter.add_formatter(&formatter_type::format_short_year);
  278. }
  279. void on_full_year()
  280. {
  281. m_formatter.add_formatter(&formatter_type::format_full_year);
  282. }
  283. void on_numeric_month()
  284. {
  285. m_formatter.add_formatter(&formatter_type::format_numeric_month);
  286. }
  287. void on_short_month()
  288. {
  289. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'b' >);
  290. }
  291. void on_full_month()
  292. {
  293. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'B' >);
  294. }
  295. void on_month_day(bool leading_zero)
  296. {
  297. if (leading_zero)
  298. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_month_day< '0' >);
  299. else
  300. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_month_day< ' ' >);
  301. }
  302. void on_numeric_week_day()
  303. {
  304. m_formatter.add_formatter(&formatter_type::format_week_day);
  305. }
  306. void on_short_week_day()
  307. {
  308. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'a' >);
  309. }
  310. void on_full_week_day()
  311. {
  312. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_through_locale< 'A' >);
  313. }
  314. void on_hours(bool leading_zero)
  315. {
  316. if (leading_zero)
  317. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours< '0' >);
  318. else
  319. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours< ' ' >);
  320. }
  321. void on_hours_12(bool leading_zero)
  322. {
  323. if (leading_zero)
  324. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours_12< '0' >);
  325. else
  326. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_hours_12< ' ' >);
  327. }
  328. void on_minutes()
  329. {
  330. m_formatter.add_formatter(&formatter_type::format_minutes);
  331. }
  332. void on_seconds()
  333. {
  334. m_formatter.add_formatter(&formatter_type::format_seconds);
  335. }
  336. void on_fractional_seconds()
  337. {
  338. m_formatter.add_formatter(&formatter_type::format_fractional_seconds);
  339. }
  340. void on_am_pm(bool upper_case)
  341. {
  342. if (upper_case)
  343. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_am_pm< true >);
  344. else
  345. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_am_pm< false >);
  346. }
  347. void on_duration_sign(bool display_positive)
  348. {
  349. if (display_positive)
  350. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_sign< true >);
  351. else
  352. m_formatter.add_formatter(&formatter_type::BOOST_NESTED_TEMPLATE format_sign< false >);
  353. }
  354. void on_iso_time_zone()
  355. {
  356. }
  357. void on_extended_iso_time_zone()
  358. {
  359. }
  360. };
  361. } // namespace aux
  362. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  363. } // namespace boost
  364. #include <boost/log/detail/footer.hpp>
  365. #endif // BOOST_LOG_DETAIL_DECOMPOSED_TIME_HPP_INCLUDED_