BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bbldc_calendardaterangedaycountadapter

Detailed Description

Outline

Purpose

Provide a parameterized day-count convention implementation.

Classes

Description

This component provides a parameterized (template) implementation, bbldc::CalendarDateRangeDayCountAdapter, of the bbldc::DateRangeDayCount protocol. The template argument can be any type supporting the following two class methods.

int daysDiff(const bdlt::Date& beginDate,
const bdlt::Date& endDate,
const bdlt::Calendar& calendar);
double yearsDiff(const bdlt::Date& beginDate,
const bdlt::Date& endDate,
const bdlt::Calendar& calendar);
Definition bdlt_calendar.h:569
Definition bdlt_date.h:294

The template class bbldc::CalendarDateRangeDayCountAdapter provides convenient support for run-time polymorphic choice of day-count conventions (via conventional use of a base-class pointer or reference) without having to implement each derived type explicitly. In this sense, bbldc::CalendarDateRangeDayCountAdapter adapts the various concrete calendar-based day-count convention classes (e.g., bbldc::CalendarBus252) to a run-time binding mechanism.

The bbldc::DateRangeDayCount protocol requires two methods, firstDate and lastDate, that define a date range for which calculations are valid, to reflect the valid range of, say, a calendar required for the computations. For "calendar" day-count implementations, the valid date range is the valid range of the calendar.

Usage

This section illustrates intended use of this component.

Example 1: Adapting bbldc::CalendarBus252

This example shows the procedure for using bbldc::CalendarDateRangeDayCountAdapter to adapt the bbldc::CalendarBus252 day-count convention to the bbldc::DateRangeDayCount protocol, and then the use of the day-count methods. First, we create a calendar with a valid range spanning 2003 and typical weekend days:

bdlt::Calendar calendar;
calendar.setValidRange(bdlt::Date(2003, 1, 1), bdlt::Date(2003, 12, 31));
void addWeekendDay(DayOfWeek::Enum weekendDay)
void setValidRange(const Date &firstDate, const Date &lastDate)
Definition bdlt_calendar.h:1588
@ e_SUN
Definition bdlt_dayofweek.h:125
@ e_SAT
Definition bdlt_dayofweek.h:131

Then, we define an instance of the adapted day-count convention and obtain a reference to the bbldc::DateRangeDayCount:

myDcc(calendar);
const bbldc::DateRangeDayCount& dcc = myDcc;
Definition bbldc_calendardaterangedaycountadapter.h:155
Definition bbldc_daterangedaycount.h:191

Next, create two bdlt::Date variables, d1 and d2, with which to use the day-count convention methods:

const bdlt::Date d1(2003, 10, 19);
const bdlt::Date d2(2003, 12, 31);

Now, use the base-class reference to compute the day count between the two dates:

const int daysDiff = dcc.daysDiff(d1, d2);
assert(52 == daysDiff);
virtual int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const =0

Finally, use the base-class reference to compute the year fraction between the two dates:

const double yearsDiff = dcc.yearsDiff(d1, d2);
// Need fuzzy comparison since 'yearsDiff' is a 'double'.
assert(yearsDiff > 0.2063 && yearsDiff < 0.2064);
virtual double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const =0