|
BDE 4.14.0 Production release
|
Implement utilities for converting times between time zones.
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.
The following examples demonstrate how to use a baltzo::TimeZoneUtilImp to perform common operations on time values:
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:
Next, we create two local-time descriptors, one for standard time and one for daylight-saving time:
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 )
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:
Next, we verify that the time zone information we have created is considered well-defined (as discussed above):
Finally, we create a baltzo::TestLoader object, provide it the description of newYork, and use it to initialize a baltzo::ZoneinfoCache object:
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":
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:
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".