BDE 4.14.0 Production release
Loading...
Searching...
No Matches
baltzo_testloader.h
Go to the documentation of this file.
1/// @file baltzo_testloader.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// baltzo_testloader.h -*-C++-*-
8#ifndef INCLUDED_BALTZO_TESTLOADER
9#define INCLUDED_BALTZO_TESTLOADER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup baltzo_testloader baltzo_testloader
15/// @brief Provide a test implementation of the `baltzo::Loader` protocol.
16/// @addtogroup bal
17/// @{
18/// @addtogroup baltzo
19/// @{
20/// @addtogroup baltzo_testloader
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#baltzo_testloader-purpose"> Purpose</a>
25/// * <a href="#baltzo_testloader-classes"> Classes </a>
26/// * <a href="#baltzo_testloader-description"> Description </a>
27/// * <a href="#baltzo_testloader-usage"> Usage </a>
28/// * <a href="#baltzo_testloader-example-1-populating-a-baltzo-testloader-with-time-zone-information"> Example 1: Populating a baltzo::TestLoader with Time-Zone Information </a>
29/// * <a href="#baltzo_testloader-example-2-accessing-time-zone-information-from-a-baltzo-testloader"> Example 2: Accessing Time-Zone Information From a baltzo::TestLoader </a>
30///
31/// # Purpose {#baltzo_testloader-purpose}
32/// Provide a test implementation of the `baltzo::Loader` protocol.
33///
34/// # Classes {#baltzo_testloader-classes}
35///
36/// - baltzo::TestLoader: concrete implementation of `baltzo::Loader` protocol
37///
38/// @see baltzo_loader, baltzo_zoneinfo
39///
40/// # Description {#baltzo_testloader-description}
41/// This component provides `baltzo::TestLoader`, a concrete test
42/// implementation of the `baltzo::Loader` protocol for loading a
43/// `baltzo::Zoneinfo` object. The following inheritance hierarchy diagram
44/// shows the classes involved and their methods:
45/// @code
46/// ,------------------.
47/// ( baltzo::TestLoader )
48/// `------------------'
49/// | ctor
50/// | setTimeZone
51/// V
52/// ,--------------.
53/// ( baltzo::Loader )
54/// `--------------'
55/// dtor
56/// loadTimeZone
57/// @endcode
58/// This test implementation maintains a mapping of time-zone identifiers to
59/// `baltzo::Zoneinfo` objects. Clients can associate a time-zone object with a
60/// time-zone identifier using the `setTimeZone` method. A subsequent call to
61/// the protocol method `loadTimeZone` for that time-zone identifier will return
62/// the supplied `baltzo::Zoneinfo` object.
63///
64/// ## Usage {#baltzo_testloader-usage}
65///
66///
67/// The following examples demonstrate how to populate a `baltzo::TestLoader`
68/// with time.zone information, and then access that information through the
69/// `baltzo::Loader` protocol.
70///
71/// ### Example 1: Populating a baltzo::TestLoader with Time-Zone Information {#baltzo_testloader-example-1-populating-a-baltzo-testloader-with-time-zone-information}
72///
73///
74/// We start by creating a `baltzo::Zoneinfo` object, which we will eventually
75/// populate with a subset of data for "America/New_York":
76/// @code
77/// baltzo::Zoneinfo newYorkTimeZone;
78/// @endcode
79/// Next, we populate `newYorkTimeZone` with the correct time-zone identifier
80/// and two types of local time (standard time, and daylight-saving time):
81/// @code
82/// const char *NEW_YORK_ID = "America/New_York";
83/// newYorkTimeZone.setIdentifier(NEW_YORK_ID);
84///
85/// baltzo::LocalTimeDescriptor est(-5 * 60 * 60, false, "EST");
86/// baltzo::LocalTimeDescriptor edt(-4 * 60 * 60, true, "EDT");
87/// @endcode
88/// Then, we create a series of transitions between these local time
89/// descriptors for the years 2007-2011. Note that the United States
90/// transitions to daylight saving time on the second Sunday in March, at 2am
91/// local time (7am UTC), and transitions back to standard time on the first
92/// Sunday in November at 2am local time (6am UTC). Also note, that these rules
93/// for generating transitions was different prior to 2007, and may be changed
94/// at some point in the future.
95/// @code
96/// bdlt::Time edtTime(7, 0, 0); // UTC transition time
97/// bdlt::Time estTime(6, 0, 0); // UTC transition time
98/// static const int edtDays[5] = { 11, 9, 8, 14, 13 };
99/// static const int estDays[5] = { 4, 2, 1, 7, 6 };
100/// for (int year = 2007; year < 2012; ++year) {
101/// int edtDay = edtDays[year - 2007];
102/// int estDay = estDays[year - 2007];
103///
104/// bdlt::Datetime edtTransition(bdlt::Date(year, 3, edtDay), edtTime);
105/// bdlt::Datetime estTransition(bdlt::Date(year, 11, estDay), estTime);
106///
107/// bsls::Types::Int64 edtTransitionT =
108/// bdlt::EpochUtil::convertToTimeT64(edtTransition);
109///
110/// bsls::Types::Int64 estTransitionT =
111/// bdlt::EpochUtil::convertToTimeT64(estTransition);
112/// @endcode
113/// Now, having created values representing the daylight saving time
114/// transitions (in UTC), we insert the transitions into the `baltzo::Zoneinfo`
115/// object `newYorkTimeZone`:
116/// @code
117/// newYorkTimeZone.addTransition(edtTransitionT, edt);
118/// newYorkTimeZone.addTransition(estTransitionT, est);
119/// }
120/// @endcode
121/// Now, we create a `baltzo::TestLoader` object and configure it with
122/// `newYorkTimeZone`, which the test loader will associate with the identifier
123/// `newYorkTimeZone.identifier()` (whose value is "America/New_York"):
124/// @code
125/// baltzo::TestLoader testLoader;
126/// testLoader.setTimeZone(newYorkTimeZone);
127/// @endcode
128///
129/// ### Example 2: Accessing Time-Zone Information From a baltzo::TestLoader {#baltzo_testloader-example-2-accessing-time-zone-information-from-a-baltzo-testloader}
130///
131///
132/// In the next example, we will use the `baltzo::TestLoader` we initialized in
133/// the preceding example, to load time-zone information for New York via the
134/// `baltzo::Loader` protocol.
135///
136/// We start by creating a `baltzo::Loader` reference to `testLoader`:
137/// @code
138/// baltzo::Loader& loader = testLoader;
139/// @endcode
140/// Now we used the protocol method `loadTimeZone` to load time-zone
141/// information for New York:
142/// @code
143/// baltzo::Zoneinfo resultNewYork;
144/// int status = loader.loadTimeZone(&resultNewYork, "America/New_York");
145/// assert(0 == status);
146/// @endcode
147/// Finally, we verify that the returned time-zone information,
148/// `resultNewYork`, is equivalent to `newYorkTimeZone`, which we we used to
149/// configure `testLoader`:
150/// @code
151/// assert(newYorkTimeZone == resultNewYork);
152/// @endcode
153/// @}
154/** @} */
155/** @} */
156
157/** @addtogroup bal
158 * @{
159 */
160/** @addtogroup baltzo
161 * @{
162 */
163/** @addtogroup baltzo_testloader
164 * @{
165 */
166
167#include <balscm_version.h>
168
169#include <baltzo_loader.h>
170#include <baltzo_zoneinfo.h>
171
172#include <bslma_default.h>
173#include <bslma_bslallocator.h>
175
177
178#include <bsls_keyword.h>
179
180#include <bsl_iosfwd.h>
181#include <bsl_map.h>
182#include <bsl_string.h>
183
184
185namespace baltzo {
186
187 // ================
188 // class TestLoader
189 // ================
190
191/// This class provides a concrete test implementation of the `Loader`
192/// protocol (an abstract interface) for obtaining a time zone. This test
193/// implementation maintains a mapping of time-zone identifiers to
194/// `Zoneinfo` objects. Time zone information objects are associated with a
195/// time-zone identifier using the `setTimeZone` method, and can be
196/// subsequently accessed by calling the protocol method `loadTimeZone` with
197/// the same identifier.
198///
199/// See @ref baltzo_testloader
200class TestLoader: public Loader {
201
202 private:
203 /// A `TimeZoneMap` is a type that maps a string time-zone identifier to
204 /// information about that time zone.
206
207 // DATA
208 TimeZoneMap d_timeZones; // set of time zones maintained by this test
209 // loader
210
211 private:
212 // NOT IMPLEMENTED
213 TestLoader(const TestLoader&);
214 TestLoader& operator=(const TestLoader&);
215
216 public:
217 // TYPES
219
220 // TRAITS
222
223 // CREATORS
224
225 TestLoader();
226 /// Create a `TestLoader` object. Optionally specify an `allocator`
227 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
228 /// otherwise, the default allocator is used. By default the test
229 /// loader will return `ErrorCode::k_UNSUPPORTED_ID` for all time-zone
230 /// identifiers.
231 explicit TestLoader(const allocator_type& allocator);
232
233 /// Destroy this `TestLoader` object;
235
236 // MANIPULATORS
237
238 /// Set, to the specified `timeZone`, the time-zone information that
239 /// will be returned by `loadTimeZone` for the identifier
240 /// `timeZone.identifier()`.
241 void setTimeZone(const Zoneinfo& timeZone);
242
243 /// Set the time-zone data this test loader will return for the
244 /// specified `timeZoneId` to the Zoneinfo value defined by reading the
245 /// specified `timeZoneData` buffer, holding data in the Zoneinfo
246 /// standard binary file format, of at least the specified
247 /// `timeZoneDataNumBytes`. Return 0 on success, or a negative value
248 /// if an error occurs. The behavior is undefined unless
249 /// `timeZoneData` contains at least `timeZoneDataNumBytes` bytes.
250 int setTimeZone(const char *timeZoneId,
251 const char *timeZoneData,
252 int timeZoneDataNumBytes);
253
254 /// Load into the specified `result` the time-zone information for the
255 /// time-zone identified by the specified `timeZoneId`. Return 0 on
256 /// success, and a non-zero value otherwise. A return status of
257 /// `ErrorCode::k_UNSUPPORTED_ID` indicates that `timeZoneId` is not
258 /// recognized. If an error occurs during the operation, `result` will
259 /// be left in a valid but unspecified state.
260 int loadTimeZone(Zoneinfo *result, const char *timeZoneId)
262
263 // ACCESSORS
264
265 // Aspects
266
267 /// Return the allocator used by this object to supply memory. Note
268 /// that if no allocator was supplied at construction the default
269 /// allocator in effect at construction is used.
271
272 /// Write the set of time zones maintained by this object to the
273 /// specified output `stream` in a human-readable format, and return a
274 /// reference to `stream`. Optionally specify an initial indentation
275 /// `level`, whose absolute value is incremented recursively for nested
276 /// objects. If `level` is specified, optionally specify
277 /// `spacesPerLevel`, whose absolute value indicates the number of
278 /// spaces per indentation level for this and all of its nested objects.
279 /// If `level` is negative, suppress indentation of the first line. If
280 /// `spacesPerLevel` is negative, format the entire output on one line,
281 /// suppressing all but the initial indentation (as governed by
282 /// `level`). If `stream` is not valid on entry, this operation has no
283 /// effect. Note that the format is not fully specified, and can change
284 /// without notice.
285 bsl::ostream& print(bsl::ostream& stream,
286 int level = 0,
287 int spacesPerLevel = 4) const;
288
289};
290
291// FREE OPERATORS
292
293/// Write the set of time zones maintained by the specified `loader` to the
294/// specified output `stream` in a single-line format, and return a
295/// reference to `stream`. If `stream` is not valid on entry, this
296/// operation has no effect. Note that this human-readable format is not
297/// fully specified, can change without notice, and is logically equivalent
298/// to:
299/// @code
300/// print(stream, 0, -1);
301/// @endcode
302bsl::ostream& operator<<(bsl::ostream& stream, const TestLoader& loader);
303
304// ============================================================================
305// INLINE DEFINITIONS
306// ============================================================================
307
308 // ----------------
309 // class TestLoader
310 // ----------------
311
312// CREATORS
313inline
315: d_timeZones()
316{
317}
318
319inline
321: d_timeZones(allocator)
322{
323}
324
325// ACCESSORS
326inline
328{
329 return d_timeZones.get_allocator();
330}
331
332} // close package namespace
333
334// FREE FUNCTIONS
335inline
336bsl::ostream& baltzo::operator<<(bsl::ostream& stream,
337 const TestLoader& loader)
338{
339 return loader.print(stream, 0, -1);
340}
341
342
343
344#endif
345
346// ----------------------------------------------------------------------------
347// Copyright 2018 Bloomberg Finance L.P.
348//
349// Licensed under the Apache License, Version 2.0 (the "License");
350// you may not use this file except in compliance with the License.
351// You may obtain a copy of the License at
352//
353// http://www.apache.org/licenses/LICENSE-2.0
354//
355// Unless required by applicable law or agreed to in writing, software
356// distributed under the License is distributed on an "AS IS" BASIS,
357// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
358// See the License for the specific language governing permissions and
359// limitations under the License.
360// ----------------------------- END-OF-FILE ----------------------------------
361
362/** @} */
363/** @} */
364/** @} */
Definition baltzo_loader.h:266
Definition baltzo_testloader.h:200
bsl::allocator< char > allocator_type
Definition baltzo_testloader.h:218
~TestLoader() BSLS_KEYWORD_OVERRIDE
Destroy this TestLoader object;.
int loadTimeZone(Zoneinfo *result, const char *timeZoneId) BSLS_KEYWORD_OVERRIDE
void setTimeZone(const Zoneinfo &timeZone)
TestLoader()
Definition baltzo_testloader.h:314
allocator_type get_allocator() const
Definition baltzo_testloader.h:327
BSLMF_NESTED_TRAIT_DECLARATION(TestLoader, bslma::UsesBslmaAllocator)
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition baltzo_zoneinfo.h:429
Definition bslma_bslallocator.h:580
Definition bslstl_map.h:619
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_map.h:3428
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition baltzo_datafileloader.h:263
bsl::ostream & operator<<(bsl::ostream &stream, DstPolicy::Enum value)
Definition bdlb_printmethods.h:283
Definition bslma_usesbslmaallocator.h:343