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)
8 * \file date_time_format_parser.hpp
9 * \author Andrey Semashev
12 * The header contains a parser for date and time format strings.
15 #ifndef BOOST_LOG_DETAIL_DATE_TIME_FORMAT_PARSER_HPP_INCLUDED_
16 #define BOOST_LOG_DETAIL_DATE_TIME_FORMAT_PARSER_HPP_INCLUDED_
19 #include <boost/range/as_literal.hpp>
20 #include <boost/range/iterator_range_core.hpp>
21 #include <boost/log/detail/config.hpp>
22 #include <boost/log/detail/header.hpp>
24 #ifdef BOOST_HAS_PRAGMA_ONCE
30 BOOST_LOG_OPEN_NAMESPACE
35 * This is the interface the parser will use to notify the caller about various components of date in the format string.
37 template< typename CharT >
38 struct date_format_parser_callback
40 //! Character type used by the parser
41 typedef CharT char_type;
44 virtual ~date_format_parser_callback() {}
47 * \brief The function is called when the parser discovers a string literal in the format string
49 * \param lit The string of characters not interpreted as a placeholder
51 virtual void on_literal(iterator_range< const char_type* > const& lit) = 0;
54 * \brief The method is called when an unknown placeholder is found in the format string
56 * \param ph The placeholder with the leading percent sign
58 virtual void on_placeholder(iterator_range< const char_type* > const& ph)
60 // By default interpret all unrecognized placeholders as literals
65 * \brief The function is called when the short year placeholder is found in the format string
67 virtual void on_short_year()
69 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('y'), static_cast< char_type >('\0') };
70 on_placeholder(boost::as_literal(placeholder));
74 * \brief The function is called when the full year placeholder is found in the format string
76 virtual void on_full_year()
78 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('Y'), static_cast< char_type >('\0') };
79 on_placeholder(boost::as_literal(placeholder));
83 * \brief The function is called when the numeric month placeholder is found in the format string
85 virtual void on_numeric_month()
87 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('m'), static_cast< char_type >('\0') };
88 on_placeholder(boost::as_literal(placeholder));
92 * \brief The function is called when the short alphabetic month placeholder is found in the format string
94 virtual void on_short_month()
96 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('b'), static_cast< char_type >('\0') };
97 on_placeholder(boost::as_literal(placeholder));
101 * \brief The function is called when the full alphabetic month placeholder is found in the format string
103 virtual void on_full_month()
105 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('B'), static_cast< char_type >('\0') };
106 on_placeholder(boost::as_literal(placeholder));
110 * \brief The function is called when the numeric day of month placeholder is found in the format string
112 * \param leading_zero If \c true, the day should be formatted with leading zero, otherwise with leading space
114 virtual void on_month_day(bool leading_zero)
116 const char_type placeholder[3] = { static_cast< char_type >('%'), (leading_zero ? static_cast< char_type >('d') : static_cast< char_type >('e')), static_cast< char_type >('\0') };
117 on_placeholder(boost::as_literal(placeholder));
121 * \brief The function is called when the numeric day of week placeholder is found in the format string
123 virtual void on_numeric_week_day()
125 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('w'), static_cast< char_type >('\0') };
126 on_placeholder(boost::as_literal(placeholder));
130 * \brief The function is called when the short alphabetic day of week placeholder is found in the format string
132 virtual void on_short_week_day()
134 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('a'), static_cast< char_type >('\0') };
135 on_placeholder(boost::as_literal(placeholder));
139 * \brief The function is called when the full alphabetic day of week placeholder is found in the format string
141 virtual void on_full_week_day()
143 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('A'), static_cast< char_type >('\0') };
144 on_placeholder(boost::as_literal(placeholder));
148 * \brief The function is called when the ISO-formatted date is found in the format string
150 virtual void on_iso_date()
158 * \brief The function is called when the extended ISO-formatted date is found in the format string
160 virtual void on_extended_iso_date()
162 const char_type delimiter[2] = { static_cast< char_type >('-'), static_cast< char_type >('\0') };
164 on_literal(boost::as_literal(delimiter));
166 on_literal(boost::as_literal(delimiter));
172 * This is the interface the parser will use to notify the caller about various components of date in the format string.
174 template< typename CharT >
175 struct time_format_parser_callback
177 //! Character type used by the parser
178 typedef CharT char_type;
181 virtual ~time_format_parser_callback() {}
184 * \brief The function is called when the parser discovers a string literal in the format string
186 * \param lit The string of characters not interpreted as a placeholder
188 virtual void on_literal(iterator_range< const char_type* > const& lit) = 0;
191 * \brief The method is called when an unknown placeholder is found in the format string
193 * \param ph The placeholder with the leading percent sign
195 virtual void on_placeholder(iterator_range< const char_type* > const& ph)
197 // By default interpret all unrecognized placeholders as literals
202 * \brief The function is called when the hours placeholder is found in the format string
204 * The placeholder is used for 24-hour clock and duration formatting.
206 * \param leading_zero If \c true, the one-digit number of hours should be formatted with leading zero, otherwise with leading space
208 virtual void on_hours(bool leading_zero)
210 const char_type placeholder[3] = { static_cast< char_type >('%'), (leading_zero ? static_cast< char_type >('O') : static_cast< char_type >('k')), static_cast< char_type >('\0') };
211 on_placeholder(boost::as_literal(placeholder));
215 * \brief The function is called when the hours placeholder is found in the format string
217 * The placeholder is used for 12-hour clock formatting. It should not be used for duration formatting.
219 * \param leading_zero If \c true, the one-digit number of hours should be formatted with leading zero, otherwise with leading space
221 virtual void on_hours_12(bool leading_zero)
223 const char_type placeholder[3] = { static_cast< char_type >('%'), (leading_zero ? static_cast< char_type >('I') : static_cast< char_type >('l')), static_cast< char_type >('\0') };
224 on_placeholder(boost::as_literal(placeholder));
228 * \brief The function is called when the minutes placeholder is found in the format string
230 virtual void on_minutes()
232 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('M'), static_cast< char_type >('\0') };
233 on_placeholder(boost::as_literal(placeholder));
237 * \brief The function is called when the seconds placeholder is found in the format string
239 virtual void on_seconds()
241 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('S'), static_cast< char_type >('\0') };
242 on_placeholder(boost::as_literal(placeholder));
246 * \brief The function is called when the fractional seconds placeholder is found in the format string
248 virtual void on_fractional_seconds()
250 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('f'), static_cast< char_type >('\0') };
251 on_placeholder(boost::as_literal(placeholder));
255 * \brief The function is called when the day period (AM/PM) placeholder is found in the format string
257 * The placeholder is used for 12-hour clock formatting. It should not be used for duration formatting.
259 * \param upper_case If \c true, the day period will be upper case, and lower case otherwise
261 virtual void on_am_pm(bool upper_case)
263 const char_type placeholder[3] = { static_cast< char_type >('%'), (upper_case ? static_cast< char_type >('p') : static_cast< char_type >('P')), static_cast< char_type >('\0') };
264 on_placeholder(boost::as_literal(placeholder));
268 * \brief The function is called when the time duration sign placeholder is found in the format string
270 * The placeholder is used for duration formatting. It should not be used for time point formatting.
272 * \param display_positive If \c true, the positive sign will be explicitly displayed, otherwise only negative sign will be displayed
274 virtual void on_duration_sign(bool display_positive)
276 const char_type placeholder[3] = { static_cast< char_type >('%'), (display_positive ? static_cast< char_type >('+') : static_cast< char_type >('-')), static_cast< char_type >('\0') };
277 on_placeholder(boost::as_literal(placeholder));
281 * \brief The function is called when the ISO time zone placeholder is found in the format string
283 virtual void on_iso_time_zone()
285 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('q'), static_cast< char_type >('\0') };
286 on_placeholder(boost::as_literal(placeholder));
290 * \brief The function is called when the extended ISO time zone placeholder is found in the format string
292 virtual void on_extended_iso_time_zone()
294 const char_type placeholder[3] = { static_cast< char_type >('%'), static_cast< char_type >('Q'), static_cast< char_type >('\0') };
295 on_placeholder(boost::as_literal(placeholder));
299 * \brief The function is called when the ISO-formatted time is found in the format string
301 virtual void on_iso_time()
309 * \brief The function is called when the extended ISO-formatted time is found in the format string
311 virtual void on_extended_iso_time()
313 const char_type delimiter[2] = { static_cast< char_type >(':'), static_cast< char_type >('\0') };
315 on_literal(boost::as_literal(delimiter));
317 on_literal(boost::as_literal(delimiter));
322 * \brief The function is called when the extended ISO-formatted time with fractional seconds is found in the format string
324 virtual void on_default_time()
326 on_extended_iso_time();
328 const char_type delimiter[2] = { static_cast< char_type >('.'), static_cast< char_type >('\0') };
329 on_literal(boost::as_literal(delimiter));
330 on_fractional_seconds();
335 * This is the interface the parser will use to notify the caller about various components of date in the format string.
337 template< typename CharT >
338 struct date_time_format_parser_callback :
339 public date_format_parser_callback< CharT >,
340 public time_format_parser_callback< CharT >
342 //! Character type used by the parser
343 typedef CharT char_type;
346 virtual ~date_time_format_parser_callback() {}
349 * \brief The function is called when the parser discovers a string literal in the format string
351 * \param lit The string of characters not interpreted as a placeholder
353 virtual void on_literal(iterator_range< const char_type* > const& lit) = 0;
356 * \brief The method is called when an unknown placeholder is found in the format string
358 * \param ph The placeholder with the leading percent sign
360 virtual void on_placeholder(iterator_range< const char_type* > const& ph)
362 // By default interpret all unrecognized placeholders as literals
368 * \brief Parses the date format string and invokes the callback object
370 * \pre <tt>begin <= end</tt>, both pointers must not be \c NULL
371 * \param begin Pointer to the first character of the sequence
372 * \param end Pointer to the after-the-last character of the sequence
373 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
375 template< typename CharT >
376 BOOST_LOG_API void parse_date_format(const CharT* begin, const CharT* end, date_format_parser_callback< CharT >& callback);
379 * \brief Parses the date format string and invokes the callback object
381 * \param str The format string to parse
382 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
384 template< typename CharT, typename TraitsT, typename AllocatorT >
385 inline void parse_date_format(std::basic_string< CharT, TraitsT, AllocatorT > const& str, date_format_parser_callback< CharT >& callback)
387 const CharT* p = str.c_str();
388 return parse_date_format(p, p + str.size(), callback);
392 * \brief Parses the date format string and invokes the callback object
394 * \pre <tt>str != NULL</tt>, <tt>str</tt> points to a zero-terminated string.
395 * \param str The format string to parse
396 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
398 template< typename CharT >
399 inline void parse_date_format(const CharT* str, date_format_parser_callback< CharT >& callback)
401 return parse_date_format(str, str + std::char_traits< CharT >::length(str), callback);
405 * \brief Parses the time format string and invokes the callback object
407 * \pre <tt>begin <= end</tt>, both pointers must not be \c NULL
408 * \param begin Pointer to the first character of the sequence
409 * \param end Pointer to the after-the-last character of the sequence
410 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
412 template< typename CharT >
413 BOOST_LOG_API void parse_time_format(const CharT* begin, const CharT* end, time_format_parser_callback< CharT >& callback);
416 * \brief Parses the time format string and invokes the callback object
418 * \param str The format string to parse
419 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
421 template< typename CharT, typename TraitsT, typename AllocatorT >
422 inline void parse_time_format(std::basic_string< CharT, TraitsT, AllocatorT > const& str, time_format_parser_callback< CharT >& callback)
424 const CharT* p = str.c_str();
425 return parse_time_format(p, p + str.size(), callback);
429 * \brief Parses the time format string and invokes the callback object
431 * \pre <tt>str != NULL</tt>, <tt>str</tt> points to a zero-terminated string.
432 * \param str The format string to parse
433 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
435 template< typename CharT >
436 inline void parse_time_format(const CharT* str, time_format_parser_callback< CharT >& callback)
438 return parse_time_format(str, str + std::char_traits< CharT >::length(str), callback);
442 * \brief Parses the date and time format string and invokes the callback object
444 * \pre <tt>begin <= end</tt>, both pointers must not be \c NULL
445 * \param begin Pointer to the first character of the sequence
446 * \param end Pointer to the after-the-last character of the sequence
447 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
449 template< typename CharT >
450 BOOST_LOG_API void parse_date_time_format(const CharT* begin, const CharT* end, date_time_format_parser_callback< CharT >& callback);
453 * \brief Parses the date and time format string and invokes the callback object
455 * \param str The format string to parse
456 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
458 template< typename CharT, typename TraitsT, typename AllocatorT >
459 inline void parse_date_time_format(std::basic_string< CharT, TraitsT, AllocatorT > const& str, date_time_format_parser_callback< CharT >& callback)
461 const CharT* p = str.c_str();
462 return parse_date_time_format(p, p + str.size(), callback);
466 * \brief Parses the date and time format string and invokes the callback object
468 * \pre <tt>str != NULL</tt>, <tt>str</tt> points to a zero-terminated string.
469 * \param str The format string to parse
470 * \param callback Reference to the callback object that will be invoked by the parser as it processes the input
472 template< typename CharT >
473 inline void parse_date_time_format(const CharT* str, date_time_format_parser_callback< CharT >& callback)
475 return parse_date_time_format(str, str + std::char_traits< CharT >::length(str), callback);
480 BOOST_LOG_CLOSE_NAMESPACE // namespace log
484 #include <boost/log/detail/footer.hpp>
486 #endif // BOOST_LOG_DETAIL_DATE_TIME_FORMAT_PARSER_HPP_INCLUDED_