Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component baltzo_testloader
[Package baltzo]

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

Namespaces

namespace  baltzo

Detailed Description

Outline
Purpose:
Provide a test implementation of the baltzo::Loader protocol.
Classes:
baltzo::TestLoader concrete implementation of baltzo::Loader protocol
See also:
Component baltzo_loader, Component 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:
   ,------------------.
  ( baltzo::TestLoader )
   `------------------'
            |      ctor
            |      setTimeZone
            V
    ,--------------.
   ( baltzo::Loader )
    `--------------'
                   dtor
                   loadTimeZone
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;
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");
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 =
                            bdlt::EpochUtil::convertToTimeT64(edtTransition);

      bsls::Types::Int64 estTransitionT =
                            bdlt::EpochUtil::convertToTimeT64(estTransition);
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);
  }
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);
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;
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);
Finally, we verify that the returned time-zone information, resultNewYork, is equivalent to newYorkTimeZone, which we we used to configure testLoader:
  assert(newYorkTimeZone == resultNewYork);