annotate DEPENDENCIES/generic/include/boost/log/detail/decomposed_time.hpp @ 133:4acb5d8d80b6 tip

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