Provide a protocol for basic day-count calculations.
More...
Namespaces |
namespace | bbldc |
Detailed Description
- Outline
-
-
- Purpose:
- Provide a protocol for basic day-count calculations.
-
- Classes:
-
- See also:
- Component bbldc_basicbasicdaycountadapter
-
- Description:
- This component provides a protocol,
bbldc::BasicDayCount
, for implementing an arbitrary day-count convention. Concrete implementations of this protocol may implement, say, the ISMA 30/360 day-count convention, or a custom day-count convention appropriate for some niche market.
- Several of the components in
bbldc
provide individual day-count convention support through interfaces that are functionally identical to the abstract interface provided by this component, except that they do not inherit from bbldc::BasicDayCount
. In conjunction with the adapter components (e.g., bbldc_basicbasicdaycountadapter
), bbldc::BasicDayCount
is intended to allow run-time binding of these and other similar day-count implementations.
-
- Usage:
- This section illustrates intended use of this component.
-
- Example 1: Definition and Use of a Concrete Day-Count Convention:
- This example shows the definition and use of a simple concrete day-count convention. This functionality suffices to demonstrate the requisite steps for having a working day-count convention:
-
Define a concrete day-count type derived from
bbldc::BasicDayCount
.
-
Implement the pure virtual
daysDiff
and yearsDiff
methods.
-
Instantiate and use an object of the concrete type.
- First, define the (derived)
my_DayCountConvention
class and implement its constructor inline (for convenience, directly within the derived-class definition):
class my_DayCountConvention : public bbldc::BasicDayCount {
public:
my_DayCountConvention() { }
virtual ~my_DayCountConvention();
virtual int daysDiff(const bdlt::Date& beginDate,
const bdlt::Date& endDate) const;
virtual double yearsDiff(const bdlt::Date& beginDate,
const bdlt::Date& endDate) const;
};
Then, implement the destructor. Note, however, that we always implement a virtual destructor (non-inline) in the .cpp file (to indicate the unique location of the class's virtual table):
my_DayCountConvention::~my_DayCountConvention() { }
Next, we implement the (virtual) daysDiff
and yearsDiff
methods, which incorporate the "policy" of what it means for this day-count convention to calculate day count and year fraction: int my_DayCountConvention::daysDiff(const bdlt::Date& beginDate,
const bdlt::Date& endDate) const
{
return endDate - beginDate;
}
double my_DayCountConvention::yearsDiff(const bdlt::Date& beginDate,
const bdlt::Date& endDate) const
{
return static_cast<double>((endDate - beginDate) / 365.0);
}
Then, create two bdlt::Date
variables, d1
and d2
, to use with the my_DayCountConvention
object and its day-count convention methods: Next, we obtain a bbldc::BasicDayCount
reference from an instantiated my_DayCountConvention
: Now, we compute the day count between the two dates: const int daysDiff = dcc.daysDiff(d1, d2);
assert(73 == daysDiff);
Finally, we compute the year fraction between the two dates: const double yearsDiff = dcc.yearsDiff(d1, d2);
assert(0.1999 < yearsDiff && 0.2001 > yearsDiff);