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

Detailed Description

Outline

Purpose

Provide non-primitive operations on bdlt::DatetimeInterval.

Classes

See also
bdlt_datetimeinterval

Description

This component provides non-primitive operations on bdlt::DatetimeInterval objects. In particular, bdlt::DatetimeIntervalUtil supplies factory methods for bdlt::DatetimeInterval objects.

This utility component provides the following (static) methods:

bdlt::DatetimeInterval makeDays(int days);
bdlt::DatetimeInterval makeMilliseconds(bsls::Types::Int64 milliseconds);
bdlt::DatetimeInterval makeMicroseconds(bsls::Types::Int64 microseconds);
Definition bdlt_datetimeinterval.h:201
long long Int64
Definition bsls_types.h:132

Usage

This section illustrates intended use of this component.

Example 1: Simple Usage of the Various make Functions

This example shows how we can create a bdlt::DatetimeInterval objects having values of 1 day, 2 hours, 3 minutes, 4 seconds, 5 millisecond, and 6 microseconds by using the bdlt::DatetimeInterval constructor and, more readably, by using the make* functions.

First, start with a default (0) bdlt::DatetimeInterval:

Next, add 1 day to it, and assert that both objects are equal:

d += bdlt::DatetimeInterval(1, 0, 0, 0, 0, 0);
assert(m == d);
static DatetimeInterval makeDays(int days)
Definition bdlt_datetimeintervalutil.h:212

Then, add 2 hours to it, and assert that both objects are equal:

d += bdlt::DatetimeInterval(0, 2, 0, 0, 0, 0);
assert(m == d);
static DatetimeInterval makeHours(bsls::Types::Int64 hours)
Definition bdlt_datetimeintervalutil.h:218

Next, add 3 minutes to it, and assert that both objects are equal:

d += bdlt::DatetimeInterval(0, 0, 3, 0, 0, 0);
assert(m == d);
static DatetimeInterval makeMinutes(bsls::Types::Int64 minutes)
Definition bdlt_datetimeintervalutil.h:224

Then, add 4 seconds to it, and assert that both objects are equal:

d += bdlt::DatetimeInterval(0, 0, 0, 4, 0, 0);
assert(m == d);
static DatetimeInterval makeSeconds(bsls::Types::Int64 seconds)
Definition bdlt_datetimeintervalutil.h:230

Next, add 5 milliseconds to it, and assert that both objects are equal:

d += bdlt::DatetimeInterval(0, 0, 0, 0, 5, 0);
assert(m == d);
static DatetimeInterval makeMilliseconds(bsls::Types::Int64 milliseconds)
Definition bdlt_datetimeintervalutil.h:236

Then, add 6 microseconds to it, and assert that both objects are equal:

d += bdlt::DatetimeInterval(0, 0, 0, 0, 0, 6);
assert(m == d);
static DatetimeInterval makeMicroseconds(bsls::Types::Int64 microseconds)
Definition bdlt_datetimeintervalutil.h:243

Finally, we create an create a DatetimeInterval with the final value and compare to the objects built in steps:

bdlt::DatetimeInterval f(1, 2, 3, 4, 5, 6);
assert(f == m);
assert(f == d);

Example 2: How to Improve Readability Using the make Functions

This example shows how we can create a bdlt::Datetime objects having a value of now + 2 hours and 30 minutes by using the bdlt::DatetimeInterval constructor and, more readably, by using the make* functions.

First, create a bdlt::Datetime object having the current time:

Definition bdlt_datetime.h:331
static bsls::TimeInterval now()
Definition bdlt_currenttime.h:290

Now, create the bdlt::DatetimeInterval objects and assign the desired values to them using the makeHours and makeMinutes functions, and using the bdlt::DatetimeInterval constructor:

Finally, assert that both results are equal:

assert(nextEventTime == altEventTime);