BDE 4.14.0 Production release
|
Macros | |
#define | BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS |
Provide a representation of a time interval.
This component provides a value-semantic type, bsls::TimeInterval
, that is capable of representing a signed time interval with nanosecond resolution.
A time interval has a value that is independent of its representation. Conceptually, a time interval may be thought of as a signed, arbitrary-precision floating-point number denominated in seconds (or in days, or in fortnights, if one prefers). A bsls::TimeInterval
represents this value as two fields: seconds and nanoseconds. In the "canonical
representation" of a time interval, the seconds
field may have any 64-bit signed integer value, with the nanoseconds
field limited to the range [ -999,999,999..999,999,999 ]
, and with the additional constraint that the two fields are either both non-negative or both non-positive. When setting the value of a time interval via its two-field representation, any integer value may be used in either field, with the constraint that the resulting number of seconds be representable as a 64-bit signed integer. Similarly, the two field values may be accessed in the canonical representation using the seconds
and nanoseconds
methods.
Binary arithmetic and relational operators taking two bsls::TimeInterval
objects, or a bsls::TimeInterval
object and a double
, are provided. A double
operand, representing a real number of seconds, is first converted to a bsls::TimeInterval
object before performing the operation. Under such circumstances, the fractional part of the double
, if any, is rounded to the nearest whole number of nanoseconds.
The user-defined literal operator"" _h
, operator"" _min
, operator"" _s
, operator"" _ms
, operator"" _us
and operator"" _ns
are declared for the TimeInterval
. These suffixes can be applied to integer literals and allow to create an object, representing the specified number of hours, minutes, seconds, milliseconds, microseconds or nanoseconds respectively:
The operators providing literals are available in the BloombergLP::bsls::literals::TimeIntervalLiterals
namespace (where literals
and TimeIntervalLiterals
are both inline namespaces). Because of inline namespaces, there are several viable options for a using declaration, but we recommend using namespace bsls::TimeIntervalLiterals
, which minimizes the scope of the using declaration.
Note that user defined literals can be used only if the compiler supports the C++11 standard.
This section illustrates intended use of this component.
The following example demonstrates how to create and manipulate a bsls::TimeInterval
object.
First, we default construct a TimeInterval
object, interval
:
Next, we set the value of interval
to 1 second and 10 nanoseconds (a time interval of 1000000010 nanoseconds):
Then, we add 3 seconds to interval
:
Next, we create a copy of interval
, intervalPrime
:
Finally, we assign 3.14 seconds to intervalPrime
, and then add 2.73 seconds more:
#define BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS |