C++ Boost

gregorian::date

 


Overall Index -- Gregorian Index -- Posix Time Index

Date Documentation

Header -- Construction -- Construct from String -- Construct from Clock -- Accessors -- Conversion To String -- Operators

Introduction

The class boost::gregorian::date is the primary interface for date programming. In general, the date class is immutable once constructed although it does allow assignment.

Other techniques for creating dates include date iterators and date algorithms or generators.

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

SyntaxDescriptionExample
date(greg_year year, greg_month month, greg_day day) Construct from parts of date. Throws bad_year, bad_day_of_month, or bad_day_month (derivatives of std::out_of_range) if the year, month or day are out of range. date d(2002,Jan,10)
date(date d) Copy constructor date d1(d)
date(special_values sv) Constructor for infinitites, not-a-date-time, max_date, and min_date date d1(neg_infin);
date d2(pos_infin);
date d3(not_a_date_time);
date d4(max_date);
date d5(min_date);

Construction From String

SyntaxDescriptionExample
date from_string(const std::string&) From delimited date string where with order year-month-day eg: 2002-1-25 std::string ds("2002/1/25");
date d(from_string(ds))
date from_undelimited_string(const std::string&) From iso type date string where with order year-month-day eg: 20020125 std::string ds("20020125");
date d(from_undelimited_string(ds))

Construction From Clock

SyntaxDescriptionExample
day_clock::local_day() Get the local day based on the time zone settings of the computer. date d(day_clock::local_day())
day_clock::universal_day() Get the UTC day. date d(day_clock::universal_day())

Accessors

SyntaxDescriptionExample
greg_year year() const Get the year part of the date. date d(2002,Jan,10); d.year() --> 2002;
greg_month month() const Get the month part of the date. date d(2002,Jan,10); d.month() --> 1;
greg_day day() const Get the day part of the date. date d(2002,Jan,10); d.day() --> 10;
greg_ymd year_month_day() const Return a year_month_day struct. More efficient when all 3 parts of the date are needed. date d(2002,Jan,10);
date::ymd_type ymd = d.year_month_day();
ymd.year --> 2002, ymd.month --> 1, ymd.day --> 10
greg_day_of_week day_of_week() const Get the day of the week (eg: Sunday, Monday, etc. date d(2002,Jan,10); d.day() --> Thursday;
bool is_infinity() const Returns true if date is either positive or negative infinity date d(pos_infin); d.is_infinity() --> true;
bool is_neg_infinity() const Returns true if date is negative infinity date d(neg_infin); d.is_neg_infinity() --> true;
bool is_pos_infinity() const Returns true if date is positive infinity date d(neg_infin); d.is_pos_infinity() --> true;
bool is_not_a_date() const Returns true if value is not a date date d(not_a_date_time); d.is_not_a_date() --> true;
long modjulian_day() const Returns the modified julian day for the date.
long julian_day() const Returns the julian day for the date.
int week_number() const Returns the ISO 8601 week number for the date.

Conversion To String

SyntaxDescriptionExample
std::string to_simple_string(date d) To YYYY-mmm-DD string where mmm 3 char month name. 2002-Jan-01
std::string to_iso_string(date d) To YYYYMMDD where all components are integers. 20020131
std::string to_iso_extended_string(date d) To YYYY-MM-DD where all components are integers. 2002-01-31

Operators

SyntaxDescriptionExample
operator<< Stream output operator date d(2002,Jan,1)
std::cout << d << std::endl;
operator==, operator!=,
operator>, operator<
operator>=, operator<=
A full complement of comparison operators d1 == d2, etc
date operator+(date_duration) const Return a date adding a day offset date d(2002,Jan,1);
date_duration dd(1);
date d2 = d + dd;
date operator-(date_duration) const Return a date by adding a day offset date d(2002,Jan,1);
date_duration dd(1);
date d2 = d - dd;
date_duration operator-(date) const Return a date duration by subtracting two dates date d1(2002,Jan,1);
date d2(2002,Jan,2);
date_duration dd = d2-d1;


Last modified: Sun Jan 19 16:01:22 MST 2003 by Jeff Garland © 2000-2002