Gregorian Date SystemIntroduction -- Usage Examples IntroductionThe gregorian date system provides a date programming system based the Gregorian Calendar. The first introduction of the Gregorian calendar was in 1582 to fix an error in the Julian Calendar. However, many local jurisdictions did not adopt this change until much later. Thus there is potential confusion with historical dates. The implemented calendar is a "proleptic Gregorian calendar" which extends dates back prior to the Gregorian Calendar's first adoption in 1582. The current implementation supports dates in the range 1400-Jan-01 to 9999-Dec-31. Many references will represent dates prior to 1582 using the Julian Calendar, so caution is in order if accurate calculations are required on historic dates. See Calendrical Calculations by Reingold & Dershowitz for more details. Date information from Calendrical Calculations has been used to cross-test the correctness of the Gregorian calendar implementation. All types for the gregorian system are found in namespace boost::gregorian. The library supports a convenience header boost/date_time/gregorian/gregorian_types.hpp that will include all the classes of the library with no input/output dependency. Another header boost/date_time/gregorian/gregorian.hpp will include the types and the input/output code. The class boost::gregorian::date is the primary temporal type for users. If you are interested in learning about writing programs do specialized date calculations such as finding the "first sunday in april" see the date generators and algorithms page. Usage Examples
Introduction --
Header --
Construction --
Construct from String --
Construct from Clock --
Accessors --
Convert to String --
Operators --
Struct tm Functions
IntroductionThe 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 from another date. Techniques for creating dates include reading the current date from the clock, using date iterators, and date algorithms or generators. Internally boost::gregorian::date is stored as a 32 bit integer type. The class is specifically designed to NOT contain virtual functions. This design allows for efficient calculation and memory usage with large collections of dates. The construction of a date validates all input so that it is not possible to construct and 'invalid' date. That is 2001-Feb-29 cannot be constructed as a date. Various exceptions derived from std::out_of_range are thrown to indicate which aspect of the date input is invalid. Note that the special value not-a-date-time can be used as 'invalid' or 'null' date if so desired. 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
Construct from String
Construct from Clock
Accessors
Convert to String
Operators
Struct tm FunctionsFunctions for converting a
Introduction --
Header --
Construction --
Accessors --
Operators --
Additional Duration Types
IntroductionThe 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
Accessors
Operators
Additional Duration TypesThese additional types are logical representations of spans of days.
Reversibility of Operations Pitfall
A natural expectation when adding a number of months to a date, and then subtracting the same number of months, is to end up exactly where you started. This is most often the result the
When the starting date is in the middle of a month, adding or subtracting any number of months will result in a date that is the same day of month (e.g. if you start on the 15th, you will end on the 15th). When a date is the last day of the month, adding or subtracting any number of months will give a result that is also the last day of the month (e.g if you start on Jan 31st, you will land on: Feb 28th, Mar 31st, etc). // using months duration type date d(2005, Nov, 30); // last day of November d + months(1); // result is last day of December "2005-Dec-31" d - months(1); // result is last day of October "2005-Oct-31" // using month_iterator month_iterator itr(d); // last day of November ++itr; // result is last day of December "2005-Dec-31" --itr; // back to original starting point "2005-Nov-30" --itr; // last day of October "2005-Oct-31"
If the start date is the 28th, 29th, or 30th in a 31 day month, the result of adding or subtracting a month may result in the snap-to-end-of-month behavior kicking in unexpectedly. This would cause the final result to be different that the starting date. // using months duration type date d(2005, Nov, 29); d += months(1); // "2005-Dec-29" d += months(1); // "2006-Jan-29" d += months(1); // "2006-Feb-28" --> snap-to-end-of-month behavior kicks in d += months(1); // "2006-Mar-31" --> unexpected result d -= months(4); // "2005-Nov-30" --> unexpected result, not where we started // using month_iterator month_iterator itr(date(2005, Dec, 30)); ++itr; // "2006-Jan-30" --> ok ++itr; // "2006-Feb-28" --> snap-to DOES NOT kick in ++itr; // "2006-Mar-30" --> ok --itr; // "2006-Feb-28" --> ok --itr; // "2006-Jan-30" --> ok --itr; // "2005-Dec-30" --> ok, back where we started
The additional duration types (
Introduction --
Header --
Construction --
Mutators --
Accessors --
Convert to String --
Operators
IntroductionThe 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 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 types
Construction
Mutators
Accessors
Convert to String
Operators
Introduction --
Header --
Overview
IntroductionDate 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
Date Generators/AlgorithmsIntroduction -- Header -- Class Overview -- Function OverviewIntroductionDate 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
Function Overview
Introduction --
Header --
Functions
IntroductionThe class boost::gregorian::gregorian_calendar implements the functions necessary to create the gregorian date system. It converts to the year-month-day form of a date to a day number representation and back. For most purposes this class is simply accessed by gregorian::date and is not used directly by the user. However, there are useful functions that might be of use such as the end_of_month_day function. The print month example demonstrates this. 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
Functions
|