# Date Generators/Algorithms

Date Generators/Algorithms
Introduction -- Header -- Class Overview -- Function Overview

Introduction

Date algorithms or generators are tools for generating other dates or schedules of dates. A generator function starts with some part of a date such as a month and day and is supplied another part to then generate a concrete date. This allows the programmer to represent concepts such as "The first Sunday in February" and then create a concrete set of dates when provided with one or more years. Note: As of boost version 1_31_0, date generator names have been changed. Old names are still available but are no longer documented and may someday be deprecated

Also provided are stand-alone functions for generating a date, or calculation a duration of days. These functions take a date object and a weekday object as parameters.

All date generator classes and functions are in the boost::gregorian namespace.

The print holidays example shows a detailed usage example.

Header

    #include "boost/date_time/gregorian/gregorian.hpp"

Overview

Class and get_date Parameter | Description
--- | ---
Example

    year_based_generator
    date get_date(greg_year year)

A unifying (abstract) date_generator base type for: `partial_date`, `nth_day_of_the_week_in_month`, `first_day_of_the_week_in_month`, and `last_day_of_the_week_in_month`.

The print holidays example shows a detailed usage example.

    last_day_of_the_week_in_month(greg_weekday,
                                  greg_month)
    date get_date(greg_year year)

Calculate something like last Monday of January

    last_day_of_the_week_in_month lwdm(Monday,Jan);
    date d = lwdm.get_date(2002);
    //2002-Jan-28

    first_day_of_the_week_in_month(greg_weekday,
                                   greg_month)
    date get_date(greg_year year)

Calculate something like first Monday of January

    first_day_of_the_week_in_month fdm(Monday,Jan);
    date d = fdm.get_date(2002);
    //2002-Jan-07

    nth_day_of_the_week_in_month(week_num,
                                 greg_weekday,
                                 greg_month)
    date get_date(greg_year year)

`week_num` is a public enum member of `nth_day_of_the_week_in_month`. Calculate something like first Monday of January, second Tuesday of March, Third Sunday of December, etc. (first through fifth are provided, fifth is the equivalent of last)

    typedef nth_day_of_the_week_in_month nth_dow;
    nth_dow ndm(nth_dow::third, Monday,Jan);
    date d = ndm.get_date(2002);
    //2002-Jan-21

    partial_date(greg_day, greg_month)
    date get_date(greg_year year)

Generates a date by applying the year to the given month and day.

    partial_date pd(1,Jan);
    date d = pd.get_date(2002);
    //2002-Jan-01

    first_day_of_the_week_after(greg_weekday)
    date get_date(date d)

Calculate something like First Sunday after Jan 1,2002

    first_day_of_the_week_after fdaf(Monday);
    date d = fdaf.get_date(date(2002,Jan,1));
    //2002-Jan-07

    first_day_of_the_week_before(greg_weekday)
    date get_date(date d)

Calculate something like First Monday before Feb 1,2002

    first_day_of_the_week_before fdbf(Monday);
    date d = fdbf.get_date(date(2002,Feb,1));
    //2002-Jan-28

Function Overview

Function Prototype | Description
--- | ---
Example

    days days_until_weekday date, greg_weekday)

Calculates the number of days from given date until given weekday.

    date d(2004,Jun,1); // Tuesday
    greg_weekday gw(Friday);
    days_until_weekday(d, gw); // 3 days

    days days_before_weekday(date, greg_weekday)

Calculates the number of day from given date to previous given weekday.

    date d(2004,Jun,1); // Tuesday
    greg_weekday gw(Friday);
    days_before_weekday(d, gw); // 4 days

    date next_weekday(date, greg_weekday)

Generates a date object representing the date of the following weekday from the given date. Can return the given date.

    date d(2004,Jun,1); // Tuesday
    greg_weekday gw1(Friday);
    greg_weekday gw2(Tuesday);
    next_weekday(d, gw1); // 2004-Jun-4
    next_weekday(d, gw2); // 2004-Jun-1

    date previous_weekday(date, greg_weekday)

Generates a date object representing the date of the previous weekday from the given date. Can return the given date.

    date d(2004,Jun,1); // Tuesday
    greg_weekday gw1(Friday);
    greg_weekday gw2(Tuesday);
    previous_weekday(d, gw1); // 2004-May-28
    previous_weekday(d, gw2); // 2004-Jun-1