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

Detailed Description

Outline

Purpose

Provide an attribute class to configure ISO 8601 string generation.

Classes

See also
bdlt_iso8601util, bdlt_iso8601utilparseconfiguration

Description

This component provides an unconstrained (value-semantic) attribute class, bdlt::Iso8601UtilConfiguration, that may be used to configure various aspects of generated ISO 8601 strings.

Attributes

Name Type Default
------------------------- ---- -------
fractionalSecondPrecision int 3
omitColonInZoneDesignator bool false
useCommaForDecimalSign bool false
useZAbbreviationForUtc bool false

Default Configuration

This component also provides a (process-wide) default configuration that may be set and retrieved via the setDefaultConfiguration and defaultConfiguration class methods, respectively. See Usage Example 2 for further details.

Usage

This section illustrates intended use of this component.

Example 1: Configuring ISO 8601 String Generation

This example demonstrates creation of a bdlt::Iso8601UtilConfiguration object that may be used to influence the format of the output produced by a hypothetical utility, my::Iso8601Util, that generates and parses ISO 8601 strings for bdlt vocabulary types (see bdlt_iso8601util , which provides just such functionality). In particular, suppose that given a sample bdlt::TimeTz object:

const bdlt::TimeTz timeTz(bdlt::Time(8, 59, 59, 123), 240);
Definition bdlt_timetz.h:190
Definition bdlt_time.h:196

my::Iso8601Util produces, by default, the following (valid) ISO 8601 string:

08:59:59.123+04:00

However, we would like to produce the following (also valid) ISO 8601 string instead:

08:59:59,123+0400

bdlt::Iso8601UtilConfiguration can be used to obtain the desired result assuming that my::Iso8601Util uses bdlt::Iso8601UtilConfiguration to affect the format of generated strings in this fashion (e.g., again see bdlt_iso8601util ).

First, we construct a bdlt::Iso8601UtilConfiguration object that has the default value:

assert( configuration.fractionalSecondPrecision() == 3);
assert(!configuration.omitColonInZoneDesignator());
assert(!configuration.useCommaForDecimalSign());
assert(!configuration.useZAbbreviationForUtc());
Definition bdlt_iso8601utilconfiguration.h:225
int fractionalSecondPrecision() const
Definition bdlt_iso8601utilconfiguration.h:457
bool omitColonInZoneDesignator() const
Definition bdlt_iso8601utilconfiguration.h:463
bool useCommaForDecimalSign() const
Definition bdlt_iso8601utilconfiguration.h:469
bool useZAbbreviationForUtc() const
Definition bdlt_iso8601utilconfiguration.h:475

Then, we modify configuration to indicate that we want to use , as the decimal sign (in fractional seconds):

configuration.setUseCommaForDecimalSign(true);
assert( configuration.fractionalSecondPrecision() == 3);
assert(!configuration.omitColonInZoneDesignator());
assert( configuration.useCommaForDecimalSign());
assert(!configuration.useZAbbreviationForUtc());
void setUseCommaForDecimalSign(bool value)

Finally, we modify configuration to indicate that we want to omit the : in zone designators:

configuration.setOmitColonInZoneDesignator(true);
assert( configuration.fractionalSecondPrecision() == 3);
assert( configuration.omitColonInZoneDesignator());
assert( configuration.useCommaForDecimalSign());
assert(!configuration.useZAbbreviationForUtc());
void setOmitColonInZoneDesignator(bool value)

Our configuration object can now be supplied to my::Iso8601Util to produce the desired result.

Example 2: Setting the Process-Wide Default Configuration

This example demonstrates how to establish the process-wide default configuration.

First, we retrieve the default configuration in effect at process start-up and note that it has the default-constructed value:

assert(bdlt::Iso8601UtilConfiguration() == configuration);
assert( configuration.fractionalSecondPrecision() == 3);
assert(!configuration.omitColonInZoneDesignator());
assert(!configuration.useCommaForDecimalSign());
assert(!configuration.useZAbbreviationForUtc());
static Iso8601UtilConfiguration defaultConfiguration()
Definition bdlt_iso8601utilconfiguration.h:407

Next, we modify configuration to indicate that we want to output Z when the zone designator is UTC (i.e., instead of +00:00):

configuration.setUseZAbbreviationForUtc(true);
assert( configuration.fractionalSecondPrecision() == 3);
assert(!configuration.omitColonInZoneDesignator());
assert(!configuration.useCommaForDecimalSign());
assert( configuration.useZAbbreviationForUtc());
void setUseZAbbreviationForUtc(bool value)

Then, we modify configuration to display milliseconds:

assert( configuration.fractionalSecondPrecision() == 6);
assert(!configuration.omitColonInZoneDesignator());
assert(!configuration.useCommaForDecimalSign());
assert( configuration.useZAbbreviationForUtc());
void setFractionalSecondPrecision(int value)

Now, we set the default configuration to the value of our configuration object:

static void setDefaultConfiguration(const Iso8601UtilConfiguration &configuration)
Definition bdlt_iso8601utilconfiguration.h:414

Finally, we verify that the default configuration was updated as expected:

const bdlt::Iso8601UtilConfiguration newConfiguration =
assert( newConfiguration.fractionalSecondPrecision() == 6);
assert(!newConfiguration.omitColonInZoneDesignator());
assert(!newConfiguration.useCommaForDecimalSign());
assert( newConfiguration.useZAbbreviationForUtc());

Note that the expected usage is that the process-wide configuration will be established once, early in main, and not changed throughout the lifetime of a process.