olsontz.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (c) 2003-2013, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * Author: Alan Liu
  9. * Created: July 21 2003
  10. * Since: ICU 2.8
  11. **********************************************************************
  12. */
  13. #ifndef OLSONTZ_H
  14. #define OLSONTZ_H
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_FORMATTING
  17. #include "unicode/basictz.h"
  18. #include "umutex.h"
  19. struct UResourceBundle;
  20. U_NAMESPACE_BEGIN
  21. class SimpleTimeZone;
  22. /**
  23. * A time zone based on the Olson tz database. Olson time zones change
  24. * behavior over time. The raw offset, rules, presence or absence of
  25. * daylight savings time, and even the daylight savings amount can all
  26. * vary.
  27. *
  28. * This class uses a resource bundle named "zoneinfo". Zoneinfo is a
  29. * table containing different kinds of resources. In several places,
  30. * zones are referred to using integers. A zone's integer is a number
  31. * from 0..n-1, where n is the number of zones, with the zones sorted
  32. * in lexicographic order.
  33. *
  34. * 1. Zones. These have keys corresponding to the Olson IDs, e.g.,
  35. * "Asia/Shanghai". Each resource describes the behavior of the given
  36. * zone. Zones come in two different formats.
  37. *
  38. * a. Zone (table). A zone is a table resource contains several
  39. * type of resources below:
  40. *
  41. * - typeOffsets:intvector (Required)
  42. *
  43. * Sets of UTC raw/dst offset pairs in seconds. Entries at
  44. * 2n represents raw offset and 2n+1 represents dst offset
  45. * paired with the raw offset at 2n. The very first pair represents
  46. * the initial zone offset (before the first transition) always.
  47. *
  48. * - trans:intvector (Optional)
  49. *
  50. * List of transition times represented by 32bit seconds from the
  51. * epoch (1970-01-01T00:00Z) in ascending order.
  52. *
  53. * - transPre32/transPost32:intvector (Optional)
  54. *
  55. * List of transition times before/after 32bit minimum seconds.
  56. * Each time is represented by a pair of 32bit integer.
  57. *
  58. * - typeMap:bin (Optional)
  59. *
  60. * Array of bytes representing the mapping between each transition
  61. * time (transPre32/trans/transPost32) and its corresponding offset
  62. * data (typeOffsets).
  63. *
  64. * - finalRule:string (Optional)
  65. *
  66. * If a recurrent transition rule is applicable to a zone forever
  67. * after the final transition time, finalRule represents the rule
  68. * in Rules data.
  69. *
  70. * - finalRaw:int (Optional)
  71. *
  72. * When finalRule is available, finalRaw is required and specifies
  73. * the raw (base) offset of the rule.
  74. *
  75. * - finalYear:int (Optional)
  76. *
  77. * When finalRule is available, finalYear is required and specifies
  78. * the start year of the rule.
  79. *
  80. * - links:intvector (Optional)
  81. *
  82. * When this zone data is shared with other zones, links specifies
  83. * all zones including the zone itself. Each zone is referenced by
  84. * integer index.
  85. *
  86. * b. Link (int, length 1). A link zone is an int resource. The
  87. * integer is the zone number of the target zone. The key of this
  88. * resource is an alternate name for the target zone. This data
  89. * is corresponding to Link data in the tz database.
  90. *
  91. *
  92. * 2. Rules. These have keys corresponding to the Olson rule IDs,
  93. * with an underscore prepended, e.g., "_EU". Each resource describes
  94. * the behavior of the given rule using an intvector, containing the
  95. * onset list, the cessation list, and the DST savings. The onset and
  96. * cessation lists consist of the month, dowim, dow, time, and time
  97. * mode. The end result is that the 11 integers describing the rule
  98. * can be passed directly into the SimpleTimeZone 13-argument
  99. * constructor (the other two arguments will be the raw offset, taken
  100. * from the complex zone element 5, and the ID string, which is not
  101. * used), with the times and the DST savings multiplied by 1000 to
  102. * scale from seconds to milliseconds.
  103. *
  104. * 3. Regions. An array specifies mapping between zones and regions.
  105. * Each item is either a 2-letter ISO country code or "001"
  106. * (UN M.49 - World). This data is generated from "zone.tab"
  107. * in the tz database.
  108. */
  109. class U_I18N_API OlsonTimeZone: public BasicTimeZone {
  110. public:
  111. /**
  112. * Construct from a resource bundle.
  113. * @param top the top-level zoneinfo resource bundle. This is used
  114. * to lookup the rule that `res' may refer to, if there is one.
  115. * @param res the resource bundle of the zone to be constructed
  116. * @param tzid the time zone ID
  117. * @param ec input-output error code
  118. */
  119. OlsonTimeZone(const UResourceBundle* top,
  120. const UResourceBundle* res,
  121. const UnicodeString& tzid,
  122. UErrorCode& ec);
  123. /**
  124. * Copy constructor
  125. */
  126. OlsonTimeZone(const OlsonTimeZone& other);
  127. /**
  128. * Destructor
  129. */
  130. virtual ~OlsonTimeZone();
  131. /**
  132. * Assignment operator
  133. */
  134. OlsonTimeZone& operator=(const OlsonTimeZone& other);
  135. /**
  136. * Returns true if the two TimeZone objects are equal.
  137. */
  138. virtual UBool operator==(const TimeZone& other) const;
  139. /**
  140. * TimeZone API.
  141. */
  142. virtual OlsonTimeZone* clone() const;
  143. /**
  144. * TimeZone API.
  145. */
  146. static UClassID U_EXPORT2 getStaticClassID();
  147. /**
  148. * TimeZone API.
  149. */
  150. virtual UClassID getDynamicClassID() const;
  151. /**
  152. * TimeZone API. Do not call this; prefer getOffset(UDate,...).
  153. */
  154. virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month,
  155. int32_t day, uint8_t dayOfWeek,
  156. int32_t millis, UErrorCode& ec) const;
  157. /**
  158. * TimeZone API. Do not call this; prefer getOffset(UDate,...).
  159. */
  160. virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month,
  161. int32_t day, uint8_t dayOfWeek,
  162. int32_t millis, int32_t monthLength,
  163. UErrorCode& ec) const;
  164. /**
  165. * TimeZone API.
  166. */
  167. virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
  168. int32_t& dstOffset, UErrorCode& ec) const;
  169. /**
  170. * BasicTimeZone API.
  171. */
  172. virtual void getOffsetFromLocal(UDate date, int32_t nonExistingTimeOpt, int32_t duplicatedTimeOpt,
  173. int32_t& rawoff, int32_t& dstoff, UErrorCode& ec) const;
  174. /**
  175. * TimeZone API. This method has no effect since objects of this
  176. * class are quasi-immutable (the base class allows the ID to be
  177. * changed).
  178. */
  179. virtual void setRawOffset(int32_t offsetMillis);
  180. /**
  181. * TimeZone API. For a historical zone, the raw offset can change
  182. * over time, so this API is not useful. In order to approximate
  183. * expected behavior, this method returns the raw offset for the
  184. * current moment in time.
  185. */
  186. virtual int32_t getRawOffset() const;
  187. /**
  188. * TimeZone API. For a historical zone, whether DST is used or
  189. * not varies over time. In order to approximate expected
  190. * behavior, this method returns TRUE if DST is observed at any
  191. * point in the current year.
  192. */
  193. virtual UBool useDaylightTime() const;
  194. /**
  195. * TimeZone API.
  196. */
  197. virtual UBool inDaylightTime(UDate date, UErrorCode& ec) const;
  198. /**
  199. * TimeZone API.
  200. */
  201. virtual int32_t getDSTSavings() const;
  202. /**
  203. * TimeZone API. Also comare historic transitions.
  204. */
  205. virtual UBool hasSameRules(const TimeZone& other) const;
  206. /**
  207. * BasicTimeZone API.
  208. * Gets the first time zone transition after the base time.
  209. * @param base The base time.
  210. * @param inclusive Whether the base time is inclusive or not.
  211. * @param result Receives the first transition after the base time.
  212. * @return TRUE if the transition is found.
  213. */
  214. virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const;
  215. /**
  216. * BasicTimeZone API.
  217. * Gets the most recent time zone transition before the base time.
  218. * @param base The base time.
  219. * @param inclusive Whether the base time is inclusive or not.
  220. * @param result Receives the most recent transition before the base time.
  221. * @return TRUE if the transition is found.
  222. */
  223. virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const;
  224. /**
  225. * BasicTimeZone API.
  226. * Returns the number of <code>TimeZoneRule</code>s which represents time transitions,
  227. * for this time zone, that is, all <code>TimeZoneRule</code>s for this time zone except
  228. * <code>InitialTimeZoneRule</code>. The return value range is 0 or any positive value.
  229. * @param status Receives error status code.
  230. * @return The number of <code>TimeZoneRule</code>s representing time transitions.
  231. */
  232. virtual int32_t countTransitionRules(UErrorCode& status) const;
  233. /**
  234. * Gets the <code>InitialTimeZoneRule</code> and the set of <code>TimeZoneRule</code>
  235. * which represent time transitions for this time zone. On successful return,
  236. * the argument initial points to non-NULL <code>InitialTimeZoneRule</code> and
  237. * the array trsrules is filled with 0 or multiple <code>TimeZoneRule</code>
  238. * instances up to the size specified by trscount. The results are referencing the
  239. * rule instance held by this time zone instance. Therefore, after this time zone
  240. * is destructed, they are no longer available.
  241. * @param initial Receives the initial timezone rule
  242. * @param trsrules Receives the timezone transition rules
  243. * @param trscount On input, specify the size of the array 'transitions' receiving
  244. * the timezone transition rules. On output, actual number of
  245. * rules filled in the array will be set.
  246. * @param status Receives error status code.
  247. */
  248. virtual void getTimeZoneRules(const InitialTimeZoneRule*& initial,
  249. const TimeZoneRule* trsrules[], int32_t& trscount, UErrorCode& status) const;
  250. /**
  251. * Internal API returning the canonical ID of this zone.
  252. * This ID won't be affected by setID().
  253. */
  254. const UChar *getCanonicalID() const;
  255. private:
  256. /**
  257. * Default constructor. Creates a time zone with an empty ID and
  258. * a fixed GMT offset of zero.
  259. */
  260. OlsonTimeZone();
  261. private:
  262. void constructEmpty();
  263. void getHistoricalOffset(UDate date, UBool local,
  264. int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt,
  265. int32_t& rawoff, int32_t& dstoff) const;
  266. int16_t transitionCount() const;
  267. int64_t transitionTimeInSeconds(int16_t transIdx) const;
  268. double transitionTime(int16_t transIdx) const;
  269. /*
  270. * Following 3 methods return an offset at the given transition time index.
  271. * When the index is negative, return the initial offset.
  272. */
  273. int32_t zoneOffsetAt(int16_t transIdx) const;
  274. int32_t rawOffsetAt(int16_t transIdx) const;
  275. int32_t dstOffsetAt(int16_t transIdx) const;
  276. /*
  277. * Following methods return the initial offset.
  278. */
  279. int32_t initialRawOffset() const;
  280. int32_t initialDstOffset() const;
  281. /**
  282. * Number of transitions in each time range
  283. */
  284. int16_t transitionCountPre32;
  285. int16_t transitionCount32;
  286. int16_t transitionCountPost32;
  287. /**
  288. * Time of each transition in seconds from 1970 epoch before 32bit second range (<= 1900).
  289. * Each transition in this range is represented by a pair of int32_t.
  290. * Length is transitionCount int32_t's. NULL if no transitions in this range.
  291. */
  292. const int32_t *transitionTimesPre32; // alias into res; do not delete
  293. /**
  294. * Time of each transition in seconds from 1970 epoch in 32bit second range.
  295. * Length is transitionCount int32_t's. NULL if no transitions in this range.
  296. */
  297. const int32_t *transitionTimes32; // alias into res; do not delete
  298. /**
  299. * Time of each transition in seconds from 1970 epoch after 32bit second range (>= 2038).
  300. * Each transition in this range is represented by a pair of int32_t.
  301. * Length is transitionCount int32_t's. NULL if no transitions in this range.
  302. */
  303. const int32_t *transitionTimesPost32; // alias into res; do not delete
  304. /**
  305. * Number of types, 1..255
  306. */
  307. int16_t typeCount;
  308. /**
  309. * Offset from GMT in seconds for each type.
  310. * Length is typeCount int32_t's. At least one type (a pair of int32_t)
  311. * is required.
  312. */
  313. const int32_t *typeOffsets; // alias into res; do not delete
  314. /**
  315. * Type description data, consisting of transitionCount uint8_t
  316. * type indices (from 0..typeCount-1).
  317. * Length is transitionCount int16_t's. NULL if no transitions.
  318. */
  319. const uint8_t *typeMapData; // alias into res; do not delete
  320. /**
  321. * A SimpleTimeZone that governs the behavior for date >= finalMillis.
  322. */
  323. SimpleTimeZone *finalZone; // owned, may be NULL
  324. /**
  325. * For date >= finalMillis, the finalZone will be used.
  326. */
  327. double finalStartMillis;
  328. /**
  329. * For year >= finalYear, the finalZone will be used.
  330. */
  331. int32_t finalStartYear;
  332. /*
  333. * Canonical (CLDR) ID of this zone
  334. */
  335. const UChar *canonicalID;
  336. /* BasicTimeZone support */
  337. void clearTransitionRules(void);
  338. void deleteTransitionRules(void);
  339. void checkTransitionRules(UErrorCode& status) const;
  340. public: // Internal, for access from plain C code
  341. void initTransitionRules(UErrorCode& status);
  342. private:
  343. InitialTimeZoneRule *initialRule;
  344. TimeZoneTransition *firstTZTransition;
  345. int16_t firstTZTransitionIdx;
  346. TimeZoneTransition *firstFinalTZTransition;
  347. TimeArrayTimeZoneRule **historicRules;
  348. int16_t historicRuleCount;
  349. SimpleTimeZone *finalZoneWithStartYear; // hack
  350. UInitOnce transitionRulesInitOnce = U_INITONCE_INITIALIZER;
  351. };
  352. inline int16_t
  353. OlsonTimeZone::transitionCount() const {
  354. return transitionCountPre32 + transitionCount32 + transitionCountPost32;
  355. }
  356. inline double
  357. OlsonTimeZone::transitionTime(int16_t transIdx) const {
  358. return (double)transitionTimeInSeconds(transIdx) * U_MILLIS_PER_SECOND;
  359. }
  360. inline int32_t
  361. OlsonTimeZone::zoneOffsetAt(int16_t transIdx) const {
  362. int16_t typeIdx = (transIdx >= 0 ? typeMapData[transIdx] : 0) << 1;
  363. return typeOffsets[typeIdx] + typeOffsets[typeIdx + 1];
  364. }
  365. inline int32_t
  366. OlsonTimeZone::rawOffsetAt(int16_t transIdx) const {
  367. int16_t typeIdx = (transIdx >= 0 ? typeMapData[transIdx] : 0) << 1;
  368. return typeOffsets[typeIdx];
  369. }
  370. inline int32_t
  371. OlsonTimeZone::dstOffsetAt(int16_t transIdx) const {
  372. int16_t typeIdx = (transIdx >= 0 ? typeMapData[transIdx] : 0) << 1;
  373. return typeOffsets[typeIdx + 1];
  374. }
  375. inline int32_t
  376. OlsonTimeZone::initialRawOffset() const {
  377. return typeOffsets[0];
  378. }
  379. inline int32_t
  380. OlsonTimeZone::initialDstOffset() const {
  381. return typeOffsets[1];
  382. }
  383. inline const UChar*
  384. OlsonTimeZone::getCanonicalID() const {
  385. return canonicalID;
  386. }
  387. U_NAMESPACE_END
  388. #endif // !UCONFIG_NO_FORMATTING
  389. #endif // OLSONTZ_H
  390. //eof