BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_timeutil.h
Go to the documentation of this file.
1/// @file bdlt_timeutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_timeutil.h -*-C++-*-
8#ifndef INCLUDED_BDLT_TIMEUTIL
9#define INCLUDED_BDLT_TIMEUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_timeutil bdlt_timeutil
15/// @brief Provide common non-primitive operations on `bdlt::Time`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_timeutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_timeutil-purpose"> Purpose</a>
25/// * <a href="#bdlt_timeutil-classes"> Classes </a>
26/// * <a href="#bdlt_timeutil-description"> Description </a>
27/// * <a href="#bdlt_timeutil-converting-from-seconds-from-midnight-to-bdlt-datetime"> Converting from Seconds-from-Midnight to bdlt::Datetime </a>
28/// * <a href="#bdlt_timeutil-usage"> Usage </a>
29/// * <a href="#bdlt_timeutil-example-1"> Example 1 </a>
30/// * <a href="#bdlt_timeutil-example-2"> Example 2 </a>
31///
32/// # Purpose {#bdlt_timeutil-purpose}
33/// Provide common non-primitive operations on `bdlt::Time`.
34///
35/// # Classes {#bdlt_timeutil-classes}
36///
37/// - bdlt::TimeUtil: namespace for static functions operating on `bdlt::Time`
38///
39/// @see bdlt_time
40///
41/// # Description {#bdlt_timeutil-description}
42/// This component provides non-primitive operations on
43/// `bdlt::Time` objects. In particular, the `bdlt::TimeUtil` namespace defined
44/// in this component provides conversions among `bdlt::Time` values and their
45/// corresponding non-negative integral values (e.g., `convertFromHHMM`,
46/// `convertToHHMMSSmmm`), and methods to validate such integral values (e.g.,
47/// `isValidHHMMSS`) before passing them to the corresponding "convertFrom"
48/// method.
49///
50/// ## Converting from Seconds-from-Midnight to bdlt::Datetime {#bdlt_timeutil-converting-from-seconds-from-midnight-to-bdlt-datetime}
51///
52///
53/// Seconds-from-midnight is a date-time representation used in some financial
54/// applications. Seconds-from-midnight is a lossy representation (using
55/// heuristics to determine the date), and conversions for that representation
56/// are provided in a higher-level package that is not part of the
57/// BDE open-source libraries (see @ref bsitzo_secondsfrommidnightutil ).
58///
59/// ## Usage {#bdlt_timeutil-usage}
60///
61///
62/// Following are examples illustrating basic use of this component.
63///
64/// ### Example 1 {#bdlt_timeutil-example-1}
65///
66///
67/// First, we demonstrate how to use `bdlt::TimeUtil` to
68/// convert from an integer representation of time in "HHMMSSmmm" format to a
69/// `bdlt::Time`. Our first time will be around 3:45 pm.
70/// @code
71/// // format: HHMMSSmmm
72/// int timeValue = 154502789;
73///
74/// bdlt::Time result = bdlt::TimeUtil::convertFromHHMMSSmmm(timeValue);
75///
76/// bsl::cout << result << bsl::endl;
77/// @endcode
78/// The code above produces the following on `stdout`:
79/// @code
80/// 15:45:02.789
81/// @endcode
82/// Then, we demonstrate a different time, 3:32:24.832 am. Note that we do not
83/// lead the integer value with `0`:
84/// @code
85/// // format: HHMMSSmmm
86/// timeValue = 33224832; // Do not start with leading '0' as that would
87/// // make the value octal and incorrect.
88///
89/// result = bdlt::TimeUtil::convertFromHHMMSSmmm(timeValue);
90///
91/// bsl::cout << result << bsl::endl;
92/// @endcode
93/// The code above produces the following on `stdout`:
94/// @code
95/// 03:32:24.832
96/// @endcode
97/// Now, we demonstrate how `bdlt::TimeUtil` provides methods that can be used
98/// to validate integral time values before passing them to the various
99/// "convert" methods. For example:
100/// @code
101/// assert( bdlt::TimeUtil::isValidHHMMSSmmm(timeValue));
102/// @endcode
103/// Finally, we demonstrate catching an invalid time value, 12:61:02.789 pm:
104/// @code
105/// // format: HHMMSSmmm
106/// int badTimeValue = 126102789;
107///
108/// assert(!bdlt::TimeUtil::isValidHHMMSSmmm(badTimeValue));
109/// @endcode
110///
111/// ### Example 2 {#bdlt_timeutil-example-2}
112///
113///
114/// The following snippet of code demonstrates how to use `bdlt::TimeUtil` to
115/// convert from a `bdlt::Time` to an integer representation of time in "HHMM",
116/// "HHMMSS", and "HHMMSSmmm" formats:
117/// @code
118/// bdlt::Time time(12, 45, 2, 789);
119/// int timeHHMM = bdlt::TimeUtil::convertToHHMM(time);
120/// int timeHHMMSS = bdlt::TimeUtil::convertToHHMMSS(time);
121/// int timeHHMMSSmmm = bdlt::TimeUtil::convertToHHMMSSmmm(time);
122///
123/// bsl::cout << "Time in HHMM: " << timeHHMM << bsl::endl;
124/// bsl::cout << "Time in HHMMSS: " << timeHHMMSS << bsl::endl;
125/// bsl::cout << "Time in HHMMSSmmm: " << timeHHMMSSmmm << bsl::endl;
126/// @endcode
127/// The code above produces the following on `stdout`:
128/// @code
129/// Time in HHMM: 1245
130/// Time in HHMMSS: 124502
131/// Time in HHMMSSmmm: 124502789
132/// @endcode
133/// Note that the millisecond and/or second fields of `bdlt::Time` are ignored
134/// depending on the conversion method that is called.
135/// @}
136/** @} */
137/** @} */
138
139/** @addtogroup bdl
140 * @{
141 */
142/** @addtogroup bdlt
143 * @{
144 */
145/** @addtogroup bdlt_timeutil
146 * @{
147 */
148
149#include <bdlscm_version.h>
150
151#include <bdlt_time.h>
152
153#include <bsls_assert.h>
154
155#include <bsl_optional.h>
156
157
158namespace bdlt {
159
160 // ===============
161 // struct TimeUtil
162 // ===============
163
164/// This `struct` provides a namespace for common non-primitive procedures
165/// that operate on `Time` objects. These methods are alias-safe and
166/// exception-neutral.
167///
168/// See @ref bdlt_timeutil
169struct TimeUtil {
170
171 private:
172 // PRIVATE TYPES
173 enum {
174 k_HHMMSSMMM_HH_FACTOR = 10000000,
175 k_HHMMSSMMM_MM_FACTOR = 100000,
176 k_HHMMSSMMM_SS_FACTOR = 1000,
177
178 k_HHMMSS_HH_FACTOR = 10000,
179 k_HHMMSS_MM_FACTOR = 100,
180
181 k_HHMM_HH_FACTOR = 100
182 };
183
184 public:
185 // CLASS METHODS
186
187 /// Return the `bdlt::Time` value corresponding to the specified
188 /// `timeValue`, where `timeValue` is a non-negative integer that, when
189 /// expressed in decimal notation, contains exactly four digits
190 /// (counting leading zeros, if any): two digits for the hour and two
191 /// digits for the minute. For example, 309 is converted to
192 /// `Time(3, 9)` (03:09:00.000). More formally, `timeValue` is
193 /// interpreted as:
194 /// @code
195 /// hour * 100 + minute
196 /// @endcode
197 ///
198 /// \pre The behavior is undefined unless `timeValue` represents a valid time
199 /// in the allowable range for `bdlt::Time`
200 /// (00:00:00.000 - 23:59:00.000, and 24:00:00.000).
201 static Time convertFromHHMM(int timeValue);
202
203 /// Return the `bdlt::Time` value corresponding to the specified
204 /// `timeValue`, where `timeValue` is a non-negative integer that, when
205 /// expressed in decimal notation, contains exactly six digits (counting
206 /// leading zeros, if any): two digits for the hour, two digits for the
207 /// minute, and two digits for the second. For example, 30907 is
208 /// converted to `Time(3, 9, 7)` (03:09:07.000). More formally,
209 /// `timeValue` is interpreted as:
210 /// @code
211 /// hour * 10000 + minute * 100 + second
212 /// @endcode
213 ///
214 /// \pre The behavior is undefined unless `timeValue` represents a valid time
215 /// in the allowable range for `bdlt::Time`
216 /// (00:00:00.000 - 23:59:59.000, and 24:00:00.000).
217 static Time convertFromHHMMSS(int timeValue);
218
219 /// Return the `bdlt::Time` value corresponding to the specified
220 /// `timeValue`, where `timeValue` is a non-negative integer that, when
221 /// expressed in decimal notation, contains exactly nine digits
222 /// (counting leading zeros, if any): two digits for the hour, two
223 /// digits for the minute, two digits for the second, and three digits
224 /// for the millisecond. For example, 30907056 is converted to
225 /// `Time(3, 9, 7, 56)` (03:09:07.056). More formally, `timeValue` is
226 /// interpreted as:
227 /// @code
228 /// hour * 10000000 + minute * 100000 + second * 1000 + millisecond
229 /// @endcode
230 ///
231 /// \pre The behavior is undefined unless `timeValue` represents a valid time
232 /// in the allowable range for `bdlt::Time`
233 /// (00:00:00.000 - 23:59:59.999, and 24:00:00.000).
234 static Time convertFromHHMMSSmmm(int timeValue);
235
236 /// Return the non-negative integer representing the same time as the
237 /// specified `value` that, when expressed in decimal notation, contains
238 /// exactly four digits (counting leading zeros, if any): two digits for
239 /// the hour and two digits for the minute. For example,
240 /// `Time(3, 9, sec, ms)`, where `0 <= sec < 60` and `0 <= ms < 1000`,
241 /// is converted to 309. More formally, this method returns:
242 /// @code
243 /// value.hour() * 100 + value.minute()
244 /// @endcode
245 static int convertToHHMM(const Time& value);
246
247 /// Return the non-negative integer representing the same time as the
248 /// specified `value` that, when expressed in decimal notation, contains
249 /// exactly six digits (counting leading zeros, if any): two digits for
250 /// the hour, two digits for the minute, and two digits for the second.
251 /// For example, `Time(3, 9, 7, ms)`, where `0 <= ms < 1000`, is
252 /// converted to 30907. More formally, this method returns:
253 /// @code
254 /// value.hour() * 10000 + value.minute() * 100 + value.second()
255 /// @endcode
256 static int convertToHHMMSS(const Time& value);
257
258 /// Return the non-negative integer representing the same time as the
259 /// specified `value` that, when expressed in decimal notation, contains
260 /// exactly nine digits (counting leading zeros, if any): two digits for
261 /// the hour, two digits for the minute, two digits for the second, and
262 /// three digits for the millisecond. For example, `Time(3, 9, 7, 56)`
263 /// is converted to 30907056. More formally, this method returns:
264 /// @code
265 /// value.hour() * 10000000 + value.minute() * 100000
266 /// + value.second() * 1000
267 /// + value.millisecond()
268 /// @endcode
269 static int convertToHHMMSSmmm(const Time& value);
270
271 /// Return an `optional` having a `Time` with the specified `hour`,
272 /// `minute`, `second`, and the optionally specified `millisecond` and
273 /// `microsecond`, if those form a valid `Time` (see `Time::isValid`);
274 /// otherwise return an `optional` without a value.
275 static bsl::optional<Time> fromHms(int hour,
276 int minute,
277 int second,
278 int millisecond = 0,
279 int microsecond = 0);
280
281 /// Return `true` if the specified `timeValue` is a non-negative integer
282 /// that represents a valid four-digit time value suitable for passing
283 /// to `convertFromHHMM`, and `false` otherwise. `timeValue` is a valid
284 /// four-digit time value if, when expressed in decimal notation, it
285 /// contains exactly four digits (counting leading zeros, if any): two
286 /// digits for the hour and two digits for the minute, where either
287 /// `0 <= hour < 24` and `0 <= minute < 60`, or `2400 == timeValue`.
288 static bool isValidHHMM(int timeValue);
289
290 /// Return `true` if the specified `timeValue` is a non-negative integer
291 /// that represents a valid six-digit time value suitable for passing to
292 /// `convertFromHHMMSS`, and `false` otherwise. `timeValue` is a valid
293 /// six-digit time value if, when expressed in decimal notation, it
294 /// contains exactly six digits (counting leading zeros, if any): two
295 /// digits for the hour, two digits for the minute, and two digits for
296 /// the second, where either `0 <= hour < 24`, `0 <= minute < 60`, and
297 /// `0 <= second < 60`, or `240000 == timeValue`.
298 static bool isValidHHMMSS(int timeValue);
299
300 /// Return `true` if the specified `timeValue` is a non-negative integer
301 /// that represents a valid nine-digit time value suitable for passing
302 /// to `convertFromHHMMSSmmm`, and `false` otherwise. `timeValue` is a
303 /// valid nine-digit time value if, when expressed in decimal notation,
304 /// it contains exactly nine digits (counting leading zeros, if any):
305 /// two digits for the hour, two digits for the minute, two digits for
306 /// the second, and three digits for the millisecond, where either
307 /// `0 <= hour < 24`, `0 <= minute < 60`, `0 <= second < 60`, and
308 /// `0 <= millisecond < 1000`, or `240000000 == timeValue`.
309 static bool isValidHHMMSSmmm(int timeValue);
310};
311
312// ============================================================================
313// INLINE FUNCTION DEFINITIONS
314// ============================================================================
315
316 // ---------------
317 // struct TimeUtil
318 // ---------------
319
320 // -----------------
321 // Level-0 Functions
322 // -----------------
323
324// CLASS METHODS
325inline
326bool TimeUtil::isValidHHMM(int timeValue)
327{
328 return Time::isValid(timeValue / k_HHMM_HH_FACTOR,
329 timeValue % k_HHMM_HH_FACTOR);
330}
331
332inline
333bool TimeUtil::isValidHHMMSS(int timeValue)
334{
335 return Time::isValid(timeValue / k_HHMMSS_HH_FACTOR,
336 (timeValue % k_HHMMSS_HH_FACTOR) / k_HHMMSS_MM_FACTOR,
337 timeValue % k_HHMMSS_MM_FACTOR);
338}
339
340inline
341bool TimeUtil::isValidHHMMSSmmm(int timeValue)
342{
343 return Time::isValid(
344 timeValue / k_HHMMSSMMM_HH_FACTOR,
345 (timeValue % k_HHMMSSMMM_HH_FACTOR) / k_HHMMSSMMM_MM_FACTOR,
346 (timeValue % k_HHMMSSMMM_MM_FACTOR) / k_HHMMSSMMM_SS_FACTOR,
347 timeValue % k_HHMMSSMMM_SS_FACTOR);
348}
349
350 // -------------------
351 // All Other Functions
352 // -------------------
353
354inline
356{
358
359 return Time(timeValue / k_HHMM_HH_FACTOR,
360 timeValue % k_HHMM_HH_FACTOR);
361}
362
363inline
365{
367
368 return Time(timeValue / k_HHMMSS_HH_FACTOR,
369 (timeValue % k_HHMMSS_HH_FACTOR) / k_HHMMSS_MM_FACTOR,
370 timeValue % k_HHMMSS_MM_FACTOR);
371}
372
373inline
375{
377
378 return Time(timeValue / k_HHMMSSMMM_HH_FACTOR,
379 (timeValue % k_HHMMSSMMM_HH_FACTOR) / k_HHMMSSMMM_MM_FACTOR,
380 (timeValue % k_HHMMSSMMM_MM_FACTOR) / k_HHMMSSMMM_SS_FACTOR,
381 timeValue % k_HHMMSSMMM_SS_FACTOR);
382}
383
384inline
386{
387 return value.hour() * k_HHMM_HH_FACTOR + value.minute();
388}
389
390inline
392{
393 return value.hour() * k_HHMMSS_HH_FACTOR
394 + value.minute() * k_HHMMSS_MM_FACTOR
395 + value.second();
396}
397
398inline
400{
401 return value.hour() * k_HHMMSSMMM_HH_FACTOR
402 + value.minute() * k_HHMMSSMMM_MM_FACTOR
403 + value.second() * k_HHMMSSMMM_SS_FACTOR
404 + value.millisecond();
405}
406
407inline
409 int minute,
410 int second,
411 int millisecond,
412 int microsecond)
413{
414 return Time::isValid(hour, minute, second, millisecond, microsecond)
416 hour,
417 minute,
418 second,
419 millisecond,
420 microsecond)
421 : bsl::nullopt;
422}
423
424} // close package namespace
425
426
427#endif
428
429// ----------------------------------------------------------------------------
430// Copyright 2015 Bloomberg Finance L.P.
431//
432// Licensed under the Apache License, Version 2.0 (the "License");
433// you may not use this file except in compliance with the License.
434// You may obtain a copy of the License at
435//
436// http://www.apache.org/licenses/LICENSE-2.0
437//
438// Unless required by applicable law or agreed to in writing, software
439// distributed under the License is distributed on an "AS IS" BASIS,
440// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
441// See the License for the specific language governing permissions and
442// limitations under the License.
443// ----------------------------- END-OF-FILE ----------------------------------
444
445/** @} */
446/** @} */
447/** @} */
Definition bdlt_time.h:195
static bool isValid(int hour, int minute=0, int second=0, int millisecond=0, int microsecond=0)
Definition bdlt_time.h:725
int second() const
Return the value of the second attribute of this time object.
Definition bdlt_time.h:956
int millisecond() const
Return the value of the millisecond attribute of this time object.
Definition bdlt_time.h:940
int minute() const
Return the value of the minute attribute of this time object.
Definition bdlt_time.h:948
int hour() const
Return the value of the hour attribute of this time object.
Definition bdlt_time.h:926
Definition bslstl_optional.h:2043
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicisma30360.h:112
const nullopt_t nullopt
const in_place_t in_place
Definition bdlt_timeutil.h:169
static int convertToHHMMSSmmm(const Time &value)
Definition bdlt_timeutil.h:399
static bsl::optional< Time > fromHms(int hour, int minute, int second, int millisecond=0, int microsecond=0)
Definition bdlt_timeutil.h:408
static bool isValidHHMMSSmmm(int timeValue)
Definition bdlt_timeutil.h:341
static Time convertFromHHMMSS(int timeValue)
Definition bdlt_timeutil.h:364
static Time convertFromHHMM(int timeValue)
Definition bdlt_timeutil.h:355
static int convertToHHMMSS(const Time &value)
Definition bdlt_timeutil.h:391
static int convertToHHMM(const Time &value)
Definition bdlt_timeutil.h:385
static Time convertFromHHMMSSmmm(int timeValue)
Definition bdlt_timeutil.h:374
static bool isValidHHMM(int timeValue)
Definition bdlt_timeutil.h:326
static bool isValidHHMMSS(int timeValue)
Definition bdlt_timeutil.h:333