BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bbldc_calendarbus252.h
Go to the documentation of this file.
1/// @file bbldc_calendarbus252.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_calendarbus252.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_CALENDARBUS252
9#define INCLUDED_BBLDC_CALENDARBUS252
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_calendarbus252 bbldc_calendarbus252
15/// @brief Provide stateless functions for calendar-based BUS-252.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_calendarbus252
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_calendarbus252-purpose"> Purpose</a>
25/// * <a href="#bbldc_calendarbus252-classes"> Classes </a>
26/// * <a href="#bbldc_calendarbus252-description"> Description </a>
27/// * <a href="#bbldc_calendarbus252-usage"> Usage </a>
28/// * <a href="#bbldc_calendarbus252-example-1-computing-day-count-and-year-fraction"> Example 1: Computing Day Count and Year Fraction </a>
29///
30/// # Purpose {#bbldc_calendarbus252-purpose}
31/// Provide stateless functions for calendar-based BUS-252.
32///
33/// # Classes {#bbldc_calendarbus252-classes}
34///
35/// - bbldc::CalendarBus252: BUS-252 convention stateless functions
36///
37/// # Description {#bbldc_calendarbus252-description}
38/// This component provides a `struct`, `bbldc::CalendarBus252`,
39/// that serves as a namespace for defining a suite of date-related functions
40/// used to compute the day count and the year fraction between two dates as per
41/// the BUS-252 day-count convention. In this day-count convention, the day
42/// count between two ordered dates, `beginDate` and `endDate` where
43/// `beginDate < endDate`, is exactly the number of *business* days occurring in
44/// the time period `[beginDate .. endDate)`. Reversing the order of the dates
45/// negates the result. When the two dates have the same value, the day count
46/// is 0. The year fraction is the day count divided by 252.
47///
48/// ## Usage {#bbldc_calendarbus252-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Computing Day Count and Year Fraction {#bbldc_calendarbus252-example-1-computing-day-count-and-year-fraction}
54///
55///
56/// The following snippets of code illustrate how to use `bbldc::CalendarBus252`
57/// methods. First, create two `bdlt::Date` variables, `d1` and `d2`:
58/// @code
59/// const bdlt::Date d1(2003, 10, 19);
60/// const bdlt::Date d2(2003, 12, 31);
61/// @endcode
62/// Then, create a `calendar` with a valid range spanning 2003 and typical
63/// weekend days:
64/// @code
65/// bdlt::Calendar calendar;
66/// calendar.setValidRange(bdlt::Date(2003, 1, 1), bdlt::Date(2003, 12, 31));
67/// calendar.addWeekendDay(bdlt::DayOfWeek::e_SUN);
68/// calendar.addWeekendDay(bdlt::DayOfWeek::e_SAT);
69/// @endcode
70/// Next, compute the day count between `d1` and `d2`:
71/// @code
72/// const int daysDiff = bbldc::CalendarBus252::daysDiff(d1, d2, calendar);
73/// assert(52 == daysDiff);
74/// @endcode
75/// Finally, compute the year fraction between the two dates:
76/// @code
77/// const double yearsDiff = bbldc::CalendarBus252::yearsDiff(d1,
78/// d2,
79/// calendar);
80/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
81/// assert(yearsDiff > 0.2063 && yearsDiff < 0.2064);
82/// @endcode
83/// @}
84/** @} */
85/** @} */
86
87/** @addtogroup bbl
88 * @{
89 */
90/** @addtogroup bbldc
91 * @{
92 */
93/** @addtogroup bbldc_calendarbus252
94 * @{
95 */
96
97#include <bblscm_version.h>
98
99#include <bdlt_calendar.h>
100#include <bdlt_date.h>
101
102#include <bsls_assert.h>
103#include <bsls_platform.h>
104
105
106namespace bbldc {
107
108 // =====================
109 // struct CalendarBus252
110 // =====================
111
112/// This `struct` provides a namespace for a suite of pure functions that
113/// compute values based on dates according to the BUS-252 day-count
114/// convention.
116
117 // CLASS METHODS
118
119 /// Return the (signed) number of days between the specified `beginDate`
120 /// and `endDate` according to the BUS-252 day-count convention with the
121 /// specified `calendar` providing the definition of business days. If
122 /// `beginDate <= endDate`, then the result is non-negative. The
123 /// behavior is undefined unless `calendar.isInRange(beginDate)` and
124 /// `calendar.isInRange(endDate)`. Note that reversing the order of
125 /// `beginDate` and `endDate` negates the result and that the result is
126 /// 0 when `beginDate == endDate`.
127 static int daysDiff(const bdlt::Date& beginDate,
128 const bdlt::Date& endDate,
129 const bdlt::Calendar& calendar);
130
131 /// Return the (signed fractional) number of years between the specified
132 /// `beginDate` and `endDate` according to the BUS-252 day-count
133 /// convention with the specified `calendar` providing the definition of
134 /// business days. If `beginDate <= endDate`, then the result is
135 /// non-negative. The behavior is undefined unless
136 /// `calendar.isInRange(beginDate)` and `calendar.isInRange(endDate)`.
137 /// Note that reversing the order of `beginDate` and `endDate` negates
138 /// the result; specifically,
139 /// `|yearsDiff(b, e, c) + yearsDiff(e, b, c)| <= 1.0e-15` for all
140 /// calendars `c` and valid dates `b` and `e`.
141 static double yearsDiff(const bdlt::Date& beginDate,
142 const bdlt::Date& endDate,
143 const bdlt::Calendar& calendar);
144};
145
146// ============================================================================
147// INLINE DEFINITIONS
148// ============================================================================
149
150 // ---------------------
151 // struct CalendarBus252
152 // ---------------------
153
154// CLASS METHODS
155inline
157 const bdlt::Date& endDate,
158 const bdlt::Calendar& calendar)
159{
160 BSLS_ASSERT_SAFE(calendar.isInRange(beginDate));
161 BSLS_ASSERT_SAFE(calendar.isInRange(endDate));
162
163 if (beginDate < endDate) {
164 return calendar.numBusinessDays(beginDate, endDate - 1); // RETURN
165 }
166 else if (beginDate > endDate) {
167 return -calendar.numBusinessDays(endDate, beginDate - 1); // RETURN
168 }
169 return 0;
170}
171
172inline
173double CalendarBus252::yearsDiff(const bdlt::Date& beginDate,
174 const bdlt::Date& endDate,
175 const bdlt::Calendar& calendar)
176{
177#if defined(BSLS_PLATFORM_CMP_GNU) && (BSLS_PLATFORM_CMP_VERSION >= 50301)
178 // Storing the result value in a 'volatile double' removes extra-precision
179 // available in floating-point registers.
180
181 const volatile double rv =
182#else
183 const double rv =
184#endif
185 static_cast<double>(daysDiff(beginDate, endDate, calendar)) / 252.0;
186
187 return rv;
188}
189
190} // close package namespace
191
192
193#endif
194
195// ----------------------------------------------------------------------------
196// Copyright 2017 Bloomberg Finance L.P.
197//
198// Licensed under the Apache License, Version 2.0 (the "License");
199// you may not use this file except in compliance with the License.
200// You may obtain a copy of the License at
201//
202// http://www.apache.org/licenses/LICENSE-2.0
203//
204// Unless required by applicable law or agreed to in writing, software
205// distributed under the License is distributed on an "AS IS" BASIS,
206// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
207// See the License for the specific language governing permissions and
208// limitations under the License.
209// ----------------------------- END-OF-FILE ----------------------------------
210
211/** @} */
212/** @} */
213/** @} */
Definition bdlt_calendar.h:569
bool isInRange(const Date &date) const
Definition bdlt_calendar.h:1824
int numBusinessDays() const
Definition bdlt_calendar.h:1864
Definition bdlt_date.h:294
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicactual360.h:107
Definition bbldc_calendarbus252.h:115
static double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate, const bdlt::Calendar &calendar)
Definition bbldc_calendarbus252.h:173
static int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate, const bdlt::Calendar &calendar)
Definition bbldc_calendarbus252.h:156