Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlt_datetimeintervalutil
[Package bdlt]

Provide non-primitive operations on bdlt::DatetimeInterval. More...

Namespaces

namespace  bdlt

Detailed Description

Outline
Purpose:
Provide non-primitive operations on bdlt::DatetimeInterval.
Classes:
bdlt::DatetimeIntervalUtil Utilities for bdlt::DatetimeInterval
See also:
Component 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 makeHours(bsls::Types::Int64 hours);
  bdlt::DatetimeInterval makeMinutes(bsls::Types::Int64 minutes);
  bdlt::DatetimeInterval makeSeconds(bsls::Types::Int64 seconds);
  bdlt::DatetimeInterval makeMilliseconds(bsls::Types::Int64 milliseconds);
  bdlt::DatetimeInterval makeMicroseconds(bsls::Types::Int64 microseconds);
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:
  bdlt::DatetimeInterval m;
  bdlt::DatetimeInterval d;
Next, add 1 day to it, and assert that both objects are equal:
  m += bdlt::DatetimeIntervalUtil::makeDays(1);
  d += bdlt::DatetimeInterval(1, 0, 0, 0, 0, 0);
  assert(m == d);
Then, add 2 hours to it, and assert that both objects are equal:
  m += bdlt::DatetimeIntervalUtil::makeHours(2);
  d += bdlt::DatetimeInterval(0, 2, 0, 0, 0, 0);
  assert(m == d);
Next, add 3 minutes to it, and assert that both objects are equal:
  m += bdlt::DatetimeIntervalUtil::makeMinutes(3);
  d += bdlt::DatetimeInterval(0, 0, 3, 0, 0, 0);
  assert(m == d);
Then, add 4 seconds to it, and assert that both objects are equal:
  m += bdlt::DatetimeIntervalUtil::makeSeconds(4);
  d += bdlt::DatetimeInterval(0, 0, 0, 4, 0, 0);
  assert(m == d);
Next, add 5 milliseconds to it, and assert that both objects are equal:
  m += bdlt::DatetimeIntervalUtil::makeMilliseconds(5);
  d += bdlt::DatetimeInterval(0, 0, 0, 0, 5, 0);
  assert(m == d);
Then, add 6 microseconds to it, and assert that both objects are equal:
  m += bdlt::DatetimeIntervalUtil::makeMicroseconds(6);
  d += bdlt::DatetimeInterval(0, 0, 0, 0, 0, 6);
  assert(m == d);
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: 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:
  bdlt::Datetime nextEventTime = now
                               + bdlt::DatetimeIntervalUtil::makeHours(2)
                               + bdlt::DatetimeIntervalUtil::makeMinutes(30);
  bdlt::Datetime altEventTime  = now
                               + bdlt::DatetimeInterval(0, 2, 30, 0, 0, 0);
Finally, assert that both results are equal:
  assert(nextEventTime == altEventTime);