BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bblb_schedulegenerationutil.h
Go to the documentation of this file.
1/// @file bblb_schedulegenerationutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bblb_schedulegenerationutil.h -*-C++-*-
8#ifndef INCLUDED_BBLB_SCHEDULEGENERATIONUTIL
9#define INCLUDED_BBLB_SCHEDULEGENERATIONUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bblb_schedulegenerationutil bblb_schedulegenerationutil
15/// @brief Provide functions for generating schedules of dates.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bblb
19/// @{
20/// @addtogroup bblb_schedulegenerationutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bblb_schedulegenerationutil-purpose"> Purpose</a>
25/// * <a href="#bblb_schedulegenerationutil-classes"> Classes </a>
26/// * <a href="#bblb_schedulegenerationutil-description"> Description </a>
27/// * <a href="#bblb_schedulegenerationutil-usage"> Usage </a>
28/// * <a href="#bblb_schedulegenerationutil-example-1-generating-a-schedule"> Example 1: Generating a Schedule </a>
29///
30/// # Purpose {#bblb_schedulegenerationutil-purpose}
31/// Provide functions for generating schedules of dates.
32///
33/// # Classes {#bblb_schedulegenerationutil-classes}
34///
35/// - bblb::ScheduleGenerationUtil: namespace for schedule generation functions
36///
37/// @see
38///
39/// # Description {#bblb_schedulegenerationutil-description}
40/// This component provides a `struct`,
41/// `bblb::ScheduleGenerationUtil`, that serves as a namespace for functions
42/// that generate a schedule; a set of dates limited to within a closed-interval
43/// date-range, represented by the specified `earliest` and `latest` dates.
44/// Typically, a schedule generation method can be defined by an algorithm
45/// specific to the method, an `example` date, a closed-interval represented by
46/// an `earliest` and a `latest` date, and any other information required to
47/// determine the interval between successive dates in a schedule (for example,
48/// the number of days or months between successive dates). The process of
49/// computing the dates within a schedule is exemplified using the following
50/// diagram:
51/// @code
52/// ___________|__________________|___________________|________
53/// 'example' 'earliest' 'latest'
54/// .___.___.___.___.___|___|___|___|___|___.
55/// ^ ^ ^ ^ ^
56/// 'interval':.___. | | | | |
57/// 'schedule': [d0, d1, d2, d3, d4]
58/// @endcode
59/// The schedule generated in the above diagram is `[d0, d1, d2, d3, d4]`.
60/// Notice that the `example` date does not have to reside within the closed
61/// interval (the `example` date is before the `earliest` date in this diagram).
62///
63/// More formally, the resulting `schedule` is the subset of the infinite series
64/// of dates, defined by all dates separated by an integral multiple of
65/// intervals from the `example` date, that reside within the closed-interval
66/// specified by `earliest` and `latest`.
67///
68/// The following section provides a synopsis of the main functions provided in
69/// this component:
70/// @code
71/// 'generateFromDayInterval' Generate a schedule having an
72/// interval of a fixed number of days.
73///
74/// 'generateFromDayOfMonth' Generate a schedule having an
75/// interval of a fixed number of
76/// months, with each date in the
77/// schedule occuring on a specific day
78/// of the month.
79///
80/// 'generateFromBusinessDayOfMonth' Generate a schedule having an
81/// interval of a fixed number of
82/// months, with each date in the
83/// schedule occuring on a specific
84/// business day of the month.
85///
86/// 'generateFromDayOfWeekAfterDayOfMonth' Generate a schedule having an
87/// interval of a fixed number of
88/// months, with each date in the
89/// schedule occuring on a specific day
90/// of the week on or after a specific
91/// day of the month.
92///
93/// 'generateFromDayOfWeekInMonth' Generate a schedule having an
94/// interval of a fixed number of
95/// months, with each date in the
96/// schedule occuring on a specific day
97/// of the week in a specific week of
98/// the month.
99/// @endcode
100///
101/// ## Usage {#bblb_schedulegenerationutil-usage}
102///
103///
104/// This section illustrates intended use of this component.
105///
106/// ### Example 1: Generating a Schedule {#bblb_schedulegenerationutil-example-1-generating-a-schedule}
107///
108///
109/// Suppose that we want to determine the sequence of dates that are:
110/// * integral multiples of 9 months away from July 2007,
111/// * on the 23rd day of the month,
112/// * and within the closed interval `[02/01/2012, 02/28/2015]`.
113///
114/// First, we define the inputs and output to the schedule generation function:
115/// @code
116/// bdlt::Date earliest(2012, 2, 1);
117/// bdlt::Date latest(2015, 2, 28);
118/// bdlt::Date example(2007, 7, 23);
119///
120/// bsl::vector<bdlt::Date> schedule;
121/// @endcode
122/// Now, we invoke the `generateFromDayOfMonth` routine to obtain the subset of
123/// dates:
124/// @code
125/// bblb::ScheduleGenerationUtil::generateFromDayOfMonth(
126/// &schedule,
127/// earliest,
128/// latest,
129/// example.year(),
130/// example.month(),
131/// 9, // 'intervalInMonths'
132/// 23); // 'targetDayOfMonth'
133/// @endcode
134/// Finally, we assert that the generated schedule is what we expect:
135/// @code
136/// assert(4 == schedule.size());
137/// assert(bdlt::Date(2012, 10, 23) == schedule[0]);
138/// assert(bdlt::Date(2013, 7, 23) == schedule[1]);
139/// assert(bdlt::Date(2014, 4, 23) == schedule[2]);
140/// assert(bdlt::Date(2015, 1, 23) == schedule[3]);
141/// @endcode
142/// @}
143/** @} */
144/** @} */
145
146/** @addtogroup bbl
147 * @{
148 */
149/** @addtogroup bblb
150 * @{
151 */
152/** @addtogroup bblb_schedulegenerationutil
153 * @{
154 */
155
156#include <bblscm_version.h>
157
158#include <bdlt_calendar.h>
159#include <bdlt_date.h>
160#include <bdlt_dayofweek.h>
161
162#include <bsls_libraryfeatures.h>
163
164#include <bsl_vector.h>
165
166#include <vector> // 'std::vector', 'std::pmr::vector'
167
168
169namespace bblb {
170
171 // =============================
172 // struct ScheduleGenerationUtil
173 // =============================
174
175/// This `struct` provides a namespace for utility functions that generate
176/// schedules.
177///
178/// See @ref bblb_schedulegenerationutil
180
181 // CLASS METHODS
182
183 /// Load, into the specified `schedule`, the chronologically increasing
184 /// sequence of unique dates that are integral multiples of the
185 /// specified `intervalInDays` away from the specified `example` date,
186 /// and within the specified closed-interval `[earliest, latest]`.
187 ///
188 /// \pre The behavior is undefined unless `earliest <= latest` and
189 /// `1 <= intervalInDays`.
191 bsl::vector<bdlt::Date> *schedule,
192 const bdlt::Date& earliest,
193 const bdlt::Date& latest,
194 const bdlt::Date& example,
195 int intervalInDays);
197 std::vector<bdlt::Date> *schedule,
198 const bdlt::Date& earliest,
199 const bdlt::Date& latest,
200 const bdlt::Date& example,
201 int intervalInDays);
202#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
203 static void generateFromDayInterval(
204 std::pmr::vector<bdlt::Date> *schedule,
205 const bdlt::Date& earliest,
206 const bdlt::Date& latest,
207 const bdlt::Date& example,
208 int intervalInDays);
209#endif
210
211 /// Load, into the specified `schedule`, the chronologically increasing
212 /// sequence of unique dates that are on the specified
213 /// `targetDayOfMonth` (or the last day of the month if
214 /// `targetDayOfMonth` would be past the end of the month), integral
215 /// multiples of the specified `intervalInMonths` away from the
216 /// specified `exampleYear` and `exampleMonth`, and within the specified
217 /// closed-interval `[earliest, latest]`. Optionally specify
218 /// `targetDayOfFeb` to replace `targetDayOfMonth` whenever the month of a `schedule` entry is February.
219 ///
220 /// \pre The behavior is undefined unless
221 /// `earliest <= latest`, `1 <= exampleYear <= 9999`,
222 /// `1 <= exampleMonth <= 12`, `1 <= intervalInMonths`,
223 /// `1 <= targetDayOfMonth <= 31`, and `0 <= targetDayOfFeb <= 29`.
225 bsl::vector<bdlt::Date> *schedule,
226 const bdlt::Date& earliest,
227 const bdlt::Date& latest,
228 int exampleYear,
229 int exampleMonth,
230 int intervalInMonths,
231 int targetDayOfMonth,
232 int targetDayOfFeb = 0);
234 std::vector<bdlt::Date> *schedule,
235 const bdlt::Date& earliest,
236 const bdlt::Date& latest,
237 int exampleYear,
238 int exampleMonth,
239 int intervalInMonths,
240 int targetDayOfMonth,
241 int targetDayOfFeb = 0);
242#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
243 static void generateFromDayOfMonth(
244 std::pmr::vector<bdlt::Date> *schedule,
245 const bdlt::Date& earliest,
246 const bdlt::Date& latest,
247 int exampleYear,
248 int exampleMonth,
249 int intervalInMonths,
250 int targetDayOfMonth,
251 int targetDayOfFeb = 0);
252#endif
253
254 /// Load, into the specified `schedule`, the chronologically increasing
255 /// sequence of unique dates that are on the specified
256 /// `targetBusinessDayOfMonth` (or the highest count possible in the
257 /// resulting month), integral multiples of the specified
258 /// `intervalInMonths` away from the specified `exampleYear` and
259 /// `exampleMonth`, and within the specified closed-interval
260 /// `[earliest, latest]`. Business days, as per the specified
261 /// `calendar`, are counted, if `targetBusinessDayOfMonth` is positive,
262 /// from and including the chronologically earliest business day to
263 /// chronologically later business days, and if
264 /// `targetBusinessDayOfMonth` is negative, from and including the
265 /// chronologically latest business day to chronologically earlier
266 /// business days. If any of the months required for the schedule do
267 /// not have a business day, return an empty `schedule`.
268 ///
269 /// \pre The behavior is undefined unless `earliest <= latest`,
270 /// `1 <= exampleYear <= 9999`, `1 <= exampleMonth <= 12`,
271 /// `1 <= intervalInMonths`, and
272 /// `1 <= abs(targetBusinessDayOfMonth) <= 31`.
274 bsl::vector<bdlt::Date> *schedule,
275 const bdlt::Date& earliest,
276 const bdlt::Date& latest,
277 int exampleYear,
278 int exampleMonth,
279 int intervalInMonths,
280 const bdlt::Calendar& calendar,
281 int targetBusinessDayOfMonth);
283 std::vector<bdlt::Date> *schedule,
284 const bdlt::Date& earliest,
285 const bdlt::Date& latest,
286 int exampleYear,
287 int exampleMonth,
288 int intervalInMonths,
289 const bdlt::Calendar& calendar,
290 int targetBusinessDayOfMonth);
291#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
293 std::pmr::vector<bdlt::Date> *schedule,
294 const bdlt::Date& earliest,
295 const bdlt::Date& latest,
296 int exampleYear,
297 int exampleMonth,
298 int intervalInMonths,
299 const bdlt::Calendar& calendar,
300 int targetBusinessDayOfMonth);
301#endif
302
303 /// Load, into the specified `schedule`, the chronologically increasing
304 /// sequence of unique dates that are on the specified `dayOfWeek` on or
305 /// after the specified `dayOfMonth`, integral multiples of the
306 /// specified `intervalInMonths` away from the specified `exampleYear`
307 /// and `exampleMonth`, and within the specified closed-interval
308 /// `[earliest, latest]`. If any of the months required for the
309 /// schedule have fewer than `dayOfMonth` days, return an empty `schedule`.
310 ///
311 /// \pre The behavior is undefined unless `earliest <= latest`,
312 /// `1 <= exampleYear <= 9999`, `1 <= exampleMonth <= 12`,
313 /// `1 <= intervalInMonths`, and `1 <= dayOfMonth <= 31`.
315 bsl::vector<bdlt::Date> *schedule,
316 const bdlt::Date& earliest,
317 const bdlt::Date& latest,
318 int exampleYear,
319 int exampleMonth,
320 int intervalInMonths,
321 bdlt::DayOfWeek::Enum dayOfWeek,
322 int dayOfMonth);
324 std::vector<bdlt::Date> *schedule,
325 const bdlt::Date& earliest,
326 const bdlt::Date& latest,
327 int exampleYear,
328 int exampleMonth,
329 int intervalInMonths,
330 bdlt::DayOfWeek::Enum dayOfWeek,
331 int dayOfMonth);
332#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
334 std::pmr::vector<bdlt::Date> *schedule,
335 const bdlt::Date& earliest,
336 const bdlt::Date& latest,
337 int exampleYear,
338 int exampleMonth,
339 int intervalInMonths,
340 bdlt::DayOfWeek::Enum dayOfWeek,
341 int dayOfMonth);
342#endif
343
344 /// Load, into the specified `schedule`, the chronologically increasing
345 /// sequence of unique dates that are on the specified `dayOfWeek` of
346 /// the specified `occurrenceWeek` of the month, integral multiples of
347 /// the specified `intervalInMonths` away from the specified
348 /// `exampleYear` and `exampleMonth`, and within the specified
349 /// closed-interval `[earliest, latest]`.
350 ///
351 /// \pre The behavior is undefined unless `earliest <= latest`, `1 <= exampleYear <= 9999`,
352 /// `1 <= exampleMonth <= 12`, `1 <= intervalInMonths`, and
353 /// `1 <= occurrenceWeek <= 4`.
355 bsl::vector<bdlt::Date> *schedule,
356 const bdlt::Date& earliest,
357 const bdlt::Date& latest,
358 int exampleYear,
359 int exampleMonth,
360 int intervalInMonths,
361 bdlt::DayOfWeek::Enum dayOfWeek,
362 int occurrenceWeek);
364 std::vector<bdlt::Date> *schedule,
365 const bdlt::Date& earliest,
366 const bdlt::Date& latest,
367 int exampleYear,
368 int exampleMonth,
369 int intervalInMonths,
370 bdlt::DayOfWeek::Enum dayOfWeek,
371 int occurrenceWeek);
372#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
374 std::pmr::vector<bdlt::Date> *schedule,
375 const bdlt::Date& earliest,
376 const bdlt::Date& latest,
377 int exampleYear,
378 int exampleMonth,
379 int intervalInMonths,
380 bdlt::DayOfWeek::Enum dayOfWeek,
381 int occurrenceWeek);
382#endif
383};
384
385} // close package namespace
386
387
388#endif
389
390// ----------------------------------------------------------------------------
391// Copyright 2016 Bloomberg Finance L.P.
392//
393// Licensed under the Apache License, Version 2.0 (the "License");
394// you may not use this file except in compliance with the License.
395// You may obtain a copy of the License at
396//
397// http://www.apache.org/licenses/LICENSE-2.0
398//
399// Unless required by applicable law or agreed to in writing, software
400// distributed under the License is distributed on an "AS IS" BASIS,
401// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
402// See the License for the specific language governing permissions and
403// limitations under the License.
404// ----------------------------- END-OF-FILE ----------------------------------
405
406/** @} */
407/** @} */
408/** @} */
Definition bdlt_calendar.h:570
Definition bdlt_date.h:294
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 bblb_schedulegenerationutil.h:169
Definition bblb_schedulegenerationutil.h:179
static void generateFromDayOfMonth(std::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, int exampleYear, int exampleMonth, int intervalInMonths, int targetDayOfMonth, int targetDayOfFeb=0)
static void generateFromDayInterval(std::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, const bdlt::Date &example, int intervalInDays)
static void generateFromDayOfMonth(bsl::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, int exampleYear, int exampleMonth, int intervalInMonths, int targetDayOfMonth, int targetDayOfFeb=0)
static void generateFromDayOfWeekInMonth(bsl::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, int exampleYear, int exampleMonth, int intervalInMonths, bdlt::DayOfWeek::Enum dayOfWeek, int occurrenceWeek)
static void generateFromDayOfWeekAfterDayOfMonth(std::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, int exampleYear, int exampleMonth, int intervalInMonths, bdlt::DayOfWeek::Enum dayOfWeek, int dayOfMonth)
static void generateFromBusinessDayOfMonth(std::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, int exampleYear, int exampleMonth, int intervalInMonths, const bdlt::Calendar &calendar, int targetBusinessDayOfMonth)
static void generateFromBusinessDayOfMonth(bsl::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, int exampleYear, int exampleMonth, int intervalInMonths, const bdlt::Calendar &calendar, int targetBusinessDayOfMonth)
static void generateFromDayOfWeekInMonth(std::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, int exampleYear, int exampleMonth, int intervalInMonths, bdlt::DayOfWeek::Enum dayOfWeek, int occurrenceWeek)
static void generateFromDayOfWeekAfterDayOfMonth(bsl::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, int exampleYear, int exampleMonth, int intervalInMonths, bdlt::DayOfWeek::Enum dayOfWeek, int dayOfMonth)
static void generateFromDayInterval(bsl::vector< bdlt::Date > *schedule, const bdlt::Date &earliest, const bdlt::Date &latest, const bdlt::Date &example, int intervalInDays)
Enum
Enumerated day-of-week values.
Definition bdlt_dayofweek.h:125