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