BDE 4.14.0 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.
126
127 private:
128 // PRIVATE TYPES
129 typedef TimeUnitRatio Ratio;
130 typedef bsls::Types::Int64 Int64;
131
132 // PRIVATE CLASS DATA
133 static const Int64 k_INT_MAX = INT_MAX;
134 static const Int64 k_INT_MIN = INT_MIN;
135 static const Int64 k_DATETIME_INTERVAL_SECONDS_MAX =
136 (k_INT_MAX + 1) * Ratio::k_SECONDS_PER_DAY - 1;
137 static const Int64 k_DATETIME_INTERVAL_SECONDS_MIN =
138 (k_INT_MIN - 1) * Ratio::k_SECONDS_PER_DAY + 1;
139 static const Int64 k_DATETIME_INTERVAL_NSEC_REMAINDER_MAX =
142 static const Int64 k_DATETIME_INTERVAL_NSEC_REMAINDER_MIN =
143 -k_DATETIME_INTERVAL_NSEC_REMAINDER_MAX;
144
145 public:
146 // CLASS METHODS
147
148 /// Return as a `bdlt::DatetimeInterval` the (approximate) value of the
149 /// specified `interval` truncated toward zero, to microsecond
150 /// resolution. The behavior is undefined unless the value of
151 /// `interval`, expressed with nanosecond precision, is within the range
152 /// of time intervals supported by a `DatetimeInterval` -- i.e.,
153 /// `DT_MIN * 10^6 <= TI_NSECS <= DT_MAX * 10^6`, where `TI_NSECS` is
154 /// the total number of nanoseconds in `interval`, `DT_MIN` is the
155 /// lowest (negative) value expressible by `DatetimeInterval`, and
156 /// `DT_MAX` is the highest (positive) value expressible by
157 /// `DatetimeInterval`. Note that, while `bsls::TimeInterval` has
158 /// nanosecond resolution, `bdlt::DatetimeInterval` has only microsecond
159 /// resolution.
161 const bsls::TimeInterval& interval);
162
163 /// Return as a `bsls::TimeInterval` the value of the specified
164 /// `interval`.
166 const DatetimeInterval& interval);
167};
168
169// ============================================================================
170// INLINE DEFINITIONS
171// ============================================================================
172
173 // -----------------------------
174 // struct IntervalConversionUtil
175 // -----------------------------
176
177// CLASS METHODS
178inline
180 const bsls::TimeInterval& interval)
181{
182 // Check that the value of 'interval' is within the valid range supported
183 // by 'Dateinterval'.
184
185 BSLS_ASSERT( k_DATETIME_INTERVAL_SECONDS_MIN < interval.seconds()
186 || (k_DATETIME_INTERVAL_SECONDS_MIN == interval.seconds()
187 && k_DATETIME_INTERVAL_NSEC_REMAINDER_MIN <=
188 interval.nanoseconds()));
189
190 BSLS_ASSERT( interval.seconds() < k_DATETIME_INTERVAL_SECONDS_MAX
191 || (interval.seconds() == k_DATETIME_INTERVAL_SECONDS_MAX
192 && interval.nanoseconds() <=
193 k_DATETIME_INTERVAL_NSEC_REMAINDER_MAX));
194
195 // Note that we need to access the time via a comination of the
196 // 'totalSeconds' and 'nanoseconds' accessors, because the time range
197 // expressable in a 'DatetimeInterval' is greater than can be expressed in
198 // an 'Int64' by 'totalMicroseconds'.
199
200 return DatetimeInterval(0, // days
201 0, // hours
202 0, // minutes
203 interval.totalSeconds(),
204 0, // milliseconds
205 interval.nanoseconds() /
207}
208
209inline
219
220} // close package namespace
221
222
223#endif
224
225// ----------------------------------------------------------------------------
226// Copyright 2014 Bloomberg Finance L.P.
227//
228// Licensed under the Apache License, Version 2.0 (the "License");
229// you may not use this file except in compliance with the License.
230// You may obtain a copy of the License at
231//
232// http://www.apache.org/licenses/LICENSE-2.0
233//
234// Unless required by applicable law or agreed to in writing, software
235// distributed under the License is distributed on an "AS IS" BASIS,
236// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
237// See the License for the specific language governing permissions and
238// limitations under the License.
239// ----------------------------- END-OF-FILE ----------------------------------
240
241/** @} */
242/** @} */
243/** @} */
Definition bdlt_datetimeinterval.h:201
int milliseconds() const
Definition bdlt_datetimeinterval.h:1137
bsls::Types::Int64 totalSeconds() const
Definition bdlt_datetimeinterval.h:1170
int microseconds() const
Definition bdlt_datetimeinterval.h:1144
Definition bsls_timeinterval.h:301
BSLS_KEYWORD_CONSTEXPR int nanoseconds() const
Definition bsls_timeinterval.h:1348
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 totalSeconds() const
Definition bsls_timeinterval.h:1378
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 seconds() const
Definition bsls_timeinterval.h:1354
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112
Definition bdlt_intervalconversionutil.h:125
static DatetimeInterval convertToDatetimeInterval(const bsls::TimeInterval &interval)
Definition bdlt_intervalconversionutil.h:179
static bsls::TimeInterval convertToTimeInterval(const DatetimeInterval &interval)
Definition bdlt_intervalconversionutil.h:210
Definition bdlt_timeunitratio.h:199
static const bsls::Types::Int64 k_SECONDS_PER_DAY
Definition bdlt_timeunitratio.h:256
static const int k_NANOSECONDS_PER_MILLISECOND_32
Definition bdlt_timeunitratio.h:298
static const bsls::Types::Int64 k_NANOSECONDS_PER_MICROSECOND
Definition bdlt_timeunitratio.h:205
static const int k_NANOSECONDS_PER_MICROSECOND_32
Definition bdlt_timeunitratio.h:296
static const bsls::Types::Int64 k_MICROSECONDS_PER_SECOND
Definition bdlt_timeunitratio.h:230
long long Int64
Definition bsls_types.h:132