timezone.h 41 KB

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