]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/timezone.h
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / timezone.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*************************************************************************
4 * Copyright (c) 1997-2016, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 **************************************************************************
7 *
8 * File TIMEZONE.H
9 *
10 * Modification History:
11 *
12 * Date Name Description
13 * 04/21/97 aliu Overhauled header.
14 * 07/09/97 helena Changed createInstance to createDefault.
15 * 08/06/97 aliu Removed dependency on internal header for Hashtable.
16 * 08/10/98 stephen Changed getDisplayName() API conventions to match
17 * 08/19/98 stephen Changed createTimeZone() to never return 0
18 * 09/02/98 stephen Sync to JDK 1.2 8/31
19 * - Added getOffset(... monthlen ...)
20 * - Added hasSameRules()
21 * 09/15/98 stephen Added getStaticClassID
22 * 12/03/99 aliu Moved data out of static table into icudata.dll.
23 * Hashtable replaced by new static data structures.
24 * 12/14/99 aliu Made GMT public.
25 * 08/15/01 grhoten Made GMT private and added the getGMT() function
26 **************************************************************************
27 */
28
29 #ifndef TIMEZONE_H
30 #define TIMEZONE_H
31
32 #include "unicode/utypes.h"
33
34 /**
35 * \file
36 * \brief C++ API: TimeZone object
37 */
38
39 #if !UCONFIG_NO_FORMATTING
40
41 #include "unicode/uobject.h"
42 #include "unicode/unistr.h"
43 #include "unicode/ures.h"
44 #include "unicode/ucal.h"
45
46 #if U_SHOW_CPLUSPLUS_API
47 U_NAMESPACE_BEGIN
48
49 class StringEnumeration;
50
51 /**
52 *
53 * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
54 * savings.
55 *
56 * <p>
57 * Typically, you get a <code>TimeZone</code> using <code>createDefault</code>
58 * which creates a <code>TimeZone</code> based on the time zone where the program
59 * is running. For example, for a program running in Japan, <code>createDefault</code>
60 * creates a <code>TimeZone</code> object based on Japanese Standard Time.
61 *
62 * <p>
63 * You can also get a <code>TimeZone</code> using <code>createTimeZone</code> along
64 * with a time zone ID. For instance, the time zone ID for the US Pacific
65 * Time zone is "America/Los_Angeles". So, you can get a Pacific Time <code>TimeZone</code> object
66 * with:
67 * \htmlonly<blockquote>\endhtmlonly
68 * <pre>
69 * TimeZone *tz = TimeZone::createTimeZone("America/Los_Angeles");
70 * </pre>
71 * \htmlonly</blockquote>\endhtmlonly
72 * You can use the <code>createEnumeration</code> method to iterate through
73 * all the supported time zone IDs, or the <code>getCanonicalID</code> method to check
74 * if a time zone ID is supported or not. You can then choose a
75 * supported ID to get a <code>TimeZone</code>.
76 * If the time zone you want is not represented by one of the
77 * supported IDs, then you can create a custom time zone ID with
78 * the following syntax:
79 *
80 * \htmlonly<blockquote>\endhtmlonly
81 * <pre>
82 * GMT[+|-]hh[[:]mm]
83 * </pre>
84 * \htmlonly</blockquote>\endhtmlonly
85 *
86 * For example, you might specify GMT+14:00 as a custom
87 * time zone ID. The <code>TimeZone</code> that is returned
88 * when you specify a custom time zone ID uses the specified
89 * offset from GMT(=UTC) and does not observe daylight saving
90 * time. For example, you might specify GMT+14:00 as a custom
91 * time zone ID to create a TimeZone representing 14 hours ahead
92 * of GMT (with no daylight saving time). In addition,
93 * <code>getCanonicalID</code> can also be used to
94 * normalize a custom time zone ID.
95 *
96 * TimeZone is an abstract class representing a time zone. A TimeZone is needed for
97 * Calendar to produce local time for a particular time zone. A TimeZone comprises
98 * three basic pieces of information:
99 * <ul>
100 * <li>A time zone offset; that, is the number of milliseconds to add or subtract
101 * from a time expressed in terms of GMT to convert it to the same time in that
102 * time zone (without taking daylight savings time into account).</li>
103 * <li>Logic necessary to take daylight savings time into account if daylight savings
104 * time is observed in that time zone (e.g., the days and hours on which daylight
105 * savings time begins and ends).</li>
106 * <li>An ID. This is a text string that uniquely identifies the time zone.</li>
107 * </ul>
108 *
109 * (Only the ID is actually implemented in TimeZone; subclasses of TimeZone may handle
110 * daylight savings time and GMT offset in different ways. Currently we have the following
111 * TimeZone subclasses: RuleBasedTimeZone, SimpleTimeZone, and VTimeZone.)
112 * <P>
113 * The TimeZone class contains a static list containing a TimeZone object for every
114 * combination of GMT offset and daylight-savings time rules currently in use in the
115 * world, each with a unique ID. Each ID consists of a region (usually a continent or
116 * ocean) and a city in that region, separated by a slash, (for example, US Pacific
117 * Time is "America/Los_Angeles.") Because older versions of this class used
118 * three- or four-letter abbreviations instead, there is also a table that maps the older
119 * abbreviations to the newer ones (for example, "PST" maps to "America/Los_Angeles").
120 * Anywhere the API requires an ID, you can use either form.
121 * <P>
122 * To create a new TimeZone, you call the factory function TimeZone::createTimeZone()
123 * and pass it a time zone ID. You can use the createEnumeration() function to
124 * obtain a list of all the time zone IDs recognized by createTimeZone().
125 * <P>
126 * You can also use TimeZone::createDefault() to create a TimeZone. This function uses
127 * platform-specific APIs to produce a TimeZone for the time zone corresponding to
128 * the client's computer's physical location. For example, if you're in Japan (assuming
129 * your machine is set up correctly), TimeZone::createDefault() will return a TimeZone
130 * for Japanese Standard Time ("Asia/Tokyo").
131 */
132 class U_I18N_API TimeZone : public UObject {
133 public:
134 /**
135 * @stable ICU 2.0
136 */
137 virtual ~TimeZone();
138
139 /**
140 * Returns the "unknown" time zone.
141 * It behaves like the GMT/UTC time zone but has the
142 * <code>UCAL_UNKNOWN_ZONE_ID</code> = "Etc/Unknown".
143 * createTimeZone() returns a mutable clone of this time zone if the input ID is not recognized.
144 *
145 * @return the "unknown" time zone.
146 * @see UCAL_UNKNOWN_ZONE_ID
147 * @see createTimeZone
148 * @see getGMT
149 * @stable ICU 49
150 */
151 static const TimeZone& U_EXPORT2 getUnknown();
152
153 /**
154 * The GMT (=UTC) time zone has a raw offset of zero and does not use daylight
155 * savings time. This is a commonly used time zone.
156 *
157 * <p>Note: For backward compatibility reason, the ID used by the time
158 * zone returned by this method is "GMT", although the ICU's canonical
159 * ID for the GMT time zone is "Etc/GMT".
160 *
161 * @return the GMT/UTC time zone.
162 * @see getUnknown
163 * @stable ICU 2.0
164 */
165 static const TimeZone* U_EXPORT2 getGMT(void);
166
167 /**
168 * Creates a <code>TimeZone</code> for the given ID.
169 * @param ID the ID for a <code>TimeZone</code>, such as "America/Los_Angeles",
170 * or a custom ID such as "GMT-8:00".
171 * @return the specified <code>TimeZone</code>, or a mutable clone of getUnknown()
172 * if the given ID cannot be understood or if the given ID is "Etc/Unknown".
173 * The return result is guaranteed to be non-NULL.
174 * If you require that the specific zone asked for be returned,
175 * compare the result with getUnknown() or check the ID of the return result.
176 * @stable ICU 2.0
177 */
178 static TimeZone* U_EXPORT2 createTimeZone(const UnicodeString& ID);
179
180 /**
181 * Returns an enumeration over system time zone IDs with the given
182 * filter conditions.
183 * @param zoneType The system time zone type.
184 * @param region The ISO 3166 two-letter country code or UN M.49
185 * three-digit area code. When NULL, no filtering
186 * done by region.
187 * @param rawOffset An offset from GMT in milliseconds, ignoring
188 * the effect of daylight savings time, if any.
189 * When NULL, no filtering done by zone offset.
190 * @param ec Output param to filled in with a success or
191 * an error.
192 * @return an enumeration object, owned by the caller.
193 * @stable ICU 4.8
194 */
195 static StringEnumeration* U_EXPORT2 createTimeZoneIDEnumeration(
196 USystemTimeZoneType zoneType,
197 const char* region,
198 const int32_t* rawOffset,
199 UErrorCode& ec);
200
201 /**
202 * Returns an enumeration over all recognized time zone IDs. (i.e.,
203 * all strings that createTimeZone() accepts)
204 *
205 * @return an enumeration object, owned by the caller.
206 * @stable ICU 2.4
207 */
208 static StringEnumeration* U_EXPORT2 createEnumeration();
209
210 /**
211 * Returns an enumeration over time zone IDs with a given raw
212 * offset from GMT. There may be several times zones with the
213 * same GMT offset that differ in the way they handle daylight
214 * savings time. For example, the state of Arizona doesn't
215 * observe daylight savings time. If you ask for the time zone
216 * IDs corresponding to GMT-7:00, you'll get back an enumeration
217 * over two time zone IDs: "America/Denver," which corresponds to
218 * Mountain Standard Time in the winter and Mountain Daylight Time
219 * in the summer, and "America/Phoenix", which corresponds to
220 * Mountain Standard Time year-round, even in the summer.
221 *
222 * @param rawOffset an offset from GMT in milliseconds, ignoring
223 * the effect of daylight savings time, if any
224 * @return an enumeration object, owned by the caller
225 * @stable ICU 2.4
226 */
227 static StringEnumeration* U_EXPORT2 createEnumeration(int32_t rawOffset);
228
229 /**
230 * Returns an enumeration over time zone IDs associated with the
231 * given country. Some zones are affiliated with no country
232 * (e.g., "UTC"); these may also be retrieved, as a group.
233 *
234 * @param country The ISO 3166 two-letter country code, or NULL to
235 * retrieve zones not affiliated with any country.
236 * @return an enumeration object, owned by the caller
237 * @stable ICU 2.4
238 */
239 static StringEnumeration* U_EXPORT2 createEnumeration(const char* country);
240
241 /**
242 * Returns the number of IDs in the equivalency group that
243 * includes the given ID. An equivalency group contains zones
244 * that have the same GMT offset and rules.
245 *
246 * <p>The returned count includes the given ID; it is always >= 1.
247 * The given ID must be a system time zone. If it is not, returns
248 * zero.
249 * @param id a system time zone ID
250 * @return the number of zones in the equivalency group containing
251 * 'id', or zero if 'id' is not a valid system ID
252 * @see #getEquivalentID
253 * @stable ICU 2.0
254 */
255 static int32_t U_EXPORT2 countEquivalentIDs(const UnicodeString& id);
256
257 /**
258 * Returns an ID in the equivalency group that
259 * includes the given ID. An equivalency group contains zones
260 * that have the same GMT offset and rules.
261 *
262 * <p>The given index must be in the range 0..n-1, where n is the
263 * value returned by <code>countEquivalentIDs(id)</code>. For
264 * some value of 'index', the returned value will be equal to the
265 * given id. If the given id is not a valid system time zone, or
266 * if 'index' is out of range, then returns an empty string.
267 * @param id a system time zone ID
268 * @param index a value from 0 to n-1, where n is the value
269 * returned by <code>countEquivalentIDs(id)</code>
270 * @return the ID of the index-th zone in the equivalency group
271 * containing 'id', or an empty string if 'id' is not a valid
272 * system ID or 'index' is out of range
273 * @see #countEquivalentIDs
274 * @stable ICU 2.0
275 */
276 static const UnicodeString U_EXPORT2 getEquivalentID(const UnicodeString& id,
277 int32_t index);
278
279 /**
280 * Creates an instance of TimeZone detected from the current host
281 * system configuration. Note that ICU4C does not change the default
282 * time zone unless TimeZone::adoptDefault(TimeZone*) or
283 * TimeZone::setDefault(const TimeZone&) is explicitly called by a
284 * user. This method does not update the current ICU's default,
285 * and may return a different TimeZone from the one returned by
286 * TimeZone::createDefault().
287 *
288 * @return A new instance of TimeZone detected from the current host system
289 * configuration.
290 * @stable ICU 55
291 */
292 static TimeZone* U_EXPORT2 detectHostTimeZone();
293
294 /**
295 * Creates a new copy of the default TimeZone for this host. Unless the default time
296 * zone has already been set using adoptDefault() or setDefault(), the default is
297 * determined by querying the system using methods in TPlatformUtilities. If the
298 * system routines fail, or if they specify a TimeZone or TimeZone offset which is not
299 * recognized, the TimeZone indicated by the ID kLastResortID is instantiated
300 * and made the default.
301 *
302 * @return A default TimeZone. Clients are responsible for deleting the time zone
303 * object returned.
304 * @stable ICU 2.0
305 */
306 static TimeZone* U_EXPORT2 createDefault(void);
307
308 /**
309 * Sets the default time zone (i.e., what's returned by createDefault()) to be the
310 * specified time zone. If NULL is specified for the time zone, the default time
311 * zone is set to the default host time zone. This call adopts the TimeZone object
312 * passed in; the client is no longer responsible for deleting it.
313 *
314 * <p>This function is not thread safe. It is an error for multiple threads
315 * to concurrently attempt to set the default time zone, or for any thread
316 * to attempt to reference the default zone while another thread is setting it.
317 *
318 * @param zone A pointer to the new TimeZone object to use as the default.
319 * @stable ICU 2.0
320 */
321 static void U_EXPORT2 adoptDefault(TimeZone* zone);
322
323 #ifndef U_HIDE_SYSTEM_API
324 /**
325 * Same as adoptDefault(), except that the TimeZone object passed in is NOT adopted;
326 * the caller remains responsible for deleting it.
327 *
328 * <p>See the thread safety note under adoptDefault().
329 *
330 * @param zone The given timezone.
331 * @system
332 * @stable ICU 2.0
333 */
334 static void U_EXPORT2 setDefault(const TimeZone& zone);
335 #endif /* U_HIDE_SYSTEM_API */
336
337 /**
338 * Returns the timezone data version currently used by ICU.
339 * @param status Output param to filled in with a success or an error.
340 * @return the version string, such as "2007f"
341 * @stable ICU 3.8
342 */
343 static const char* U_EXPORT2 getTZDataVersion(UErrorCode& status);
344
345 /**
346 * Returns the canonical system timezone ID or the normalized
347 * custom time zone ID for the given time zone ID.
348 * @param id The input time zone ID to be canonicalized.
349 * @param canonicalID Receives the canonical system time zone ID
350 * or the custom time zone ID in normalized format.
351 * @param status Receives the status. When the given time zone ID
352 * is neither a known system time zone ID nor a
353 * valid custom time zone ID, U_ILLEGAL_ARGUMENT_ERROR
354 * is set.
355 * @return A reference to the result.
356 * @stable ICU 4.0
357 */
358 static UnicodeString& U_EXPORT2 getCanonicalID(const UnicodeString& id,
359 UnicodeString& canonicalID, UErrorCode& status);
360
361 /**
362 * Returns the canonical system time zone ID or the normalized
363 * custom time zone ID for the given time zone ID.
364 * @param id The input time zone ID to be canonicalized.
365 * @param canonicalID Receives the canonical system time zone ID
366 * or the custom time zone ID in normalized format.
367 * @param isSystemID Receives if the given ID is a known system
368 * time zone ID.
369 * @param status Receives the status. When the given time zone ID
370 * is neither a known system time zone ID nor a
371 * valid custom time zone ID, U_ILLEGAL_ARGUMENT_ERROR
372 * is set.
373 * @return A reference to the result.
374 * @stable ICU 4.0
375 */
376 static UnicodeString& U_EXPORT2 getCanonicalID(const UnicodeString& id,
377 UnicodeString& canonicalID, UBool& isSystemID, UErrorCode& status);
378
379 /**
380 * Converts a system time zone ID to an equivalent Windows time zone ID. For example,
381 * Windows time zone ID "Pacific Standard Time" is returned for input "America/Los_Angeles".
382 *
383 * <p>There are system time zones that cannot be mapped to Windows zones. When the input
384 * system time zone ID is unknown or unmappable to a Windows time zone, then the result will be
385 * empty, but the operation itself remains successful (no error status set on return).
386 *
387 * <p>This implementation utilizes <a href="http://unicode.org/cldr/charts/supplemental/zone_tzid.html">
388 * Zone-Tzid mapping data</a>. The mapping data is updated time to time. To get the latest changes,
389 * please read the ICU user guide section <a href="http://userguide.icu-project.org/datetime/timezone#TOC-Updating-the-Time-Zone-Data">
390 * Updating the Time Zone Data</a>.
391 *
392 * @param id A system time zone ID.
393 * @param winid Receives a Windows time zone ID. When the input system time zone ID is unknown
394 * or unmappable to a Windows time zone ID, then an empty string is set on return.
395 * @param status Receives the status.
396 * @return A reference to the result (<code>winid</code>).
397 * @see getIDForWindowsID
398 *
399 * @stable ICU 52
400 */
401 static UnicodeString& U_EXPORT2 getWindowsID(const UnicodeString& id,
402 UnicodeString& winid, UErrorCode& status);
403
404 /**
405 * Converts a Windows time zone ID to an equivalent system time zone ID
406 * for a region. For example, system time zone ID "America/Los_Angeles" is returned
407 * for input Windows ID "Pacific Standard Time" and region "US" (or <code>null</code>),
408 * "America/Vancouver" is returned for the same Windows ID "Pacific Standard Time" and
409 * region "CA".
410 *
411 * <p>Not all Windows time zones can be mapped to system time zones. When the input
412 * Windows time zone ID is unknown or unmappable to a system time zone, then the result
413 * will be empty, but the operation itself remains successful (no error status set on return).
414 *
415 * <p>This implementation utilizes <a href="http://unicode.org/cldr/charts/supplemental/zone_tzid.html">
416 * Zone-Tzid mapping data</a>. The mapping data is updated time to time. To get the latest changes,
417 * please read the ICU user guide section <a href="http://userguide.icu-project.org/datetime/timezone#TOC-Updating-the-Time-Zone-Data">
418 * Updating the Time Zone Data</a>.
419 *
420 * @param winid A Windows time zone ID.
421 * @param region A null-terminated region code, or <code>NULL</code> if no regional preference.
422 * @param id Receives a system time zone ID. When the input Windows time zone ID is unknown
423 * or unmappable to a system time zone ID, then an empty string is set on return.
424 * @param status Receives the status.
425 * @return A reference to the result (<code>id</code>).
426 * @see getWindowsID
427 *
428 * @stable ICU 52
429 */
430 static UnicodeString& U_EXPORT2 getIDForWindowsID(const UnicodeString& winid, const char* region,
431 UnicodeString& id, UErrorCode& status);
432
433 /**
434 * Returns true if the two TimeZones are equal. (The TimeZone version only compares
435 * IDs, but subclasses are expected to also compare the fields they add.)
436 *
437 * @param that The TimeZone object to be compared with.
438 * @return True if the given TimeZone is equal to this TimeZone; false
439 * otherwise.
440 * @stable ICU 2.0
441 */
442 virtual UBool operator==(const TimeZone& that) const;
443
444 /**
445 * Returns true if the two TimeZones are NOT equal; that is, if operator==() returns
446 * false.
447 *
448 * @param that The TimeZone object to be compared with.
449 * @return True if the given TimeZone is not equal to this TimeZone; false
450 * otherwise.
451 * @stable ICU 2.0
452 */
453 UBool operator!=(const TimeZone& that) const {return !operator==(that);}
454
455 /**
456 * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add
457 * to GMT to get local time in this time zone, taking daylight savings time into
458 * account) as of a particular reference date. The reference date is used to determine
459 * whether daylight savings time is in effect and needs to be figured into the offset
460 * that is returned (in other words, what is the adjusted GMT offset in this time zone
461 * at this particular date and time?). For the time zones produced by createTimeZone(),
462 * the reference data is specified according to the Gregorian calendar, and the date
463 * and time fields are local standard time.
464 *
465 * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
466 * which returns both the raw and the DST offset for a given time. This method
467 * is retained only for backward compatibility.
468 *
469 * @param era The reference date's era
470 * @param year The reference date's year
471 * @param month The reference date's month (0-based; 0 is January)
472 * @param day The reference date's day-in-month (1-based)
473 * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
474 * @param millis The reference date's milliseconds in day, local standard time
475 * @param status Output param to filled in with a success or an error.
476 * @return The offset in milliseconds to add to GMT to get local time.
477 * @stable ICU 2.0
478 */
479 virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
480 uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const = 0;
481
482 /**
483 * Gets the time zone offset, for current date, modified in case of
484 * daylight savings. This is the offset to add *to* UTC to get local time.
485 *
486 * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
487 * which returns both the raw and the DST offset for a given time. This method
488 * is retained only for backward compatibility.
489 *
490 * @param era the era of the given date.
491 * @param year the year in the given date.
492 * @param month the month in the given date.
493 * Month is 0-based. e.g., 0 for January.
494 * @param day the day-in-month of the given date.
495 * @param dayOfWeek the day-of-week of the given date.
496 * @param milliseconds the millis in day in <em>standard</em> local time.
497 * @param monthLength the length of the given month in days.
498 * @param status Output param to filled in with a success or an error.
499 * @return the offset to add *to* GMT to get local time.
500 * @stable ICU 2.0
501 */
502 virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
503 uint8_t dayOfWeek, int32_t milliseconds,
504 int32_t monthLength, UErrorCode& status) const = 0;
505
506 /**
507 * Returns the time zone raw and GMT offset for the given moment
508 * in time. Upon return, local-millis = GMT-millis + rawOffset +
509 * dstOffset. All computations are performed in the proleptic
510 * Gregorian calendar. The default implementation in the TimeZone
511 * class delegates to the 8-argument getOffset().
512 *
513 * @param date moment in time for which to return offsets, in
514 * units of milliseconds from January 1, 1970 0:00 GMT, either GMT
515 * time or local wall time, depending on `local'.
516 * @param local if true, `date' is local wall time; otherwise it
517 * is in GMT time.
518 * @param rawOffset output parameter to receive the raw offset, that
519 * is, the offset not including DST adjustments
520 * @param dstOffset output parameter to receive the DST offset,
521 * that is, the offset to be added to `rawOffset' to obtain the
522 * total offset between local and GMT time. If DST is not in
523 * effect, this value is zero; otherwise it is a positive value,
524 * typically one hour.
525 * @param ec input-output error code
526 *
527 * @stable ICU 2.8
528 */
529 virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
530 int32_t& dstOffset, UErrorCode& ec) const;
531
532 /**
533 * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
534 * to GMT to get local time, before taking daylight savings time into account).
535 *
536 * @param offsetMillis The new raw GMT offset for this time zone.
537 * @stable ICU 2.0
538 */
539 virtual void setRawOffset(int32_t offsetMillis) = 0;
540
541 /**
542 * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
543 * to GMT to get local time, before taking daylight savings time into account).
544 *
545 * @return The TimeZone's raw GMT offset.
546 * @stable ICU 2.0
547 */
548 virtual int32_t getRawOffset(void) const = 0;
549
550 /**
551 * Fills in "ID" with the TimeZone's ID.
552 *
553 * @param ID Receives this TimeZone's ID.
554 * @return A reference to 'ID'
555 * @stable ICU 2.0
556 */
557 UnicodeString& getID(UnicodeString& ID) const;
558
559 /**
560 * Sets the TimeZone's ID to the specified value. This doesn't affect any other
561 * fields (for example, if you say<
562 * blockquote><pre>
563 * . TimeZone* foo = TimeZone::createTimeZone("America/New_York");
564 * . foo.setID("America/Los_Angeles");
565 * </pre>\htmlonly</blockquote>\endhtmlonly
566 * the time zone's GMT offset and daylight-savings rules don't change to those for
567 * Los Angeles. They're still those for New York. Only the ID has changed.)
568 *
569 * @param ID The new time zone ID.
570 * @stable ICU 2.0
571 */
572 void setID(const UnicodeString& ID);
573
574 /**
575 * Enum for use with getDisplayName
576 * @stable ICU 2.4
577 */
578 enum EDisplayType {
579 /**
580 * Selector for short display name
581 * @stable ICU 2.4
582 */
583 SHORT = 1,
584 /**
585 * Selector for long display name
586 * @stable ICU 2.4
587 */
588 LONG,
589 /**
590 * Selector for short generic display name
591 * @stable ICU 4.4
592 */
593 SHORT_GENERIC,
594 /**
595 * Selector for long generic display name
596 * @stable ICU 4.4
597 */
598 LONG_GENERIC,
599 /**
600 * Selector for short display name derived
601 * from time zone offset
602 * @stable ICU 4.4
603 */
604 SHORT_GMT,
605 /**
606 * Selector for long display name derived
607 * from time zone offset
608 * @stable ICU 4.4
609 */
610 LONG_GMT,
611 /**
612 * Selector for short display name derived
613 * from the time zone's fallback name
614 * @stable ICU 4.4
615 */
616 SHORT_COMMONLY_USED,
617 /**
618 * Selector for long display name derived
619 * from the time zone's fallback name
620 * @stable ICU 4.4
621 */
622 GENERIC_LOCATION
623 };
624
625 /**
626 * Returns a name of this time zone suitable for presentation to the user
627 * in the default locale.
628 * This method returns the long name, not including daylight savings.
629 * If the display name is not available for the locale,
630 * then this method returns a string in the localized GMT offset format
631 * such as <code>GMT[+-]HH:mm</code>.
632 * @param result the human-readable name of this time zone in the default locale.
633 * @return A reference to 'result'.
634 * @stable ICU 2.0
635 */
636 UnicodeString& getDisplayName(UnicodeString& result) const;
637
638 /**
639 * Returns a name of this time zone suitable for presentation to the user
640 * in the specified locale.
641 * This method returns the long name, not including daylight savings.
642 * If the display name is not available for the locale,
643 * then this method returns a string in the localized GMT offset format
644 * such as <code>GMT[+-]HH:mm</code>.
645 * @param locale the locale in which to supply the display name.
646 * @param result the human-readable name of this time zone in the given locale
647 * or in the default locale if the given locale is not recognized.
648 * @return A reference to 'result'.
649 * @stable ICU 2.0
650 */
651 UnicodeString& getDisplayName(const Locale& locale, UnicodeString& result) const;
652
653 /**
654 * Returns a name of this time zone suitable for presentation to the user
655 * in the default locale.
656 * If the display name is not available for the locale,
657 * then this method returns a string in the localized GMT offset format
658 * such as <code>GMT[+-]HH:mm</code>.
659 * @param daylight if true, return the daylight savings name.
660 * @param style
661 * @param result the human-readable name of this time zone in the default locale.
662 * @return A reference to 'result'.
663 * @stable ICU 2.0
664 */
665 UnicodeString& getDisplayName(UBool daylight, EDisplayType style, UnicodeString& result) const;
666
667 /**
668 * Returns a name of this time zone suitable for presentation to the user
669 * in the specified locale.
670 * If the display name is not available for the locale,
671 * then this method returns a string in the localized GMT offset format
672 * such as <code>GMT[+-]HH:mm</code>.
673 * @param daylight if true, return the daylight savings name.
674 * @param style
675 * @param locale the locale in which to supply the display name.
676 * @param result the human-readable name of this time zone in the given locale
677 * or in the default locale if the given locale is not recognized.
678 * @return A refence to 'result'.
679 * @stable ICU 2.0
680 */
681 UnicodeString& getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const;
682
683 /**
684 * Queries if this time zone uses daylight savings time.
685 * @return true if this time zone uses daylight savings time,
686 * false, otherwise.
687 * <p><strong>Note:</strong>The default implementation of
688 * ICU TimeZone uses the tz database, which supports historic
689 * rule changes, for system time zones. With the implementation,
690 * there are time zones that used daylight savings time in the
691 * past, but no longer used currently. For example, Asia/Tokyo has
692 * never used daylight savings time since 1951. Most clients would
693 * expect that this method to return <code>FALSE</code> for such case.
694 * The default implementation of this method returns <code>TRUE</code>
695 * when the time zone uses daylight savings time in the current
696 * (Gregorian) calendar year.
697 * <p>In Java 7, <code>observesDaylightTime()</code> was added in
698 * addition to <code>useDaylightTime()</code>. In Java, <code>useDaylightTime()</code>
699 * only checks if daylight saving time is observed by the last known
700 * rule. This specification might not be what most users would expect
701 * if daylight saving time is currently observed, but not scheduled
702 * in future. In this case, Java's <code>userDaylightTime()</code> returns
703 * <code>false</code>. To resolve the issue, Java 7 added <code>observesDaylightTime()</code>,
704 * which takes the current rule into account. The method <code>observesDaylightTime()</code>
705 * was added in ICU4J for supporting API signature compatibility with JDK.
706 * In general, ICU4C also provides JDK compatible methods, but the current
707 * implementation <code>userDaylightTime()</code> serves the purpose
708 * (takes the current rule into account), <code>observesDaylightTime()</code>
709 * is not added in ICU4C. In addition to <code>useDaylightTime()</code>, ICU4C
710 * <code>BasicTimeZone</code> class (Note that <code>TimeZone::createTimeZone(const UnicodeString &ID)</code>
711 * always returns a <code>BasicTimeZone</code>) provides a series of methods allowing
712 * historic and future time zone rule iteration, so you can check if daylight saving
713 * time is observed or not within a given period.
714 *
715 * @stable ICU 2.0
716 */
717 virtual UBool useDaylightTime(void) const = 0;
718
719 /**
720 * Queries if the given date is in daylight savings time in
721 * this time zone.
722 * This method is wasteful since it creates a new GregorianCalendar and
723 * deletes it each time it is called. This is a deprecated method
724 * and provided only for Java compatibility.
725 *
726 * @param date the given UDate.
727 * @param status Output param filled in with success/error code.
728 * @return true if the given date is in daylight savings time,
729 * false, otherwise.
730 * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead.
731 */
732 virtual UBool inDaylightTime(UDate date, UErrorCode& status) const = 0;
733
734 /**
735 * Returns true if this zone has the same rule and offset as another zone.
736 * That is, if this zone differs only in ID, if at all.
737 * @param other the <code>TimeZone</code> object to be compared with
738 * @return true if the given zone is the same as this one,
739 * with the possible exception of the ID
740 * @stable ICU 2.0
741 */
742 virtual UBool hasSameRules(const TimeZone& other) const;
743
744 /**
745 * Clones TimeZone objects polymorphically. Clients are responsible for deleting
746 * the TimeZone object cloned.
747 *
748 * @return A new copy of this TimeZone object.
749 * @stable ICU 2.0
750 */
751 virtual TimeZone* clone(void) const = 0;
752
753 /**
754 * Return the class ID for this class. This is useful only for
755 * comparing to a return value from getDynamicClassID().
756 * @return The class ID for all objects of this class.
757 * @stable ICU 2.0
758 */
759 static UClassID U_EXPORT2 getStaticClassID(void);
760
761 /**
762 * Returns a unique class ID POLYMORPHICALLY. This method is to
763 * implement a simple version of RTTI, since not all C++ compilers support genuine
764 * RTTI. Polymorphic operator==() and clone() methods call this method.
765 * <P>
766 * Concrete subclasses of TimeZone must use the UOBJECT_DEFINE_RTTI_IMPLEMENTATION
767 * macro from uobject.h in their implementation to provide correct RTTI information.
768 * @return The class ID for this object. All objects of a given class have the
769 * same class ID. Objects of other classes have different class IDs.
770 * @stable ICU 2.0
771 */
772 virtual UClassID getDynamicClassID(void) const = 0;
773
774 /**
775 * Returns the amount of time to be added to local standard time
776 * to get local wall clock time.
777 * <p>
778 * The default implementation always returns 3600000 milliseconds
779 * (i.e., one hour) if this time zone observes Daylight Saving
780 * Time. Otherwise, 0 (zero) is returned.
781 * <p>
782 * If an underlying TimeZone implementation subclass supports
783 * historical Daylight Saving Time changes, this method returns
784 * the known latest daylight saving value.
785 *
786 * @return the amount of saving time in milliseconds
787 * @stable ICU 3.6
788 */
789 virtual int32_t getDSTSavings() const;
790
791 /**
792 * Gets the region code associated with the given
793 * system time zone ID. The region code is either ISO 3166
794 * 2-letter country code or UN M.49 3-digit area code.
795 * When the time zone is not associated with a specific location,
796 * for example - "Etc/UTC", "EST5EDT", then this method returns
797 * "001" (UN M.49 area code for World).
798 *
799 * @param id The system time zone ID.
800 * @param region Output buffer for receiving the region code.
801 * @param capacity The size of the output buffer.
802 * @param status Receives the status. When the given time zone ID
803 * is not a known system time zone ID,
804 * U_ILLEGAL_ARGUMENT_ERROR is set.
805 * @return The length of the output region code.
806 * @stable ICU 4.8
807 */
808 static int32_t U_EXPORT2 getRegion(const UnicodeString& id,
809 char *region, int32_t capacity, UErrorCode& status);
810
811 protected:
812
813 /**
814 * Default constructor. ID is initialized to the empty string.
815 * @stable ICU 2.0
816 */
817 TimeZone();
818
819 /**
820 * Construct a TimeZone with a given ID.
821 * @param id a system time zone ID
822 * @stable ICU 2.0
823 */
824 TimeZone(const UnicodeString &id);
825
826 /**
827 * Copy constructor.
828 * @param source the object to be copied.
829 * @stable ICU 2.0
830 */
831 TimeZone(const TimeZone& source);
832
833 /**
834 * Default assignment operator.
835 * @param right the object to be copied.
836 * @stable ICU 2.0
837 */
838 TimeZone& operator=(const TimeZone& right);
839
840 #ifndef U_HIDE_INTERNAL_API
841 /**
842 * Utility function. For internally loading rule data.
843 * @param top Top resource bundle for tz data
844 * @param ruleid ID of rule to load
845 * @param oldbundle Old bundle to reuse or NULL
846 * @param status Status parameter
847 * @return either a new bundle or *oldbundle
848 * @internal
849 */
850 static UResourceBundle* loadRule(const UResourceBundle* top, const UnicodeString& ruleid, UResourceBundle* oldbundle, UErrorCode&status);
851 #endif /* U_HIDE_INTERNAL_API */
852
853 private:
854 friend class ZoneMeta;
855
856
857 static TimeZone* createCustomTimeZone(const UnicodeString&); // Creates a time zone based on the string.
858
859 /**
860 * Finds the given ID in the Olson tzdata. If the given ID is found in the tzdata,
861 * returns the pointer to the ID resource. This method is exposed through ZoneMeta class
862 * for ICU internal implementation and useful for building hashtable using a time zone
863 * ID as a key.
864 * @param id zone id string
865 * @return the pointer of the ID resource, or NULL.
866 */
867 static const char16_t* findID(const UnicodeString& id);
868
869 /**
870 * Resolve a link in Olson tzdata. When the given id is known and it's not a link,
871 * the id itself is returned. When the given id is known and it is a link, then
872 * dereferenced zone id is returned. When the given id is unknown, then it returns
873 * NULL.
874 * @param id zone id string
875 * @return the dereferenced zone or NULL
876 */
877 static const char16_t* dereferOlsonLink(const UnicodeString& id);
878
879 /**
880 * Returns the region code associated with the given zone,
881 * or NULL if the zone is not known.
882 * @param id zone id string
883 * @return the region associated with the given zone
884 */
885 static const char16_t* getRegion(const UnicodeString& id);
886
887 public:
888 #ifndef U_HIDE_INTERNAL_API
889 /**
890 * Returns the region code associated with the given zone,
891 * or NULL if the zone is not known.
892 * @param id zone id string
893 * @param status Status parameter
894 * @return the region associated with the given zone
895 * @internal
896 */
897 static const char16_t* getRegion(const UnicodeString& id, UErrorCode& status);
898 #endif /* U_HIDE_INTERNAL_API */
899
900 private:
901 /**
902 * Parses the given custom time zone identifier
903 * @param id id A string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or
904 * GMT[+-]hh.
905 * @param sign Receves parsed sign, 1 for positive, -1 for negative.
906 * @param hour Receives parsed hour field
907 * @param minute Receives parsed minute field
908 * @param second Receives parsed second field
909 * @return Returns TRUE when the given custom id is valid.
910 */
911 static UBool parseCustomID(const UnicodeString& id, int32_t& sign, int32_t& hour,
912 int32_t& minute, int32_t& second);
913
914 /**
915 * Parse a custom time zone identifier and return the normalized
916 * custom time zone identifier for the given custom id string.
917 * @param id a string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or
918 * GMT[+-]hh.
919 * @param normalized Receives the normalized custom ID
920 * @param status Receives the status. When the input ID string is invalid,
921 * U_ILLEGAL_ARGUMENT_ERROR is set.
922 * @return The normalized custom id string.
923 */
924 static UnicodeString& getCustomID(const UnicodeString& id, UnicodeString& normalized,
925 UErrorCode& status);
926
927 /**
928 * Returns the normalized custome time zone ID for the given offset fields.
929 * @param hour offset hours
930 * @param min offset minutes
931 * @param sec offset seconds
932 * @param negative sign of the offset, TRUE for negative offset.
933 * @param id Receves the format result (normalized custom ID)
934 * @return The reference to id
935 */
936 static UnicodeString& formatCustomID(int32_t hour, int32_t min, int32_t sec,
937 UBool negative, UnicodeString& id);
938
939 UnicodeString fID; // this time zone's ID
940
941 friend class TZEnumeration;
942 };
943
944
945 // -------------------------------------
946
947 inline UnicodeString&
948 TimeZone::getID(UnicodeString& ID) const
949 {
950 ID = fID;
951 return ID;
952 }
953
954 // -------------------------------------
955
956 inline void
957 TimeZone::setID(const UnicodeString& ID)
958 {
959 fID = ID;
960 }
961 U_NAMESPACE_END
962 #endif // U_SHOW_CPLUSPLUS_API
963
964 #endif /* #if !UCONFIG_NO_FORMATTING */
965
966 #endif //_TIMEZONE
967 //eof