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

Detailed Description

Outline

Purpose

Provide an attribute class for characterizing local time values.

Classes

See also
baltzo_zoneinfo

Description

This component provides a single, simply constrained (value-semantic) attribute class, baltzo::LocalTimeDescriptor, that is used to characterize subsets of local time values within time zones. Note that this class is consistent with the "local-time types" found in the "Zoneinfo" representation of a time zone (see baltzo_zoneinfo ).

Attributes

Name Type Default Simple Constraints
------------------ ----------- ------- ------------------
description bsl::string "" none
dstInEffectFlag bool false none
utcOffsetInSeconds int 0 [-86399 .. 86399]
Definition bslstl_string.h:1281

For example, in New York on January 1, 2011, the local time is Eastern Standard Time, Daylight-Saving Time (DST) is not in effect, and the offset from UTC is -5 hours. We can represent this information using a baltzo::LocalTimeDescriptor object whose description is "EST", dstInEffectFlag is false, and utcOffsetInSeconds is -18,000 (-5*60*60). Note that description is not canonical, and is intended for development and debugging only.

Usage

This section illustrates intended use of this component.

Example 1: Converting Between UTC and Local Times

When using the "Zoneinfo" database, we want to represent and access the local time information contained in the "Zoneinfo" binary data files. Once we have obtained this information, we can use it to convert times from one time zone to another. The following code illustrates how to perform such conversions using baltzo::LocalTimeDescriptor.

First, we define a baltzo::LocalTimeDescriptor object that characterizes the local time in effect for New York Daylight-Saving Time in 2010:

enum { NEW_YORK_DST_OFFSET = -4 * 60 * 60 }; // -4 hours in seconds
baltzo::LocalTimeDescriptor newYorkDst(NEW_YORK_DST_OFFSET, true, "EDT");
assert(NEW_YORK_DST_OFFSET == newYorkDst.utcOffsetInSeconds());
assert( true == newYorkDst.dstInEffectFlag());
assert( "EDT" == newYorkDst.description());
Definition baltzo_localtimedescriptor.h:189

Then, we create a bdlt::Datetime representing the time "Jul 20, 2010 11:00" in New York:

bdlt::Datetime newYorkDatetime(2010, 7, 20, 11, 0, 0);
Definition bdlt_datetime.h:331

Next, we convert newYorkDatetime to its corresponding UTC value using the newYorkDst descriptor (created above); note that, when converting from a local time to a UTC time, the signed offset from UTC is subtracted from the local time:

bdlt::Datetime utcDatetime = newYorkDatetime;
utcDatetime.addSeconds(-newYorkDst.utcOffsetInSeconds());
Datetime & addSeconds(bsls::Types::Int64 seconds)
Definition bdlt_datetime.h:2024

Then, we verify that the result corresponds to the expected UTC time, "Jul 20, 2010 15:00":

assert(bdlt::Datetime(2010, 7, 20, 15, 0, 0) == utcDatetime);

Next, we define a baltzo::LocalTimeDescriptor object that describes the local time in effect for Rome in the summer of 2010:

enum { ROME_DST_OFFSET = 2 * 60 * 60 }; // 2 hours in seconds
baltzo::LocalTimeDescriptor romeDst(ROME_DST_OFFSET, true, "CEST");
assert(ROME_DST_OFFSET == romeDst.utcOffsetInSeconds());
assert( true == romeDst.dstInEffectFlag());
assert( "CEST" == romeDst.description());

Now, we convert utcDatetime to its corresponding local-time value in Rome using the romeDst descriptor (created above):

bdlt::Datetime romeDatetime = utcDatetime;
romeDatetime.addSeconds(romeDst.utcOffsetInSeconds());

Notice that, when converting from UTC time to local time, the signed offset from UTC is added to UTC time rather than subtracted.

Finally, we verify that the result corresponds to the expected local time, "Jul 20, 2010 17:00":

assert(bdlt::Datetime(2010, 7, 20, 17, 0, 0) == romeDatetime);