BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_systemtime.h
Go to the documentation of this file.
1/// @file bsls_systemtime.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_systemtime.h -*-C++-*-
8#ifndef INCLUDED_BSLS_SYSTEMTIME
9#define INCLUDED_BSLS_SYSTEMTIME
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_systemtime bsls_systemtime
15/// @brief Provide utilities to retrieve the system time.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_systemtime
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_systemtime-purpose"> Purpose</a>
25/// * <a href="#bsls_systemtime-classes"> Classes </a>
26/// * <a href="#bsls_systemtime-description"> Description </a>
27/// * <a href="#bsls_systemtime-reference-time-point"> Reference Time Point </a>
28/// * <a href="#bsls_systemtime-thread-safety"> Thread Safety </a>
29/// * <a href="#bsls_systemtime-usage"> Usage </a>
30/// * <a href="#bsls_systemtime-example-1-getting-current-wall-clock-time"> Example 1: Getting Current Wall Clock Time </a>
31///
32/// # Purpose {#bsls_systemtime-purpose}
33/// Provide utilities to retrieve the system time.
34///
35/// # Classes {#bsls_systemtime-classes}
36///
37/// - bsls::SystemTime: namespace for system-time functions
38///
39/// @see bsls_timeinterval
40///
41/// # Description {#bsls_systemtime-description}
42/// This component provides a `struct`, `bsls::SystemTime`, in
43/// which are defined a series of static methods for retrieving the current
44/// system time. This component provides access to a monotonic clock and a
45/// real-time (wall) clock.
46///
47/// ## Reference Time Point {#bsls_systemtime-reference-time-point}
48///
49///
50/// The `bsls::TimeInterval` objects returned by functions in this component
51/// identify a time by providing a time interval from some fixed reference time
52/// point (or "epoch"). The clock types supported by @ref bsls_systemtime (see
53/// @ref bsls_systemclocktype ) define a reference time point as described in the
54/// table below:
55/// @code
56/// bsls::SystemClockType::Enum Reference Time Point
57/// --------------------------- --------------------
58/// e_REALTIME January 1, 1970 00:00.000 UTC (POSIX epoch)
59/// e_MONOTONIC platform-dependent
60/// @endcode
61/// The `e_MONOTONIC` clock's reference time point is an unspecified,
62/// platform-dependent, value. This means that the monotonic clock cannot be
63/// reliably used to determine the absolute wall clock time. Monotonic clock
64/// times are frequently used to specify a fixed point in the future relative to
65/// the current (monotonic) clock time (e.g., for a timed-wait on a condition
66/// variable). Note that the monotonic clock time may be (though certainly is
67/// not guaranteed to be) an arbitrary value relative to the process start time,
68/// meaning that `bsls::TimeInterval` values from the monotonic clock should
69/// *not* be shared between processes.
70///
71/// ## Thread Safety {#bsls_systemtime-thread-safety}
72///
73///
74/// The functions provided by `bsls::SystemTime` are *thread-safe*.
75///
76/// ## Usage {#bsls_systemtime-usage}
77///
78///
79/// This section illustrates intended use of this component.
80///
81/// ### Example 1: Getting Current Wall Clock Time {#bsls_systemtime-example-1-getting-current-wall-clock-time}
82///
83///
84/// The following snippets of code illustrate how to use this utility component
85/// to obtain the system time by calling `now` and `nowRealtimeClock`.
86///
87/// First, we call `nowRealtimeClock`, and set `t1`, to the current time
88/// according to the real-time clock:
89/// @code
90/// bsls::TimeInterval t1 = bsls::SystemTime::nowRealtimeClock();
91///
92/// assert(bsls::TimeInterval() != t1);
93/// @endcode
94/// Next, we sleep for 1 second:
95/// @code
96/// sleep(1);
97/// @endcode
98/// Now, we call `now`, and supply `e_REALTIME` to indicate a real-time clock
99/// value should be returned, and then set `t2` to the current time according
100/// to the real-time clock:
101/// @code
102/// bsls::TimeInterval t2 = bsls::SystemTime::now(
103/// bsls::SystemClockType::e_REALTIME);
104///
105/// assert(bsls::TimeInterval() != t2);
106/// @endcode
107/// Finally, we verify the interval between `t1` and `t2` is close to 1 second:
108/// @code
109/// bsls::TimeInterval interval = t2 - t1;
110///
111/// assert(bsls::TimeInterval(.8) <= interval &&
112/// interval <= bsls::TimeInterval(2.5));
113/// @endcode
114/// @}
115/** @} */
116/** @} */
117
118/** @addtogroup bsl
119 * @{
120 */
121/** @addtogroup bsls
122 * @{
123 */
124/** @addtogroup bsls_systemtime
125 * @{
126 */
127
128#include <bsls_assert.h>
129#include <bsls_systemclocktype.h>
130#include <bsls_timeinterval.h>
131
132
133namespace bsls {
134
135 // =================
136 // struct SystemTime
137 // =================
138
139/// This `struct` provides a namespace for system-time-retrieval functions.
140///
141/// See @ref bsls_systemtime
143
144 public:
145 // CLASS METHODS
146
147 /// Return the `TimeInterval` value representing the current system
148 /// time according to the specified `clockType`. The returned value is
149 /// the time interval between the reference time point defined for the
150 /// supplied `clockType` (see {Reference Time Point}) and the current
151 /// time.
152 static TimeInterval now(SystemClockType::Enum clockType);
153
154 /// Return the `TimeInterval` value representing the current system time
155 /// according to the monotonic clock. The returned value is the time
156 /// interval between the reference time point for the monotonic clock
157 /// (see {Reference Time Point}) and the current time.
159
160 /// Return the `TimeInterval` value representing the current system time
161 /// according to the real-time clock. The returned value is the time
162 /// interval between the reference time point for the real-time clock
163 /// (see {Reference Time Point}) and the current time.
165};
166
167// ============================================================================
168// INLINE DEFINITIONS
169// ============================================================================
170
171 // ----------------
172 // class SystemTime
173 // ----------------
174
175// CLASS METHODS
176inline
178{
179 switch (clockType) {
180 case SystemClockType::e_MONOTONIC: return nowMonotonicClock(); // RETURN
181 case SystemClockType::e_REALTIME: return nowRealtimeClock(); // RETURN
182 }
183 BSLS_ASSERT_OPT_UNREACHABLE("Invalid `clockType` parameter value");
184 return TimeInterval();
185}
186
187} // close package namespace
188
189
190#endif
191
192// ----------------------------------------------------------------------------
193// Copyright 2014 Bloomberg Finance L.P.
194//
195// Licensed under the Apache License, Version 2.0 (the "License");
196// you may not use this file except in compliance with the License.
197// You may obtain a copy of the License at
198//
199// http://www.apache.org/licenses/LICENSE-2.0
200//
201// Unless required by applicable law or agreed to in writing, software
202// distributed under the License is distributed on an "AS IS" BASIS,
203// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
204// See the License for the specific language governing permissions and
205// limitations under the License.
206// ----------------------------- END-OF-FILE ----------------------------------
207
208/** @} */
209/** @} */
210/** @} */
Definition bsls_timeinterval.h:307
#define BSLS_ASSERT_OPT_UNREACHABLE(X)
Definition bsls_assert.h:2065
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlt_iso8601util.h:707
Enum
Definition bsls_systemclocktype.h:119
@ e_MONOTONIC
Definition bsls_systemclocktype.h:128
@ e_REALTIME
Definition bsls_systemclocktype.h:122
Definition bsls_systemtime.h:142
static TimeInterval now(SystemClockType::Enum clockType)
Definition bsls_systemtime.h:177
static TimeInterval nowRealtimeClock()
static TimeInterval nowMonotonicClock()