BDE 4.39.x Production Release
Loading...
Searching...
No Matches
baltzo_timezoneutilimp.h
Go to the documentation of this file.
1/// @file baltzo_timezoneutilimp.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// baltzo_timezoneutilimp.h -*-C++-*-
8#ifndef INCLUDED_BALTZO_TIMEZONEUTILIMP
9#define INCLUDED_BALTZO_TIMEZONEUTILIMP
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup baltzo_timezoneutilimp baltzo_timezoneutilimp
15/// @brief Implement utilities for converting times between time zones.
16/// @addtogroup bal
17/// @{
18/// @addtogroup baltzo
19/// @{
20/// @addtogroup baltzo_timezoneutilimp
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#baltzo_timezoneutilimp-purpose"> Purpose</a>
25/// * <a href="#baltzo_timezoneutilimp-classes"> Classes </a>
26/// * <a href="#baltzo_timezoneutilimp-description"> Description </a>
27/// * <a href="#baltzo_timezoneutilimp-usage"> Usage </a>
28/// * <a href="#baltzo_timezoneutilimp-prologue-initializing-an-example-baltzo-zoneinfocache-object"> Prologue: Initializing an Example baltzo::ZoneinfoCache Object </a>
29/// * <a href="#baltzo_timezoneutilimp-example-1-converting-from-a-utc-time-to-a-local-time"> Example 1: Converting from a UTC Time to a Local Time </a>
30///
31/// # Purpose {#baltzo_timezoneutilimp-purpose}
32/// Implement utilities for converting times between time zones.
33///
34/// # Classes {#baltzo_timezoneutilimp-classes}
35///
36/// - baltzo::TimeZoneUtilImp: implementation utilities for converting times
37///
38/// @see baltzo_localdatetime, baltzo_zoneinfo,
39/// baltzo_defaultzoneinfocache
40///
41/// # Description {#baltzo_timezoneutilimp-description}
42/// This component provides a namespace, `baltzo::TimeZoneUtilImp`,
43/// containing a set of utility functions for converting time values to and
44/// from, their corresponding local time representations in (possibly) different
45/// time zones. The primary methods provided include: `convertUtcToLocalTime`
46/// for converting time values to their corresponding local-time values in some
47/// time zone; `convertLocalToUtc`, for converting a local-time value into the
48/// corresponding UTC time value; and `initLocalTime` for initializing a
49/// local-time value. Additionally the `loadLocalTimeForUtc` method enable
50/// clients to obtain information about a time value, such as whether the
51/// provided time is a daylight-saving time value.
52///
53/// ## Usage {#baltzo_timezoneutilimp-usage}
54///
55///
56/// The following examples demonstrate how to use a `baltzo::TimeZoneUtilImp` to
57/// perform common operations on time values:
58///
59/// ### Prologue: Initializing an Example baltzo::ZoneinfoCache Object {#baltzo_timezoneutilimp-prologue-initializing-an-example-baltzo-zoneinfocache-object}
60///
61///
62/// Before using the methods provided by `baltzo::TimeZoneUtilImp` we must first
63/// define a `baltzo::ZoneinfoCache` object containing information about various
64/// time zones. For the purposes of this example, we will define a sample cache
65/// containing only data for New York loaded through a `baltzo::TestLoader`
66/// object. Note that, in general, clients should use data from an external
67/// data source (see @ref baltzo_datafileloader ).
68///
69/// First, we create a Zoneinfo object for New York, and populate `newYork` with
70/// a correct time zone identifier:
71/// @code
72/// baltzo::Zoneinfo newYork;
73/// newYork.setIdentifier("America/New_York");
74/// @endcode
75/// Next, we create two local-time descriptors, one for standard time and one
76/// for daylight-saving time:
77/// @code
78/// baltzo::LocalTimeDescriptor est(-18000, false, "EST");
79/// baltzo::LocalTimeDescriptor edt(-14400, true, "EDT");
80/// @endcode
81/// Then, we set the initial descriptor for `newYork` to Eastern Standard Time.
82/// Note that such an initial transition is required for a `baltzo::Zoneinfo`
83/// object to be considered Well-Defined (see @ref baltzo_zoneinfoutil )
84/// @code
85/// const bsls::Epoch::TimeT64 firstTransitionTime =
86/// bdlt::EpochUtil::convertToTimeT64(bdlt::Datetime(1, 1, 1));
87///
88/// newYork.addTransition(firstTransitionTime, est);
89/// @endcode
90/// Next, we create a series of transitions between these local-time descriptors
91/// for the years 2007-2011. Note that the United States transitions to
92/// daylight saving time on the second Sunday in March, at 2am local time (07:00
93/// UTC), and transitions back to standard time on the first Sunday in November
94/// at 2am local time (06:00 UTC), resulting in an even number of transitions:
95/// @code
96/// static const bdlt::Datetime TRANSITION_TIMES[] = {
97/// bdlt::Datetime(2007, 3, 11, 7),
98/// bdlt::Datetime(2007, 11, 4, 6),
99/// bdlt::Datetime(2008, 3, 9, 7),
100/// bdlt::Datetime(2008, 11, 2, 6),
101/// bdlt::Datetime(2009, 3, 8, 7),
102/// bdlt::Datetime(2009, 11, 1, 6),
103/// bdlt::Datetime(2010, 3, 14, 7),
104/// bdlt::Datetime(2010, 11, 7, 6),
105/// bdlt::Datetime(2011, 3, 13, 7),
106/// bdlt::Datetime(2011, 11, 6, 6),
107/// };
108/// const int NUM_TRANSITION_TIMES =
109/// sizeof TRANSITION_TIMES / sizeof *TRANSITION_TIMES;
110/// assert(0 == NUM_TRANSITION_TIMES % 2);
111///
112/// for (int i = 0; i < NUM_TRANSITION_TIMES; i += 2) {
113///
114/// const bsls::Epoch::TimeT64 edtTransitionTime =
115/// bdlt::EpochUtil::convertToTimeT64(TRANSITION_TIMES[i]);
116/// newYork.addTransition(edtTransitionTime, edt);
117///
118/// const bsls::Epoch::TimeT64 estTransitionTime =
119/// bdlt::EpochUtil::convertToTimeT64(TRANSITION_TIMES[i + 1]);
120/// newYork.addTransition(estTransitionTime, est);
121/// }
122/// @endcode
123/// Next, we verify that the time zone information we have created is considered
124/// well-defined (as discussed above):
125/// @code
126/// assert(true == baltzo::ZoneinfoUtil::isWellFormed(newYork));
127/// @endcode
128/// Finally, we create a `baltzo::TestLoader` object, provide it the description
129/// of `newYork`, and use it to initialize a `baltzo::ZoneinfoCache` object:
130/// @code
131/// baltzo::TestLoader loader;
132/// loader.setTimeZone(newYork);
133/// baltzo::ZoneinfoCache cache(&loader);
134/// @endcode
135///
136/// ### Example 1: Converting from a UTC Time to a Local Time {#baltzo_timezoneutilimp-example-1-converting-from-a-utc-time-to-a-local-time}
137///
138///
139/// In this example we demonstrate how to convert a UTC time to the
140/// corresponding local time using the `convertUtcToLocalTime` class method.
141///
142/// We start by creating a `bdlt::Datetime` representing the UTC time "Dec 12,
143/// 2010 15:00":
144/// @code
145/// bdlt::Datetime utcTime(2010, 12, 12, 15, 0, 0);
146/// @endcode
147/// Now, we call `convertUtcToLocalTime` and supply as input `utcTime`, the time
148/// zone identifier for New York ("America/New_York"), and the cache of time
149/// zone information created in the prologue:
150/// @code
151/// bdlt::DatetimeTz localNYTime;
152/// baltzo::TimeZoneUtilImp::convertUtcToLocalTime(&localNYTime,
153/// "America/New_York",
154/// utcTime,
155/// &cache);
156/// @endcode
157/// Finally we verify that `localNYTime` is "Dec 12, 2010 10:00+5:00", the time
158/// in New York corresponding to the UTC time "Dec 12, 2010 15:00".
159/// @code
160/// assert(utcTime == localNYTime.utcDatetime());
161/// assert(bdlt::Datetime(2010, 12, 12, 10) == localNYTime.localDatetime());
162/// assert(-5 * 60 == localNYTime.offset());
163/// @endcode
164/// @}
165/** @} */
166/** @} */
167
168/** @addtogroup bal
169 * @{
170 */
171/** @addtogroup baltzo
172 * @{
173 */
174/** @addtogroup baltzo_timezoneutilimp
175 * @{
176 */
177
178#include <balscm_version.h>
179
180#include <baltzo_dstpolicy.h>
182#include <baltzo_zoneinfo.h>
183
184#include <bdlt_datetime.h>
185#include <bdlt_datetimetz.h>
186
187#include <bsl_iosfwd.h>
188
189
190namespace baltzo {
191
192class LocalTimePeriod;
193class ZoneinfoCache;
194
195 // =====================
196 // class TimeZoneUtilImp
197 // =====================
198
199/// This `struct` provides a namespace for utility functions that convert
200/// time values to, and from, local time.
201///
202/// See @ref baltzo_timezoneutilimp
204
205 // CLASS METHODS
206
207 /// Load, into the specified `result`, the local date-time value, in the
208 /// time zone indicated by the specified `resultTimeZoneId`,
209 /// corresponding to the specified `utcTime`, using time zone
210 /// information supplied by the specified `cache`. Return 0 on success,
211 /// and a non-zero value otherwise. A return status of
212 /// `ErrorCode::k_UNSUPPORTED_ID` indicates that `resultTimeZoneId` is
213 /// not recognized, and a return status of `ErrorCode::k_OUT_OF_RANGE`
214 /// indicates that an out of range value of `result` would have
215 /// occurred.
217 const char *resultTimeZoneId,
218 const bdlt::Datetime& utcTime,
219 ZoneinfoCache *cache);
220
221 /// Load, into the specified `result`, attributes characterizing local
222 /// time indicated by the specified `transition` in the specified `timeZone`.
223 ///
224 /// \pre The behavior is undefined unless
225 /// `ZoneinfoUtil::isWellFormed(timeZone)` is `true` and `transition` is
226 /// a valid, non-ending, iterator into the sequence of transitions
227 /// described by `timeZone`.
229 LocalTimePeriod *result,
230 const Zoneinfo::TransitionConstIterator& transition,
231 const Zoneinfo& timeZone);
232
233 /// Load, into the specified `result`, the local date-time value --
234 /// including the local date, time, and resolved UTC offset -- indicated
235 /// by the specified `localTime` in the time zone indicated by the
236 /// specified `timeZoneId`, using the specified `dstPolicy` to interpret
237 /// whether or not `localTime` represents a daylight-saving time value,
238 /// and using time zone information supplied by the specified `cache`.
239 /// Load, into the specified `resultValidity` the value indicating the
240 /// whether `localTime` is unique, ambiguous but valid, or invalid.
241 /// Return 0 on success, and a non-zero value otherwise. A return
242 /// status of `ErrorCode::k_UNSUPPORTED_ID` indicates that `timeZoneId` is not recognized.
243 ///
244 /// \pre The behavior is undefined unless the result of
245 /// the initialization falls within the supported epoch.
246 static int initLocalTime(bdlt::DatetimeTz *result,
247 LocalTimeValidity::Enum *resultValidity,
248 const bdlt::Datetime& localTime,
249 const char *timeZoneId,
250 DstPolicy::Enum dstPolicy,
251 ZoneinfoCache *cache);
252
253 /// Load, into the specified `result`, attributes characterizing local
254 /// time at the specified `utcTime` in the time zone indicated by the
255 /// specified `timeZoneId` (e.g., the offset from UTC, whether it is
256 /// daylight-saving time), as well as the time interval over which those
257 /// attributes apply, using time zone information supplied by the
258 /// specified `cache`. Return 0 on success, and a non-zero value
259 /// otherwise. A return status of `ErrorCode::k_UNSUPPORTED_ID`
260 /// indicates that `timeZoneId` is not recognized.
262 const char *timeZoneId,
263 const bdlt::Datetime& utcTime,
264 ZoneinfoCache *cache);
265
266 /// Load, into the specified `result`, the local time and UTC offset of
267 /// the specified `localTime` in the specified `timeZone`, using the
268 /// specified `dstPolicy` to interpret whether or not `localTime`
269 /// represents a daylight-saving time value; load into the specified
270 /// `resultValidity` an indication of whether `localTime` is valid and
271 /// unique, valid but ambiguous, or invalid; load into the specified
272 /// `transitionIter` an iterator pointing to the transition that
273 /// characterizes the attributes of `localTime`.
274 ///
275 /// \pre The behavior is undefined unless `ZoneinfoUtil::isWellFormed(timeZone)` is `true`
276 /// and the result of the resolution falls within the supported epoch.
277 static void resolveLocalTime(
278 bdlt::DatetimeTz *result,
279 LocalTimeValidity::Enum *resultValidity,
280 Zoneinfo::TransitionConstIterator *transitionIter,
281 const bdlt::Datetime& localTime,
282 DstPolicy::Enum dstPolicy,
283 const Zoneinfo& timeZone);
284
285};
286
287} // close package namespace
288
289
290#endif
291
292// ----------------------------------------------------------------------------
293// Copyright 2015 Bloomberg Finance L.P.
294//
295// Licensed under the Apache License, Version 2.0 (the "License");
296// you may not use this file except in compliance with the License.
297// You may obtain a copy of the License at
298//
299// http://www.apache.org/licenses/LICENSE-2.0
300//
301// Unless required by applicable law or agreed to in writing, software
302// distributed under the License is distributed on an "AS IS" BASIS,
303// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
304// See the License for the specific language governing permissions and
305// limitations under the License.
306// ----------------------------- END-OF-FILE ----------------------------------
307
308/** @} */
309/** @} */
310/** @} */
Definition baltzo_localtimeperiod.h:208
Definition baltzo_zoneinfocache.h:232
Definition baltzo_zoneinfo.h:429
TransitionSequence::const_iterator TransitionConstIterator
Definition baltzo_zoneinfo.h:489
Definition bdlt_datetimetz.h:308
Definition bdlt_datetime.h:330
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition baltzo_datafileloader.h:259
Enum
Definition baltzo_dstpolicy.h:137
Enum
Definition baltzo_localtimevalidity.h:151
Definition baltzo_timezoneutilimp.h:203
static int loadLocalTimePeriodForUtc(LocalTimePeriod *result, const char *timeZoneId, const bdlt::Datetime &utcTime, ZoneinfoCache *cache)
static void createLocalTimePeriod(LocalTimePeriod *result, const Zoneinfo::TransitionConstIterator &transition, const Zoneinfo &timeZone)
static int initLocalTime(bdlt::DatetimeTz *result, LocalTimeValidity::Enum *resultValidity, const bdlt::Datetime &localTime, const char *timeZoneId, DstPolicy::Enum dstPolicy, ZoneinfoCache *cache)
static void resolveLocalTime(bdlt::DatetimeTz *result, LocalTimeValidity::Enum *resultValidity, Zoneinfo::TransitionConstIterator *transitionIter, const bdlt::Datetime &localTime, DstPolicy::Enum dstPolicy, const Zoneinfo &timeZone)
static int convertUtcToLocalTime(bdlt::DatetimeTz *result, const char *resultTimeZoneId, const bdlt::Datetime &utcTime, ZoneinfoCache *cache)