# Date Duration (aka Days)

Introduction -- Header -- Construction -- Accessors -- Operators -- Additional Duration Types

Introduction

The class boost::gregorian::date_duration is a simple day count used for arithmetic with gregorian::date. A duration can be either positive or negative.

As of version 1_32 the date_duration class has been typedef'd as days in the boost::gregorian namespace. Throughout the examples you will find days used instead of date_duration.

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

Construction

Syntax | Description
--- | ---
Example

    date_duration(long)

Create a duration count.

    date_duration dd(3); //3 days

    days(special_values sv)

Constructor for infinities, not-a-date-time, max_date_time, and min_date_time

    days dd1(neg_infin);
    days dd2(pos_infin);
    days dd3(not_a_date_time);
    days dd4(max_date_time);
    days dd5(min_date_time);

Accessors

Syntax | Description
--- | ---
Example

    long days() const

Get the day count.

    date_duration dd(3); dd.days() --> 3

    bool is_negative() const

True if number of days is less than zero.

    date_duration dd(-1); dd.is_negative() --> true

    static date_duration unit()

Return smallest possible unit of duration type.

    date_duration::unit() --> date_duration(1)

    bool is_special() const

Returns true if days is any `special_value`

    days dd(pos_infin);
    days dd2(not_a_date_time);
    days dd3(25);
    dd.is_special(); // --> true
    dd2.is_special(); // --> true
    dd3.is_special(); // --> false

Operators

Syntax | Description
--- | ---
Example

    operator<<, operator>>

Streaming operators. Note: As of version 1.33, streaming operations have been greatly improved. See Date Time IO System for more details (including exceptions and error conditions).

    date d(not_a_date_time);
    stringstream ss("2002-Jan-01");
    ss >> d;
    std::cout << d; // "2002-Jan-01"

    operator==, operator!=,
    operator>, operator<,
    operator>=, operator<=

A full complement of comparison operators

    dd1 == dd2, etc

    date_duration operator+(date_duration) const

Add date durations.

    date_duration dd1(3);
    date_duration dd2(5);
    date_duration dd3 = dd1 + dd2;

    date_duration operator-(date_duration) const

Subtract durations.

    date_duration dd1(3);
    date_duration dd2(5);
    date_duration dd3 = dd1 - dd2;

Additional Duration Types

These additional types are logical representations of spans of days.

Syntax | Description
--- | ---
Example

    months(int num_of_months)

A logical month representation. Depending on the usage, this `months` object may cover a span of 28 to 31 days. The objects also use a snap to end-of-month behavior when used in conjunction with a date that is the last day of a given month. WARNING: this behavior may lead to unexpected results. See: Reversibility of Operations Pitfall for complete details and alternatives.

    months single(1);
    date leap_year(2004,Jan,31);
    date norm_year(2005,Jan,31);
    leap_year + single; // => 2004-Feb-29
    norm_year + single; // => 2005-Feb-28
    date(2005,Jan,1) + single; // => 2005-Feb-01
    date(2005,Feb,1) + single; // => 2005-Mar-01
    date(2005,Feb,28) + single; // => 2005-Mar-31

    years(int num_of_years)

A logical representation of a year. The `years` object has the same behavior as the `months` objects with regards to the end-of-the-month.

    years single(1);
    date(2003,Feb,28) + single;
    // results in => 2004-Feb-29
    date(2004,Feb,29) + single;
    // results in => 2005-Feb-28

    weeks(int num_of_weeks)

A duration type representing a number of `n * 7` days.

    weeks single(1);
    date(2005,Jan,1) + single; // => 2005-Jan-08