BDE 4.14.0 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
156namespace bdlt {
157
158 // ===============
159 // struct TimeUtil
160 // ===============
161
162/// This `struct` provides a namespace for common non-primitive procedures
163/// that operate on `Time` objects. These methods are alias-safe and
164/// exception-neutral.
165struct TimeUtil {
166
167 private:
168 // PRIVATE TYPES
169 enum {
170 k_HHMMSSMMM_HH_FACTOR = 10000000,
171 k_HHMMSSMMM_MM_FACTOR = 100000,
172 k_HHMMSSMMM_SS_FACTOR = 1000,
173
174 k_HHMMSS_HH_FACTOR = 10000,
175 k_HHMMSS_MM_FACTOR = 100,
176
177 k_HHMM_HH_FACTOR = 100
178 };
179
180 public:
181 // CLASS METHODS
182
183 /// Return the `bdlt::Time` value corresponding to the specified
184 /// `timeValue`, where `timeValue` is a non-negative integer that, when
185 /// expressed in decimal notation, contains exactly four digits
186 /// (counting leading zeros, if any): two digits for the hour and two
187 /// digits for the minute. For example, 309 is converted to
188 /// `Time(3, 9)` (03:09:00.000). More formally, `timeValue` is
189 /// interpreted as:
190 /// @code
191 /// hour * 100 + minute
192 /// @endcode
193 /// The behavior is undefined unless `timeValue` represents a valid time
194 /// in the allowable range for `bdlt::Time`
195 /// (00:00:00.000 - 23:59:00.000, and 24:00:00.000).
196 static Time convertFromHHMM(int timeValue);
197
198 /// Return the `bdlt::Time` value corresponding to the specified
199 /// `timeValue`, where `timeValue` is a non-negative integer that, when
200 /// expressed in decimal notation, contains exactly six digits (counting
201 /// leading zeros, if any): two digits for the hour, two digits for the
202 /// minute, and two digits for the second. For example, 30907 is
203 /// converted to `Time(3, 9, 7)` (03:09:07.000). More formally,
204 /// `timeValue` is interpreted as:
205 /// @code
206 /// hour * 10000 + minute * 100 + second
207 /// @endcode
208 /// The behavior is undefined unless `timeValue` represents a valid time
209 /// in the allowable range for `bdlt::Time`
210 /// (00:00:00.000 - 23:59:59.000, and 24:00:00.000).
211 static Time convertFromHHMMSS(int timeValue);
212
213 /// Return the `bdlt::Time` value corresponding to the specified
214 /// `timeValue`, where `timeValue` is a non-negative integer that, when
215 /// expressed in decimal notation, contains exactly nine digits
216 /// (counting leading zeros, if any): two digits for the hour, two
217 /// digits for the minute, two digits for the second, and three digits
218 /// for the millisecond. For example, 30907056 is converted to
219 /// `Time(3, 9, 7, 56)` (03:09:07.056). More formally, `timeValue` is
220 /// interpreted as:
221 /// @code
222 /// hour * 10000000 + minute * 100000 + second * 1000 + millisecond
223 /// @endcode
224 /// The behavior is undefined unless `timeValue` represents a valid time
225 /// in the allowable range for `bdlt::Time`
226 /// (00:00:00.000 - 23:59:59.999, and 24:00:00.000).
227 static Time convertFromHHMMSSmmm(int timeValue);
228
229 /// Return the non-negative integer representing the same time as the
230 /// specified `value` that, when expressed in decimal notation, contains
231 /// exactly four digits (counting leading zeros, if any): two digits for
232 /// the hour and two digits for the minute. For example,
233 /// `Time(3, 9, sec, ms)`, where `0 <= sec < 60` and `0 <= ms < 1000`,
234 /// is converted to 309. More formally, this method returns:
235 /// @code
236 /// value.hour() * 100 + value.minute()
237 /// @endcode
238 static int convertToHHMM(const Time& value);
239
240 /// Return the non-negative integer representing the same time as the
241 /// specified `value` that, when expressed in decimal notation, contains
242 /// exactly six digits (counting leading zeros, if any): two digits for
243 /// the hour, two digits for the minute, and two digits for the second.
244 /// For example, `Time(3, 9, 7, ms)`, where `0 <= ms < 1000`, is
245 /// converted to 30907. More formally, this method returns:
246 /// @code
247 /// value.hour() * 10000 + value.minute() * 100 + value.second()
248 /// @endcode
249 static int convertToHHMMSS(const Time& value);
250
251 /// Return the non-negative integer representing the same time as the
252 /// specified `value` that, when expressed in decimal notation, contains
253 /// exactly nine digits (counting leading zeros, if any): two digits for
254 /// the hour, two digits for the minute, two digits for the second, and
255 /// three digits for the millisecond. For example, `Time(3, 9, 7, 56)`
256 /// is converted to 30907056. More formally, this method returns:
257 /// @code
258 /// value.hour() * 10000000 + value.minute() * 100000
259 /// + value.second() * 1000
260 /// + value.millisecond()
261 /// @endcode
262 static int convertToHHMMSSmmm(const Time& value);
263
264 /// Return `true` if the specified `timeValue` is a non-negative integer
265 /// that represents a valid four-digit time value suitable for passing
266 /// to `convertFromHHMM`, and `false` otherwise. `timeValue` is a valid
267 /// four-digit time value if, when expressed in decimal notation, it
268 /// contains exactly four digits (counting leading zeros, if any): two
269 /// digits for the hour and two digits for the minute, where either
270 /// `0 <= hour < 24` and `0 <= minute < 60`, or `2400 == timeValue`.
271 static bool isValidHHMM(int timeValue);
272
273 /// Return `true` if the specified `timeValue` is a non-negative integer
274 /// that represents a valid six-digit time value suitable for passing to
275 /// `convertFromHHMMSS`, and `false` otherwise. `timeValue` is a valid
276 /// six-digit time value if, when expressed in decimal notation, it
277 /// contains exactly six digits (counting leading zeros, if any): two
278 /// digits for the hour, two digits for the minute, and two digits for
279 /// the second, where either `0 <= hour < 24`, `0 <= minute < 60`, and
280 /// `0 <= second < 60`, or `240000 == timeValue`.
281 static bool isValidHHMMSS(int timeValue);
282
283 /// Return `true` if the specified `timeValue` is a non-negative integer
284 /// that represents a valid nine-digit time value suitable for passing
285 /// to `convertFromHHMMSSmmm`, and `false` otherwise. `timeValue` is a
286 /// valid nine-digit time value if, when expressed in decimal notation,
287 /// it contains exactly nine digits (counting leading zeros, if any):
288 /// two digits for the hour, two digits for the minute, two digits for
289 /// the second, and three digits for the millisecond, where either
290 /// `0 <= hour < 24`, `0 <= minute < 60`, `0 <= second < 60`, and
291 /// `0 <= millisecond < 1000`, or `240000000 == timeValue`.
292 static bool isValidHHMMSSmmm(int timeValue);
293};
294
295// ============================================================================
296// INLINE FUNCTION DEFINITIONS
297// ============================================================================
298
299 // ---------------
300 // struct TimeUtil
301 // ---------------
302
303 // -----------------
304 // Level-0 Functions
305 // -----------------
306
307// CLASS METHODS
308inline
309bool TimeUtil::isValidHHMM(int timeValue)
310{
311 return Time::isValid(timeValue / k_HHMM_HH_FACTOR,
312 timeValue % k_HHMM_HH_FACTOR);
313}
314
315inline
316bool TimeUtil::isValidHHMMSS(int timeValue)
317{
318 return Time::isValid(timeValue / k_HHMMSS_HH_FACTOR,
319 (timeValue % k_HHMMSS_HH_FACTOR) / k_HHMMSS_MM_FACTOR,
320 timeValue % k_HHMMSS_MM_FACTOR);
321}
322
323inline
324bool TimeUtil::isValidHHMMSSmmm(int timeValue)
325{
326 return Time::isValid(
327 timeValue / k_HHMMSSMMM_HH_FACTOR,
328 (timeValue % k_HHMMSSMMM_HH_FACTOR) / k_HHMMSSMMM_MM_FACTOR,
329 (timeValue % k_HHMMSSMMM_MM_FACTOR) / k_HHMMSSMMM_SS_FACTOR,
330 timeValue % k_HHMMSSMMM_SS_FACTOR);
331}
332
333 // -------------------
334 // All Other Functions
335 // -------------------
336
337inline
339{
341
342 return Time(timeValue / k_HHMM_HH_FACTOR,
343 timeValue % k_HHMM_HH_FACTOR);
344}
345
346inline
348{
350
351 return Time(timeValue / k_HHMMSS_HH_FACTOR,
352 (timeValue % k_HHMMSS_HH_FACTOR) / k_HHMMSS_MM_FACTOR,
353 timeValue % k_HHMMSS_MM_FACTOR);
354}
355
356inline
358{
360
361 return Time(timeValue / k_HHMMSSMMM_HH_FACTOR,
362 (timeValue % k_HHMMSSMMM_HH_FACTOR) / k_HHMMSSMMM_MM_FACTOR,
363 (timeValue % k_HHMMSSMMM_MM_FACTOR) / k_HHMMSSMMM_SS_FACTOR,
364 timeValue % k_HHMMSSMMM_SS_FACTOR);
365}
366
367inline
369{
370 return value.hour() * k_HHMM_HH_FACTOR + value.minute();
371}
372
373inline
375{
376 return value.hour() * k_HHMMSS_HH_FACTOR
377 + value.minute() * k_HHMMSS_MM_FACTOR
378 + value.second();
379}
380
381inline
383{
384 return value.hour() * k_HHMMSSMMM_HH_FACTOR
385 + value.minute() * k_HHMMSSMMM_MM_FACTOR
386 + value.second() * k_HHMMSSMMM_SS_FACTOR
387 + value.millisecond();
388}
389
390} // close package namespace
391
392
393#endif
394
395// ----------------------------------------------------------------------------
396// Copyright 2015 Bloomberg Finance L.P.
397//
398// Licensed under the Apache License, Version 2.0 (the "License");
399// you may not use this file except in compliance with the License.
400// You may obtain a copy of the License at
401//
402// http://www.apache.org/licenses/LICENSE-2.0
403//
404// Unless required by applicable law or agreed to in writing, software
405// distributed under the License is distributed on an "AS IS" BASIS,
406// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
407// See the License for the specific language governing permissions and
408// limitations under the License.
409// ----------------------------- END-OF-FILE ----------------------------------
410
411/** @} */
412/** @} */
413/** @} */
Definition bdlt_time.h:196
static bool isValid(int hour, int minute=0, int second=0, int millisecond=0, int microsecond=0)
Definition bdlt_time.h:697
int second() const
Return the value of the second attribute of this time object.
Definition bdlt_time.h:928
int millisecond() const
Return the value of the millisecond attribute of this time object.
Definition bdlt_time.h:912
int minute() const
Return the value of the minute attribute of this time object.
Definition bdlt_time.h:920
int hour() const
Return the value of the hour attribute of this time object.
Definition bdlt_time.h:898
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112
Definition bdlt_timeutil.h:165
static int convertToHHMMSSmmm(const Time &value)
Definition bdlt_timeutil.h:382
static bool isValidHHMMSSmmm(int timeValue)
Definition bdlt_timeutil.h:324
static Time convertFromHHMMSS(int timeValue)
Definition bdlt_timeutil.h:347
static Time convertFromHHMM(int timeValue)
Definition bdlt_timeutil.h:338
static int convertToHHMMSS(const Time &value)
Definition bdlt_timeutil.h:374
static int convertToHHMM(const Time &value)
Definition bdlt_timeutil.h:368
static Time convertFromHHMMSSmmm(int timeValue)
Definition bdlt_timeutil.h:357
static bool isValidHHMM(int timeValue)
Definition bdlt_timeutil.h:309
static bool isValidHHMMSS(int timeValue)
Definition bdlt_timeutil.h:316