# Date Iterators

Introduction -- Header -- Overview

Introduction

Date iterators provide a standard mechanism for iteration through dates. Date iterators are a model of Bidirectional Iterator and can be used to populate collections with dates and other date generation tasks. For example, the print month example iterates through all the days in a month and prints them.

All of the iterators here derive from boost::gregorian::date_iterator.

Header

    #include "boost/date_time/gregorian/gregorian.hpp" //include all types plus i/o
    or
    #include "boost/date_time/gregorian/gregorian_types.hpp" //no i/o just types

Overview

Syntax | Description
--- | ---
Example

    date_iterator

Common (abstract) base class for all day level iterators.

    day_iterator(date start_date, int day_count=1)

Iterate `day_count` days at a time. This iterator does not provide postfix increment/decrement operators. Only prefix operators are provided.

    day_iterator day_itr(date(2005,Jan,1));
    ++d_itr; // 2005-Jan-02
    day_iterator 2day_itr(date(2005,Feb,1),2);
    ++2d_itr; // 2005-Feb-03

    week_iterator(...)
      Parameters:
        date start_date
        int week_offset (defaults to 1)

Iterate `week_offset` weeks at a time. This iterator does not provide postfix increment/decrement operators. Only prefix operators are provided.

    week_iterator wk_itr(date(2005,Jan,1));
    ++wk_itr; // 2005-Jan-08
    week_iterator 2wk_itr(date(2005,Jan,1),2);
    ++2wk_itr; // 2005-Feb-15

    month_iterator(...)
      Parameters:
        date start_date
        int month_offset (defaults to 1)

Iterate `month_offset` months. There are special rules for handling the end of the month. These are: if start date is last day of the month, always adjust to last day of the month. If date is beyond the end of the month (e.g. Jan 31 + 1 month) adjust back to end of month (for more details and examples of this, see Reversibility of Operations Pitfall. NOTE: the `month_iterator` is not effected by this pitfall.) This iterator does not provide postfix increment/decrement operators. Only prefix operators are provided.

    month_iterator m_itr(date(2005,Jan,1));
    ++m_itr; // 2005-Feb-01
    month_iterator 2m_itr(date(2005,Feb,1),2);
    ++2m_itr; // 2005-Apr-01

    year_iterator(...)
      Parameters:
        date start_date
        int year_offset (defaults to 1)

Iterate year_offset years. The year_iterator will always land on the day of the date parameter except when date is Feb 28 in a non-leap year. In this case the iterator will return Feb 29 for leap years (eg: 2003-Feb-28, 2004-Feb-29, 2005-Feb-28). This iterator does not provide postfix increment/decrement operators. Only prefix operators are provided.

    year_iterator y_itr(date(2005,Jan,1));
    ++y_itr; // 2006-Jan-01
    year_iterator 2y_itr(date(2005,Feb,1),2);
    ++2y_itr; // 2007-Feb-01