BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bbldc_basicnl365.h
Go to the documentation of this file.
1/// @file bbldc_basicnl365.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_basicnl365.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_BASICNL365
9#define INCLUDED_BBLDC_BASICNL365
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_basicnl365 bbldc_basicnl365
15/// @brief Provide stateless functions for the NL/365 convention.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_basicnl365
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_basicnl365-purpose"> Purpose</a>
25/// * <a href="#bbldc_basicnl365-classes"> Classes </a>
26/// * <a href="#bbldc_basicnl365-description"> Description </a>
27/// * <a href="#bbldc_basicnl365-usage"> Usage </a>
28/// * <a href="#bbldc_basicnl365-example-1-computing-day-count-and-year-fraction"> Example 1: Computing Day Count and Year Fraction </a>
29///
30/// # Purpose {#bbldc_basicnl365-purpose}
31/// Provide stateless functions for the NL/365 convention.
32///
33/// # Classes {#bbldc_basicnl365-classes}
34///
35/// - bbldc::BasicNl365: NL/365 convention stateless functions
36///
37/// # Description {#bbldc_basicnl365-description}
38/// This component provides a `struct`, `bbldc::BasicNl365`, that
39/// serves as a namespace for defining a suite of date-related functions used to
40/// compute the day count and the year fraction between two dates as per the
41/// NL/365 day-count convention. In this day-count convention, we simply
42/// measure the number of non-leap days occurring in a time period, and to
43/// calculate years, divide that by 365. Note that this means the number of
44/// years between January 1, 2004 and January 1, 2005 is 1.0. No end-of-month
45/// rule adjustments are made.
46///
47/// ## Usage {#bbldc_basicnl365-usage}
48///
49///
50/// This section illustrates intended use of this component.
51///
52/// ### Example 1: Computing Day Count and Year Fraction {#bbldc_basicnl365-example-1-computing-day-count-and-year-fraction}
53///
54///
55/// The following snippets of code illustrate how to use `bbldc::BasicNl365`
56/// methods. First, create four `bdlt::Date` variables:
57/// @code
58/// const bdlt::Date dA(2004, 2, 1);
59/// const bdlt::Date dB(2004, 3, 1);
60/// const bdlt::Date dC(2004, 5, 1);
61/// const bdlt::Date dD(2005, 2, 1);
62/// @endcode
63/// Then, compute the day count between some pairs of these dates:
64/// @code
65/// int daysDiff;
66/// daysDiff = bbldc::BasicNl365::daysDiff(dA, dB);
67/// assert( 28 == daysDiff);
68/// daysDiff = bbldc::BasicNl365::daysDiff(dA, dC);
69/// assert( 89 == daysDiff);
70/// daysDiff = bbldc::BasicNl365::daysDiff(dA, dD);
71/// assert(365 == daysDiff);
72/// daysDiff = bbldc::BasicNl365::daysDiff(dB, dC);
73/// assert( 61 == daysDiff);
74/// @endcode
75/// Finally, compute the year fraction between some of the dates:
76/// @code
77/// double yearsDiff;
78/// yearsDiff = bbldc::BasicNl365::yearsDiff(dA, dC);
79/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
80/// assert(yearsDiff < 0.2439 && yearsDiff > 0.2438);
81/// yearsDiff = bbldc::BasicNl365::yearsDiff(dA, dD);
82/// assert(1.0 == yearsDiff);
83/// @endcode
84/// @}
85/** @} */
86/** @} */
87
88/** @addtogroup bbl
89 * @{
90 */
91/** @addtogroup bbldc
92 * @{
93 */
94/** @addtogroup bbldc_basicnl365
95 * @{
96 */
97
98#include <bblscm_version.h>
99
100#include <bdlt_date.h>
101
102#include <bsls_platform.h>
103
104
105namespace bbldc {
106
107 // =================
108 // struct BasicNl365
109 // =================
110
111/// This `struct` provides a namespace for a suite of pure functions that
112/// compute values based on dates according to the NL/365 day-count
113/// convention.
115
116 // CLASS METHODS
117
118 /// Return the (signed) number of days between the specified `beginDate`
119 /// and `endDate` according to the NL/365 day-count convention. If
120 /// `beginDate <= endDate` then the result is non-negative. Note that
121 /// reversing the order of `beginDate` and `endDate` negates the result.
122 static int daysDiff(const bdlt::Date& beginDate,
123 const bdlt::Date& endDate);
124
125 /// Return the (signed fractional) number of years between the specified
126 /// `beginDate` and `endDate` according to the NL/365 day-count
127 /// convention. If `beginDate <= endDate` then the result is
128 /// non-negative. Note that reversing the order of `beginDate` and
129 /// `endDate` negates the result; specifically,
130 /// `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for all dates `b`
131 /// and `e`.
132 static double yearsDiff(const bdlt::Date& beginDate,
133 const bdlt::Date& endDate);
134};
135
136// ============================================================================
137// INLINE DEFINITIONS
138// ============================================================================
139
140 // -----------------
141 // struct BasicNl365
142 // -----------------
143
144// CLASS METHODS
145inline
146double BasicNl365::yearsDiff(const bdlt::Date& beginDate,
147 const bdlt::Date& endDate)
148{
149#if defined(BSLS_PLATFORM_CMP_GNU) && (BSLS_PLATFORM_CMP_VERSION >= 50301)
150 // Storing the result value in a 'volatile double' removes extra-precision
151 // available in floating-point registers.
152
153 const volatile double rv =
154#else
155 const double rv =
156#endif
157 daysDiff(beginDate, endDate) / 365.0;
158
159 return rv;
160}
161
162} // close package namespace
163
164
165#endif
166
167// ----------------------------------------------------------------------------
168// Copyright 2017 Bloomberg Finance L.P.
169//
170// Licensed under the Apache License, Version 2.0 (the "License");
171// you may not use this file except in compliance with the License.
172// You may obtain a copy of the License at
173//
174// http://www.apache.org/licenses/LICENSE-2.0
175//
176// Unless required by applicable law or agreed to in writing, software
177// distributed under the License is distributed on an "AS IS" BASIS,
178// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
179// See the License for the specific language governing permissions and
180// limitations under the License.
181// ----------------------------- END-OF-FILE ----------------------------------
182
183/** @} */
184/** @} */
185/** @} */
Definition bdlt_date.h:294
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicactual360.h:107
Definition bbldc_basicnl365.h:114
static double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate)
Definition bbldc_basicnl365.h:146
static int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate)