michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2007-2013, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: */ michael@0: #ifndef RBTZ_H michael@0: #define RBTZ_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Rule based customizable time zone michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/basictz.h" michael@0: #include "unicode/unistr.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: // forward declaration michael@0: class UVector; michael@0: struct Transition; michael@0: michael@0: /** michael@0: * a BasicTimeZone subclass implemented in terms of InitialTimeZoneRule and TimeZoneRule instances michael@0: * @see BasicTimeZone michael@0: * @see InitialTimeZoneRule michael@0: * @see TimeZoneRule michael@0: */ michael@0: class U_I18N_API RuleBasedTimeZone : public BasicTimeZone { michael@0: public: michael@0: /** michael@0: * Constructs a RuleBasedTimeZone object with the ID and the michael@0: * InitialTimeZoneRule. The input InitialTimeZoneRule michael@0: * is adopted by this RuleBasedTimeZone, thus the caller must not michael@0: * delete it. michael@0: * @param id The time zone ID. michael@0: * @param initialRule The initial time zone rule. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: RuleBasedTimeZone(const UnicodeString& id, InitialTimeZoneRule* initialRule); michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: * @param source The RuleBasedTimeZone object to be copied. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: RuleBasedTimeZone(const RuleBasedTimeZone& source); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual ~RuleBasedTimeZone(); michael@0: michael@0: /** michael@0: * Assignment operator. michael@0: * @param right The object to be copied. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: RuleBasedTimeZone& operator=(const RuleBasedTimeZone& right); michael@0: michael@0: /** michael@0: * Return true if the given TimeZone objects are michael@0: * semantically equal. Objects of different subclasses are considered unequal. michael@0: * @param that The object to be compared with. michael@0: * @return true if the given TimeZone objects are michael@0: *semantically equal. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual UBool operator==(const TimeZone& that) const; michael@0: michael@0: /** michael@0: * Return true if the given TimeZone objects are michael@0: * semantically unequal. Objects of different subclasses are considered unequal. michael@0: * @param that The object to be compared with. michael@0: * @return true if the given TimeZone objects are michael@0: * semantically unequal. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual UBool operator!=(const TimeZone& that) const; michael@0: michael@0: /** michael@0: * Adds the TimeZoneRule which represents time transitions. michael@0: * The TimeZoneRule must have start times, that is, the result michael@0: * of isTransitionRule() must be true. Otherwise, U_ILLEGAL_ARGUMENT_ERROR michael@0: * is set to the error code. michael@0: * The input TimeZoneRule is adopted by this michael@0: * RuleBasedTimeZone on successful completion of this method, michael@0: * thus, the caller must not delete it when no error is returned. michael@0: * After all rules are added, the caller must call complete() method to michael@0: * make this RuleBasedTimeZone ready to handle common time michael@0: * zone functions. michael@0: * @param rule The TimeZoneRule. michael@0: * @param status Output param to filled in with a success or an error. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: void addTransitionRule(TimeZoneRule* rule, UErrorCode& status); michael@0: michael@0: /** michael@0: * Makes the TimeZoneRule ready to handle actual timezone michael@0: * calcuation APIs. This method collects time zone rules specified michael@0: * by the caller via the constructor and addTransitionRule() and michael@0: * builds internal structure for making the object ready to support michael@0: * time zone APIs such as getOffset(), getNextTransition() and others. michael@0: * @param status Output param to filled in with a success or an error. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: void complete(UErrorCode& status); michael@0: michael@0: /** michael@0: * Clones TimeZone objects polymorphically. Clients are responsible for deleting michael@0: * the TimeZone object cloned. michael@0: * michael@0: * @return A new copy of this TimeZone object. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual TimeZone* clone(void) const; michael@0: michael@0: /** michael@0: * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add michael@0: * to GMT to get local time in this time zone, taking daylight savings time into michael@0: * account) as of a particular reference date. The reference date is used to determine michael@0: * whether daylight savings time is in effect and needs to be figured into the offset michael@0: * that is returned (in other words, what is the adjusted GMT offset in this time zone michael@0: * at this particular date and time?). For the time zones produced by createTimeZone(), michael@0: * the reference data is specified according to the Gregorian calendar, and the date michael@0: * and time fields are local standard time. michael@0: * michael@0: *

Note: Don't call this method. Instead, call the getOffset(UDate...) overload, michael@0: * which returns both the raw and the DST offset for a given time. This method michael@0: * is retained only for backward compatibility. michael@0: * michael@0: * @param era The reference date's era michael@0: * @param year The reference date's year michael@0: * @param month The reference date's month (0-based; 0 is January) michael@0: * @param day The reference date's day-in-month (1-based) michael@0: * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday) michael@0: * @param millis The reference date's milliseconds in day, local standard time michael@0: * @param status Output param to filled in with a success or an error. michael@0: * @return The offset in milliseconds to add to GMT to get local time. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day, michael@0: uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Gets the time zone offset, for current date, modified in case of michael@0: * daylight savings. This is the offset to add *to* UTC to get local time. michael@0: * michael@0: *

Note: Don't call this method. Instead, call the getOffset(UDate...) overload, michael@0: * which returns both the raw and the DST offset for a given time. This method michael@0: * is retained only for backward compatibility. michael@0: * michael@0: * @param era The reference date's era michael@0: * @param year The reference date's year michael@0: * @param month The reference date's month (0-based; 0 is January) michael@0: * @param day The reference date's day-in-month (1-based) michael@0: * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday) michael@0: * @param millis The reference date's milliseconds in day, local standard time michael@0: * @param monthLength The length of the given month in days. michael@0: * @param status Output param to filled in with a success or an error. michael@0: * @return The offset in milliseconds to add to GMT to get local time. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day, michael@0: uint8_t dayOfWeek, int32_t millis, michael@0: int32_t monthLength, UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Returns the time zone raw and GMT offset for the given moment michael@0: * in time. Upon return, local-millis = GMT-millis + rawOffset + michael@0: * dstOffset. All computations are performed in the proleptic michael@0: * Gregorian calendar. The default implementation in the TimeZone michael@0: * class delegates to the 8-argument getOffset(). michael@0: * michael@0: * @param date moment in time for which to return offsets, in michael@0: * units of milliseconds from January 1, 1970 0:00 GMT, either GMT michael@0: * time or local wall time, depending on `local'. michael@0: * @param local if true, `date' is local wall time; otherwise it michael@0: * is in GMT time. michael@0: * @param rawOffset output parameter to receive the raw offset, that michael@0: * is, the offset not including DST adjustments michael@0: * @param dstOffset output parameter to receive the DST offset, michael@0: * that is, the offset to be added to `rawOffset' to obtain the michael@0: * total offset between local and GMT time. If DST is not in michael@0: * effect, this value is zero; otherwise it is a positive value, michael@0: * typically one hour. michael@0: * @param ec input-output error code michael@0: * @stable ICU 3.8 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: * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add michael@0: * to GMT to get local time, before taking daylight savings time into account). michael@0: * michael@0: * @param offsetMillis The new raw GMT offset for this time zone. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual void setRawOffset(int32_t offsetMillis); michael@0: michael@0: /** michael@0: * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add michael@0: * to GMT to get local time, before taking daylight savings time into account). michael@0: * michael@0: * @return The TimeZone's raw GMT offset. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual int32_t getRawOffset(void) const; michael@0: michael@0: /** michael@0: * Queries if this time zone uses daylight savings time. michael@0: * @return true if this time zone uses daylight savings time, michael@0: * false, otherwise. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual UBool useDaylightTime(void) const; michael@0: michael@0: /** michael@0: * Queries if the given date is in daylight savings time in michael@0: * this time zone. michael@0: * This method is wasteful since it creates a new GregorianCalendar and michael@0: * deletes it each time it is called. This is a deprecated method michael@0: * and provided only for Java compatibility. michael@0: * michael@0: * @param date the given UDate. michael@0: * @param status Output param filled in with success/error code. michael@0: * @return true if the given date is in daylight savings time, michael@0: * false, otherwise. michael@0: * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead. michael@0: */ michael@0: virtual UBool inDaylightTime(UDate date, UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Returns true if this zone has the same rule and offset as another zone. michael@0: * That is, if this zone differs only in ID, if at all. michael@0: * @param other the TimeZone object to be compared with michael@0: * @return true if the given zone is the same as this one, michael@0: * with the possible exception of the ID michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual UBool hasSameRules(const TimeZone& other) const; michael@0: michael@0: /** 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: * @stable ICU 3.8 michael@0: */ michael@0: virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const; michael@0: michael@0: /** 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: * @stable ICU 3.8 michael@0: */ michael@0: virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const; michael@0: michael@0: /** michael@0: * Returns the number of TimeZoneRules which represents time transitions, michael@0: * for this time zone, that is, all TimeZoneRules 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 TimeZoneRules representing time transitions. michael@0: * @stable ICU 3.8 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: * @stable ICU 3.8 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: * Get time zone offsets from local wall time. michael@0: * @internal michael@0: */ michael@0: virtual void getOffsetFromLocal(UDate date, int32_t nonExistingTimeOpt, int32_t duplicatedTimeOpt, michael@0: int32_t& rawOffset, int32_t& dstOffset, UErrorCode& status) const; michael@0: michael@0: private: michael@0: void deleteRules(void); michael@0: void deleteTransitions(void); michael@0: UVector* copyRules(UVector* source); michael@0: TimeZoneRule* findRuleInFinal(UDate date, UBool local, michael@0: int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const; michael@0: UBool findNext(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const; michael@0: UBool findPrev(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const; michael@0: int32_t getLocalDelta(int32_t rawBefore, int32_t dstBefore, int32_t rawAfter, int32_t dstAfter, michael@0: int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const; michael@0: UDate getTransitionTime(Transition* transition, UBool local, michael@0: int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const; michael@0: void getOffsetInternal(UDate date, UBool local, int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt, michael@0: int32_t& rawOffset, int32_t& dstOffset, UErrorCode& ec) const; michael@0: void completeConst(UErrorCode &status) const; michael@0: michael@0: InitialTimeZoneRule *fInitialRule; michael@0: UVector *fHistoricRules; michael@0: UVector *fFinalRules; michael@0: UVector *fHistoricTransitions; michael@0: UBool fUpToDate; michael@0: michael@0: public: michael@0: /** michael@0: * Return the class ID for this class. This is useful only for comparing to michael@0: * a return value from getDynamicClassID(). For example: michael@0: *

michael@0:      * .   Base* polymorphic_pointer = createPolymorphicObject();
michael@0:      * .   if (polymorphic_pointer->getDynamicClassID() ==
michael@0:      * .       erived::getStaticClassID()) ...
michael@0:      * 
michael@0: * @return The class ID for all objects of this class. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(void); michael@0: michael@0: /** michael@0: * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This michael@0: * method is to implement a simple version of RTTI, since not all C++ michael@0: * compilers support genuine RTTI. Polymorphic operator==() and clone() michael@0: * methods call this method. michael@0: * michael@0: * @return The class ID for this object. All objects of a michael@0: * given class have the same class ID. Objects of michael@0: * other classes have different class IDs. michael@0: * @stable ICU 3.8 michael@0: */ michael@0: virtual UClassID getDynamicClassID(void) const; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // RBTZ_H michael@0: michael@0: //eof