BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bbldc_perioddaycountutil.h
Go to the documentation of this file.
1/// @file bbldc_perioddaycountutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_perioddaycountutil.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_PERIODDAYCOUNTUTIL
9#define INCLUDED_BBLDC_PERIODDAYCOUNTUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_perioddaycountutil bbldc_perioddaycountutil
15/// @brief Support for day-count calculations of `enum`-specified conventions.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_perioddaycountutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_perioddaycountutil-purpose"> Purpose</a>
25/// * <a href="#bbldc_perioddaycountutil-classes"> Classes </a>
26/// * <a href="#bbldc_perioddaycountutil-description"> Description </a>
27/// * <a href="#bbldc_perioddaycountutil-usage"> Usage </a>
28/// * <a href="#bbldc_perioddaycountutil-example-1-computing-day-count-and-year-fraction"> Example 1: Computing Day Count and Year Fraction </a>
29///
30/// # Purpose {#bbldc_perioddaycountutil-purpose}
31/// Support for day-count calculations of `enum`-specified conventions.
32///
33/// # Classes {#bbldc_perioddaycountutil-classes}
34///
35/// - bbldc::PeriodDayCountUtil: `enum`-specified day-count calculations
36///
37/// @see bbldc_daycountconvention, bbldc_periodicmaactualactual
38///
39/// # Description {#bbldc_perioddaycountutil-description}
40/// This component provides a `struct`,
41/// `bbldc::PeriodDayCountUtil`, that defines a suite of date-related functions
42/// used to compute the day count and the year fraction between two dates as
43/// prescribed by an enumerated day-count convention. Specifically, the
44/// `daysDiff` and `yearsDiff` methods defined in `bbldc::PeriodDayCountUtil`
45/// take a trailing `DayCountConvention::Enum` argument indicating which
46/// particular period-based day-count convention to apply.
47///
48/// ## Usage {#bbldc_perioddaycountutil-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Computing Day Count and Year Fraction {#bbldc_perioddaycountutil-example-1-computing-day-count-and-year-fraction}
54///
55///
56/// The following snippets of code illustrate how to use
57/// `bbldc::PeriodDayCountUtil` methods. First, create two `bdlt::Date`
58/// variables, `d1` and `d2`:
59/// @code
60/// const bdlt::Date d1(2003, 10, 19);
61/// const bdlt::Date d2(2003, 12, 31);
62/// @endcode
63/// Then, create a schedule of period dates, `sched`, corresponding to a
64/// quarterly payment (`periodYearDiff == 0.25`):
65/// @code
66/// bsl::vector<bdlt::Date> sched;
67/// sched.push_back(bdlt::Date(2003, 10, 1));
68/// sched.push_back(bdlt::Date(2004, 1, 1));
69/// @endcode
70/// Now, compute the day count between `d1` and `d2` according to the ICMA
71/// Actual/Actual day-count convention:
72/// @code
73/// const int daysDiff = bbldc::PeriodDayCountUtil::daysDiff(
74/// d1,
75/// d2,
76/// bbldc::DayCountConvention::e_PERIOD_ICMA_ACTUAL_ACTUAL);
77/// assert(73 == daysDiff);
78/// @endcode
79/// Finally, compute the year fraction between the two dates according to the
80/// ICMA Actual/Actual day-count convention:
81/// @code
82/// const double yearsDiff = bbldc::PeriodDayCountUtil::yearsDiff(
83/// d1,
84/// d2,
85/// sched,
86/// 0.25,
87/// bbldc::DayCountConvention::e_PERIOD_ICMA_ACTUAL_ACTUAL);
88/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
89/// assert(yearsDiff > 0.1983 && yearsDiff < 0.1985);
90/// @endcode
91/// @}
92/** @} */
93/** @} */
94
95/** @addtogroup bbl
96 * @{
97 */
98/** @addtogroup bbldc
99 * @{
100 */
101/** @addtogroup bbldc_perioddaycountutil
102 * @{
103 */
104
105#include <bblscm_version.h>
106
108
109#include <bdlt_date.h>
110
111#include <bsls_libraryfeatures.h>
112
113#include <bsl_vector.h>
114
115#include <vector>
116
117
118namespace bbldc {
119
120 // =========================
121 // struct PeriodDayCountUtil
122 // =========================
123
124/// This `struct` provides a namespace for a suite of pure functions that
125/// compute values based on dates according to enumerated day-count
126/// conventions.
128
129 private:
130 // PRIVATE CLASS METHODS
131
132 /// Return the (signed fractional) number of years between the specified
133 /// `beginDate` and `endDate` according to the specified day-count
134 /// `convention` with periods starting on the specified range
135 /// `[ periodDateBegin, periodDateEnd )` values and each period having a
136 /// duration of the specified `periodYearDiff` years (e.g., 0.25 for
137 /// quarterly periods). If `beginDate <= endDate` then the result is
138 /// non-negative. The behavior is undefined unless
139 /// `periodDateEnd - periodDateBegin >= 2`, the values contained in the
140 /// range are unique and sorted from minimum to maximum,
141 /// `min(beginDate, endDate) >= *periodDateBegin`,
142 /// `max(beginDate, endDate) <= *(periodDateEnd - 1)`, and
143 /// `isSupported(convention)`. Note that reversing the order of
144 /// `beginDate` and `endDate` negates the result; specifically,
145 /// `|yearsDiff(b,e,pd,pyd,c) + yearsDiff(e,b,pd,pyd,c)| <= 1.0e-15` for
146 /// all dates `b` and `e`, periods `pd`, and year fraction per period
147 /// `pyd`.
148 static double yearsDiffImp(const bdlt::Date& beginDate,
149 const bdlt::Date& endDate,
150 const bdlt::Date *periodDateBegin,
151 const bdlt::Date *periodDateEnd,
152 double periodYearDiff,
153 DayCountConvention::Enum convention);
154
155 public:
156 // CLASS METHODS
157
158 /// Return the (signed) number of days between the specified `beginDate`
159 /// and `endDate` according to the specified day-count `convention`. If
160 /// `beginDate <= endDate` then the result is non-negative. The
161 /// behavior is undefined unless `isSupported(convention)`. Note that
162 /// reversing the order of `beginDate` and `endDate` negates the result.
163 static int daysDiff(const bdlt::Date& beginDate,
164 const bdlt::Date& endDate,
165 DayCountConvention::Enum convention);
166
167 static bool isSupported(DayCountConvention::Enum convention);
168
169 static double yearsDiff(const bdlt::Date& beginDate,
170 const bdlt::Date& endDate,
171 const bsl::vector<bdlt::Date>& periodDate,
172 double periodYearDiff,
173 DayCountConvention::Enum convention);
174 /// Return `true` if the specified `convention` is valid for use in
175 /// `daysDiff` and `yearsDiff`, and `false` otherwise.
176 static double yearsDiff(const bdlt::Date& beginDate,
177 const bdlt::Date& endDate,
178 const std::vector<bdlt::Date>& periodDate,
179 double periodYearDiff,
180 DayCountConvention::Enum convention);
181#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
182 static double yearsDiff(const bdlt::Date& beginDate,
183 const bdlt::Date& endDate,
184 const std::pmr::vector<bdlt::Date>& periodDate,
185 double periodYearDiff,
186 DayCountConvention::Enum convention);
187#endif
188 // Return the (signed fractional) number of years between the specified
189 // 'beginDate' and 'endDate' according to the specified day-count
190 // 'convention' with periods starting on the specified 'periodDate'
191 // values and each period having a duration of the specified
192 // 'periodYearDiff' years (e.g., 0.25 for quarterly periods). If
193 // 'beginDate <= endDate' then the result is non-negative. The
194 // behavior is undefined unless 'periodDate.size() >= 2', the values
195 // contained in 'periodDate' are unique and sorted from minimum to
196 // maximum, 'min(beginDate, endDate) >= periodDate.front()',
197 // 'max(beginDate, endDate) <= periodDate.back()', and
198 // 'isSupported(convention)'. Note that reversing the order of
199 // 'beginDate' and 'endDate' negates the result; specifically,
200 // '|yearsDiff(b,e,pd,pyd,c) + yearsDiff(e,b,pd,pyd,c)| <= 1.0e-15' for
201 // all dates 'b' and 'e', periods 'pd', and year fraction per period
202 // 'pyd'.
203};
204
205// ============================================================================
206// INLINE DEFINITIONS
207// ============================================================================
208
209 // -------------------------
210 // struct PeriodDayCountUtil
211 // -------------------------
212
213// CLASS METHODS
214inline
216 const bdlt::Date& beginDate,
217 const bdlt::Date& endDate,
218 const bsl::vector<bdlt::Date>& periodDate,
219 double periodYearDiff,
220 DayCountConvention::Enum convention)
221{
222 return yearsDiffImp(beginDate,
223 endDate,
224 periodDate.data(),
225 periodDate.data() + periodDate.size(),
226 periodYearDiff,
227 convention);
228}
229
230inline
232 const bdlt::Date& beginDate,
233 const bdlt::Date& endDate,
234 const std::vector<bdlt::Date>& periodDate,
235 double periodYearDiff,
236 DayCountConvention::Enum convention)
237{
238 // Some implmentations of 'std::vector', notably Aix and Solaris, do not
239 // provide the 'data' accessor.
240
241 const bdlt::Date *begin = periodDate.empty() ? 0 : &*periodDate.begin();
242 const bdlt::Date *end = begin + periodDate.size();
243
244 return yearsDiffImp(beginDate,
245 endDate,
246 begin,
247 end,
248 periodYearDiff,
249 convention);
250}
251
252#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
253inline
255 const bdlt::Date& beginDate,
256 const bdlt::Date& endDate,
257 const std::pmr::vector<bdlt::Date>& periodDate,
258 double periodYearDiff,
259 DayCountConvention::Enum convention)
260{
261 // This function is defined here to avoid having to include
262 // 'memory_resource' in the .h file.
263
264 return yearsDiffImp(beginDate,
265 endDate,
266 periodDate.data(),
267 periodDate.data() + periodDate.size(),
268 periodYearDiff,
269 convention);
270}
271#endif
272
273} // close package namespace
274
275
276#endif
277
278// ----------------------------------------------------------------------------
279// Copyright 2015 Bloomberg Finance L.P.
280//
281// Licensed under the Apache License, Version 2.0 (the "License");
282// you may not use this file except in compliance with the License.
283// You may obtain a copy of the License at
284//
285// http://www.apache.org/licenses/LICENSE-2.0
286//
287// Unless required by applicable law or agreed to in writing, software
288// distributed under the License is distributed on an "AS IS" BASIS,
289// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
290// See the License for the specific language governing permissions and
291// limitations under the License.
292// ----------------------------- END-OF-FILE ----------------------------------
293
294/** @} */
295/** @} */
296/** @} */
Definition bdlt_date.h:294
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:2664
VALUE_TYPE * data() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2587
Definition bslstl_vector.h:1025
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicactual360.h:107
Enum
Definition bbldc_daycountconvention.h:131
Definition bbldc_perioddaycountutil.h:127
static bool isSupported(DayCountConvention::Enum convention)
static double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate, const bsl::vector< bdlt::Date > &periodDate, double periodYearDiff, DayCountConvention::Enum convention)
Definition bbldc_perioddaycountutil.h:215
static int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate, DayCountConvention::Enum convention)