BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_intervalconversionutil.h
Go to the documentation of this file.
1/// @file bdlt_intervalconversionutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_intervalconversionutil.h -*-C++-*-
8#ifndef INCLUDED_BDLT_INTERVALCONVERSIONUTIL
9#define INCLUDED_BDLT_INTERVALCONVERSIONUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_intervalconversionutil bdlt_intervalconversionutil
15/// @brief Provide functions to convert between time-interval representations.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_intervalconversionutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_intervalconversionutil-purpose"> Purpose</a>
25/// * <a href="#bdlt_intervalconversionutil-classes"> Classes </a>
26/// * <a href="#bdlt_intervalconversionutil-description"> Description </a>
27/// * <a href="#bdlt_intervalconversionutil-usage"> Usage </a>
28/// * <a href="#bdlt_intervalconversionutil-example-1-interfacing-with-an-api-that-uses-bsls-timeinterval"> Example 1: Interfacing With an API That Uses bsls::TimeInterval </a>
29///
30/// # Purpose {#bdlt_intervalconversionutil-purpose}
31/// Provide functions to convert between time-interval representations.
32///
33/// # Classes {#bdlt_intervalconversionutil-classes}
34///
35/// - bdlt::IntervalConversionUtil: functions to convert time intervals
36///
37/// @see bdlt_datetimeinterval, bsls_timeinterval
38///
39/// # Description {#bdlt_intervalconversionutil-description}
40/// This component provides a utility `struct`,
41/// `bdlt::IntervalConversionUtil`, that defines functions to convert between
42/// C++ value types providing different representations of time intervals,
43/// (e.g., `bsls::TimeInterval` and `bdlt::DatetimeInterval`).
44///
45/// ## Usage {#bdlt_intervalconversionutil-usage}
46///
47///
48/// This section illustrates intended use of this component.
49///
50/// ### Example 1: Interfacing With an API That Uses bsls::TimeInterval {#bdlt_intervalconversionutil-example-1-interfacing-with-an-api-that-uses-bsls-timeinterval}
51///
52///
53/// Some APIs, such as `bsls::SystemTime`, use `bsls::TimeInterval` in their
54/// interface. In order to use those APIs in components implemented in terms of
55/// `bdlt::DatetimeInterval`, it is necessary to convert between the
56/// `bsls::TimeInterval` and `bdlt::DatetimeInterval` representations for a time
57/// interval. This conversion can be accomplished conveniently using
58/// `bdlt::IntervalConversionUtil`.
59///
60/// Suppose we wish to pass the system time -- as returned by
61/// `bsls::SystemTime::nowRealtimeClock` -- to a function that displays a time
62/// that is represented as a `bdlt::DatetimeInterval` since the UNIX epoch.
63///
64/// First, we include the declaration of the function that displays a
65/// `bdlt::DatetimeInterval`:
66/// @code
67/// void displayTime(const bdlt::DatetimeInterval& timeSinceEpoch);
68/// @endcode
69/// Then, we obtain the current system time from `bsls::SystemTime`, and store
70/// it in a `bsls::TimeInterval`:
71/// @code
72/// bsls::TimeInterval systemTime = bsls::SystemTime::nowRealtimeClock();
73/// @endcode
74/// Now, we convert the `bsls::TimeInterval` into a `bdlt::DatetimeInterval`
75/// using `convertToDatetimeInterval`:
76/// @code
77/// bdlt::DatetimeInterval timeSinceEpoch =
78/// bdlt::IntervalConversionUtil::convertToDatetimeInterval(systemTime);
79///
80/// assert(timeSinceEpoch.totalMilliseconds() ==
81/// systemTime.totalMilliseconds());
82/// @endcode
83/// Finally, we display the time by passing the converted value to
84/// `displayTime`:
85/// @code
86/// displayTime(timeSinceEpoch);
87/// @endcode
88/// @}
89/** @} */
90/** @} */
91
92/** @addtogroup bdl
93 * @{
94 */
95/** @addtogroup bdlt
96 * @{
97 */
98/** @addtogroup bdlt_intervalconversionutil
99 * @{
100 */
101
102#include <bdlscm_version.h>
103
105#include <bdlt_timeunitratio.h>
106
107#include <bsls_assert.h>
108#include <bsls_buildtarget.h>
109#include <bsls_review.h>
110#include <bsls_timeinterval.h>
111#include <bsls_types.h>
112
113#include <bsl_climits.h>
114
115
116namespace bdlt {
117
118 // =============================
119 // struct IntervalConversionUtil
120 // =============================
121
122/// This utility `struct`, `IntervalConversionUtil`, defines functions to
123/// convert between `bsls::TimeInterval` and `bdlt::DatetimeInterval`
124/// representations of time intervals.
125///
126/// See @ref bdlt_intervalconversionutil
128
129 private:
130 // PRIVATE TYPES
131 typedef TimeUnitRatio Ratio;
132 typedef bsls::Types::Int64 Int64;
133
134 // PRIVATE CLASS DATA
135 static const Int64 k_INT_MAX = INT_MAX;
136 static const Int64 k_INT_MIN = INT_MIN;
137 static const Int64 k_DATETIME_INTERVAL_SECONDS_MAX =
138 (k_INT_MAX + 1) * Ratio::k_SECONDS_PER_DAY - 1;
139 static const Int64 k_DATETIME_INTERVAL_SECONDS_MIN =
140 (k_INT_MIN - 1) * Ratio::k_SECONDS_PER_DAY + 1;
141 static const Int64 k_DATETIME_INTERVAL_NSEC_REMAINDER_MAX =
144 static const Int64 k_DATETIME_INTERVAL_NSEC_REMAINDER_MIN =
145 -k_DATETIME_INTERVAL_NSEC_REMAINDER_MAX;
146
147 public:
148 // CLASS METHODS
149
150 /// Return as a `bdlt::DatetimeInterval` the (approximate) value of the
151 /// specified `interval` truncated toward zero, to microsecond resolution.
152 ///
153 /// \pre The behavior is undefined unless the value of
154 /// `interval`, expressed with nanosecond precision, is within the range
155 /// of time intervals supported by a `DatetimeInterval` -- i.e.,
156 /// `DT_MIN * 10^6 <= TI_NSECS <= DT_MAX * 10^6`, where `TI_NSECS` is
157 /// the total number of nanoseconds in `interval`, `DT_MIN` is the
158 /// lowest (negative) value expressible by `DatetimeInterval`, and
159 /// `DT_MAX` is the highest (positive) value expressible by `DatetimeInterval`.
160 ///
161 /// \note Note that, while `bsls::TimeInterval` has
162 /// nanosecond resolution, `bdlt::DatetimeInterval` has only microsecond
163 /// resolution.
165 const bsls::TimeInterval& interval);
166
167 /// Return as a `bsls::TimeInterval` the value of the specified
168 /// `interval`.
170 const DatetimeInterval& interval);
171};
172
173// ============================================================================
174// INLINE DEFINITIONS
175// ============================================================================
176
177 // -----------------------------
178 // struct IntervalConversionUtil
179 // -----------------------------
180
181// CLASS METHODS
182inline
184 const bsls::TimeInterval& interval)
185{
186 // Check that the value of 'interval' is within the valid range supported
187 // by 'Dateinterval'.
188
189 BSLS_ASSERT( k_DATETIME_INTERVAL_SECONDS_MIN < interval.seconds()
190 || (k_DATETIME_INTERVAL_SECONDS_MIN == interval.seconds()
191 && k_DATETIME_INTERVAL_NSEC_REMAINDER_MIN <=
192 interval.nanoseconds()));
193
194 BSLS_ASSERT( interval.seconds() < k_DATETIME_INTERVAL_SECONDS_MAX
195 || (interval.seconds() == k_DATETIME_INTERVAL_SECONDS_MAX
196 && interval.nanoseconds() <=
197 k_DATETIME_INTERVAL_NSEC_REMAINDER_MAX));
198
199 // Note that we need to access the time via a comination of the
200 // 'totalSeconds' and 'nanoseconds' accessors, because the time range
201 // expressable in a 'DatetimeInterval' is greater than can be expressed in
202 // an 'Int64' by 'totalMicroseconds'.
203
204 return DatetimeInterval(0, // days
205 0, // hours
206 0, // minutes
207 interval.totalSeconds(),
208 0, // milliseconds
209 interval.nanoseconds() /
211}
212
213inline
223
224} // close package namespace
225
226
227#endif
228
229// ----------------------------------------------------------------------------
230// Copyright 2014 Bloomberg Finance L.P.
231//
232// Licensed under the Apache License, Version 2.0 (the "License");
233// you may not use this file except in compliance with the License.
234// You may obtain a copy of the License at
235//
236// http://www.apache.org/licenses/LICENSE-2.0
237//
238// Unless required by applicable law or agreed to in writing, software
239// distributed under the License is distributed on an "AS IS" BASIS,
240// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
241// See the License for the specific language governing permissions and
242// limitations under the License.
243// ----------------------------- END-OF-FILE ----------------------------------
244
245/** @} */
246/** @} */
247/** @} */
Definition bdlt_datetimeinterval.h:201
int milliseconds() const
Definition bdlt_datetimeinterval.h:1188
bsls::Types::Int64 totalSeconds() const
Definition bdlt_datetimeinterval.h:1221
int microseconds() const
Definition bdlt_datetimeinterval.h:1195
Definition bsls_timeinterval.h:307
BSLS_KEYWORD_CONSTEXPR int nanoseconds() const
Definition bsls_timeinterval.h:1423
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 totalSeconds() const
Definition bsls_timeinterval.h:1453
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 seconds() const
Definition bsls_timeinterval.h:1429
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#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_intervalconversionutil.h:127
static DatetimeInterval convertToDatetimeInterval(const bsls::TimeInterval &interval)
Definition bdlt_intervalconversionutil.h:183
static bsls::TimeInterval convertToTimeInterval(const DatetimeInterval &interval)
Definition bdlt_intervalconversionutil.h:214
Definition bdlt_timeunitratio.h:201
static const bsls::Types::Int64 k_SECONDS_PER_DAY
Definition bdlt_timeunitratio.h:258
static const int k_NANOSECONDS_PER_MILLISECOND_32
Definition bdlt_timeunitratio.h:300
static const bsls::Types::Int64 k_NANOSECONDS_PER_MICROSECOND
Definition bdlt_timeunitratio.h:207
static const int k_NANOSECONDS_PER_MICROSECOND_32
Definition bdlt_timeunitratio.h:298
static const bsls::Types::Int64 k_MICROSECONDS_PER_SECOND
Definition bdlt_timeunitratio.h:232
long long Int64
Definition bsls_types.h:134