# Date Time Formatter/Parser Objects

Date Time Formatter/Parser Objects
Periods | Date Generators | Special Values | Format Date Parser

Periods

The period_formatter and period_parser provide a uniform interface for the input and output of date_periods, time_periods, and in a future release, local_date_time_periods. The user has control over the delimiters, formats of the date/time components, and the form the period takes. The format of the date/time components is controlled via the date_time input and output facets.

Period Form

Periods are constructed with open ranged parameters. The first value is the starting point, and is included in the period. The end value is not included but immediately follows the last value: [begin/end). However, a period can be streamed as either an open range or a closed range.

    [2003-Jan-01/2003-Dec-31] <-- period holding 365 days
    [2003-Jan-01/2004-Jan-01) <-- period holding 365 days

Delimiters

There are four delimiters. The default values are

- "\" - separator
- "[" - start delimiter
- ")" - open range end delimiter
- "]" - closed range end delimiter

A user can provide a custom set of delimiters. Custom delimiters may contain spaces.

Customization

The period form and delimiters can be set as construction parameters or by means of accessor functions. A custom period parser/formatter can then be used as a construction parameter to a new facet, or can be set in an existing facet via an accessor function.

Period Formatter/Parser Reference
The complete class reference can be found here: `Period Formatter Doxygen Reference` and here: `Period Parser Doxygen Reference`

Period Formatter Construction

Syntax | Description
--- | ---

    period_formatter(...)
      Parameters:
        range_display_options
        char_type*
        char_type*
        char_type*
        char_type*

NOTE: All five construction parameters have default values so this constructor also doubles as the default constructor. The `range_display_options` is a public type enum of the `period_formatter` class. The possible choices are AS_OPEN_RANGE or AS_CLOSED_RANGE. The closed range is the default. A period has three significant points: the begining, the last, and the end. A closed range period takes the form [begin,end), where an open range period takes the form [begin,last]. The four char_type* parameters are: the period separator, the start delimiter, the open range end delimiter, and the closed range end delimiter.

Period Formatter Accessors

Syntax | Description
--- | ---
Example

    range_display_options range_option()

Returns the current setting for the range display (either AS_OPEN_RANGE or AS_CLOSED_RANGE).

    void range_option(...)
      Parameter:
        range_display_options

Sets the option for range display (either AS_OPEN_RANGE or AS_CLOSED_RANGE).

    void delimiter_strings(...)
      Parameters:
        string_type
        string_type
        string_type
        string_type

Set new delimiter strings in the formatter.

    string beg("->| ");
    string sep(" || ");
    string opn(" ->|");
    string clo(" |<-");
    pf.delimiter_strings(beg, sep,
                         opn, clo);

    put_period_start_delimeter(...)
      Return Type:
        OutItrT
      Parameter:
        OutItrT

Puts the start delimiter into the stream at position pointed to by OutItrT parameter.

    put_period_sepatator(...)
      Return Type:
        OutItrT
      Parameter:
        OutItrT

Puts the separator into the stream at position pointed to by OutItrT parameter.

    put_period_end_delimeter(...)
      Return Type:
        OutItrT
      Parameter:
        OutItrT

Puts the end delimiter into the stream at position pointed to by OutItrT parameter.

    OutItrT put_period(...)
      Parameters:
        OutItrT
        ios_base
        char_type
        period_type
        facet_type

Puts a period into the stream using the set values for delimiters, separator, and range display. The facet parameter is used to put the date (or time) objects of the period.

Period Parser Construction

Syntax | Description
--- | ---

    period_parser(...)
      Parameters:
        period_range_option
        char_type*
        char_type*
        char_type*
        char_type*

NOTE: All five construction parameters have default values so this constructor also doubles as the default constructor. The `period_range_option` is a public type enum of the `period_parser` class. The possible choices are AS_OPEN_RANGE or AS_CLOSED_RANGE. The closed range is the default. A period has three significant points: the begining, the last, and the end. A closed range period takes the form [begin,end), where an open range period takes the form [begin,last]. The four char_type* parameters are: the period separator, the start delimiter, the open range end delimiter, and the closed range end delimiter.

    period_parser(period_parser)

Copy constructor

Period Parser Accessors

Syntax | Description
--- | ---
Example

    period_range_option range_option()

Returns the current setting for the period range (either AS_OPEN_RANGE or AS_CLOSED_RANGE).

    void range_option(...)
      Parameter:
        period_range_option

Sets the option for period range (either AS_OPEN_RANGE or AS_CLOSED_RANGE).

    void delimiter_strings(...)
      Parameters:
        string_type
        string_type
        string_type
        string_type

Set new delimiter strings in the parser.

    string beg("->| ");
    string sep(" || ");
    string opn(" ->|");
    string clo(" |<-");
    pp.delimiter_strings(beg, sep,
                         opn, clo);

    collection_type delimiter_strings()

Returns the set of delimiter strings currently held in the parser.

    period_type get_period(...)
      Parameters:
        stream_itr_type
        stream_itr_type
        ios_base
        period_type
        duration_type
        facet_type

Parses a period from the stream. The iterator parameters point to the begining and end of the stream. The duration_type is relevant to the period type, for example: A `date_period` would use `days` as a duration_type. The period will be parsed according to the formats and strings found in the facet parameter.

_____________________________________________________

Date Generators

The date_generator formatter and parser provide flexibility by allowing the user to use custom "phrase elements". These phrase elements are the "in-between" words in the date_generators. For example, in the date_generator "Second Monday of March", "Second" and "of" are the phrase elements, where "Monday" and "March" are the date elements. Customization of the date elements is done with the facet. The order of the date and phrase elements cannot be changed. When parsing, all elements of the date_generator phrase must parse correctly or an ios_base::failure exception will be thrown.

Customization

The default "phrase_strings" are:

- "first"
- "second"
- "third"
- "fourth"
- "fifth"
- "last"
- "before"
- "after"
- "of"

A custom set of phrase_strings must maintain this order of occurance (Ex: "1st", "2nd", "3rd", "4th", "5th", "last", "prior", "past", "in").

Examples using default phrase_strings and default facet formats for weekday & month:

    "first Tue of Mar"

And using custom phrase_strings:

    "1st Tue in Mar"

The custom set of phrase elements can be set as construction parameters or through an accessor function.A custom date_generator parser/formatter can then be used as a construction parameter to a new facet, or can be set in an existing facet via an accessor function.

IMPORTANT NOTE: Prior to 1.33, partial_date was output as "1 Jan" with a single *or* double digit number for the day. The new behavior is to *always* place a double digit number for the day - "01 Jan".

Date Generator Reference
The complete class references can be found here: `Date Generator Formatter Doxygen Reference` and here: `Date Generator Parser Doxygen Reference`

Date Generator Formatter Construction

Syntax | Description
--- | ---

    date_generator_formatter()

Uses the default date generator elements.

    date_generator_formatter(...)
      Parameters:
        string_type first_element
        string_type second_element
        string_type third_element
        string_type fourth_element
        string_type fifth_element
        string_type last_element
        string_type before_element
        string_type after_element
        string_type of_element

Constructs a date_generator_formatter using the given element strings.

Date Generator Formatter Accessors

Syntax | Description
--- | ---
Example

    void elements(...)
      Parameters:
        collection_type
        phrase_elements

Replace the current phrase elements with a collection of new ones. The `phrase_elements` parameter is an enum that indicates what the first element in the new collection is (defaults to first).

    // col is a collection holding
    // "final", "prior", "following",
    // and "in"
    typedef date_generator_formatter dgf;
    dgf formatter();
    formatter.elements(col, dgf::last);
    // complete elements in dgf are now:
    "first", "second", "third",
    "fourth", "fifth", "final",
    "prior", "following", and "in"

    put_partial_date(...)
      Return Type:
        facet_type::OutItrT
      Parameters:
        OutItrT next
        ios_base
        char_type fill
        partial_date
        facet_type

A put function for partial_date. This is a templated function that takes a facet_type as a parameter.

Put a partial_date => "dd Month".

    put_nth_kday(...)
      Return Type:
        facet_type::OutItrT
      Parameters:
        OutItrT next
        ios_base
        char_type fill
        nth_kday_type
        facet_type

A put function for nth_kday_type. This is a templated function that takes a facet_type as a parameter.

Put an nth_day_of_the_week_in_month => "nth weekday of month".

    put_first_kday(...)
      Return Type:
        facet_type::OutItrT
      Parameters:
        OutItrT next
        ios_base
        char_type fill
        first_kday_type
        facet_type

A put function for first_kday_type. This is a templated function that takes a facet_type as a parameter.

Put a first_day_of_the_week_in_month => "first weekday of month".

    put_last_kday(...)
      Return Type:
        facet_type::OutItrT
      Parameters:
        OutItrT next
        ios_base
        char_type fill
        last_kday_type
        facet_type

A put function for last_kday_type. This is a templated function that takes a facet_type as a parameter.

Put a last_day_of_the_week_in_month => "last weekday of month".

    put_kday_before(...)
      Return Type:
        facet_type::OutItrT
      Parameters:
        OutItrT next
        ios_base
        char_type fill
        kday_before_type
        facet_type

A put function for kday_before_type. This is a templated function that takes a facet_type as a parameter.

Put a first_day_of_the_week_before => "weekday before"

    put_kday_after(...)
      Return Type:
        facet_type::OutItrT
      Parameters:
        OutItrT next
        ios_base
        char_type fill
        kday_after_type
        facet_type

A put function for kday_after_type. This is a templated function that takes a facet_type as a parameter.

Put a first_day_of_the_week_after => "weekday after".

Date Generator Parser Construction

Syntax | Description
--- | ---

    date_generator_parser()

Uses the default date generator elements.

    date_generator_parser(...)
      Parameter:
        date_generator_parser

Copy Constructor

    date_generator_parser(...)
      Parameters:
        string_type first_element
        string_type second_element
        string_type third_element
        string_type fourth_element
        string_type fifth_element
        string_type last_element
        string_type before_element
        string_type after_element
        string_type of_element

Constructs a date_generator_parser using the given element strings.

Date Generator Parser Accessors

Syntax | Description
--- | ---
Example

    void element_strings(...)
      Parameter:
        collection_type

Replace the set of date generator element string with a new set.

    void element_strings(...)
      Parameters:
        string_type first
        string_type second
        string_type third
        string_type fourth
        string_type fifth
        string_type last
        string_type before
        string_type after
        string_type of

Replace the set of date generator elements with new values.

    get_partial_date_type(...)
      Return Type:
        facet_type::partial_date_type
      Parameters:
        stream_itr_type next
        stream_itr_type str_end
        ios_base
        facet_type

A templated function that parses a date_generator from the stream.

Parses a partial_date => "dd Month".

    get_nth_kday_type(...)
      Return Type:
        facet_type::nth_kday_type
      Parameters:
        stream_itr_type next
        stream_itr_type str_end
        ios_base
        facet_type

A templated function that parses a date_generator from the stream.

Parses an nth_day_of_the_week_in_month => "nth weekday of month".

    get_first_kday_type(...)
      Return Type:
        facet_type::firat_kday_type
      Parameters:
        stream_itr_type next
        stream_itr_type str_end
        ios_base
        facet_type

A templated function that parses a date_generator from the stream.

Parses a first_day_of_the_week_in_month => "first weekday of month".

    get_last_kday_type(...)
      Return Type:
        facet_type::last_kday_type
      Parameters:
        stream_itr_type next
        stream_itr_type str_end
        ios_base
        facet_type

A templated function that parses a date_generator from the stream.

Parses a last_day_of_the_week_in_month => "last weekday of month".

    get_kday_before_type(...)
      Return Type:
        facet_type::kday_before_type
      Parameters:
        stream_itr_type next
        stream_itr_type str_end
        ios_base
        facet_type

A templated function that parses a date_generator from the stream.

Parses a first_day_of_the_week_before => "weekday before"

    get_kday_after_type(...)
      Return Type:
        facet_type::kday_after_type
      Parameters:
        stream_itr_type next
        stream_itr_type str_end
        ios_base
        facet_type

A templated function that parses a date_generator from the stream.

Parses a first_day_of_the_week_after => "weekday after".

_____________________________________________________

Special Values

The date_time library uses five special_values. They are:

- not_a_date_time
- neg_infin
- pos_infin
- min_date_time
- max_date_time

The default set of strings used to represent these types are: "not-a-date-time", "-infinity", "+infinity", "minimum-date-time", "maximum-date-time". When output, the min_date-time and max_date_time appear as normal date/time representations: "1400-Jan-01" and "9999-Dec-31" repectively.

Customization

The special values parser/formatter allows the user to set custom strings for these special values. These strings can be set as construction parameters to a new facet, or can be set in an existing facet via an accessor function.

Special Values Formatter/Parser Reference
The complete class references can be found here: `Special Values Formatter Doxygen Reference` and here: `Special Values Parser Doxygen Reference`

Special Values Formatter Constructor

Syntax | Description
--- | ---

    special_values_formatter()

Constructor uses defaults for special value strings.

    special_values_formatter(...)
      Parameters:
        collection_type::iterator
        collection_type::iterator

Constructs using values in collection. NOTE: Only the first three strings of the collection will be used. Strings for minimum_date_time and maximum_date_time are ignored as those special values are output as normal dates/times.

    special_values_formatter(...)
      Parameters:
        char_type*
        char_type*

Constructs special values formatter from an array of strings.

Special Values Formatter Accessors

Syntax | Description
--- | ---
Example

    OutItrT put_special(...)
      Parameters:
        OutItrT next
        special_values value

Puts the given special value into the stream.

    date d1(not_a_date_time);
    date d2(minimum_date_time);
    special_values_formatter formatter;
    formatter.put_special(itr, d1);
    // Puts: "not-a-date-time"
    formatter.put_special(itr, d2);
    // Puts: "1400-Jan-01"

Special Values Parser Constructor

Syntax | Description
--- | ---

    special_values_parser()

    special_values_parser(...)
      Parameters:
        collection_type::iterator
        collection_type::iterator

Constructs a special values parser using the strings in the collection.

    special_values_parser(...)
      Parameter:
        scpecial_values_parser

Copy constructor.

    special_values_parser(...)
      Parameters:
        string_type nadt_str
        string_type neg_inf_str
        string_type pos_inf_str
        string_type min_dt_str
        string_type max_dt_str

Constructs a special values parser using the supplied strings.

Special Values Parser Accessors

Syntax | Description
--- | ---
Example

    void sv_strings(...)
      Parameters:
        string_type nadt_str
        string_type neg_inf_str
        string_type pos_inf_str
        string_type min_dt_str
        string_type max_dt_str

Replace the set of special value strings with the given ones.

    bool match(...)
      Parameters:
        stream_itr_type beg
        stream_itr_type end
        match_results

Returns true if parse was successful. Upon a successful parse, `mr.current_match` will be set an int values corresponding to the equivalent special_value.

    // stream holds "maximum_date_time"
    typedef special_values_parser svp;
    svp parser;
    svp::match_results mr;
    if(parser.match(itr, str_end, mr)) {
      d = date(static_cast<special_values>(
               mr.match_results))
    } else {
      // error, failed parse
    }
    // d == "9999-Dec-31"

_____________________________________________________

Format Date Parser

The format date parser is the object that holds the strings for months and weekday names, as well as their abbreviations. Custom sets of strings can be set at construction time, or, the strings in an existing format_date_parser can be replaced through accessor functions. Both the constructor and the accessor functions take a vector of strings as their arguments.

Format Date Parser Reference
The complete class reference can be found here: `Doxygen Reference`

Format Date Parser Constructor

Syntax | Description
--- | ---

    format_date_parser(...)
                    Parameters:
                    string_type format
                    std::locale

Creates a parser that uses the given format for parsing dates (in those functions where there is no format parameter). The names and abbreviations used are extracted from the given locale.

    format_date_parser(...)
      Parameters:
        string_type format
        input_collection_type
        input_collection_type
        input_collection_type
        input_collection_type

Creates a parser from using the given components. The input_collection_type parameters are for: short month names, long month names, short weekday names, and long weekday names (in that order). These collections must contain values for every month and every weekday (begining with January and Sunday).

    format_date_parser(...)
      Parameters:
        format_date_parser

Copy Constructor

Format Date Parser Accessors

Syntax | Description
--- | ---
Example

    string_type format()

Returns the format that will be used when parsing dates in those functions where there is no format parameter.

    void format(string_type)

Sets the format that will be used when parsing dates in those functions where there is no format parameter.

    void short_month_names(...)
      Parameter:
        input_collection_type names

Replace the short month names used by the parser. The collection must contain values for each month, starting with January.

    void long_month_names(...)
      Parameter:
        input_collection_type names

Replace the long month names used by the parser. The collection must contain values for each month, starting with January.

    void short_weekday_names(...)
      Parameter:
        input_collection_type names

Replace the short weekday names used by the parser. The collection must contain values for each weekday, starting with Sunday.

    void long_weekday_names(...)
      Parameter:
        input_collection_type names

Replace the long weekday names used by the parser. The collection must contain values for each weekday, starting with Sunday.

    date_type parse_date(...)
      Parameters:
        string_type input
        string_type format
        special_values_parser

Parse a date from the given input using the given format.

    string inp("2005-Apr-15");
    string format("%Y-%b-%d");
    date d;
    d = parser.parse_date(inp,
                          format,
                          svp);
    // d == 2005-Apr-15

    date_type parse_date(...)
      Parameters:
        istreambuf_iterator input
        istreambuf_iterator str_end
        special_values_parser

Parse a date from stream using the parser's format.

    date_type parse_date(...)
      Parameters:
        istreambuf_iterator input
        istreambuf_iterator str_end
        string_type format
        special_values_parser

Parse a date from stream using the given format.

    // stream holds "2005-04-15"
    string format("%Y-%m-%d");
    date d;
    d = parser.parse_date(itr,
                          str_end,
                          format,
                          svp);
    // d == 2005-Apr-15

    month_type parse_month(...)
      Parameters:
        istreambuf_iterator input
        istreambuf_iterator str_end
        string_type format

Parses a month from stream using given format. Throws bad_month if unable to parse.

    // stream holds "March"
    string format("%B");
    greg_month m;
    m = parser.parse_month(itr,
                           str_end,
                           format);
    // m == March

    day_type parse_day_of_month(...)
      Parameters:
        istreambuf_iterator input
        istreambuf_iterator str_end

Parses a day_of_month from stream. The day must appear as a two digit number (01-31), or a bad_day_of_month will be thrown.

    // stream holds "01"
    greg_day d;
    d = parser.parse_day_of_month(itr,
                           str_end);
    // d == 1st

    day_type parse_var_day_of_month(...)
      Parameters:
        istreambuf_iterator input
        istreambuf_iterator str_end

Parses a day_of_month from stream. The day must appear as a one or two digit number (1-31), or a bad_day_of_month will be thrown.

    // stream holds "1"
    greg_day d;
    d = parser.parse_var_day_of_month(itr,
                           str_end);
    // d == 1st

    day_of_week_type parse_weekday(...)
      Parameters:
        istreambuf_iterator input
        istreambuf_iterator str_end
        string_type format

Parse a weekday from stream according to the given format. Throws a bad_weekday if unable to parse.

    // stream holds "Tue"
    string format("%a");
    greg_weekday wd;
    wd = parser.parse_weekday(itr,
                              str_end,
                              format);
    // wd == Tuesday

    year_type parse_year(...)
      Parameters:
        istreambuf_iterator input
        istreambuf_iterator str_end
        string_type format

Parse a year from stream according to given format. Throws bad year if unable to parse.

    // stream holds "98"
    string format("%y");
    greg_year y;
    y = parser.parse_year(itr,
                          str_end,
                          format);
    // y == 1998