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

Detailed Description

Outline

Purpose

Provide a test implementation of the baltzo::Loader protocol.

Classes

See also
baltzo_loader, baltzo_zoneinfo

Description

This component provides baltzo::TestLoader, a concrete test implementation of the baltzo::Loader protocol for loading a baltzo::Zoneinfo object. The following inheritance hierarchy diagram shows the classes involved and their methods:

,------------------.
`------------------'
| ctor
| setTimeZone
V
,--------------.
( baltzo::Loader )
`--------------'
dtor
loadTimeZone
Definition baltzo_testloader.h:200

This test implementation maintains a mapping of time-zone identifiers to baltzo::Zoneinfo objects. Clients can associate a time-zone object with a time-zone identifier using the setTimeZone method. A subsequent call to the protocol method loadTimeZone for that time-zone identifier will return the supplied baltzo::Zoneinfo object.

Usage

The following examples demonstrate how to populate a baltzo::TestLoader with time.zone information, and then access that information through the baltzo::Loader protocol.

Example 1: Populating a baltzo::TestLoader with Time-Zone Information

We start by creating a baltzo::Zoneinfo object, which we will eventually populate with a subset of data for "America/New_York":

baltzo::Zoneinfo newYorkTimeZone;
Definition baltzo_zoneinfo.h:429

Next, we populate newYorkTimeZone with the correct time-zone identifier and two types of local time (standard time, and daylight-saving time):

const char *NEW_YORK_ID = "America/New_York";
newYorkTimeZone.setIdentifier(NEW_YORK_ID);
baltzo::LocalTimeDescriptor est(-5 * 60 * 60, false, "EST");
baltzo::LocalTimeDescriptor edt(-4 * 60 * 60, true, "EDT");
Definition baltzo_localtimedescriptor.h:189
void setIdentifier(const bsl::string_view &value)
Definition baltzo_zoneinfo.h:769

Then, we create a series of transitions between these local time descriptors for the years 2007-2011. Note that the United States transitions to daylight saving time on the second Sunday in March, at 2am local time (7am UTC), and transitions back to standard time on the first Sunday in November at 2am local time (6am UTC). Also note, that these rules for generating transitions was different prior to 2007, and may be changed at some point in the future.

bdlt::Time edtTime(7, 0, 0); // UTC transition time
bdlt::Time estTime(6, 0, 0); // UTC transition time
static const int edtDays[5] = { 11, 9, 8, 14, 13 };
static const int estDays[5] = { 4, 2, 1, 7, 6 };
for (int year = 2007; year < 2012; ++year) {
int edtDay = edtDays[year - 2007];
int estDay = estDays[year - 2007];
bdlt::Datetime edtTransition(bdlt::Date(year, 3, edtDay), edtTime);
bdlt::Datetime estTransition(bdlt::Date(year, 11, estDay), estTime);
bsls::Types::Int64 edtTransitionT =
bsls::Types::Int64 estTransitionT =
Definition bdlt_date.h:294
Definition bdlt_datetime.h:331
Definition bdlt_time.h:196
static TimeT64 convertToTimeT64(const Datetime &datetime)
Definition bdlt_epochutil.h:471
long long Int64
Definition bsls_types.h:132

Now, having created values representing the daylight saving time transitions (in UTC), we insert the transitions into the baltzo::Zoneinfo object newYorkTimeZone:

newYorkTimeZone.addTransition(edtTransitionT, edt);
newYorkTimeZone.addTransition(estTransitionT, est);
}
void addTransition(bdlt::EpochUtil::TimeT64 utcTime, const LocalTimeDescriptor &descriptor)

Now, we create a baltzo::TestLoader object and configure it with newYorkTimeZone, which the test loader will associate with the identifier newYorkTimeZone.identifier() (whose value is "America/New_York"):

baltzo::TestLoader testLoader;
testLoader.setTimeZone(newYorkTimeZone);
void setTimeZone(const Zoneinfo &timeZone)

Example 2: Accessing Time-Zone Information From a baltzo::TestLoader

In the next example, we will use the baltzo::TestLoader we initialized in the preceding example, to load time-zone information for New York via the baltzo::Loader protocol.

We start by creating a baltzo::Loader reference to testLoader:

baltzo::Loader& loader = testLoader;
Definition baltzo_loader.h:266

Now we used the protocol method loadTimeZone to load time-zone information for New York:

baltzo::Zoneinfo resultNewYork;
int status = loader.loadTimeZone(&resultNewYork, "America/New_York");
assert(0 == status);
virtual int loadTimeZone(Zoneinfo *result, const char *timeZoneId)=0

Finally, we verify that the returned time-zone information, resultNewYork, is equivalent to newYorkTimeZone, which we we used to configure testLoader:

assert(newYorkTimeZone == resultNewYork);