BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_timeunitratio.h
Go to the documentation of this file.
1/// @file bdlt_timeunitratio.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_timeunitratio.h -*-C++-*-
8#ifndef INCLUDED_BDLT_TIMEUNITRATIO
9#define INCLUDED_BDLT_TIMEUNITRATIO
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_timeunitratio bdlt_timeunitratio
15/// @brief Provide constants characterizing ratios between common time units.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_timeunitratio
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_timeunitratio-purpose"> Purpose</a>
25/// * <a href="#bdlt_timeunitratio-classes"> Classes </a>
26/// * <a href="#bdlt_timeunitratio-description"> Description </a>
27/// * <a href="#bdlt_timeunitratio-usage"> Usage </a>
28/// * <a href="#bdlt_timeunitratio-example-1-breaking-a-time-interval-into-component-parts"> Example 1: Breaking a Time Interval Into Component Parts </a>
29///
30/// # Purpose {#bdlt_timeunitratio-purpose}
31/// Provide constants characterizing ratios between common time units.
32///
33/// # Classes {#bdlt_timeunitratio-classes}
34///
35/// - bdlt::TimeUnitRatio: namespace for common time-unit-ratio constants
36///
37/// # Description {#bdlt_timeunitratio-description}
38/// This component provides a utility `struct`,
39/// `bdlt::TimeUnitRatio`, that defines a namespace for constants that
40/// characterize the ratios between commonly used time units. Ratios are
41/// provided for combinations of all time units from a nanosecond up through a
42/// day.
43///
44/// Each constant defined in this namespace has at least two forms, a full form
45/// and an abbreviated form following the patterns:
46/// @code
47/// bdlt::TimeUnitRatio::k_SMALLUNITS_PER_LARGEUNIT
48/// bdlt::TimeUnitRatio::k_SU_PER_LU
49/// @endcode
50/// Where `SMALLUNIT` and `LARGEUNIT` are members of the following group:
51/// @code
52/// NANOSECOND
53/// MICROSECOND
54/// MILLISECOND
55/// SECOND
56/// MINUTE
57/// HOUR
58/// DAY
59/// @endcode
60/// and `SU` and `LU` are abbreviations in the following group:
61/// @code
62/// NS // abbreviation for `NANOSECOND`
63/// US // abbreviation for `MICROSECOND`
64/// MS // abbreviation for `MILLISECOND`
65/// S // abbreviation for `SECOND`
66/// M // abbreviation for `MINUTE`
67/// H // abbreviation for `HOUR`
68/// D // abbreviation for `DAY`
69/// @endcode
70/// Note that `SMALLUNIT` (`SU`) is always a smaller unit than `LARGEUNIT`
71/// (`LU`). Thus, the (whole) number of microseconds in an hour is
72/// characterized by the constants:
73/// @code
74/// bdlt::TimeUnitRatio::k_MICROSECONDS_PER_HOUR
75/// bdlt::TimeUnitRatio::k_US_PER_H
76/// @endcode
77/// Additionally, ratios that can be represented by a 32-bit integer also have
78/// two corresponding 32-bit constants, having the same names as the constants
79/// described above, but incorporate a `_32` suffix. Thus, the number of
80/// milliseconds per minute is characterized by four constants:
81/// @code
82/// bdlt::TimeUnitRatio::k_MILLISECONDS_PER_MINUTE
83/// bdlt::TimeUnitRatio::k_MS_PER_M
84/// bdlt::TimeUnitRatio::k_MILLISECONDS_PER_MINUTE_32
85/// bdlt::TimeUnitRatio::k_MS_PER_M_32
86/// @endcode
87/// By providing both 64- and 32-bit versions of the constants, this component
88/// allows the programmer to minimize the use of casts in time unit
89/// calculations.
90///
91/// ## Usage {#bdlt_timeunitratio-usage}
92///
93///
94/// This section illustrates intended use of this component.
95///
96/// ### Example 1: Breaking a Time Interval Into Component Parts {#bdlt_timeunitratio-example-1-breaking-a-time-interval-into-component-parts}
97///
98///
99/// Components that deal with time often need to convert between various units.
100/// Each component that needs to perform such conversions could derive the
101/// constants needed locally, but doing so would result in an inconsistent
102/// vocabulary across components, and multiple opportunities for bugs.
103/// `bdlt::TimeUnitRatio` achieves the desired consistency and avoids bugs by
104/// providing a single location for the constants used in such conversions.
105///
106/// Suppose we have a time interval described as an integral number of
107/// nanoseconds, and we need to break it down into its constituent second,
108/// millisecond, microsecond, and nanosecond parts.
109///
110/// First, we define a variable representing the number of nanoseconds that have
111/// elapsed since a particular event:
112/// @code
113/// bsls::Types::Int64 interval = 62003004005; // nanoseconds since event
114/// @endcode
115/// Then, we extract the minutes part from the total, using the constant
116/// `bdlt::TimeUnitRatio::k_NS_PER_M`:
117/// @code
118/// bsls::Types::Int64 minutesPart =
119/// interval / bdlt::TimeUnitRatio::k_NS_PER_M;
120/// @endcode
121/// Next, we calculate the remaining nanoseconds using the same constant:
122/// @code
123/// bsls::Types::Int64 remainder = interval % bdlt::TimeUnitRatio::k_NS_PER_M;
124/// @endcode
125/// Then, we extract the seconds part from the remainder, using the constant
126/// `bdlt::TimeUnitRatio::k_NS_PER_S`:
127/// @code
128/// bsls::Types::Int64 secondsPart =
129/// remainder / bdlt::TimeUnitRatio::k_NS_PER_S;
130/// @endcode
131/// Next, we calculate the remaining nanoseconds using the same constant:
132/// @code
133/// remainder %= bdlt::TimeUnitRatio::k_NS_PER_S;
134/// @endcode
135/// Then, we extract the milliseconds part from the remainder, using the
136/// constant `bdlt::TimeUnitRatio::k_NS_PER_MS`:
137/// @code
138/// bsls::Types::Int64 millisecondsPart =
139/// remainder / bdlt::TimeUnitRatio::k_NS_PER_MS;
140/// @endcode
141/// Next, we calculate the remaining nanoseconds using the same constant:
142/// @code
143/// remainder %= bdlt::TimeUnitRatio::k_NS_PER_MS;
144/// @endcode
145/// Then, we extract the microseconds part from the remainder, using the
146/// constant `bdlt::TimeUnitRatio::k_NS_PER_US`:
147/// @code
148/// bsls::Types::Int64 microsecondsPart =
149/// remainder / bdlt::TimeUnitRatio::k_NS_PER_US;
150/// @endcode
151/// Next, we calculate the remaining nanoseconds using the same constant:
152/// @code
153/// remainder %= bdlt::TimeUnitRatio::k_NS_PER_US;
154/// @endcode
155/// Now, we extract the nanoseconds part, which is exactly the remainder we
156/// already have:
157/// @code
158/// bsls::Types::Int64 nanosecondsPart = remainder;
159/// @endcode
160/// Finally, we confirm that the parts we have extracted all have the correct
161/// values:
162/// @code
163/// assert(1 == minutesPart);
164/// assert(2 == secondsPart);
165/// assert(3 == millisecondsPart);
166/// assert(4 == microsecondsPart);
167/// assert(5 == nanosecondsPart);
168/// @endcode
169/// Note that in practice, the number of nanoseconds since the event would be
170/// provided by some system utility, and not a constant as was shown here for
171/// purposes of exposition.
172/// @}
173/** @} */
174/** @} */
175
176/** @addtogroup bdl
177 * @{
178 */
179/** @addtogroup bdlt
180 * @{
181 */
182/** @addtogroup bdlt_timeunitratio
183 * @{
184 */
185
186#include <bdlscm_version.h>
187
188#include <bsls_types.h>
189
190
191namespace bdlt {
192
193 // ====================
194 // struct TimeUnitRatio
195 // ====================
196
197/// This `struct` provides a namespace for constants characterizing ratios
198/// between commonly-used time units.
200
201 // CLASS DATA
202
203 // Fundamental ratios
204
211
212 // Derived ratios
213
216
219
222
225
228
229
232
235
238
241
242
245
248
251
252
255
258
261
262 // Abbreviations
263
272
279
284
288
291
293
294 // 32-Bit Variations
295
302
309
314
318
321
323
324 // Abbreviated 32-Bit Variations
325
329
333
338
341 static const int k_S_PER_D_32 = k_SECONDS_PER_DAY;
342
344 static const int k_M_PER_D_32 = k_MINUTES_PER_DAY;
345
346 static const int k_H_PER_D_32 = k_HOURS_PER_DAY;
347};
348
349} // close package namespace
350
351
352#endif
353
354// ----------------------------------------------------------------------------
355// Copyright 2014 Bloomberg Finance L.P.
356//
357// Licensed under the Apache License, Version 2.0 (the "License");
358// you may not use this file except in compliance with the License.
359// You may obtain a copy of the License at
360//
361// http://www.apache.org/licenses/LICENSE-2.0
362//
363// Unless required by applicable law or agreed to in writing, software
364// distributed under the License is distributed on an "AS IS" BASIS,
365// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
366// See the License for the specific language governing permissions and
367// limitations under the License.
368// ----------------------------- END-OF-FILE ----------------------------------
369
370/** @} */
371/** @} */
372/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112
Definition bdlt_timeunitratio.h:199
static const bsls::Types::Int64 k_M_PER_D
Definition bdlt_timeunitratio.h:290
static const bsls::Types::Int64 k_NANOSECONDS_PER_DAY
Definition bdlt_timeunitratio.h:226
static const int k_S_PER_D_32
Definition bdlt_timeunitratio.h:341
static const bsls::Types::Int64 k_S_PER_M
Definition bdlt_timeunitratio.h:285
static const int k_S_PER_M_32
Definition bdlt_timeunitratio.h:339
static const bsls::Types::Int64 k_NANOSECONDS_PER_HOUR
Definition bdlt_timeunitratio.h:223
static const int k_H_PER_D_32
Definition bdlt_timeunitratio.h:346
static const int k_US_PER_MS_32
Definition bdlt_timeunitratio.h:330
static const bsls::Types::Int64 k_NS_PER_D
Definition bdlt_timeunitratio.h:271
static const int k_HOURS_PER_DAY_32
Definition bdlt_timeunitratio.h:322
static const int k_MS_PER_D_32
Definition bdlt_timeunitratio.h:337
static const int k_SECONDS_PER_MINUTE_32
Definition bdlt_timeunitratio.h:315
static const int k_MS_PER_M_32
Definition bdlt_timeunitratio.h:335
static const bsls::Types::Int64 k_MS_PER_S
Definition bdlt_timeunitratio.h:280
static const bsls::Types::Int64 k_MS_PER_M
Definition bdlt_timeunitratio.h:281
static const bsls::Types::Int64 k_US_PER_H
Definition bdlt_timeunitratio.h:277
static const bsls::Types::Int64 k_MICROSECONDS_PER_DAY
Definition bdlt_timeunitratio.h:239
static const bsls::Types::Int64 k_MILLISECONDS_PER_HOUR
Definition bdlt_timeunitratio.h:246
static const int k_SECONDS_PER_DAY_32
Definition bdlt_timeunitratio.h:317
static const bsls::Types::Int64 k_SECONDS_PER_HOUR
Definition bdlt_timeunitratio.h:253
static const int k_M_PER_H_32
Definition bdlt_timeunitratio.h:343
static const int k_NS_PER_US_32
Definition bdlt_timeunitratio.h:326
static const bsls::Types::Int64 k_M_PER_H
Definition bdlt_timeunitratio.h:289
static const int k_MINUTES_PER_HOUR_32
Definition bdlt_timeunitratio.h:319
static const bsls::Types::Int64 k_NS_PER_US
Definition bdlt_timeunitratio.h:264
static const bsls::Types::Int64 k_NANOSECONDS_PER_SECOND
Definition bdlt_timeunitratio.h:217
static const int k_M_PER_D_32
Definition bdlt_timeunitratio.h:344
static const bsls::Types::Int64 k_US_PER_MS
Definition bdlt_timeunitratio.h:273
static const bsls::Types::Int64 k_SECONDS_PER_DAY
Definition bdlt_timeunitratio.h:256
static const int k_MILLISECONDS_PER_HOUR_32
Definition bdlt_timeunitratio.h:312
static const int k_MS_PER_S_32
Definition bdlt_timeunitratio.h:334
static const bsls::Types::Int64 k_US_PER_D
Definition bdlt_timeunitratio.h:278
static const int k_MILLISECONDS_PER_MINUTE_32
Definition bdlt_timeunitratio.h:311
static const int k_MICROSECONDS_PER_MILLISECOND_32
Definition bdlt_timeunitratio.h:303
static const int k_NANOSECONDS_PER_MILLISECOND_32
Definition bdlt_timeunitratio.h:298
static const bsls::Types::Int64 k_HOURS_PER_DAY
Definition bdlt_timeunitratio.h:210
static const int k_NS_PER_S_32
Definition bdlt_timeunitratio.h:328
static const int k_MILLISECONDS_PER_SECOND_32
Definition bdlt_timeunitratio.h:310
static const bsls::Types::Int64 k_MS_PER_H
Definition bdlt_timeunitratio.h:282
static const bsls::Types::Int64 k_NS_PER_MS
Definition bdlt_timeunitratio.h:266
static const int k_SECONDS_PER_HOUR_32
Definition bdlt_timeunitratio.h:316
static const bsls::Types::Int64 k_S_PER_H
Definition bdlt_timeunitratio.h:286
static const bsls::Types::Int64 k_MICROSECONDS_PER_MINUTE
Definition bdlt_timeunitratio.h:233
static const int k_MICROSECONDS_PER_SECOND_32
Definition bdlt_timeunitratio.h:305
static const bsls::Types::Int64 k_MINUTES_PER_HOUR
Definition bdlt_timeunitratio.h:209
static const int k_US_PER_M_32
Definition bdlt_timeunitratio.h:332
static const bsls::Types::Int64 k_NS_PER_S
Definition bdlt_timeunitratio.h:268
static const int k_MINUTES_PER_DAY_32
Definition bdlt_timeunitratio.h:320
static const bsls::Types::Int64 k_MINUTES_PER_DAY
Definition bdlt_timeunitratio.h:259
static const bsls::Types::Int64 k_H_PER_D
Definition bdlt_timeunitratio.h:292
static const int k_S_PER_H_32
Definition bdlt_timeunitratio.h:340
static const bsls::Types::Int64 k_US_PER_S
Definition bdlt_timeunitratio.h:275
static const bsls::Types::Int64 k_NANOSECONDS_PER_MICROSECOND
Definition bdlt_timeunitratio.h:205
static const bsls::Types::Int64 k_NS_PER_M
Definition bdlt_timeunitratio.h:269
static const bsls::Types::Int64 k_US_PER_M
Definition bdlt_timeunitratio.h:276
static const int k_MS_PER_H_32
Definition bdlt_timeunitratio.h:336
static const int k_MICROSECONDS_PER_MINUTE_32
Definition bdlt_timeunitratio.h:307
static const bsls::Types::Int64 k_NANOSECONDS_PER_MILLISECOND
Definition bdlt_timeunitratio.h:214
static const bsls::Types::Int64 k_MICROSECONDS_PER_HOUR
Definition bdlt_timeunitratio.h:236
static const bsls::Types::Int64 k_S_PER_D
Definition bdlt_timeunitratio.h:287
static const bsls::Types::Int64 k_MILLISECONDS_PER_DAY
Definition bdlt_timeunitratio.h:249
static const bsls::Types::Int64 k_MILLISECONDS_PER_MINUTE
Definition bdlt_timeunitratio.h:243
static const int k_US_PER_S_32
Definition bdlt_timeunitratio.h:331
static const int k_NANOSECONDS_PER_MICROSECOND_32
Definition bdlt_timeunitratio.h:296
static const bsls::Types::Int64 k_MS_PER_D
Definition bdlt_timeunitratio.h:283
static const bsls::Types::Int64 k_NANOSECONDS_PER_MINUTE
Definition bdlt_timeunitratio.h:220
static const bsls::Types::Int64 k_SECONDS_PER_MINUTE
Definition bdlt_timeunitratio.h:208
static const int k_NS_PER_MS_32
Definition bdlt_timeunitratio.h:327
static const bsls::Types::Int64 k_NS_PER_H
Definition bdlt_timeunitratio.h:270
static const int k_MILLISECONDS_PER_DAY_32
Definition bdlt_timeunitratio.h:313
static const bsls::Types::Int64 k_MICROSECONDS_PER_SECOND
Definition bdlt_timeunitratio.h:230
static const bsls::Types::Int64 k_MICROSECONDS_PER_MILLISECOND
Definition bdlt_timeunitratio.h:206
static const bsls::Types::Int64 k_MILLISECONDS_PER_SECOND
Definition bdlt_timeunitratio.h:207
static const int k_NANOSECONDS_PER_SECOND_32
Definition bdlt_timeunitratio.h:300
long long Int64
Definition bsls_types.h:132