BDE 4.39.x 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.
107///
108/// See @ref bdlt_dayofweekutil
110
111 // CLASS METHODS
112
113 /// Return the `DayOfWeek::Enum` value that is the specified (signed)
114 /// `numDays` from the specified `dayOfWeek`.
115 ///
116 /// \note Note that `DayOfWeek::Enum` is closed under this operation (in the expected
117 /// manner). For example, the following hold `true`:
118 /// @code
119 /// DayOfWeek::e_MON == DayOfWeekUtil::add(DayOfWeek::e_SUN, 1);
120 /// DayOfWeek::e_SAT == DayOfWeekUtil::add(DayOfWeek::e_SUN, -1);
121 /// DayOfWeek::e_SAT == DayOfWeekUtil::add(DayOfWeek::e_SUN, 6);
122 /// DayOfWeek::e_MON == DayOfWeekUtil::add(DayOfWeek::e_SUN, -6);
123 /// DayOfWeek::e_SUN == DayOfWeekUtil::add(DayOfWeek::e_SUN, 7);
124 /// DayOfWeek::e_SUN == DayOfWeekUtil::add(DayOfWeek::e_SUN, -7);
125 /// DayOfWeek::e_MON == DayOfWeekUtil::add(DayOfWeek::e_SUN, 8);
126 /// DayOfWeek::e_SAT == DayOfWeekUtil::add(DayOfWeek::e_SUN, -8);
127 /// @endcode
129 int numDays);
130};
131
132// ============================================================================
133// INLINE FUNCTION DEFINITIONS
134// ============================================================================
135
136 // --------------------
137 // struct DayOfWeekUtil
138 // --------------------
139
140// CLASS METHODS
141inline
143 DayOfWeek::Enum dayOfWeek,
144 int numDays)
145{
146 // The '+ 7' prevents a negative result.
147
148 return static_cast<DayOfWeek::Enum>(
149 (static_cast<int>(dayOfWeek) - 1 + numDays % 7 + 7) % 7 + 1);
150}
151
152} // close package namespace
153
154
155#endif
156
157// ----------------------------------------------------------------------------
158// Copyright 2018 Bloomberg Finance L.P.
159//
160// Licensed under the Apache License, Version 2.0 (the "License");
161// you may not use this file except in compliance with the License.
162// You may obtain a copy of the License at
163//
164// http://www.apache.org/licenses/LICENSE-2.0
165//
166// Unless required by applicable law or agreed to in writing, software
167// distributed under the License is distributed on an "AS IS" BASIS,
168// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
169// See the License for the specific language governing permissions and
170// limitations under the License.
171// ----------------------------- END-OF-FILE ----------------------------------
172
173/** @} */
174/** @} */
175/** @} */
#define BSLS_CPP11_CONSTEXPR
Definition bsls_cpp11.h:289
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicisma30360.h:112
Definition bdlt_dayofweekutil.h:109
static BSLS_CPP11_CONSTEXPR DayOfWeek::Enum add(DayOfWeek::Enum dayOfWeek, int numDays)
Definition bdlt_dayofweekutil.h:142
Enum
Enumerated day-of-week values.
Definition bdlt_dayofweek.h:125