Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Class template period_parser

boost::date_time::period_parser — Not a facet, but a class used to specify and control period parsing.


// In header: <boost/date_time/period_parser.hpp>

template<typename date_type, typename CharT> 
class period_parser {
  // types
  typedef std::basic_string< CharT >                string_type;    
  typedef CharT                                     char_type;      
  typedef std::istreambuf_iterator< CharT >         stream_itr_type;
  typedef string_parse_tree< CharT >                parse_tree_type;
  typedef parse_tree_type::parse_match_result_type  match_results;  
  typedef std::vector< std::basic_string< CharT > > collection_type;

  enum period_range_option { AS_OPEN_RANGE, AS_CLOSED_RANGE };

  // construct/copy/destruct
  period_parser(period_range_option = AS_CLOSED_RANGE, 
                const char_type *const = default_period_separator, 
                const char_type *const = default_period_start_delimeter, 
                const char_type *const = default_period_open_range_end_delimeter, 
                const char_type *const = default_period_closed_range_end_delimeter);

  // public member functions
  period_range_option range_option() const;
  void range_option(period_range_option);
  collection_type delimiter_strings() const;
  void delimiter_strings(const string_type &, const string_type &, 
                         const string_type &, const string_type &);
  template<typename period_type, typename duration_type, typename facet_type> 
    period_type get_period(stream_itr_type &, stream_itr_type &, 
                           std::ios_base &, const period_type &, 
                           const duration_type &, const facet_type &) const;

  // private member functions
  void consume_delim(stream_itr_type &, stream_itr_type &, 
                     const string_type &) const;

  // public data members
  static const char_type default_period_separator;
  static const char_type default_period_start_delimeter;
  static const char_type default_period_open_range_end_delimeter;
  static const char_type default_period_closed_range_end_delimeter;


Provides settings for the following:

  • period_separator – default '/'

  • period_open_start_delimeter – default '['

  • period_open_range_end_delimeter – default ')'

  • period_closed_range_end_delimeter – default ']'

  • display_as_open_range, display_as_closed_range – default closed_range

For a typical date_period, the contents of the input stream would be


where the date format is controlled by the date facet

period_parser public construct/copy/destruct

  1. period_parser(period_range_option range_opt = AS_CLOSED_RANGE, 
                  const char_type *const period_separator = default_period_separator, 
                  const char_type *const period_start_delimeter = default_period_start_delimeter, 
                  const char_type *const period_open_range_end_delimeter = default_period_open_range_end_delimeter, 
                  const char_type *const period_closed_range_end_delimeter = default_period_closed_range_end_delimeter);
    Constructor that sets up period parser options.

period_parser public member functions

  1. period_range_option range_option() const;
  2. void range_option(period_range_option option);
  3. collection_type delimiter_strings() const;
  4. void delimiter_strings(const string_type & separator, 
                           const string_type & start_delim, 
                           const string_type & open_end_delim, 
                           const string_type & closed_end_delim);
  5. template<typename period_type, typename duration_type, typename facet_type> 
      period_type get_period(stream_itr_type & sitr, stream_itr_type & stream_end, 
                             std::ios_base & a_ios, const period_type &, 
                             const duration_type & dur_unit, 
                             const facet_type & facet) const;
    Generic code to parse a period – no matter the period type.

    This generic code will parse any period using a facet to to get the 'elements'. For example, in the case of a date_period the elements will be instances of a date which will be parsed according the to setup in the passed facet parameter.

    The steps for parsing a period are always the same:

    • consume the start delimiter

    • get start element

    • consume the separator

    • get either last or end element depending on range settings

    • consume the end delimeter depending on range settings

    Thus for a typical date period the contents of the input stream might look like this:

    [March 01, 2004/June 07, 2004]   <-- closed range
    [March 01, 2004/June 08, 2004)   <-- open range

period_parser private member functions

  1. void consume_delim(stream_itr_type & sitr, stream_itr_type & stream_end, 
                       const string_type & delim) const;
    throws ios_base::failure if delimiter and parsed data do not match