BDE 4.14.0 Production release
|
Provide constants characterizing ratios between common time units.
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:
Where SMALLUNIT
and LARGEUNIT
are members of the following group:
and SU
and LU
are abbreviations in the following group:
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:
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:
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.
This section illustrates intended use of this component.
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:
Then, we extract the minutes part from the total, using the constant bdlt::TimeUnitRatio::k_NS_PER_M
:
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
:
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
:
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
:
Next, we calculate the remaining nanoseconds using the same constant:
Now, we extract the nanoseconds part, which is exactly the remainder we already have:
Finally, we confirm that the parts we have extracted all have the correct values:
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.