1 /* 2 ******************************************************************************* 3 * Copyright (C) 2007, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 #ifndef RBTZ_H 8 #define RBTZ_H 9 10 #include "unicode/utypes.h" 11 12 /** 13 * \file 14 * \brief C++ API: Rule based customizable time zone 15 */ 16 17 #if !UCONFIG_NO_FORMATTING 18 19 #include "unicode/basictz.h" 20 #include "unicode/unistr.h" 21 22 U_NAMESPACE_BEGIN 23 24 // forward declaration 25 class UVector; 26 struct Transition; 27 28 class U_I18N_API RuleBasedTimeZone : public BasicTimeZone { 29 public: 30 /** 31 * Constructs a <code>RuleBasedTimeZone</code> object with the ID and the 32 * <code>InitialTimeZoneRule</code>. The input <code>InitialTimeZoneRule</code> 33 * is adopted by this <code>RuleBasedTimeZone</code>, thus the caller must not 34 * delete it. 35 * @param id The time zone ID. 36 * @param initialRule The initial time zone rule. 37 * @draft ICU 3.8 38 */ 39 RuleBasedTimeZone(const UnicodeString& id, InitialTimeZoneRule* initialRule); 40 41 /** 42 * Copy constructor. 43 * @param source The RuleBasedTimeZone object to be copied. 44 * @draft ICU 3.8 45 */ 46 RuleBasedTimeZone(const RuleBasedTimeZone& source); 47 48 /** 49 * Destructor. 50 * @draft ICU 3.8 51 */ 52 virtual ~RuleBasedTimeZone(); 53 54 /** 55 * Assignment operator. 56 * @param right The object to be copied. 57 * @draft ICU 3.8 58 */ 59 RuleBasedTimeZone& operator=(const RuleBasedTimeZone& right); 60 61 /** 62 * Return true if the given <code>TimeZone</code> objects are 63 * semantically equal. Objects of different subclasses are considered unequal. 64 * @param that The object to be compared with. 65 * @return true if the given <code>TimeZone</code> objects are 66 *semantically equal. 67 * @draft ICU 3.8 68 */ 69 virtual UBool operator==(const TimeZone& that) const; 70 71 /** 72 * Return true if the given <code>TimeZone</code> objects are 73 * semantically unequal. Objects of different subclasses are considered unequal. 74 * @param that The object to be compared with. 75 * @return true if the given <code>TimeZone</code> objects are 76 * semantically unequal. 77 * @draft ICU 3.8 78 */ 79 virtual UBool operator!=(const TimeZone& that) const; 80 81 /** 82 * Adds the <code>TimeZoneRule</code> which represents time transitions. 83 * The <code>TimeZoneRule</code> must have start times, that is, the result 84 * of isTransitionRule() must be true. Otherwise, U_ILLEGAL_ARGUMENT_ERROR 85 * is set to the error code. 86 * The input <code>TimeZoneRule</code> is adopted by this 87 * <code>RuleBasedTimeZone</code> on successful completion of this method, 88 * thus, the caller must not delete it when no error is returned. 89 * After all rules are added, the caller must call complete() method to 90 * make this <code>RuleBasedTimeZone</code> ready to handle common time 91 * zone functions. 92 * @param rule The <code>TimeZoneRule</code>. 93 * @param status Output param to filled in with a success or an error. 94 * @draft ICU 3.8 95 */ 96 void addTransitionRule(TimeZoneRule* rule, UErrorCode& status); 97 98 /** 99 * Makes the <code>TimeZoneRule</code> ready to handle actual timezone 100 * calcuation APIs. This method collects time zone rules specified 101 * by the caller via the constructor and addTransitionRule() and 102 * builds internal structure for making the object ready to support 103 * time zone APIs such as getOffset(), getNextTransition() and others. 104 * @param status Output param to filled in with a success or an error. 105 * @draft ICU 3.8 106 */ 107 void complete(UErrorCode& status); 108 109 /** 110 * Clones TimeZone objects polymorphically. Clients are responsible for deleting 111 * the TimeZone object cloned. 112 * 113 * @return A new copy of this TimeZone object. 114 * @draft ICU 3.8 115 */ 116 virtual TimeZone* clone(void) const; 117 118 /** 119 * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add 120 * to GMT to get local time in this time zone, taking daylight savings time into 121 * account) as of a particular reference date. The reference date is used to determine 122 * whether daylight savings time is in effect and needs to be figured into the offset 123 * that is returned (in other words, what is the adjusted GMT offset in this time zone 124 * at this particular date and time?). For the time zones produced by createTimeZone(), 125 * the reference data is specified according to the Gregorian calendar, and the date 126 * and time fields are local standard time. 127 * 128 * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload, 129 * which returns both the raw and the DST offset for a given time. This method 130 * is retained only for backward compatibility. 131 * 132 * @param era The reference date's era 133 * @param year The reference date's year 134 * @param month The reference date's month (0-based; 0 is January) 135 * @param day The reference date's day-in-month (1-based) 136 * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday) 137 * @param millis The reference date's milliseconds in day, local standard time 138 * @param status Output param to filled in with a success or an error. 139 * @return The offset in milliseconds to add to GMT to get local time. 140 * @draft ICU 3.8 141 */ 142 virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day, 143 uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const; 144 145 /** 146 * Gets the time zone offset, for current date, modified in case of 147 * daylight savings. This is the offset to add *to* UTC to get local time. 148 * 149 * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload, 150 * which returns both the raw and the DST offset for a given time. This method 151 * is retained only for backward compatibility. 152 * 153 * @param era The reference date's era 154 * @param year The reference date's year 155 * @param month The reference date's month (0-based; 0 is January) 156 * @param day The reference date's day-in-month (1-based) 157 * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday) 158 * @param millis The reference date's milliseconds in day, local standard time 159 * @param monthLength The length of the given month in days. 160 * @param status Output param to filled in with a success or an error. 161 * @return The offset in milliseconds to add to GMT to get local time. 162 * @draft ICU 3.8 163 */ 164 virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day, 165 uint8_t dayOfWeek, int32_t millis, 166 int32_t monthLength, UErrorCode& status) const; 167 168 /** 169 * Returns the time zone raw and GMT offset for the given moment 170 * in time. Upon return, local-millis = GMT-millis + rawOffset + 171 * dstOffset. All computations are performed in the proleptic 172 * Gregorian calendar. The default implementation in the TimeZone 173 * class delegates to the 8-argument getOffset(). 174 * 175 * @param date moment in time for which to return offsets, in 176 * units of milliseconds from January 1, 1970 0:00 GMT, either GMT 177 * time or local wall time, depending on `local'. 178 * @param local if true, `date' is local wall time; otherwise it 179 * is in GMT time. 180 * @param rawOffset output parameter to receive the raw offset, that 181 * is, the offset not including DST adjustments 182 * @param dstOffset output parameter to receive the DST offset, 183 * that is, the offset to be added to `rawOffset' to obtain the 184 * total offset between local and GMT time. If DST is not in 185 * effect, this value is zero; otherwise it is a positive value, 186 * typically one hour. 187 * @param ec input-output error code 188 * @draft ICU 3.8 189 */ 190 virtual void getOffset(UDate date, UBool local, int32_t& rawOffset, 191 int32_t& dstOffset, UErrorCode& ec) const; 192 193 /** 194 * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add 195 * to GMT to get local time, before taking daylight savings time into account). 196 * 197 * @param offsetMillis The new raw GMT offset for this time zone. 198 * @draft ICU 3.8 199 */ 200 virtual void setRawOffset(int32_t offsetMillis); 201 202 /** 203 * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add 204 * to GMT to get local time, before taking daylight savings time into account). 205 * 206 * @return The TimeZone's raw GMT offset. 207 * @draft ICU 3.8 208 */ 209 virtual int32_t getRawOffset(void) const; 210 211 /** 212 * Queries if this time zone uses daylight savings time. 213 * @return true if this time zone uses daylight savings time, 214 * false, otherwise. 215 * @draft ICU 3.8 216 */ 217 virtual UBool useDaylightTime(void) const; 218 219 /** 220 * Queries if the given date is in daylight savings time in 221 * this time zone. 222 * This method is wasteful since it creates a new GregorianCalendar and 223 * deletes it each time it is called. This is a deprecated method 224 * and provided only for Java compatibility. 225 * 226 * @param date the given UDate. 227 * @param status Output param filled in with success/error code. 228 * @return true if the given date is in daylight savings time, 229 * false, otherwise. 230 * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead. 231 */ 232 virtual UBool inDaylightTime(UDate date, UErrorCode& status) const; 233 234 /** 235 * Returns true if this zone has the same rule and offset as another zone. 236 * That is, if this zone differs only in ID, if at all. 237 * @param other the <code>TimeZone</code> object to be compared with 238 * @return true if the given zone is the same as this one, 239 * with the possible exception of the ID 240 * @draft ICU 3.8 241 */ 242 virtual UBool hasSameRules(const TimeZone& other) const; 243 244 /** 245 * Gets the first time zone transition after the base time. 246 * @param base The base time. 247 * @param inclusive Whether the base time is inclusive or not. 248 * @param result Receives the first transition after the base time. 249 * @return TRUE if the transition is found. 250 * @draft ICU 3.8 251 */ 252 virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) /*const*/; 253 254 /** 255 * Gets the most recent time zone transition before the base time. 256 * @param base The base time. 257 * @param inclusive Whether the base time is inclusive or not. 258 * @param result Receives the most recent transition before the base time. 259 * @return TRUE if the transition is found. 260 * @draft ICU 3.8 261 */ 262 virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) /*const*/; 263 264 /** 265 * Returns the number of <code>TimeZoneRule</code>s which represents time transitions, 266 * for this time zone, that is, all <code>TimeZoneRule</code>s for this time zone except 267 * <code>InitialTimeZoneRule</code>. The return value range is 0 or any positive value. 268 * @param status Receives error status code. 269 * @return The number of <code>TimeZoneRule</code>s representing time transitions. 270 * @draft ICU 3.8 271 */ 272 virtual int32_t countTransitionRules(UErrorCode& status) /*const*/; 273 274 /** 275 * Gets the <code>InitialTimeZoneRule</code> and the set of <code>TimeZoneRule</code> 276 * which represent time transitions for this time zone. On successful return, 277 * the argument initial points to non-NULL <code>InitialTimeZoneRule</code> and 278 * the array trsrules is filled with 0 or multiple <code>TimeZoneRule</code> 279 * instances up to the size specified by trscount. The results are referencing the 280 * rule instance held by this time zone instance. Therefore, after this time zone 281 * is destructed, they are no longer available. 282 * @param initial Receives the initial timezone rule 283 * @param trsrules Receives the timezone transition rules 284 * @param trscount On input, specify the size of the array 'transitions' receiving 285 * the timezone transition rules. On output, actual number of 286 * rules filled in the array will be set. 287 * @param status Receives error status code. 288 * @draft ICU 3.8 289 */ 290 virtual void getTimeZoneRules(const InitialTimeZoneRule*& initial, 291 const TimeZoneRule* trsrules[], int32_t& trscount, UErrorCode& status) /*const*/; 292 293 /** 294 * Get time zone offsets from local wall time. 295 * @internal 296 */ 297 virtual void getOffsetFromLocal(UDate date, int32_t nonExistingTimeOpt, int32_t duplicatedTimeOpt, 298 int32_t& rawOffset, int32_t& dstOffset, UErrorCode& status) /*const*/; 299 300 private: 301 void deleteRules(void); 302 void deleteTransitions(void); 303 UVector* copyRules(UVector* source); 304 TimeZoneRule* findRuleInFinal(UDate date, UBool local, 305 int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const; 306 UBool findNext(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const; 307 UBool findPrev(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const; 308 int32_t getLocalDelta(int32_t rawBefore, int32_t dstBefore, int32_t rawAfter, int32_t dstAfter, 309 int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const; 310 UDate getTransitionTime(Transition* transition, UBool local, 311 int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const; 312 void getOffsetInternal(UDate date, UBool local, int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt, 313 int32_t& rawOffset, int32_t& dstOffset, UErrorCode& ec) const; 314 315 InitialTimeZoneRule *fInitialRule; 316 UVector *fHistoricRules; 317 UVector *fFinalRules; 318 UVector *fHistoricTransitions; 319 UBool fUpToDate; 320 321 public: 322 /** 323 * Return the class ID for this class. This is useful only for comparing to 324 * a return value from getDynamicClassID(). For example: 325 * <pre> 326 * . Base* polymorphic_pointer = createPolymorphicObject(); 327 * . if (polymorphic_pointer->getDynamicClassID() == 328 * . erived::getStaticClassID()) ... 329 * </pre> 330 * @return The class ID for all objects of this class. 331 * @draft ICU 3.8 332 */ 333 static UClassID U_EXPORT2 getStaticClassID(void); 334 335 /** 336 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This 337 * method is to implement a simple version of RTTI, since not all C++ 338 * compilers support genuine RTTI. Polymorphic operator==() and clone() 339 * methods call this method. 340 * 341 * @return The class ID for this object. All objects of a 342 * given class have the same class ID. Objects of 343 * other classes have different class IDs. 344 * @draft ICU 3.8 345 */ 346 virtual UClassID getDynamicClassID(void) const; 347 }; 348 349 U_NAMESPACE_END 350 351 #endif /* #if !UCONFIG_NO_FORMATTING */ 352 353 #endif // RBTZ_H 354 355 //eof 356