BDE 4.14.0 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(.9) <= interval &&
112/// interval <= bsls::TimeInterval(1.1));
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.
141
142 public:
143 // CLASS METHODS
144
145 /// Return the `TimeInterval` value representing the current system
146 /// time according to the specified `clockType`. The returned value is
147 /// the time interval between the reference time point defined for the
148 /// supplied `clockType` (see {Reference Time Point}) and the current
149 /// time.
150 static TimeInterval now(SystemClockType::Enum clockType);
151
152 /// Return the `TimeInterval` value representing the current system time
153 /// according to the monotonic clock. The returned value is the time
154 /// interval between the reference time point for the monotonic clock
155 /// (see {Reference Time Point}) and the current time.
157
158 /// Return the `TimeInterval` value representing the current system time
159 /// according to the real-time clock. The returned value is the time
160 /// interval between the reference time point for the real-time clock
161 /// (see {Reference Time Point}) and the current time.
163};
164
165// ============================================================================
166// INLINE DEFINITIONS
167// ============================================================================
168
169 // ----------------
170 // class SystemTime
171 // ----------------
172
173// CLASS METHODS
174inline
176{
177 switch (clockType) {
178 case SystemClockType::e_MONOTONIC: return nowMonotonicClock(); // RETURN
179 case SystemClockType::e_REALTIME: return nowRealtimeClock(); // RETURN
180 }
181 BSLS_ASSERT_OPT("Invalid clockType parameter value" && 0);
182 return TimeInterval();
183}
184
185} // close package namespace
186
187
188#endif
189
190// ----------------------------------------------------------------------------
191// Copyright 2014 Bloomberg Finance L.P.
192//
193// Licensed under the Apache License, Version 2.0 (the "License");
194// you may not use this file except in compliance with the License.
195// You may obtain a copy of the License at
196//
197// http://www.apache.org/licenses/LICENSE-2.0
198//
199// Unless required by applicable law or agreed to in writing, software
200// distributed under the License is distributed on an "AS IS" BASIS,
201// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202// See the License for the specific language governing permissions and
203// limitations under the License.
204// ----------------------------- END-OF-FILE ----------------------------------
205
206/** @} */
207/** @} */
208/** @} */
Definition bsls_timeinterval.h:301
#define BSLS_ASSERT_OPT(X)
Definition bsls_assert.h:1856
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691
Enum
Definition bsls_systemclocktype.h:117
@ e_MONOTONIC
Definition bsls_systemclocktype.h:126
@ e_REALTIME
Definition bsls_systemclocktype.h:120
This struct provides a namespace for system-time-retrieval functions.
Definition bsls_systemtime.h:140
static TimeInterval now(SystemClockType::Enum clockType)
Definition bsls_systemtime.h:175
static TimeInterval nowRealtimeClock()
static TimeInterval nowMonotonicClock()