BDE 4.39.x 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.
127///
128/// See @ref bbldc_perioddaycountutil
130
131 private:
132 // PRIVATE CLASS METHODS
133
134 /// Return the (signed fractional) number of years between the specified
135 /// `beginDate` and `endDate` according to the specified day-count
136 /// `convention` with periods starting on the specified range
137 /// `[ periodDateBegin, periodDateEnd )` values and each period having a
138 /// duration of the specified `periodYearDiff` years (e.g., 0.25 for
139 /// quarterly periods). If `beginDate <= endDate` then the result is non-negative.
140 ///
141 /// \pre The behavior is undefined unless
142 /// `periodDateEnd - periodDateBegin >= 2`, the values contained in the
143 /// range are unique and sorted from minimum to maximum,
144 /// `min(beginDate, endDate) >= *periodDateBegin`,
145 /// `max(beginDate, endDate) <= *(periodDateEnd - 1)`, and `isSupported(convention)`.
146 ///
147 /// \note Note that reversing the order of
148 /// `beginDate` and `endDate` negates the result; specifically,
149 /// `|yearsDiff(b,e,pd,pyd,c) + yearsDiff(e,b,pd,pyd,c)| <= 1.0e-15` for
150 /// all dates `b` and `e`, periods `pd`, and year fraction per period
151 /// `pyd`.
152 static double yearsDiffImp(const bdlt::Date& beginDate,
153 const bdlt::Date& endDate,
154 const bdlt::Date *periodDateBegin,
155 const bdlt::Date *periodDateEnd,
156 double periodYearDiff,
157 DayCountConvention::Enum convention);
158
159 public:
160 // CLASS METHODS
161
162 /// Return the (signed) number of days between the specified `beginDate`
163 /// and `endDate` according to the specified day-count `convention`. If
164 /// `beginDate <= endDate` then the result is non-negative.
165 ///
166 /// \pre The behavior is undefined unless `isSupported(convention)`.
167 ///
168 /// \note Note that reversing the order of `beginDate` and `endDate` negates the result.
169 static int daysDiff(const bdlt::Date& beginDate,
170 const bdlt::Date& endDate,
171 DayCountConvention::Enum convention);
172
173 /// Return `true` if the specified `convention` is valid for use in
174 /// `daysDiff` and `yearsDiff`, and `false` otherwise.
175 static bool isSupported(DayCountConvention::Enum convention);
176
177 static double yearsDiff(const bdlt::Date& beginDate,
178 const bdlt::Date& endDate,
179 const bsl::vector<bdlt::Date>& periodDate,
180 double periodYearDiff,
181 DayCountConvention::Enum convention);
182 static double yearsDiff(const bdlt::Date& beginDate,
183 const bdlt::Date& endDate,
184 const std::vector<bdlt::Date>& periodDate,
185 double periodYearDiff,
186 DayCountConvention::Enum convention);
187#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
188 static double yearsDiff(const bdlt::Date& beginDate,
189 const bdlt::Date& endDate,
190 const std::pmr::vector<bdlt::Date>& periodDate,
191 double periodYearDiff,
192 DayCountConvention::Enum convention);
193#endif
194 // Return the (signed fractional) number of years between the specified
195 // 'beginDate' and 'endDate' according to the specified day-count
196 // 'convention' with periods starting on the specified 'periodDate'
197 // values and each period having a duration of the specified
198 // 'periodYearDiff' years (e.g., 0.25 for quarterly periods). If
199 // 'beginDate <= endDate' then the result is non-negative. The
200 // behavior is undefined unless 'periodDate.size() >= 2', the values
201 // contained in 'periodDate' are unique and sorted from minimum to
202 // maximum, 'min(beginDate, endDate) >= periodDate.front()',
203 // 'max(beginDate, endDate) <= periodDate.back()', and
204 // 'isSupported(convention)'. Note that reversing the order of
205 // 'beginDate' and 'endDate' negates the result; specifically,
206 // '|yearsDiff(b,e,pd,pyd,c) + yearsDiff(e,b,pd,pyd,c)| <= 1.0e-15' for
207 // all dates 'b' and 'e', periods 'pd', and year fraction per period
208 // 'pyd'.
209};
210
211// ============================================================================
212// INLINE DEFINITIONS
213// ============================================================================
214
215 // -------------------------
216 // struct PeriodDayCountUtil
217 // -------------------------
218
219// CLASS METHODS
220inline
222 const bdlt::Date& beginDate,
223 const bdlt::Date& endDate,
224 const bsl::vector<bdlt::Date>& periodDate,
225 double periodYearDiff,
226 DayCountConvention::Enum convention)
227{
228 return yearsDiffImp(beginDate,
229 endDate,
230 periodDate.data(),
231 periodDate.data() + periodDate.size(),
232 periodYearDiff,
233 convention);
234}
235
236inline
238 const bdlt::Date& beginDate,
239 const bdlt::Date& endDate,
240 const std::vector<bdlt::Date>& periodDate,
241 double periodYearDiff,
242 DayCountConvention::Enum convention)
243{
244 // Some implmentations of 'std::vector', notably Aix and Solaris, do not
245 // provide the 'data' accessor.
246
247 const bdlt::Date *begin = periodDate.empty() ? 0 : &*periodDate.begin();
248 const bdlt::Date *end = begin + periodDate.size();
249
250 return yearsDiffImp(beginDate,
251 endDate,
252 begin,
253 end,
254 periodYearDiff,
255 convention);
256}
257
258#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
259inline
261 const bdlt::Date& beginDate,
262 const bdlt::Date& endDate,
263 const std::pmr::vector<bdlt::Date>& periodDate,
264 double periodYearDiff,
265 DayCountConvention::Enum convention)
266{
267 // This function is defined here to avoid having to include
268 // 'memory_resource' in the .h file.
269
270 return yearsDiffImp(beginDate,
271 endDate,
272 periodDate.data(),
273 periodDate.data() + periodDate.size(),
274 periodYearDiff,
275 convention);
276}
277#endif
278
279} // close package namespace
280
281
282#endif
283
284// ----------------------------------------------------------------------------
285// Copyright 2015 Bloomberg Finance L.P.
286//
287// Licensed under the Apache License, Version 2.0 (the "License");
288// you may not use this file except in compliance with the License.
289// You may obtain a copy of the License at
290//
291// http://www.apache.org/licenses/LICENSE-2.0
292//
293// Unless required by applicable law or agreed to in writing, software
294// distributed under the License is distributed on an "AS IS" BASIS,
295// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
296// See the License for the specific language governing permissions and
297// limitations under the License.
298// ----------------------------- END-OF-FILE ----------------------------------
299
300/** @} */
301/** @} */
302/** @} */
Definition bdlt_date.h:294
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:3019
VALUE_TYPE * data() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2942
Definition bslstl_vector.h:1120
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicactual360.h:107
Enum
Definition bbldc_daycountconvention.h:134
Definition bbldc_perioddaycountutil.h:129
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:221
static int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate, DayCountConvention::Enum convention)