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