BDE 4.14.0 Production release
|
Provide common non-primitive operations on date objects.
This component provides a struct
, bdlt::DateUtil
, that serves as a namespace for utility functions that operate on bdlt::Date
objects.
The following list of methods are provided by bdlt::DateUtil
:
The "YYYYMMDD" format is a common integral representation of a date that is human readable and maintains appropriate ordering when sorted using integer comparisons. The notation uses eight digits (from left to right): four digits for the year, two digits for the month, and two digits for the day of the month. For example, February 1, 2014, is represented by the number 20140201.
Note that the year is not restricted to values on or after 1000, so, for example, 10102 (or 00010102) represents the date January 2, 0001.
Two adjustment conventions are used to determine the behavior of the functions (addMonths
and addYears
) that adjust a date by a particular number of months or years: the end-of-month convention and the non-end-of-month convention. The difference between the two conventions is that the end-of-month convention adjusts the resulting date to the end of the month if the original date is the last day of the month, while the non-end-of-month convention does not perform this adjustment.
For example, if we add 3 months to February 28, 2013 using the non-end-of-month convention, then the resulting date will be May 28, 2013. If we do the same operation except using the end-of-month convention, then the resulting date will be May 31, 2013.
More formal definitions of the two conventions are provided below:
The End-of-Month Convention: If the original date to be adjusted is the last day of a month, or if the day of the month of the original date does not exist in the resulting date, then adjust the resulting date to be the last day of the month.
The Non-End-of-Month Convention: If the day of the month of the original date does not exist in the resulting date, then adjust the resulting date to be the last day of the month.
This section illustrates intended use of this component.
Suppose that given a starting date in the "YYYYMMDD" format, we want to generate a schedule for an event that occurs on the same day of the month for 12 months.
First, we use the bdlt::DateUtil::convertFromYYYYMMDD
function to convert the integer into a bdlt::Date
:
Now, we use the addMonthsEom
function to generate the schedule. Note that addMonthsEom
adjusts the resulting date to be the last day of the month if the original date is the last day of the month, while addMonthsNoEom
does not make this adjustment.
Finally, we print the generated schedule to the console and observe the output:
Notice that the dates have been adjusted to the end of the month. If we had used addMonthsNoEom
instead of addMonthsEom
, this adjustment would not have occurred.