text
stringlengths
0
2.2M
ASSERT(sourceDatetimeTz.utcDatetime() == targetDatetime);
//..
// Note that this time the value of the target object has been converted to
// UTC.
//
///Example 2: Configuring ISO 8601 String Generation
///- - - - - - - - - - - - - - - - - - - - - - - - -
// This example demonstrates use of a 'bdlt::Iso8601UtilConfiguration' object
// to influence the format of the ISO 8601 strings that are generated by this
// component by passing that configuration object to 'generate'. We also take
// this opportunity to illustrate the flavor of the 'generate' functions that
// outputs to a 'char *' buffer of a specified length.
//
// First, we construct a 'bdlt::TimeTz' object for which a corresponding ISO
// 8601-compliant string will be generated shortly:
//..
const bdlt::TimeTz sourceTimeTz(time, tzOffset);
//..
// For comparison with the ISO 8601 string generated below, note that streaming
// the value of 'sourceTimeTz' to 'stdout':
//..
if (veryVerbose)
bsl::cout << sourceTimeTz << bsl::endl;
//..
// produces:
//..
// 08:59:59.123+0400
//..
// Then, we construct the 'bdlt::Iso8601UtilConfiguration' object that
// indicates how we would like to effect the generated output ISO 8601 string.
// In this case, we want to use ',' as the decimal sign (in fractional seconds)
// and omit the ':' in zone designators:
//..
bdlt::Iso8601UtilConfiguration configuration;
configuration.setOmitColonInZoneDesignator(true);
configuration.setUseCommaForDecimalSign(true);
//..
// Next, we define the 'char *' buffer that will be used to stored the
// generated string. A buffer of size 'bdlt::Iso8601Util::k_TIMETZ_STRLEN + 1'
// is large enough to hold any string generated by this component for a
// 'bdlt::TimeTz' object, including a null terminator:
//..
const int BUFLEN = bdlt::Iso8601Util::k_TIMETZ_STRLEN + 1;
char buffer[BUFLEN];
//..
// Then, we use a 'generate' function that accepts our 'configuration' to
// produce an ISO 8601-compliant string for 'sourceTimeTz', this time writing
// the output to a 'char *' buffer, and assert that both the return value and
// the string that is produced are as expected. Note that in comparing the
// return value against 'BUFLEN - 5' we account for the omission of the ':'
// from the zone designator, and also for the fact that, although a null
// terminator was generated, it is not included in the character count returned
// by 'generate'. Also note that we use 'bsl::strcmp' to compare the resulting
// string knowing that we supplied a buffer having sufficient capacity to
// accommodate a null terminator:
//..
rc = bdlt::Iso8601Util::generate(buffer,
BUFLEN,
sourceTimeTz,
configuration);
ASSERT(BUFLEN - 5 == rc);
ASSERT( 0 == bsl::strcmp(buffer, "08:59:59,123+0400"));
//..
// For comparison, see the output that was produced by the streaming operator
// above.
//
// Next, we parse the string that was just produced, loading the result of the
// parse into a second 'bdlt::TimeTz' object, and assert that the parse was
// successful and that the target object has the same value as that of the
// original (i.e., 'sourceTimeTz'). Note that 'BUFLEN - 5' is passed and *not*
// 'BUFLEN' because the former indicates the correct number of characters in
// 'buffer' that we wish to parse:
//..
bdlt::TimeTz targetTimeTz;
rc = bdlt::Iso8601Util::parse(&targetTimeTz, buffer, BUFLEN - 5);
ASSERT( 0 == rc);
ASSERT(sourceTimeTz == targetTimeTz);
//..
// Then, we parse the string in 'buffer' a second time, this time loading the
// result into a 'bdlt::Time' object (instead of a 'bdlt::TimeTz'):
//..
bdlt::Time targetTime;
rc = bdlt::Iso8601Util::parse(&targetTime, buffer, BUFLEN - 5);
ASSERT( 0 == rc);
ASSERT(sourceTimeTz.utcTime() == targetTime);
//..
// Note that this time the value of the target object has been converted to
// UTC.
//
// Finally, we modify the 'configuration' to display the 'bdlt::TimeTz' without
// fractional seconds:
//..
configuration.setFractionalSecondPrecision(0);
rc = bdlt::Iso8601Util::generate(buffer,
BUFLEN,
sourceTimeTz,
configuration);