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

Detailed Description

Outline

Purpose

Provide functions to convert between time-interval representations.

Classes

See also
bdlt_datetimeinterval, bsls_timeinterval

Description

This component provides a utility struct, bdlt::IntervalConversionUtil, that defines functions to convert between C++ value types providing different representations of time intervals, (e.g., bsls::TimeInterval and bdlt::DatetimeInterval).

Usage

This section illustrates intended use of this component.

Example 1: Interfacing With an API That Uses bsls::TimeInterval

Some APIs, such as bsls::SystemTime, use bsls::TimeInterval in their interface. In order to use those APIs in components implemented in terms of bdlt::DatetimeInterval, it is necessary to convert between the bsls::TimeInterval and bdlt::DatetimeInterval representations for a time interval. This conversion can be accomplished conveniently using bdlt::IntervalConversionUtil.

Suppose we wish to pass the system time – as returned by bsls::SystemTime::nowRealtimeClock – to a function that displays a time that is represented as a bdlt::DatetimeInterval since the UNIX epoch.

First, we include the declaration of the function that displays a bdlt::DatetimeInterval:

void displayTime(const bdlt::DatetimeInterval& timeSinceEpoch);
Definition bdlt_datetimeinterval.h:201

Then, we obtain the current system time from bsls::SystemTime, and store it in a bsls::TimeInterval:

Definition bsls_timeinterval.h:301
static TimeInterval nowRealtimeClock()

Now, we convert the bsls::TimeInterval into a bdlt::DatetimeInterval using convertToDatetimeInterval:

bdlt::DatetimeInterval timeSinceEpoch =
assert(timeSinceEpoch.totalMilliseconds() ==
systemTime.totalMilliseconds());
bsls::Types::Int64 totalMilliseconds() const
Definition bdlt_datetimeinterval.h:1185
BSLS_KEYWORD_CONSTEXPR_CPP14 bsls::Types::Int64 totalMilliseconds() const
Definition bsls_timeinterval.h:1384
static DatetimeInterval convertToDatetimeInterval(const bsls::TimeInterval &interval)
Definition bdlt_intervalconversionutil.h:179

Finally, we display the time by passing the converted value to displayTime:

displayTime(timeSinceEpoch);