intl/icu/source/i18n/olsontz.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial