BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_dayofweekutil.h
Go to the documentation of this file.
1/// @file bdlt_dayofweekutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_dayofweekutil.h -*-C++-*-
8#ifndef INCLUDED_BDLT_DAYOFWEEKUTIL
9#define INCLUDED_BDLT_DAYOFWEEKUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_dayofweekutil bdlt_dayofweekutil
15/// @brief Provide common non-primitive operations on `bdlt::DayOfWeek::Enum`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_dayofweekutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_dayofweekutil-purpose"> Purpose</a>
25/// * <a href="#bdlt_dayofweekutil-classes"> Classes </a>
26/// * <a href="#bdlt_dayofweekutil-description"> Description </a>
27/// * <a href="#bdlt_dayofweekutil-usage"> Usage </a>
28/// * <a href="#bdlt_dayofweekutil-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdlt_dayofweekutil-purpose}
31/// Provide common non-primitive operations on `bdlt::DayOfWeek::Enum`.
32///
33/// # Classes {#bdlt_dayofweekutil-classes}
34///
35/// - bdlt::DayOfWeekUtil: functions operating on the day-of-week enumeration
36///
37/// @see bdlt_dayofweek
38///
39/// # Description {#bdlt_dayofweekutil-description}
40/// This utility component provides a `struct`,
41/// `bdlt::DayOfWeekUtil`, that serves as a namespace for functions operating on
42/// the day-of-week enumeration.
43///
44/// This component provides the following (static) methods:
45/// @code
46/// 'add' Determine the day of the week that results from
47/// shifting a given 'bdlt::DayOfWeek::Enum' value
48/// by a given (signed) number of days.
49/// @endcode
50///
51/// ## Usage {#bdlt_dayofweekutil-usage}
52///
53///
54/// This section illustrates intended use of this component.
55///
56/// ### Example 1: Basic Usage {#bdlt_dayofweekutil-example-1-basic-usage}
57///
58///
59/// Suppose we have some event occurring every ten days. Today is the day of
60/// the performance, assumed to be a Friday, and we want to know when the
61/// previous one took place and when the next one will be.
62///
63/// First, we create a `bdlt::DayOfWeek` variable for the current day:
64/// @code
65/// bdlt::DayOfWeek::Enum current = bdlt::DayOfWeek::e_FRI;
66/// @endcode
67/// Next, we calculate previous and following event days using the
68/// `bdlt::DayOfWeekUtil::add` function:
69/// @code
70/// bdlt::DayOfWeek::Enum previous = bdlt::DayOfWeekUtil::add(current, -10);
71/// bdlt::DayOfWeek::Enum following = bdlt::DayOfWeekUtil::add(current, 10);
72/// @endcode
73/// Finally, we verify the resultant day-of-week values:
74/// @code
75/// assert(bdlt::DayOfWeek::e_TUE == previous );
76/// assert(bdlt::DayOfWeek::e_MON == following);
77/// @endcode
78/// @}
79/** @} */
80/** @} */
81
82/** @addtogroup bdl
83 * @{
84 */
85/** @addtogroup bdlt
86 * @{
87 */
88/** @addtogroup bdlt_dayofweekutil
89 * @{
90 */
91
92#include <bdlscm_version.h>
93
94#include <bdlt_dayofweek.h>
95
96#include <bsls_cpp11.h>
97
98
99namespace bdlt {
100
101 // ====================
102 // struct DayOfWeekUtil
103 // ====================
104
105/// This `struct` provides a namespace for common non-primitive procedures
106/// that operate on `bdlt::DayOfWeek::Enum` values.
108
109 // CLASS METHODS
110
111 /// Return the `DayOfWeek::Enum` value that is the specified (signed)
112 /// `numDays` from the specified `dayOfWeek`. Note that
113 /// `DayOfWeek::Enum` is closed under this operation (in the expected
114 /// manner). For example, the following hold `true`:
115 /// @code
116 /// DayOfWeek::e_MON == DayOfWeekUtil::add(DayOfWeek::e_SUN, 1);
117 /// DayOfWeek::e_SAT == DayOfWeekUtil::add(DayOfWeek::e_SUN, -1);
118 /// DayOfWeek::e_SAT == DayOfWeekUtil::add(DayOfWeek::e_SUN, 6);
119 /// DayOfWeek::e_MON == DayOfWeekUtil::add(DayOfWeek::e_SUN, -6);
120 /// DayOfWeek::e_SUN == DayOfWeekUtil::add(DayOfWeek::e_SUN, 7);
121 /// DayOfWeek::e_SUN == DayOfWeekUtil::add(DayOfWeek::e_SUN, -7);
122 /// DayOfWeek::e_MON == DayOfWeekUtil::add(DayOfWeek::e_SUN, 8);
123 /// DayOfWeek::e_SAT == DayOfWeekUtil::add(DayOfWeek::e_SUN, -8);
124 /// @endcode
126 int numDays);
127};
128
129// ============================================================================
130// INLINE FUNCTION DEFINITIONS
131// ============================================================================
132
133 // --------------------
134 // struct DayOfWeekUtil
135 // --------------------
136
137// CLASS METHODS
138inline
140 DayOfWeek::Enum dayOfWeek,
141 int numDays)
142{
143 // The '+ 7' prevents a negative result.
144
145 return static_cast<DayOfWeek::Enum>(
146 (static_cast<int>(dayOfWeek) - 1 + numDays % 7 + 7) % 7 + 1);
147}
148
149} // close package namespace
150
151
152#endif
153
154// ----------------------------------------------------------------------------
155// Copyright 2018 Bloomberg Finance L.P.
156//
157// Licensed under the Apache License, Version 2.0 (the "License");
158// you may not use this file except in compliance with the License.
159// You may obtain a copy of the License at
160//
161// http://www.apache.org/licenses/LICENSE-2.0
162//
163// Unless required by applicable law or agreed to in writing, software
164// distributed under the License is distributed on an "AS IS" BASIS,
165// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
166// See the License for the specific language governing permissions and
167// limitations under the License.
168// ----------------------------- END-OF-FILE ----------------------------------
169
170/** @} */
171/** @} */
172/** @} */
#define BSLS_CPP11_CONSTEXPR
Definition bsls_cpp11.h:289
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112
Definition bdlt_dayofweekutil.h:107
static BSLS_CPP11_CONSTEXPR DayOfWeek::Enum add(DayOfWeek::Enum dayOfWeek, int numDays)
Definition bdlt_dayofweekutil.h:139
Enum
Enumerated day-of-week values.
Definition bdlt_dayofweek.h:123