Date Period
Introduction --
Header --
Construction --
Mutators --
Accessors --
Convert to String --
Operators
Introduction
The class boost::gregorian::date_period provides direct representation for ranges between two dates. Periods provide the ability to simplify some types of calculations by simplifying the conditional logic of the program. For example, testing if a date is within an irregular schedule such as a weekend or holiday can be accomplished using collections of date periods. This is facilitated by several methods that allow evaluation if a date_period intersects with another date period, and to generate the period resulting from the intersection. The date period calculation example provides an example of this.
A period that is created with beginning and end points being equal, or with a duration of zero, is known as a zero length period. Zero length periods are considered invalid (it is perfectly legal to construct an invalid period). For these periods, the last point will always be one unit less that the begin point.
Date periods used in combination with infinity values have the ability to represent complex concepts such as 'until further notice'.
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 typesConstructionSyntaxDescriptionExampledate_period(date, date)Create a period as [begin, end). If end is <= begin then the period will be invalid.date_period dp(date(2002,Jan,10),
date(2002,Jan,12));date_period(date, days)Create a period as [begin, begin+len) where end point would be begin+len. If len is <= zero then the period will be defined as invalid.date_period dp(date(2002,Jan,10),
days(2));date_period(date_period) Copy constructordate_period dp1(dp);MutatorsSyntaxDescriptionExampledate_period shift(days)Add duration to both begin and end.
date_period dp(date(2005,Jan,1), days(3));
dp.shift(days(3));
// dp == 2005-Jan-04 to 2005-Jan-07
date_period expand(days)Subtract duration from begin and add duration to end.
date_period dp(date(2005,Jan,2), days(2));
dp.expand(days(1));
// dp == 2005-Jan-01 to 2005-Jan-04
AccessorsSyntaxDescriptionExampledate begin() Return first day of period.date_period dp(date(2002,Jan,1),
date(2002,Jan,10));
dp.begin() --> 2002-Jan-01date last()Return last date in the perioddate_period dp(date(2002,Jan,1),
date(2002,Jan,10));
dp.last() --> 2002-Jan-09date end()Return one past the last in perioddate_period dp(date(2002,Jan,1),
date(2002,Jan,10));
dp.end() --> 2002-Jan-10days length()Return the length of the date_perioddate_period dp(date(2002,Jan,1),
days(2));
dp.length() --> 2bool is_null()True if period is not well formed. eg: end less than or equal to begin.date_period dp(date(2002,Jan,10),
date(2002,Jan,1));
dp.is_null() --> truebool contains(date)True if date is within the period. Zero length periods cannot contain any pointsdate d(2002,Jan,1);
date_period dp(d, date(2002,Jan,10));
dp.contains(date(2002,Jan,2));// true
date_period dp2(d, d);
dp.contains(date(2002,Jan,1));// falsebool contains(date_period)True if date period is within the perioddate_period dp1(date(2002,Jan,1),
date(2002,Jan,10));
date_period dp2(date(2002,Jan,2),
date(2002,Jan,3));
dp1.contains(dp2) --> true
dp2.contains(dp1) --> falsebool intersects(date_period)True if periods overlapdate_period dp1(date(2002,Jan,1),
date(2002,Jan,10));
date_period dp2(date(2002,Jan,2),
date(2002,Jan,3));
dp2.intersects(dp1) --> truedate_period intersection(date_period)Calculate the intersection of 2 periods. Null if no intersection.date_period dp1(date(2002,Jan,1),
date(2002,Jan,10));
date_period dp2(date(2002,Jan,2),
date(2002,Jan,3));
dp2.intersection(dp1) --> dp2date_period is_adjacent(date_period)Check if two periods are adjacent, but not overlapping.date_period dp1(date(2002,Jan,1),
date(2002,Jan,3));
date_period dp2(date(2002,Jan,3),
date(2002,Jan,10));
dp2.is_adjacent(dp1) --> truedate_period is_after(date)Determine the period is after a given date.date_period dp1(date(2002,Jan,10),
date(2002,Jan,30));
date d(2002,Jan,3);
dp1.is_after(d) --> truedate_period is_before(date)Determine the period is before a given date.date_period dp1(date(2002,Jan,1),
date(2002,Jan,3));
date d(2002,Jan,10);
dp1.is_before(d) --> truedate_period merge(date_period)Returns union of two periods. Null if no intersection.date_period dp1(date(2002,Jan,1),
date(2002,Jan,10));
date_period dp2(date(2002,Jan,9),
date(2002,Jan,31));
dp2.merge(dp1)
// 2002-Jan-01/2002-Jan-31date_period span(date_period)Combines two periods and any gap between them such that begin = min(p1.begin, p2.begin) and end = max(p1.end , p2.end)
date_period dp1(date(2002,Jan,1),
date(2002,Jan,5));
date_period dp2(date(2002,Jan,9),
date(2002,Jan,31));
dp2.span(dp1); // 2002-Jan-01/2002-Jan-31date_period shift(days)Add duration to both begin and end.date_period dp1(date(2002,Jan,1),
date(2002,Jan,10));
dp1.shift(days(1));
// 2002-Jan-02/2002-Jan-11date_period expand(days)Subtract duration from begin and add duration to end.date_period dp1(date(2002,Jan,4),
date(2002,Jan,10));
dp1.expand(days(2));
// 2002-Jan-02/2002-Jan-12Convert to StringSyntaxDescriptionExamplestd::string to_simple_string(date_period dp)To [YYYY-mmm-DD/YYYY-mmm-DD] string where mmm is 3 char month name.[2002-Jan-01/2002-Jan-31]OperatorsSyntaxDescriptionExampleoperator<<ostream operator for date_period. Uses facet to format time points. Typical output: [2002-Jan-01/2002-Jan-31].std::cout << dp << std::endl;operator>>istream operator for date_period. Uses facet to parse time points."[2002-Jan-01/2002-Jan-31]"operator==, operator!=,
operator>, operator<A full complement of comparison operatorsdp1 == dp2, etcoperator<True if dp1.end() less than dp2.begin()dp1 < dp2, etcoperator>True if dp1.begin() greater than dp2.end()dp1 > dp2, etc