BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_timeunitratio

Detailed Description

Outline

Purpose

Provide constants characterizing ratios between common time units.

Classes

Description

This component provides a utility struct, bdlt::TimeUnitRatio, that defines a namespace for constants that characterize the ratios between commonly used time units. Ratios are provided for combinations of all time units from a nanosecond up through a day.

Each constant defined in this namespace has at least two forms, a full form and an abbreviated form following the patterns:

bdlt::TimeUnitRatio::k_SMALLUNITS_PER_LARGEUNIT
bdlt::TimeUnitRatio::k_SU_PER_LU

Where SMALLUNIT and LARGEUNIT are members of the following group:

NANOSECOND
MICROSECOND
MILLISECOND
SECOND
MINUTE
HOUR
DAY

and SU and LU are abbreviations in the following group:

NS // abbreviation for `NANOSECOND`
US // abbreviation for `MICROSECOND`
MS // abbreviation for `MILLISECOND`
S // abbreviation for `SECOND`
M // abbreviation for `MINUTE`
H // abbreviation for `HOUR`
D // abbreviation for `DAY`

Note that SMALLUNIT (SU) is always a smaller unit than LARGEUNIT (LU). Thus, the (whole) number of microseconds in an hour is characterized by the constants:

static const bsls::Types::Int64 k_US_PER_H
Definition bdlt_timeunitratio.h:277
static const bsls::Types::Int64 k_MICROSECONDS_PER_HOUR
Definition bdlt_timeunitratio.h:236

Additionally, ratios that can be represented by a 32-bit integer also have two corresponding 32-bit constants, having the same names as the constants described above, but incorporate a _32 suffix. Thus, the number of milliseconds per minute is characterized by four constants:

static const int k_MS_PER_M_32
Definition bdlt_timeunitratio.h:335
static const bsls::Types::Int64 k_MS_PER_M
Definition bdlt_timeunitratio.h:281
static const int k_MILLISECONDS_PER_MINUTE_32
Definition bdlt_timeunitratio.h:311
static const bsls::Types::Int64 k_MILLISECONDS_PER_MINUTE
Definition bdlt_timeunitratio.h:243

By providing both 64- and 32-bit versions of the constants, this component allows the programmer to minimize the use of casts in time unit calculations.

Usage

This section illustrates intended use of this component.

Example 1: Breaking a Time Interval Into Component Parts

Components that deal with time often need to convert between various units. Each component that needs to perform such conversions could derive the constants needed locally, but doing so would result in an inconsistent vocabulary across components, and multiple opportunities for bugs. bdlt::TimeUnitRatio achieves the desired consistency and avoids bugs by providing a single location for the constants used in such conversions.

Suppose we have a time interval described as an integral number of nanoseconds, and we need to break it down into its constituent second, millisecond, microsecond, and nanosecond parts.

First, we define a variable representing the number of nanoseconds that have elapsed since a particular event:

bsls::Types::Int64 interval = 62003004005; // nanoseconds since event
long long Int64
Definition bsls_types.h:132

Then, we extract the minutes part from the total, using the constant bdlt::TimeUnitRatio::k_NS_PER_M:

bsls::Types::Int64 minutesPart =
static const bsls::Types::Int64 k_NS_PER_M
Definition bdlt_timeunitratio.h:269

Next, we calculate the remaining nanoseconds using the same constant:

Then, we extract the seconds part from the remainder, using the constant bdlt::TimeUnitRatio::k_NS_PER_S:

bsls::Types::Int64 secondsPart =
static const bsls::Types::Int64 k_NS_PER_S
Definition bdlt_timeunitratio.h:268

Next, we calculate the remaining nanoseconds using the same constant:

Then, we extract the milliseconds part from the remainder, using the constant bdlt::TimeUnitRatio::k_NS_PER_MS:

bsls::Types::Int64 millisecondsPart =
static const bsls::Types::Int64 k_NS_PER_MS
Definition bdlt_timeunitratio.h:266

Next, we calculate the remaining nanoseconds using the same constant:

Then, we extract the microseconds part from the remainder, using the constant bdlt::TimeUnitRatio::k_NS_PER_US:

bsls::Types::Int64 microsecondsPart =
static const bsls::Types::Int64 k_NS_PER_US
Definition bdlt_timeunitratio.h:264

Next, we calculate the remaining nanoseconds using the same constant:

Now, we extract the nanoseconds part, which is exactly the remainder we already have:

bsls::Types::Int64 nanosecondsPart = remainder;

Finally, we confirm that the parts we have extracted all have the correct values:

assert(1 == minutesPart);
assert(2 == secondsPart);
assert(3 == millisecondsPart);
assert(4 == microsecondsPart);
assert(5 == nanosecondsPart);

Note that in practice, the number of nanoseconds since the event would be provided by some system utility, and not a constant as was shown here for purposes of exposition.