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

Detailed Description

Outline

Purpose

Implement utilities for converting times between time zones.

Classes

See also
baltzo_localdatetime, baltzo_zoneinfo, baltzo_defaultzoneinfocache

Description

This component provides a namespace, baltzo::TimeZoneUtilImp, containing a set of utility functions for converting time values to and from, their corresponding local time representations in (possibly) different time zones. The primary methods provided include: convertUtcToLocalTime for converting time values to their corresponding local-time values in some time zone; convertLocalToUtc, for converting a local-time value into the corresponding UTC time value; and initLocalTime for initializing a local-time value. Additionally the loadLocalTimeForUtc method enable clients to obtain information about a time value, such as whether the provided time is a daylight-saving time value.

Usage

The following examples demonstrate how to use a baltzo::TimeZoneUtilImp to perform common operations on time values:

Prologue: Initializing an Example baltzo::ZoneinfoCache Object

Before using the methods provided by baltzo::TimeZoneUtilImp we must first define a baltzo::ZoneinfoCache object containing information about various time zones. For the purposes of this example, we will define a sample cache containing only data for New York loaded through a baltzo::TestLoader object. Note that, in general, clients should use data from an external data source (see baltzo_datafileloader ).

First, we create a Zoneinfo object for New York, and populate newYork with a correct time zone identifier:

newYork.setIdentifier("America/New_York");
Definition baltzo_zoneinfo.h:429
void setIdentifier(const bsl::string_view &value)
Definition baltzo_zoneinfo.h:769

Next, we create two local-time descriptors, one for standard time and one for daylight-saving time:

baltzo::LocalTimeDescriptor est(-18000, false, "EST");
baltzo::LocalTimeDescriptor edt(-14400, true, "EDT");
Definition baltzo_localtimedescriptor.h:189

Then, we set the initial descriptor for newYork to Eastern Standard Time. Note that such an initial transition is required for a baltzo::Zoneinfo object to be considered Well-Defined (see baltzo_zoneinfoutil )

const bsls::Epoch::TimeT64 firstTransitionTime =
newYork.addTransition(firstTransitionTime, est);
void addTransition(bdlt::EpochUtil::TimeT64 utcTime, const LocalTimeDescriptor &descriptor)
Definition bdlt_datetime.h:331
static TimeT64 convertToTimeT64(const Datetime &datetime)
Definition bdlt_epochutil.h:471

Next, 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 (07:00 UTC), and transitions back to standard time on the first Sunday in November at 2am local time (06:00 UTC), resulting in an even number of transitions:

static const bdlt::Datetime TRANSITION_TIMES[] = {
bdlt::Datetime(2007, 3, 11, 7),
bdlt::Datetime(2007, 11, 4, 6),
bdlt::Datetime(2008, 3, 9, 7),
bdlt::Datetime(2008, 11, 2, 6),
bdlt::Datetime(2009, 3, 8, 7),
bdlt::Datetime(2009, 11, 1, 6),
bdlt::Datetime(2010, 3, 14, 7),
bdlt::Datetime(2010, 11, 7, 6),
bdlt::Datetime(2011, 3, 13, 7),
bdlt::Datetime(2011, 11, 6, 6),
};
const int NUM_TRANSITION_TIMES =
sizeof TRANSITION_TIMES / sizeof *TRANSITION_TIMES;
assert(0 == NUM_TRANSITION_TIMES % 2);
for (int i = 0; i < NUM_TRANSITION_TIMES; i += 2) {
const bsls::Epoch::TimeT64 edtTransitionTime =
bdlt::EpochUtil::convertToTimeT64(TRANSITION_TIMES[i]);
newYork.addTransition(edtTransitionTime, edt);
const bsls::Epoch::TimeT64 estTransitionTime =
bdlt::EpochUtil::convertToTimeT64(TRANSITION_TIMES[i + 1]);
newYork.addTransition(estTransitionTime, est);
}

Next, we verify that the time zone information we have created is considered well-defined (as discussed above):

assert(true == baltzo::ZoneinfoUtil::isWellFormed(newYork));
static bool isWellFormed(const Zoneinfo &timeZone)

Finally, we create a baltzo::TestLoader object, provide it the description of newYork, and use it to initialize a baltzo::ZoneinfoCache object:

loader.setTimeZone(newYork);
baltzo::ZoneinfoCache cache(&loader);
Definition baltzo_testloader.h:200
void setTimeZone(const Zoneinfo &timeZone)
Definition baltzo_zoneinfocache.h:232

Example 1: Converting from a UTC Time to a Local Time

In this example we demonstrate how to convert a UTC time to the corresponding local time using the convertUtcToLocalTime class method.

We start by creating a bdlt::Datetime representing the UTC time "Dec 12, 2010 15:00":

bdlt::Datetime utcTime(2010, 12, 12, 15, 0, 0);

Now, we call convertUtcToLocalTime and supply as input utcTime, the time zone identifier for New York ("America/New_York"), and the cache of time zone information created in the prologue:

bdlt::DatetimeTz localNYTime;
"America/New_York",
utcTime,
&cache);
Definition bdlt_datetimetz.h:308
static int convertUtcToLocalTime(bdlt::DatetimeTz *result, const char *resultTimeZoneId, const bdlt::Datetime &utcTime, ZoneinfoCache *cache)

Finally we verify that localNYTime is "Dec 12, 2010 10:00+5:00", the time in New York corresponding to the UTC time "Dec 12, 2010 15:00".

assert(utcTime == localNYTime.utcDatetime());
assert(bdlt::Datetime(2010, 12, 12, 10) == localNYTime.localDatetime());
assert(-5 * 60 == localNYTime.offset());
Datetime localDatetime() const
Definition bdlt_datetimetz.h:660
int offset() const
Definition bdlt_datetimetz.h:666
Datetime utcDatetime() const
Definition bdlt_datetimetz.h:678