decomposed_time.hpp 13 KB

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