BDE 4.14.0 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.
202
203 // CLASS METHODS
204
205 /// Load, into the specified `result`, the local date-time value, in the
206 /// time zone indicated by the specified `resultTimeZoneId`,
207 /// corresponding to the specified `utcTime`, using time zone
208 /// information supplied by the specified `cache`. Return 0 on success,
209 /// and a non-zero value otherwise. A return status of
210 /// `ErrorCode::k_UNSUPPORTED_ID` indicates that `resultTimeZoneId` is
211 /// not recognized, and a return status of `ErrorCode::k_OUT_OF_RANGE`
212 /// indicates that an out of range value of `result` would have
213 /// occurred.
215 const char *resultTimeZoneId,
216 const bdlt::Datetime& utcTime,
217 ZoneinfoCache *cache);
218
219 /// Load, into the specified `result`, attributes characterizing local
220 /// time indicated by the specified `transition` in the specified
221 /// `timeZone`. The behavior is undefined unless
222 /// `ZoneinfoUtil::isWellFormed(timeZone)` is `true` and `transition` is
223 /// a valid, non-ending, iterator into the sequence of transitions
224 /// described by `timeZone`.
226 LocalTimePeriod *result,
227 const Zoneinfo::TransitionConstIterator& transition,
228 const Zoneinfo& timeZone);
229
230 /// Load, into the specified `result`, the local date-time value --
231 /// including the local date, time, and resolved UTC offset -- indicated
232 /// by the specified `localTime` in the time zone indicated by the
233 /// specified `timeZoneId`, using the specified `dstPolicy` to interpret
234 /// whether or not `localTime` represents a daylight-saving time value,
235 /// and using time zone information supplied by the specified `cache`.
236 /// Load, into the specified `resultValidity` the value indicating the
237 /// whether `localTime` is unique, ambiguous but valid, or invalid.
238 /// Return 0 on success, and a non-zero value otherwise. A return
239 /// status of `ErrorCode::k_UNSUPPORTED_ID` indicates that `timeZoneId`
240 /// is not recognized. The behavior is undefined unless the result of
241 /// the initialization falls within the supported epoch.
242 static int initLocalTime(bdlt::DatetimeTz *result,
243 LocalTimeValidity::Enum *resultValidity,
244 const bdlt::Datetime& localTime,
245 const char *timeZoneId,
246 DstPolicy::Enum dstPolicy,
247 ZoneinfoCache *cache);
248
249 /// Load, into the specified `result`, attributes characterizing local
250 /// time at the specified `utcTime` in the time zone indicated by the
251 /// specified `timeZoneId` (e.g., the offset from UTC, whether it is
252 /// daylight-saving time), as well as the time interval over which those
253 /// attributes apply, using time zone information supplied by the
254 /// specified `cache`. Return 0 on success, and a non-zero value
255 /// otherwise. A return status of `ErrorCode::k_UNSUPPORTED_ID`
256 /// indicates that `timeZoneId` is not recognized.
258 const char *timeZoneId,
259 const bdlt::Datetime& utcTime,
260 ZoneinfoCache *cache);
261
262 /// Load, into the specified `result`, the local time and UTC offset of
263 /// the specified `localTime` in the specified `timeZone`, using the
264 /// specified `dstPolicy` to interpret whether or not `localTime`
265 /// represents a daylight-saving time value; load into the specified
266 /// `resultValidity` an indication of whether `localTime` is valid and
267 /// unique, valid but ambiguous, or invalid; load into the specified
268 /// `transitionIter` an iterator pointing to the transition that
269 /// characterizes the attributes of `localTime`. The behavior is
270 /// undefined unless `ZoneinfoUtil::isWellFormed(timeZone)` is `true`
271 /// and the result of the resolution falls within the supported epoch.
272 static void resolveLocalTime(
273 bdlt::DatetimeTz *result,
274 LocalTimeValidity::Enum *resultValidity,
275 Zoneinfo::TransitionConstIterator *transitionIter,
276 const bdlt::Datetime& localTime,
277 DstPolicy::Enum dstPolicy,
278 const Zoneinfo& timeZone);
279
280};
281
282} // close package namespace
283
284
285#endif
286
287// ----------------------------------------------------------------------------
288// Copyright 2015 Bloomberg Finance L.P.
289//
290// Licensed under the Apache License, Version 2.0 (the "License");
291// you may not use this file except in compliance with the License.
292// You may obtain a copy of the License at
293//
294// http://www.apache.org/licenses/LICENSE-2.0
295//
296// Unless required by applicable law or agreed to in writing, software
297// distributed under the License is distributed on an "AS IS" BASIS,
298// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
299// See the License for the specific language governing permissions and
300// limitations under the License.
301// ----------------------------- END-OF-FILE ----------------------------------
302
303/** @} */
304/** @} */
305/** @} */
Definition baltzo_localtimeperiod.h:211
Definition baltzo_zoneinfocache.h:232
Definition baltzo_zoneinfo.h:429
TransitionSequence::const_iterator TransitionConstIterator
Definition baltzo_zoneinfo.h:488
Definition bdlt_datetimetz.h:308
Definition bdlt_datetime.h:331
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition baltzo_datafileloader.h:263
Enum
Definition baltzo_dstpolicy.h:131
Enum
Definition baltzo_localtimevalidity.h:149
Definition baltzo_timezoneutilimp.h:201
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)