BDE 4.14.0 Production release
|
Provide utilities to retrieve the local time offset.
This component provides a struct
, bdlt::LocalTimeOffset
, in which are defined a series of static methods for using a callback function to retrieve the local time offset (the difference between the currently executing task's local time and UTC time) at a specified UTC date and time. LocalTimeoffset
provides a function localTimeOffset
that delegates to the currently installed local time offset callback. By default, localTimeOffsetDefault
is installed as the local time offset callback. Clients can configure the default callback function by calling the setLocalTimeOffsetCallback
function.
The functions provided by bdlt::LocalTimeOffset
are thread-safe (meaning they may be called concurrently from multiple threads), including those that set and retrieve the callback function. In addition, user-supplied callback functions must be thread-safe.
This section illustrates intended use of this component.
This example demonstrates how to use bdlt::LocalTimeOffset
.
First, obtain the current UTC time - ignoring milliseconds - using bsls::SystemTime
and bdlt::EpochUtil
(note that clients may prefer bdlt_currenttime , which is not shown here for dependency reasons):
Then, obtain the local time offset:
Next, add the offset to the UTC time to obtain the local time:
Finally, stream the two time values to stdout
:
The streaming operator produces output in the following format on stdout
:
Suppose one has to provide time stamp values that always reflect local time for a given location, even when local time transitions into and out of daylight saving time. Further suppose that one must do this quite often (e.g., for every record in a high frequency log), so the performance of the default method for calculating local time offset is not adequate. Creation and installation of a specialized user-defined callback for local time offset allows one to solve this problem.
First, create a utility class that provides a method of type bdlt::LocalTimeOffset::LocalTimeOffsetCallback
that is valid for the location of interest (New York) for the period of interest (the year 2013).
Note that the transition times into and out of daylight saving for New York are given in UTC. Also notice that we do not attempt to make the localTimeOffset
method inline
, since we must take its address to install it as the callback.
Then, we install this localTimeOffset
as the local time offset callback.
Now, we can use the bdlt::LocalTimeOffset::localTimeOffset
method to obtain the local time offsets in New York on several dates of interest. The increasing values from our useCount
method assures us that the callback we defined is indeed being used.
Finally, to be neat, we restore the local time offset callback to the default callback: