BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bbldc_basicdaterangedaycountadapter.h
Go to the documentation of this file.
1/// @file bbldc_basicdaterangedaycountadapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_basicdaterangedaycountadapter.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_BASICDATERANGEDAYCOUNTADAPTER
9#define INCLUDED_BBLDC_BASICDATERANGEDAYCOUNTADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_basicdaterangedaycountadapter bbldc_basicdaterangedaycountadapter
15/// @brief Provide a parameterized day-count convention implementation.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_basicdaterangedaycountadapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_basicdaterangedaycountadapter-purpose"> Purpose</a>
25/// * <a href="#bbldc_basicdaterangedaycountadapter-classes"> Classes </a>
26/// * <a href="#bbldc_basicdaterangedaycountadapter-description"> Description </a>
27/// * <a href="#bbldc_basicdaterangedaycountadapter-usage"> Usage </a>
28/// * <a href="#bbldc_basicdaterangedaycountadapter-example-1-adapting-bbldc-basicisma30360"> Example 1: Adapting bbldc::BasicIsma30360 </a>
29///
30/// # Purpose {#bbldc_basicdaterangedaycountadapter-purpose}
31/// Provide a parameterized day-count convention implementation.
32///
33/// # Classes {#bbldc_basicdaterangedaycountadapter-classes}
34///
35/// - bbldc::BasicDateRangeDayCountAdapter: `bbldc::DateRangeDayCount` adapter
36///
37/// # Description {#bbldc_basicdaterangedaycountadapter-description}
38/// This component provides a parameterized (template)
39/// implementation, `bbldc::BasicDateRangeDayCountAdapter`, of the
40/// `bbldc::DateRangeDayCount` protocol. The template argument can be any type
41/// supporting the following two class methods.
42/// @code
43/// int daysDiff(const bdlt::Date& beginDate, const bdlt::Date& endDate);
44///
45/// double yearsDiff(const bdlt::Date& beginDate, const bdlt::Date& endDate);
46/// @endcode
47/// The template class `bbldc::BasicDateRangeDayCountAdapter` provides
48/// convenient support for run-time polymorphic choice of day-count conventions
49/// (via conventional use of a base-class pointer or reference) without having
50/// to implement each derived type explicitly. In this sense,
51/// `bbldc::BasicDateRangeDayCountAdapter` adapts the various concrete "basic"
52/// day-count convention classes (e.g., `bbldc::BasicIsma30360`) to a run-time
53/// binding mechanism.
54///
55/// The `bbldc::DateRangeDayCount` protocol requires two methods, `firstDate`
56/// and `lastDate`, that define a date range for which calculations are valid,
57/// to reflect the valid range of, say, a calendar required for the
58/// computations. For "basic" day-count implementations, the valid date range
59/// is identical to the range of `bdlt::Date`.
60///
61/// ## Usage {#bbldc_basicdaterangedaycountadapter-usage}
62///
63///
64/// This section illustrates intended use of this component.
65///
66/// ### Example 1: Adapting bbldc::BasicIsma30360 {#bbldc_basicdaterangedaycountadapter-example-1-adapting-bbldc-basicisma30360}
67///
68///
69/// This example shows the procedure for using
70/// `bbldc::BasicDateRangeDayCountAdapter` to adapt the `bbldc::BasicIsma30360`
71/// day-count convention to the `bbldc::DateRangeDayCount` protocol, and then
72/// the use of the day-count methods. First, we define an instance of the
73/// adapted day-count convention and obtain a reference to the
74/// `bbldc::DateRangeDayCount`:
75/// @code
76/// const bbldc::BasicDateRangeDayCountAdapter<bbldc::BasicIsma30360> myDcc =
77/// bbldc::BasicDateRangeDayCountAdapter<bbldc::BasicIsma30360>();
78///
79/// const bbldc::DateRangeDayCount& dcc = myDcc;
80/// @endcode
81/// Then, create two `bdlt::Date` variables, `d1` and `d2`, with which to use
82/// the day-count convention methods:
83/// @code
84/// const bdlt::Date d1(2003, 10, 18);
85/// const bdlt::Date d2(2003, 12, 31);
86/// @endcode
87/// Now, use the base-class reference to compute the day count between the two
88/// dates:
89/// @code
90/// const int daysDiff = dcc.daysDiff(d1, d2);
91/// assert(72 == daysDiff);
92/// @endcode
93/// Finally, use the base-class reference to compute the year fraction between
94/// the two dates:
95/// @code
96/// const double yearsDiff = dcc.yearsDiff(d1, d2);
97/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
98/// assert(0.1999 < yearsDiff && 0.2001 > yearsDiff);
99/// @endcode
100/// @}
101/** @} */
102/** @} */
103
104/** @addtogroup bbl
105 * @{
106 */
107/** @addtogroup bbldc
108 * @{
109 */
110/** @addtogroup bbldc_basicdaterangedaycountadapter
111 * @{
112 */
113
114#include <bblscm_version.h>
115
117
118#include <bdlt_date.h>
119
120#include <bsls_keyword.h>
121
122
123namespace bbldc {
124
125 // ===================================
126 // class BasicDateRangeDayCountAdapter
127 // ===================================
128
129/// This `class` provides an "adapter" from the specified `CONVENTION` to
130/// the `bbldc::DateRangeDayCount` protocol that can be used for determining
131/// values based on dates according to the day-count `CONVENTION`.
132///
133/// See @ref bbldc_basicdaterangedaycountadapter
134template <class CONVENTION>
136
137 public:
138 // ACCESSORS
139 int daysDiff(const bdlt::Date& beginDate, const bdlt::Date& endDate) const
141 // Return the (signed) number of days between the specified 'beginDate'
142 // and 'endDate' as per the 'CONVENTION' template policy. If
143 // 'beginDate <= endDate', then the result is non-negative. Note that
144 // reversing the order of 'beginDate' and 'endDate' negates the result.
145
146 /// Return a reference providing non-modifiable access to a `bdlt::Date` with the value 1/1/1.
147 ///
148 /// \note Note that this value is the earliest date in
149 /// the valid range of the adapted day-count convention.
151
152 /// Return a reference providing non-modifiable access to a `bdlt::Date` with the value 9999/12/31.
153 ///
154 /// \note Note that this value is the latest date
155 /// in the valid range of the adapted day-count convention.
157
158 /// Return the (signed fractional) number of years between the specified
159 /// `beginDate` and `endDate` as per the `CONVENTION` template policy.
160 /// If `beginDate <= endDate`, then the result is non-negative.
161 ///
162 /// \note Note that reversing the order of `beginDate` and `endDate` negates the
163 /// result; specifically,
164 /// `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for all dates `b`
165 /// and `e`.
166 double yearsDiff(const bdlt::Date& beginDate,
167 const bdlt::Date& endDate) const BSLS_KEYWORD_OVERRIDE;
168};
169
170// ============================================================================
171// INLINE DEFINITIONS
172// ============================================================================
173
174 // -----------------------------------
175 // class BasicDateRangeDayCountAdapter
176 // -----------------------------------
177
178// ACCESSORS
179template <class CONVENTION>
180inline
182 const bdlt::Date& beginDate,
183 const bdlt::Date& endDate) const
184{
185 return CONVENTION::daysDiff(beginDate, endDate);
186}
187
188template <class CONVENTION>
189inline
191{
192 static bdlt::Date firstDate(1, 1, 1);
193 return firstDate;
194}
195
196template <class CONVENTION>
197inline
199{
200 static bdlt::Date lastDate(9999, 12, 31);
201 return lastDate;
202}
203
204template <class CONVENTION>
205inline
207 const bdlt::Date& beginDate,
208 const bdlt::Date& endDate) const
209{
210 return CONVENTION::yearsDiff(beginDate, endDate);
211}
212
213} // close package namespace
214
215
216#endif
217
218// ----------------------------------------------------------------------------
219// Copyright 2015 Bloomberg Finance L.P.
220//
221// Licensed under the Apache License, Version 2.0 (the "License");
222// you may not use this file except in compliance with the License.
223// You may obtain a copy of the License at
224//
225// http://www.apache.org/licenses/LICENSE-2.0
226//
227// Unless required by applicable law or agreed to in writing, software
228// distributed under the License is distributed on an "AS IS" BASIS,
229// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
230// See the License for the specific language governing permissions and
231// limitations under the License.
232// ----------------------------- END-OF-FILE ----------------------------------
233
234/** @} */
235/** @} */
236/** @} */
Definition bbldc_basicdaterangedaycountadapter.h:135
const bdlt::Date & lastDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_basicdaterangedaycountadapter.h:198
int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_basicdaterangedaycountadapter.h:181
double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_basicdaterangedaycountadapter.h:206
const bdlt::Date & firstDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_basicdaterangedaycountadapter.h:190
Definition bbldc_daterangedaycount.h:191
Definition bdlt_date.h:294
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:695
Definition bbldc_basicactual360.h:107