BDE 4.14.0 Production release
Loading...
Searching...
No Matches
baltzo_loader.h
Go to the documentation of this file.
1/// @file baltzo_loader.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// baltzo_loader.h -*-C++-*-
8#ifndef INCLUDED_BALTZO_LOADER
9#define INCLUDED_BALTZO_LOADER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup baltzo_loader baltzo_loader
15/// @brief Provide a protocol for obtaining information about a time zone.
16/// @addtogroup bal
17/// @{
18/// @addtogroup baltzo
19/// @{
20/// @addtogroup baltzo_loader
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#baltzo_loader-purpose"> Purpose</a>
25/// * <a href="#baltzo_loader-classes"> Classes </a>
26/// * <a href="#baltzo_loader-description"> Description </a>
27/// * <a href="#baltzo_loader-properties-of-a-populated-baltzo-zoneinfo"> Properties of a Populated baltzo::Zoneinfo </a>
28/// * <a href="#baltzo_loader-usage"> Usage </a>
29/// * <a href="#baltzo_loader-example-1-implementing-baltzo-loader"> Example 1: Implementing baltzo::Loader </a>
30/// * <a href="#baltzo_loader-example-2-using-a-baltzo-loader"> Example 2: Using a baltzo::Loader </a>
31///
32/// # Purpose {#baltzo_loader-purpose}
33/// Provide a protocol for obtaining information about a time zone.
34///
35/// # Classes {#baltzo_loader-classes}
36///
37/// - baltzo::Loader: protocol for obtaining time-zone information
38///
39/// @see baltzo_zoneinfo, baltzo_datafileloader, baltzo_testloader
40///
41/// # Description {#baltzo_loader-description}
42/// This component provides a protocol, `baltzo::Loader`, for
43/// obtaining information about a time zone from some data-source. The
44/// protocol's primary method, `loadTimeZone`, loads a `baltzo::Zoneinfo` object
45/// describing the time zone identified by a user supplied time-zone string
46/// identifier.
47///
48/// ## Properties of a Populated baltzo::Zoneinfo {#baltzo_loader-properties-of-a-populated-baltzo-zoneinfo}
49///
50///
51/// Implementations of `baltzo::Loader` must return a `baltzo::Zoneinfo` object
52/// that is well-formed (see `baltzo::ZoneinfoUtil::isWellFormed`), and whose
53/// `identifier` property matches the requested time zone id. Implementations
54/// not meeting these constraints will generally not interoperate with other
55/// components in this package that enforce those constraints on provided
56/// `baltzo::Zoneinfo` objects, e.g., @ref baltzo_zoneinfocache .
57///
58/// ## Usage {#baltzo_loader-usage}
59///
60///
61/// This section illustrates intended use of this component.
62///
63/// ### Example 1: Implementing baltzo::Loader {#baltzo_loader-example-1-implementing-baltzo-loader}
64///
65///
66/// This example demonstrates an implementation of `baltzo::Loader` that can
67/// only return data for "America/New_York".
68///
69/// Note that in general, an implementation of `baltzo::Loader` should obtain
70/// time-zone information from an external data source (see
71/// @ref baltzo_datafileloader ).
72///
73/// First, we define the interface of our implementation:
74/// @code
75/// /// This class provides a concrete implementation of the
76/// /// `baltzo::Loader` protocol (an abstract interface) for obtaining a
77/// /// time zone. This test implementation contains only partial data of
78/// /// the "America/New_York" time zone, and is unable to obtain time-zone
79/// /// information for any other time zones.
80/// class MyLoaderImp : public baltzo::Loader {
81/// public:
82/// // CREATORS
83///
84/// /// Create a `MyLoaderImp` object.
85/// MyLoaderImp();
86///
87/// // Destroy this object.
88/// ~MyLoaderImp();
89///
90/// // MANIPULATORS
91///
92/// /// Load into the specified `result` the "Zoneinfo" time zone
93/// /// information for the time zone identified by the specified
94/// /// `timeZoneId`. Return 0 on success, and non-zero otherwise.
95/// virtual int loadTimeZone(baltzo::Zoneinfo *result,
96/// const char *timeZoneId);
97/// };
98/// @endcode
99/// Then, we implement the creators, trivially, as this class contains no
100/// instance data members.
101/// @code
102/// MyLoaderImp::MyLoaderImp()
103/// {
104/// }
105///
106/// MyLoaderImp::~MyLoaderImp()
107/// {
108/// }
109/// @endcode
110/// Next, we implement the `loadTimeZone` function:
111/// @code
112/// int MyLoaderImp::loadTimeZone(baltzo::Zoneinfo *result,
113/// const char *timeZoneId)
114/// {
115/// @endcode
116/// Then, we check the `timeZoneId` equals to "America/New_York" as this
117/// implementation is designed to demonstrate only one time zone:
118/// @code
119/// if (0 != strcmp("America/New_York", timeZoneId)) {
120/// return 1; // RETURN
121/// }
122/// @endcode
123/// Next, we load `result` with the time zone identifier for New York
124/// @code
125/// result->setIdentifier("America/New_York");
126/// @endcode
127/// Then, we create two local-time descriptors, one for standard time and one
128/// for daylight-saving time.
129/// @code
130/// baltzo::LocalTimeDescriptor edt(-14400, true, "EDT");
131/// baltzo::LocalTimeDescriptor est(-18000, false, "EST");
132/// @endcode
133/// Next, we create a series of transitions between these local time descriptors
134/// for the years 2007-2011. Note that the United States transitions to
135/// daylight saving time on the second Sunday in March, at 2am local time (7am
136/// UTC), and transition back to standard time on the first Sunday in November
137/// at 2am local time (6am UTC), resulting in an even number of transitions.
138/// Also note that, the rules for generating these transitions were changed in
139/// 2007, and may be changed again at some point in the future.
140/// @code
141/// static const bdlt::Datetime TRANSITION_TIMES[] = {
142/// bdlt::Datetime(2007, 3, 11, 7),
143/// bdlt::Datetime(2007, 11, 4, 6),
144/// bdlt::Datetime(2008, 3, 9, 7),
145/// bdlt::Datetime(2008, 11, 2, 6),
146/// bdlt::Datetime(2009, 3, 8, 7),
147/// bdlt::Datetime(2009, 11, 1, 6),
148/// bdlt::Datetime(2010, 3, 14, 7),
149/// bdlt::Datetime(2010, 11, 7, 6),
150/// bdlt::Datetime(2011, 3, 13, 7),
151/// bdlt::Datetime(2011, 11, 6, 6),
152/// };
153/// const int NUM_TRANSITION_TIMES =
154/// sizeof TRANSITION_TIMES / sizeof *TRANSITION_TIMES;
155///
156/// assert(0 == NUM_TRANSITION_TIMES % 2);
157///
158/// for (int i = 0; i < NUM_TRANSITION_TIMES; i += 2) {
159/// result->addTransition(
160/// bdlt::EpochUtil::convertToTimeT64(TRANSITION_TIMES[i]),
161/// edt);
162/// result->addTransition(
163/// bdlt::EpochUtil::convertToTimeT64(TRANSITION_TIMES[i + 1]),
164/// est);
165/// }
166/// return 0;
167/// }
168/// @endcode
169/// Finally, we define a function `f` that instantiates an object of type
170/// `MyLoaderImp`:
171/// @code
172/// void f()
173/// {
174/// MyLoaderImp a;
175/// }
176/// @endcode
177///
178/// ### Example 2: Using a baltzo::Loader {#baltzo_loader-example-2-using-a-baltzo-loader}
179///
180///
181/// In this example we use a `MyLoaderImpl` to load the data for one time zone,
182/// and print the time transitions, contained in the obtained time zone data, to
183/// standard output. Note that, the implementation of this example is for
184/// illustrative purpose only, and in general, clients should use an
185/// implementation that loads data from an external data source (e.g.,
186/// @ref baltzo_datafileloader ).
187///
188/// First, we obtain a `MyLoaderImpl` reference to `myLoader` we constructed in
189/// the previous example:
190/// @code
191/// baltzo::Loader& loader = myLoader;
192/// @endcode
193/// Now, we load the time zone data for New York:
194/// @code
195/// baltzo::Zoneinfo nyTimeZone;
196/// if (0 != loader.loadTimeZone(&nyTimeZone, "America/New_York")) {
197/// bsl::cout << "Failed to find time zone data." << bsl::endl;
198/// return -1; // RETURN
199/// }
200/// @endcode
201/// Then, we verify some basic properties of the time zone:
202/// @code
203/// assert("America/New_York" == nyTimeZone.identifier());
204/// @endcode
205/// Finally, we write to standard output the information about the
206/// daylight-saving time transitions of the loaded time zone, in New York local
207/// time:
208/// @code
209/// baltzo::Zoneinfo::TransitionConstIterator tIt =
210/// nyTimeZone.beginTransitions();
211/// for (; tIt != nyTimeZone.endTransitions(); ++tIt) {
212/// bdlt::Datetime transition =
213/// bdlt::EpochUtil::convertFromTimeT64(tIt->utcTransitionTime());
214/// const baltzo::LocalTimeDescriptor& descriptor = tIt->descriptor();
215///
216/// bsl::cout << "transition to "
217/// << descriptor.description()
218/// << " at "
219/// << transition
220/// << " UTC"
221/// << bsl::endl;
222/// }
223/// @endcode
224/// The resulting output will look like:
225/// @code
226/// transition to EDT at 11MAR2007_07:00:00.000 UTC
227/// transition to EST at 04NOV2007_06:00:00.000 UTC
228/// transition to EDT at 09MAR2008_07:00:00.000 UTC
229/// transition to EST at 02NOV2008_06:00:00.000 UTC
230/// transition to EDT at 08MAR2009_07:00:00.000 UTC
231/// transition to EST at 01NOV2009_06:00:00.000 UTC
232/// transition to EDT at 14MAR2010_07:00:00.000 UTC
233/// transition to EST at 07NOV2010_06:00:00.000 UTC
234/// transition to EDT at 13MAR2011_07:00:00.000 UTC
235/// transition to EST at 06NOV2011_06:00:00.000 UTC
236/// @endcode
237/// @}
238/** @} */
239/** @} */
240
241/** @addtogroup bal
242 * @{
243 */
244/** @addtogroup baltzo
245 * @{
246 */
247/** @addtogroup baltzo_loader
248 * @{
249 */
250
251#include <balscm_version.h>
252
253
254namespace baltzo {
255
256class Zoneinfo;
257
258 // ============
259 // class Loader
260 // ============
261
262/// This class provides a protocol (a pure abstract interface) for loading a
263/// time zone object.
264///
265/// See @ref baltzo_loader
266class Loader {
267
268 public:
269 // CREATORS
270
271 /// Destroy this object.
272 virtual ~Loader();
273
274 // MANIPULATORS
275
276 /// Load into the specified `result` the time zone information for the
277 /// time zone identified by the specified `timeZoneId`. Return 0 on
278 /// success, and a non-zero value otherwise. A return status of
279 /// `ErrorCode::k_UNSUPPORTED_ID` indicates that `timeZoneId` is not
280 /// recognized. If an error occurs during this operation, `result` will
281 /// be left in a valid, but otherwise unspecified state. On return,
282 /// `result` will be well-formed (see `ZoneinfoUtil::isWellFormed`) and
283 /// `result->identifier()` will be `timeZoneId` (concrete
284 /// implementations not meeting those constraints will not be usable by
285 /// other other components in this package -- e.g.,
286 /// @ref baltzo_zoneinfocache ).
287 virtual int loadTimeZone(Zoneinfo *result, const char *timeZoneId) = 0;
288};
289
290} // close package namespace
291
292
293#endif
294
295// ----------------------------------------------------------------------------
296// Copyright 2015 Bloomberg Finance L.P.
297//
298// Licensed under the Apache License, Version 2.0 (the "License");
299// you may not use this file except in compliance with the License.
300// You may obtain a copy of the License at
301//
302// http://www.apache.org/licenses/LICENSE-2.0
303//
304// Unless required by applicable law or agreed to in writing, software
305// distributed under the License is distributed on an "AS IS" BASIS,
306// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
307// See the License for the specific language governing permissions and
308// limitations under the License.
309// ----------------------------- END-OF-FILE ----------------------------------
310
311/** @} */
312/** @} */
313/** @} */
Definition baltzo_loader.h:266
virtual ~Loader()
Destroy this object.
virtual int loadTimeZone(Zoneinfo *result, const char *timeZoneId)=0
Definition baltzo_zoneinfo.h:429
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition baltzo_datafileloader.h:263