BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_chrono.h
Go to the documentation of this file.
1/// @file bslstl_chrono.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_chrono.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_CHRONO
9#define INCLUDED_BSLSTL_CHRONO
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_chrono bslstl_chrono
15/// @brief Provide functionality of the corresponding C++ Standard header.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_chrono
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_chrono-purpose"> Purpose</a>
25/// * <a href="#bslstl_chrono-canonical-header"> Canonical Header </a>
26/// * <a href="#bslstl_chrono-description"> Description </a>
27/// * <a href="#bslstl_chrono-user-defined-literals"> User-defined literals </a>
28/// * <a href="#bslstl_chrono-c-20-calendartz-feature-on-windows"> C++20 CalendarTZ feature on Windows </a>
29/// * <a href="#bslstl_chrono-usage"> Usage </a>
30/// * <a href="#bslstl_chrono-example-1-basic-bsl-chrono-s-udls-usage"> Example 1: Basic bsl-chrono's UDLs Usage </a>
31///
32/// # Purpose {#bslstl_chrono-purpose}
33/// Provide functionality of the corresponding C++ Standard header.
34///
35/// # Canonical Header {#bslstl_chrono-canonical-header}
36/// bsl_chrono.h
37///
38/// # Description {#bslstl_chrono-description}
39/// This component is for internal use only. Please include
40/// `<bsl_chrono.h>` directly. This component imports symbols declared in the
41/// <chrono> header file implemented in the standard library provided by the
42/// compiler vendor.
43///
44/// ## User-defined literals {#bslstl_chrono-user-defined-literals}
45///
46///
47/// This component provides a set of user-defined literals (UDL) to form
48/// `bsl::chrono::duration` objects with various duration periods such as hours,
49/// minutes, seconds, milliseconds, microseconds and nanoseconds. The
50/// ud-suffixes are preceded with the `_` symbol to distinguish between the
51/// `bsl`-chrono's UDLs and the `std`-chrono's UDLs introduced in the C++14
52/// standard and implemented in the standard library. Note that `bsl`-chrono's
53/// UDLs, unlike the `std`-chrono's UDLs, can be used in a client's code if the
54/// current compiler supports the C++11 standard.
55///
56/// Also note that `bsl`-chrono's UDL operators are declared in the
57/// `bsl::literals::chrono_literals` namespace, where `literals` and
58/// @ref chrono_literals are inline namespaces. Access to these operators can be
59/// gained with either `using namespace bsl::literals`,
60/// `using namespace bsl::chrono_literals` or
61/// `using namespace bsl::literals::chrono_literals`. But we recommend
62/// `using namespace bsl::chrono_literals` to minimize the scope of the using
63/// declaration.
64///
65/// ## C++20 CalendarTZ feature on Windows {#bslstl_chrono-c-20-calendartz-feature-on-windows}
66///
67///
68/// This feature has been provided by MSVC++ compiler since VS 2019 16.10. But
69/// that release was shipped with the following important note:
70///
71/// "While the STL generally provides all features on all supported versions of
72/// Windows, leap seconds and time zones (which change over time) require OS
73/// support that was added to Windows 10. Specifically, updating the leap
74/// second database requires Windows 10 version 1809 or later, and time zones
75/// require `icu.dll` which is provided by Windows 10 version 1903/19H1 or
76/// later. This applies to both client and server OSes; note that Windows
77/// Server 2019 is based on Windows 10 version 1809."
78///
79/// If the feature is used on a host that doesn't provide `icu.dll`, an
80/// exception with "The specified module could not be found." message will be
81/// thrown. That is why this feature is disabled by default on Windows.
82///
83/// ## Usage {#bslstl_chrono-usage}
84///
85///
86/// In this section we show intended use of this component.
87///
88/// ### Example 1: Basic bsl-chrono's UDLs Usage {#bslstl_chrono-example-1-basic-bsl-chrono-s-udls-usage}
89///
90///
91/// This example demonstrates basic use of user-defined literal operators.
92///
93/// First, we provide an access to `bsl`-chrono's UDLs.
94/// @code
95/// using namespace bsl::chrono_literals;
96/// @endcode
97/// Then, we construct two duration objects that represent a 24-hours and a half
98/// an hour time intervals using `operator""_h`.
99/// @code
100/// auto hours_in_a_day = 24_h;
101/// auto halfhour = 0.5_h;
102/// @endcode
103/// Finally, stream the two objects to `stdout`:
104/// @code
105/// printf("one day is %lld hours\n",
106/// static_cast<long long>(hours_in_a_day.count()));
107/// printf("half an hour is %.1f hours\n",
108/// static_cast<double>(halfhour.count()));
109/// @endcode
110/// The streaming operator produces output in the following format on `stdout`:
111/// @code
112/// one day is 24 hours
113/// half an hour is 0.5 hours
114/// @endcode
115/// @}
116/** @} */
117/** @} */
118
119/** @addtogroup bsl
120 * @{
121 */
122/** @addtogroup bslstl
123 * @{
124 */
125/** @addtogroup bslstl_chrono
126 * @{
127 */
128
129#include <bslscm_version.h>
130
131#include <bslstl_ratio.h>
132
134#include <bsls_keyword.h>
135#include <bsls_libraryfeatures.h>
136#include <bsls_platform.h>
137
138#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
139 #include <bsls_nativestd.h>
140#endif // not BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
141
142#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
143
144#include <chrono>
145
146namespace bsl {
147namespace chrono {
148
149using std::chrono::duration;
150using std::chrono::time_point;
151using std::chrono::system_clock;
152using std::chrono::steady_clock;
153using std::chrono::high_resolution_clock;
154using std::chrono::treat_as_floating_point;
155using std::chrono::duration_values;
156using std::chrono::duration_cast;
157using std::chrono::time_point_cast;
158using std::chrono::hours;
159using std::chrono::minutes;
160using std::chrono::seconds;
161using std::chrono::milliseconds;
162using std::chrono::microseconds;
163using std::chrono::nanoseconds;
164
165#ifdef BSLS_LIBRARYFEATURES_HAS_CPP14_BASELINE_LIBRARY
166/// This template variable represents the result value of the
167/// `std::chrono::treat_as_floating_point` meta-function.
168template <class TYPE>
169constexpr bool treat_as_floating_point_v =
170 std::chrono::treat_as_floating_point<TYPE>::value;
171#endif // BSLS_LIBRARYFEATURES_HAS_CPP14_BASELINE_LIBRARY
172
173#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
174using std::chrono::abs;
175using std::chrono::ceil;
176using std::chrono::floor;
177using std::chrono::round;
178#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
179
180#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_CALENDAR
181using std::chrono::days;
182using std::chrono::weeks;
183using std::chrono::months;
184using std::chrono::years;
185
186using std::chrono::is_clock;
187using std::chrono::is_clock_v;
188//using std::chrono::system_clock;
189 using std::chrono::sys_time;
190 using std::chrono::sys_seconds;
191 using std::chrono::sys_days;
192using std::chrono::file_clock;
193 using std::chrono::file_time;
194using std::chrono::local_t;
195 using std::chrono::local_time;
196 using std::chrono::local_seconds;
197 using std::chrono::local_days;
198
199using std::chrono::clock_time_conversion;
200using std::chrono::clock_cast;
201
202using std::chrono::last_spec;
203using std::chrono::last;
204using std::chrono::day;
205using std::chrono::month;
206 using std::chrono::January;
207 using std::chrono::February;
208 using std::chrono::March;
209 using std::chrono::April;
210 using std::chrono::May;
211 using std::chrono::June;
212 using std::chrono::July;
213 using std::chrono::August;
214 using std::chrono::September;
215 using std::chrono::October;
216 using std::chrono::November;
217 using std::chrono::December;
218using std::chrono::year;
219using std::chrono::weekday;
220 using std::chrono::Sunday;
221 using std::chrono::Monday;
222 using std::chrono::Tuesday;
223 using std::chrono::Wednesday;
224 using std::chrono::Thursday;
225 using std::chrono::Friday;
226 using std::chrono::Saturday;
227using std::chrono::weekday_indexed;
228using std::chrono::weekday_last;
229
230using std::chrono::month_day;
231using std::chrono::month_day_last;
232using std::chrono::month_weekday;
233using std::chrono::month_weekday_last;
234using std::chrono::year_month;
235using std::chrono::year_month_day;
236using std::chrono::year_month_day_last;
237using std::chrono::year_month_weekday;
238using std::chrono::year_month_weekday_last;
239
240using std::chrono::hh_mm_ss;
241
242using std::chrono::is_am;
243using std::chrono::is_pm;
244using std::chrono::make12;
245using std::chrono::make24;
246
247#ifndef BSLS_PLATFORM_OS_WINDOWS
248// See {C++20 Calendar/TZ feature on Windows}
249using std::chrono::utc_clock;
250 using std::chrono::utc_time;
251 using std::chrono::utc_seconds;
252using std::chrono::tai_clock;
253 using std::chrono::tai_time;
254 using std::chrono::tai_seconds;
255using std::chrono::gps_clock;
256 using std::chrono::gps_time;
257 using std::chrono::gps_seconds;
258#endif // ndef BSLS_PLATFORM_OS_WINDOWS
259
260using std::chrono::from_stream;
261using std::chrono::parse;
262#endif // BSLS_LIBRARYFEATURES_HAS_CPP20_CALENDAR
263
264#ifndef BSLS_PLATFORM_OS_WINDOWS
265// See {C++20 Calendar/TZ feature on Windows}
266#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_TIMEZONE
267using std::chrono::tzdb;
268using std::chrono::tzdb_list;
269using std::chrono::sys_info;
270using std::chrono::local_info;
271using std::chrono::get_tzdb_list;
272using std::chrono::get_tzdb;
273using std::chrono::remote_version;
274using std::chrono::reload_tzdb;
275
276using std::chrono::time_zone;
277using std::chrono::choose;
278using std::chrono::zoned_traits;
279using std::chrono::zoned_time;
280using std::chrono::zoned_seconds;
281using std::chrono::time_zone_link;
282
283using std::chrono::nonexistent_local_time;
284using std::chrono::ambiguous_local_time;
285
286using std::chrono::leap_second;
287using std::chrono::leap_second_info;
288using std::chrono::get_leap_second_info;
289#endif // BSLS_LIBRARYFEATURES_HAS_CPP20_TIMEZONE
290#endif // ndef BSLS_PLATFORM_OS_WINDOWS
291} // close namespace chrono
292
293#ifdef BSLS_LIBRARYFEATURES_HAS_CPP14_BASELINE_LIBRARY
294inline namespace literals {
295inline namespace chrono_literals {
296
297using namespace std::literals::chrono_literals;
298
299} // close chrono_literals namespace
300} // close literals namespace
301#endif // not BSLS_PLATFORM_OS_WINDOWS
302
303#if defined (BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) && \
304 defined (BSLS_COMPILERFEATURES_SUPPORT_INLINE_NAMESPACE)
305
306inline namespace literals {
307inline namespace chrono_literals {
308
309/// Create a `bsl::chrono::duration` object having the
310/// `bsl::ratio<3600, 1>` duration period and initialized with the
311/// specified `hrs` number of hours.
313bsl::chrono::hours operator""_h(unsigned long long hrs);
315bsl::chrono::duration<long double, bsl::ratio<3600, 1>> operator""_h(
316 long double hrs);
317
318/// Create a `bsl::chrono::duration` object having the
319/// `bsl::ratio<60, 1>` duration period and initialized with the
320/// specified `mins` number of minutes.
322bsl::chrono::minutes operator""_min(unsigned long long mins);
324bsl::chrono::duration<long double, bsl::ratio<60, 1>> operator""_min(
325 long double mins);
326
327/// Create a `bsl::chrono::duration` object having the `bsl::ratio<1>`
328/// duration period and initialized with the specified `secs` number of
329/// seconds.
331bsl::chrono::seconds operator""_s(unsigned long long secs);
333bsl::chrono::duration<long double> operator""_s(long double secs);
334
335/// Create a `bsl::chrono::duration` object having the
336/// `bsl::milli` duration period and initialized with the specified `ms`
337/// number of milliseconds.
339bsl::chrono::milliseconds operator""_ms(unsigned long long ms);
341bsl::chrono::duration<long double, bsl::milli> operator""_ms(long double ms);
342
343/// Create a `bsl::chrono::duration` object having the
344/// `bsl::micro` duration period and initialized with the specified `us`
345/// number of microseconds.
347bsl::chrono::microseconds operator""_us(unsigned long long us);
349bsl::chrono::duration<long double, bsl::micro> operator""_us(long double us);
350
351/// Create a `bsl::chrono::duration` object having the `bsl::nano`
352/// duration period and initialized with the specified `ns` number of
353/// microseconds.
355bsl::chrono::nanoseconds operator""_ns(unsigned long long ns);
357bsl::chrono::duration<long double, bsl::nano> operator""_ns(long double ns);
358
359} // close chrono_literals namespace
360} // close literals namespace
361#endif // has C++11 baseline and inline namespaces
362
363// ============================================================================
364// INLINE AND TEMPLATE FUNCTION DEFINITIONS
365// ============================================================================
366
367#if defined (BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) && \
368 defined (BSLS_COMPILERFEATURES_SUPPORT_INLINE_NAMESPACE)
369
370inline namespace literals {
371inline namespace chrono_literals {
372
374bsl::chrono::hours operator""_h(unsigned long long hrs)
375{
376 return bsl::chrono::hours(hrs);
377}
378
380bsl::chrono::duration<long double, bsl::ratio<3600, 1>> operator""_h(
381 long double hrs)
382{
383 return bsl::chrono::duration<long double, bsl::ratio<3600, 1>>(hrs);
384}
385
387bsl::chrono::minutes operator""_min(unsigned long long mins)
388{
389 return bsl::chrono::minutes(mins);
390}
391
393bsl::chrono::duration<long double, bsl::ratio<60, 1>>
394 operator""_min(long double mins)
395{
396 return bsl::chrono::duration<long double, bsl::ratio<60, 1>>(mins);
397}
398
400bsl::chrono::seconds operator""_s(unsigned long long secs)
401{
402 return bsl::chrono::seconds(secs);
403}
404
406bsl::chrono::duration<long double> operator""_s(long double secs)
407{
408 return bsl::chrono::duration<long double>(secs);
409}
410
412bsl::chrono::milliseconds operator""_ms(unsigned long long ms)
413{
414 return bsl::chrono::milliseconds(ms);
415}
416
418bsl::chrono::duration<long double, bsl::milli> operator""_ms(long double ms)
419{
420 return bsl::chrono::duration<long double, bsl::milli>(ms);
421}
422
424bsl::chrono::microseconds operator""_us(unsigned long long us)
425{
426 return bsl::chrono::microseconds(us);
427}
428
430bsl::chrono::duration<long double, bsl::micro> operator""_us(long double us)
431{
432 return bsl::chrono::duration<long double, bsl::micro>(us);
433}
434
436bsl::chrono::nanoseconds operator""_ns(unsigned long long ns)
437{
438 return bsl::chrono::nanoseconds(ns);
439}
440
442bsl::chrono::duration<long double, bsl::nano> operator""_ns(long double ns)
443{
444 return bsl::chrono::duration<long double, bsl::nano>(ns);
445}
446
447} // close chrono_literals namespace
448} // close literals namespace
449#endif // has C++11 baseline and inline namespaces
450} // close package namespace
451
452#endif // BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
453#endif // INCLUDED_BSLSTL_CHRONO
454
455// ----------------------------------------------------------------------------
456// Copyright 2018 Bloomberg Finance L.P.
457//
458// Licensed under the Apache License, Version 2.0 (the "License");
459// you may not use this file except in compliance with the License.
460// You may obtain a copy of the License at
461//
462// http://www.apache.org/licenses/LICENSE-2.0
463//
464// Unless required by applicable law or agreed to in writing, software
465// distributed under the License is distributed on an "AS IS" BASIS,
466// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
467// See the License for the specific language governing permissions and
468// limitations under the License.
469// ----------------------------- END-OF-FILE ----------------------------------
470
471/** @} */
472/** @} */
473/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:624
Definition bdlat_valuetypefunctions.h:939