BDE 4.14.0 Production release
|
Provide a representation of an interval of time.
This component implements a time interval class, bdlt::DatetimeInterval
, capable of representing the (signed) difference between two arbitrary points in time. The time interval represented by a bdlt::DatetimeInterval
object has microsecond resolution.
A time interval has a value that is independent of its representation. Conceptually, the interval between two points in time could be described using a (signed) real number of seconds (or minutes, or hours, etc.). A bdlt::DatetimeInterval
represents this value as six fields: days, hours, minutes, seconds, milliseconds, and microseconds. In the "canonical
representation" of a time interval, the days field may have any 32-bit signed integer value, with the hours, minutes, seconds, milliseconds, and microseconds fields limited to the respective ranges [-23 .. 23]
, [-59 .. 59]
, [-59 .. 59]
, [-999 .. 999]
, and [-999 .. 999]
, with the additional constraint that the six fields are either all non-negative or all non-positive. When setting the value of a time interval via its six-field representation, any integer value may be used in any field, with the constraint that the resulting number of days be representable as a 32-bit signed integer. Similarly, the field values may be accessed in the canonical representation using the days
, hours
, minutes
, seconds
, milliseconds
, and microseconds
methods.
The primary accessors for this type are days
and fractionalDayInMicroseconds
. In combination, these two methods provide complete and succinct access to the value of a DatetimeInterval
. Furthermore, the total value of the interval may be accessed in the respective field units via the totalDays
, totalHours
, totalMinutes
, totalSeconds
, totalMilliseconds
, and totalMicroseconds
methods. Note that, with the exception of totalMicroseconds
(which returns an exact result), the other "total" accessors round toward 0. Also note that the totalMicroseconds
accessor can fail for extreme DatetimeInterval
values.
The following summarizes the canonical representation of the value of a bdlt::DatetimeInterval
:
This section illustrates intended use of this component.
This example demonstrates how to create and use a bdlt::DatetimeInterval
object.
First, create an object i1
having the default value:
Then, set the value of i1
to -5 days, and then add 16 hours to that value:
Next, create i2
as a copy of i1
:
Then, add 2 days and 4 seconds to the value of i2
(in two steps), and confirm that i2
has a value that is greater than that of i1
:
Next, add 2 days and 4 seconds to the value of i1
in one step by using the addInterval
method, and confirm that i1
now has the same value as i2
:
Finally, write the value of i2
to stdout
:
The output operator produces the following format on stdout
: