michael@0: /*
michael@0: **********************************************************************
michael@0: * Copyright (c) 2003-2013, International Business Machines
michael@0: * Corporation and others. All Rights Reserved.
michael@0: **********************************************************************
michael@0: * Author: Alan Liu
michael@0: * Created: July 21 2003
michael@0: * Since: ICU 2.8
michael@0: **********************************************************************
michael@0: */
michael@0: #ifndef OLSONTZ_H
michael@0: #define OLSONTZ_H
michael@0:
michael@0: #include "unicode/utypes.h"
michael@0:
michael@0: #if !UCONFIG_NO_FORMATTING
michael@0:
michael@0: #include "unicode/basictz.h"
michael@0: #include "umutex.h"
michael@0:
michael@0: struct UResourceBundle;
michael@0:
michael@0: U_NAMESPACE_BEGIN
michael@0:
michael@0: class SimpleTimeZone;
michael@0:
michael@0: /**
michael@0: * A time zone based on the Olson tz database. Olson time zones change
michael@0: * behavior over time. The raw offset, rules, presence or absence of
michael@0: * daylight savings time, and even the daylight savings amount can all
michael@0: * vary.
michael@0: *
michael@0: * This class uses a resource bundle named "zoneinfo". Zoneinfo is a
michael@0: * table containing different kinds of resources. In several places,
michael@0: * zones are referred to using integers. A zone's integer is a number
michael@0: * from 0..n-1, where n is the number of zones, with the zones sorted
michael@0: * in lexicographic order.
michael@0: *
michael@0: * 1. Zones. These have keys corresponding to the Olson IDs, e.g.,
michael@0: * "Asia/Shanghai". Each resource describes the behavior of the given
michael@0: * zone. Zones come in two different formats.
michael@0: *
michael@0: * a. Zone (table). A zone is a table resource contains several
michael@0: * type of resources below:
michael@0: *
michael@0: * - typeOffsets:intvector (Required)
michael@0: *
michael@0: * Sets of UTC raw/dst offset pairs in seconds. Entries at
michael@0: * 2n represents raw offset and 2n+1 represents dst offset
michael@0: * paired with the raw offset at 2n. The very first pair represents
michael@0: * the initial zone offset (before the first transition) always.
michael@0: *
michael@0: * - trans:intvector (Optional)
michael@0: *
michael@0: * List of transition times represented by 32bit seconds from the
michael@0: * epoch (1970-01-01T00:00Z) in ascending order.
michael@0: *
michael@0: * - transPre32/transPost32:intvector (Optional)
michael@0: *
michael@0: * List of transition times before/after 32bit minimum seconds.
michael@0: * Each time is represented by a pair of 32bit integer.
michael@0: *
michael@0: * - typeMap:bin (Optional)
michael@0: *
michael@0: * Array of bytes representing the mapping between each transition
michael@0: * time (transPre32/trans/transPost32) and its corresponding offset
michael@0: * data (typeOffsets).
michael@0: *
michael@0: * - finalRule:string (Optional)
michael@0: *
michael@0: * If a recurrent transition rule is applicable to a zone forever
michael@0: * after the final transition time, finalRule represents the rule
michael@0: * in Rules data.
michael@0: *
michael@0: * - finalRaw:int (Optional)
michael@0: *
michael@0: * When finalRule is available, finalRaw is required and specifies
michael@0: * the raw (base) offset of the rule.
michael@0: *
michael@0: * - finalYear:int (Optional)
michael@0: *
michael@0: * When finalRule is available, finalYear is required and specifies
michael@0: * the start year of the rule.
michael@0: *
michael@0: * - links:intvector (Optional)
michael@0: *
michael@0: * When this zone data is shared with other zones, links specifies
michael@0: * all zones including the zone itself. Each zone is referenced by
michael@0: * integer index.
michael@0: *
michael@0: * b. Link (int, length 1). A link zone is an int resource. The
michael@0: * integer is the zone number of the target zone. The key of this
michael@0: * resource is an alternate name for the target zone. This data
michael@0: * is corresponding to Link data in the tz database.
michael@0: *
michael@0: *
michael@0: * 2. Rules. These have keys corresponding to the Olson rule IDs,
michael@0: * with an underscore prepended, e.g., "_EU". Each resource describes
michael@0: * the behavior of the given rule using an intvector, containing the
michael@0: * onset list, the cessation list, and the DST savings. The onset and
michael@0: * cessation lists consist of the month, dowim, dow, time, and time
michael@0: * mode. The end result is that the 11 integers describing the rule
michael@0: * can be passed directly into the SimpleTimeZone 13-argument
michael@0: * constructor (the other two arguments will be the raw offset, taken
michael@0: * from the complex zone element 5, and the ID string, which is not
michael@0: * used), with the times and the DST savings multiplied by 1000 to
michael@0: * scale from seconds to milliseconds.
michael@0: *
michael@0: * 3. Regions. An array specifies mapping between zones and regions.
michael@0: * Each item is either a 2-letter ISO country code or "001"
michael@0: * (UN M.49 - World). This data is generated from "zone.tab"
michael@0: * in the tz database.
michael@0: */
michael@0: class U_I18N_API OlsonTimeZone: public BasicTimeZone {
michael@0: public:
michael@0: /**
michael@0: * Construct from a resource bundle.
michael@0: * @param top the top-level zoneinfo resource bundle. This is used
michael@0: * to lookup the rule that `res' may refer to, if there is one.
michael@0: * @param res the resource bundle of the zone to be constructed
michael@0: * @param tzid the time zone ID
michael@0: * @param ec input-output error code
michael@0: */
michael@0: OlsonTimeZone(const UResourceBundle* top,
michael@0: const UResourceBundle* res,
michael@0: const UnicodeString& tzid,
michael@0: UErrorCode& ec);
michael@0:
michael@0: /**
michael@0: * Copy constructor
michael@0: */
michael@0: OlsonTimeZone(const OlsonTimeZone& other);
michael@0:
michael@0: /**
michael@0: * Destructor
michael@0: */
michael@0: virtual ~OlsonTimeZone();
michael@0:
michael@0: /**
michael@0: * Assignment operator
michael@0: */
michael@0: OlsonTimeZone& operator=(const OlsonTimeZone& other);
michael@0:
michael@0: /**
michael@0: * Returns true if the two TimeZone objects are equal.
michael@0: */
michael@0: virtual UBool operator==(const TimeZone& other) const;
michael@0:
michael@0: /**
michael@0: * TimeZone API.
michael@0: */
michael@0: virtual TimeZone* clone() const;
michael@0:
michael@0: /**
michael@0: * TimeZone API.
michael@0: */
michael@0: static UClassID U_EXPORT2 getStaticClassID();
michael@0:
michael@0: /**
michael@0: * TimeZone API.
michael@0: */
michael@0: virtual UClassID getDynamicClassID() const;
michael@0:
michael@0: /**
michael@0: * TimeZone API. Do not call this; prefer getOffset(UDate,...).
michael@0: */
michael@0: virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month,
michael@0: int32_t day, uint8_t dayOfWeek,
michael@0: int32_t millis, UErrorCode& ec) const;
michael@0:
michael@0: /**
michael@0: * TimeZone API. Do not call this; prefer getOffset(UDate,...).
michael@0: */
michael@0: virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month,
michael@0: int32_t day, uint8_t dayOfWeek,
michael@0: int32_t millis, int32_t monthLength,
michael@0: UErrorCode& ec) const;
michael@0:
michael@0: /**
michael@0: * TimeZone API.
michael@0: */
michael@0: virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
michael@0: int32_t& dstOffset, UErrorCode& ec) const;
michael@0:
michael@0: /**
michael@0: * BasicTimeZone API.
michael@0: */
michael@0: virtual void getOffsetFromLocal(UDate date, int32_t nonExistingTimeOpt, int32_t duplicatedTimeOpt,
michael@0: int32_t& rawoff, int32_t& dstoff, UErrorCode& ec) const;
michael@0:
michael@0: /**
michael@0: * TimeZone API. This method has no effect since objects of this
michael@0: * class are quasi-immutable (the base class allows the ID to be
michael@0: * changed).
michael@0: */
michael@0: virtual void setRawOffset(int32_t offsetMillis);
michael@0:
michael@0: /**
michael@0: * TimeZone API. For a historical zone, the raw offset can change
michael@0: * over time, so this API is not useful. In order to approximate
michael@0: * expected behavior, this method returns the raw offset for the
michael@0: * current moment in time.
michael@0: */
michael@0: virtual int32_t getRawOffset() const;
michael@0:
michael@0: /**
michael@0: * TimeZone API. For a historical zone, whether DST is used or
michael@0: * not varies over time. In order to approximate expected
michael@0: * behavior, this method returns TRUE if DST is observed at any
michael@0: * point in the current year.
michael@0: */
michael@0: virtual UBool useDaylightTime() const;
michael@0:
michael@0: /**
michael@0: * TimeZone API.
michael@0: */
michael@0: virtual UBool inDaylightTime(UDate date, UErrorCode& ec) const;
michael@0:
michael@0: /**
michael@0: * TimeZone API.
michael@0: */
michael@0: virtual int32_t getDSTSavings() const;
michael@0:
michael@0: /**
michael@0: * TimeZone API. Also comare historic transitions.
michael@0: */
michael@0: virtual UBool hasSameRules(const TimeZone& other) const;
michael@0:
michael@0: /**
michael@0: * BasicTimeZone API.
michael@0: * Gets the first time zone transition after the base time.
michael@0: * @param base The base time.
michael@0: * @param inclusive Whether the base time is inclusive or not.
michael@0: * @param result Receives the first transition after the base time.
michael@0: * @return TRUE if the transition is found.
michael@0: */
michael@0: virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const;
michael@0:
michael@0: /**
michael@0: * BasicTimeZone API.
michael@0: * Gets the most recent time zone transition before the base time.
michael@0: * @param base The base time.
michael@0: * @param inclusive Whether the base time is inclusive or not.
michael@0: * @param result Receives the most recent transition before the base time.
michael@0: * @return TRUE if the transition is found.
michael@0: */
michael@0: virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const;
michael@0:
michael@0: /**
michael@0: * BasicTimeZone API.
michael@0: * Returns the number of TimeZoneRule
s which represents time transitions,
michael@0: * for this time zone, that is, all TimeZoneRule
s for this time zone except
michael@0: * InitialTimeZoneRule
. The return value range is 0 or any positive value.
michael@0: * @param status Receives error status code.
michael@0: * @return The number of TimeZoneRule
s representing time transitions.
michael@0: */
michael@0: virtual int32_t countTransitionRules(UErrorCode& status) const;
michael@0:
michael@0: /**
michael@0: * Gets the InitialTimeZoneRule
and the set of TimeZoneRule
michael@0: * which represent time transitions for this time zone. On successful return,
michael@0: * the argument initial points to non-NULL InitialTimeZoneRule
and
michael@0: * the array trsrules is filled with 0 or multiple TimeZoneRule
michael@0: * instances up to the size specified by trscount. The results are referencing the
michael@0: * rule instance held by this time zone instance. Therefore, after this time zone
michael@0: * is destructed, they are no longer available.
michael@0: * @param initial Receives the initial timezone rule
michael@0: * @param trsrules Receives the timezone transition rules
michael@0: * @param trscount On input, specify the size of the array 'transitions' receiving
michael@0: * the timezone transition rules. On output, actual number of
michael@0: * rules filled in the array will be set.
michael@0: * @param status Receives error status code.
michael@0: */
michael@0: virtual void getTimeZoneRules(const InitialTimeZoneRule*& initial,
michael@0: const TimeZoneRule* trsrules[], int32_t& trscount, UErrorCode& status) const;
michael@0:
michael@0: /**
michael@0: * Internal API returning the canonical ID of this zone.
michael@0: * This ID won't be affected by setID().
michael@0: */
michael@0: const UChar *getCanonicalID() const;
michael@0:
michael@0: private:
michael@0: /**
michael@0: * Default constructor. Creates a time zone with an empty ID and
michael@0: * a fixed GMT offset of zero.
michael@0: */
michael@0: OlsonTimeZone();
michael@0:
michael@0: private:
michael@0:
michael@0: void constructEmpty();
michael@0:
michael@0: void getHistoricalOffset(UDate date, UBool local,
michael@0: int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt,
michael@0: int32_t& rawoff, int32_t& dstoff) const;
michael@0:
michael@0: int16_t transitionCount() const;
michael@0:
michael@0: int64_t transitionTimeInSeconds(int16_t transIdx) const;
michael@0: double transitionTime(int16_t transIdx) const;
michael@0:
michael@0: /*
michael@0: * Following 3 methods return an offset at the given transition time index.
michael@0: * When the index is negative, return the initial offset.
michael@0: */
michael@0: int32_t zoneOffsetAt(int16_t transIdx) const;
michael@0: int32_t rawOffsetAt(int16_t transIdx) const;
michael@0: int32_t dstOffsetAt(int16_t transIdx) const;
michael@0:
michael@0: /*
michael@0: * Following methods return the initial offset.
michael@0: */
michael@0: int32_t initialRawOffset() const;
michael@0: int32_t initialDstOffset() const;
michael@0:
michael@0: /**
michael@0: * Number of transitions in each time range
michael@0: */
michael@0: int16_t transitionCountPre32;
michael@0: int16_t transitionCount32;
michael@0: int16_t transitionCountPost32;
michael@0:
michael@0: /**
michael@0: * Time of each transition in seconds from 1970 epoch before 32bit second range (<= 1900).
michael@0: * Each transition in this range is represented by a pair of int32_t.
michael@0: * Length is transitionCount int32_t's. NULL if no transitions in this range.
michael@0: */
michael@0: const int32_t *transitionTimesPre32; // alias into res; do not delete
michael@0:
michael@0: /**
michael@0: * Time of each transition in seconds from 1970 epoch in 32bit second range.
michael@0: * Length is transitionCount int32_t's. NULL if no transitions in this range.
michael@0: */
michael@0: const int32_t *transitionTimes32; // alias into res; do not delete
michael@0:
michael@0: /**
michael@0: * Time of each transition in seconds from 1970 epoch after 32bit second range (>= 2038).
michael@0: * Each transition in this range is represented by a pair of int32_t.
michael@0: * Length is transitionCount int32_t's. NULL if no transitions in this range.
michael@0: */
michael@0: const int32_t *transitionTimesPost32; // alias into res; do not delete
michael@0:
michael@0: /**
michael@0: * Number of types, 1..255
michael@0: */
michael@0: int16_t typeCount;
michael@0:
michael@0: /**
michael@0: * Offset from GMT in seconds for each type.
michael@0: * Length is typeCount int32_t's. At least one type (a pair of int32_t)
michael@0: * is required.
michael@0: */
michael@0: const int32_t *typeOffsets; // alias into res; do not delete
michael@0:
michael@0: /**
michael@0: * Type description data, consisting of transitionCount uint8_t
michael@0: * type indices (from 0..typeCount-1).
michael@0: * Length is transitionCount int16_t's. NULL if no transitions.
michael@0: */
michael@0: const uint8_t *typeMapData; // alias into res; do not delete
michael@0:
michael@0: /**
michael@0: * A SimpleTimeZone that governs the behavior for date >= finalMillis.
michael@0: */
michael@0: SimpleTimeZone *finalZone; // owned, may be NULL
michael@0:
michael@0: /**
michael@0: * For date >= finalMillis, the finalZone will be used.
michael@0: */
michael@0: double finalStartMillis;
michael@0:
michael@0: /**
michael@0: * For year >= finalYear, the finalZone will be used.
michael@0: */
michael@0: int32_t finalStartYear;
michael@0:
michael@0: /*
michael@0: * Canonical (CLDR) ID of this zone
michael@0: */
michael@0: const UChar *canonicalID;
michael@0:
michael@0: /* BasicTimeZone support */
michael@0: void clearTransitionRules(void);
michael@0: void deleteTransitionRules(void);
michael@0: void checkTransitionRules(UErrorCode& status) const;
michael@0:
michael@0: public: // Internal, for access from plain C code
michael@0: void initTransitionRules(UErrorCode& status);
michael@0: private:
michael@0:
michael@0: InitialTimeZoneRule *initialRule;
michael@0: TimeZoneTransition *firstTZTransition;
michael@0: int16_t firstTZTransitionIdx;
michael@0: TimeZoneTransition *firstFinalTZTransition;
michael@0: TimeArrayTimeZoneRule **historicRules;
michael@0: int16_t historicRuleCount;
michael@0: SimpleTimeZone *finalZoneWithStartYear; // hack
michael@0: UInitOnce transitionRulesInitOnce;
michael@0: };
michael@0:
michael@0: inline int16_t
michael@0: OlsonTimeZone::transitionCount() const {
michael@0: return transitionCountPre32 + transitionCount32 + transitionCountPost32;
michael@0: }
michael@0:
michael@0: inline double
michael@0: OlsonTimeZone::transitionTime(int16_t transIdx) const {
michael@0: return (double)transitionTimeInSeconds(transIdx) * U_MILLIS_PER_SECOND;
michael@0: }
michael@0:
michael@0: inline int32_t
michael@0: OlsonTimeZone::zoneOffsetAt(int16_t transIdx) const {
michael@0: int16_t typeIdx = (transIdx >= 0 ? typeMapData[transIdx] : 0) << 1;
michael@0: return typeOffsets[typeIdx] + typeOffsets[typeIdx + 1];
michael@0: }
michael@0:
michael@0: inline int32_t
michael@0: OlsonTimeZone::rawOffsetAt(int16_t transIdx) const {
michael@0: int16_t typeIdx = (transIdx >= 0 ? typeMapData[transIdx] : 0) << 1;
michael@0: return typeOffsets[typeIdx];
michael@0: }
michael@0:
michael@0: inline int32_t
michael@0: OlsonTimeZone::dstOffsetAt(int16_t transIdx) const {
michael@0: int16_t typeIdx = (transIdx >= 0 ? typeMapData[transIdx] : 0) << 1;
michael@0: return typeOffsets[typeIdx + 1];
michael@0: }
michael@0:
michael@0: inline int32_t
michael@0: OlsonTimeZone::initialRawOffset() const {
michael@0: return typeOffsets[0];
michael@0: }
michael@0:
michael@0: inline int32_t
michael@0: OlsonTimeZone::initialDstOffset() const {
michael@0: return typeOffsets[1];
michael@0: }
michael@0:
michael@0: inline const UChar*
michael@0: OlsonTimeZone::getCanonicalID() const {
michael@0: return canonicalID;
michael@0: }
michael@0:
michael@0:
michael@0: U_NAMESPACE_END
michael@0:
michael@0: #endif // !UCONFIG_NO_FORMATTING
michael@0: #endif // OLSONTZ_H
michael@0:
michael@0: //eof