| File: | out/../deps/icu-small/source/i18n/rbtz.cpp |
| Warning: | line 89, column 24 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 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 | ||||
| 10 | #include "utypeinfo.h" // for 'typeid' to work | |||
| 11 | ||||
| 12 | #include "unicode/utypes.h" | |||
| 13 | ||||
| 14 | #if !UCONFIG_NO_FORMATTING0 | |||
| 15 | ||||
| 16 | #include "unicode/rbtz.h" | |||
| 17 | #include "unicode/gregocal.h" | |||
| 18 | #include "uvector.h" | |||
| 19 | #include "gregoimp.h" | |||
| 20 | #include "cmemory.h" | |||
| 21 | #include "umutex.h" | |||
| 22 | ||||
| 23 | U_NAMESPACE_BEGINnamespace icu_71 { | |||
| 24 | ||||
| 25 | /** | |||
| 26 | * A struct representing a time zone transition | |||
| 27 | */ | |||
| 28 | struct Transition : public UMemory { | |||
| 29 | UDate time; | |||
| 30 | TimeZoneRule* from; | |||
| 31 | TimeZoneRule* to; | |||
| 32 | }; | |||
| 33 | ||||
| 34 | U_CDECL_BEGINextern "C" { | |||
| 35 | static void U_CALLCONV | |||
| 36 | deleteTransition(void* obj) { | |||
| 37 | delete static_cast<Transition *>(obj); | |||
| 38 | } | |||
| 39 | U_CDECL_END} | |||
| 40 | ||||
| 41 | static UBool compareRules(UVector* rules1, UVector* rules2) { | |||
| 42 | if (rules1 == NULL__null && rules2 == NULL__null) { | |||
| 43 | return TRUE1; | |||
| 44 | } else if (rules1 == NULL__null || rules2 == NULL__null) { | |||
| 45 | return FALSE0; | |||
| 46 | } | |||
| 47 | int32_t size = rules1->size(); | |||
| 48 | if (size != rules2->size()) { | |||
| 49 | return FALSE0; | |||
| 50 | } | |||
| 51 | for (int32_t i = 0; i < size; i++) { | |||
| 52 | TimeZoneRule *r1 = (TimeZoneRule*)rules1->elementAt(i); | |||
| 53 | TimeZoneRule *r2 = (TimeZoneRule*)rules2->elementAt(i); | |||
| 54 | if (*r1 != *r2) { | |||
| 55 | return FALSE0; | |||
| 56 | } | |||
| 57 | } | |||
| 58 | return TRUE1; | |||
| 59 | } | |||
| 60 | ||||
| 61 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedTimeZone)UClassID RuleBasedTimeZone::getStaticClassID() { static char classID = 0; return (UClassID)&classID; } UClassID RuleBasedTimeZone ::getDynamicClassID() const { return RuleBasedTimeZone::getStaticClassID (); } | |||
| 62 | ||||
| 63 | RuleBasedTimeZone::RuleBasedTimeZone(const UnicodeString& id, InitialTimeZoneRule* initialRule) | |||
| 64 | : BasicTimeZone(id), fInitialRule(initialRule), fHistoricRules(NULL__null), fFinalRules(NULL__null), | |||
| 65 | fHistoricTransitions(NULL__null), fUpToDate(FALSE0) { | |||
| 66 | } | |||
| 67 | ||||
| 68 | RuleBasedTimeZone::RuleBasedTimeZone(const RuleBasedTimeZone& source) | |||
| 69 | : BasicTimeZone(source), fInitialRule(source.fInitialRule->clone()), | |||
| 70 | fHistoricTransitions(NULL__null), fUpToDate(FALSE0) { | |||
| 71 | fHistoricRules = copyRules(source.fHistoricRules); | |||
| 72 | fFinalRules = copyRules(source.fFinalRules); | |||
| 73 | if (source.fUpToDate) { | |||
| 74 | UErrorCode status = U_ZERO_ERROR; | |||
| 75 | complete(status); | |||
| 76 | } | |||
| 77 | } | |||
| 78 | ||||
| 79 | RuleBasedTimeZone::~RuleBasedTimeZone() { | |||
| 80 | deleteTransitions(); | |||
| 81 | deleteRules(); | |||
| 82 | } | |||
| 83 | ||||
| 84 | RuleBasedTimeZone& | |||
| ||||
| 85 | RuleBasedTimeZone::operator=(const RuleBasedTimeZone& right) { | |||
| 86 | if (*this != right) { | |||
| 87 | BasicTimeZone::operator=(right); | |||
| 88 | deleteRules(); | |||
| 89 | fInitialRule = right.fInitialRule->clone(); | |||
| ||||
| 90 | fHistoricRules = copyRules(right.fHistoricRules); | |||
| 91 | fFinalRules = copyRules(right.fFinalRules); | |||
| 92 | deleteTransitions(); | |||
| 93 | fUpToDate = FALSE0; | |||
| 94 | } | |||
| 95 | return *this; | |||
| 96 | } | |||
| 97 | ||||
| 98 | bool | |||
| 99 | RuleBasedTimeZone::operator==(const TimeZone& that) const { | |||
| 100 | if (this == &that) { | |||
| 101 | return true; | |||
| 102 | } | |||
| 103 | if (typeid(*this) != typeid(that) || !BasicTimeZone::operator==(that)) { | |||
| 104 | return false; | |||
| 105 | } | |||
| 106 | RuleBasedTimeZone *rbtz = (RuleBasedTimeZone*)&that; | |||
| 107 | if (*fInitialRule != *(rbtz->fInitialRule)) { | |||
| 108 | return false; | |||
| 109 | } | |||
| 110 | if (compareRules(fHistoricRules, rbtz->fHistoricRules) | |||
| 111 | && compareRules(fFinalRules, rbtz->fFinalRules)) { | |||
| 112 | return true; | |||
| 113 | } | |||
| 114 | return false; | |||
| 115 | } | |||
| 116 | ||||
| 117 | bool | |||
| 118 | RuleBasedTimeZone::operator!=(const TimeZone& that) const { | |||
| 119 | return !operator==(that); | |||
| 120 | } | |||
| 121 | ||||
| 122 | void | |||
| 123 | RuleBasedTimeZone::addTransitionRule(TimeZoneRule* rule, UErrorCode& status) { | |||
| 124 | LocalPointer<TimeZoneRule>lpRule(rule); | |||
| 125 | if (U_FAILURE(status)) { | |||
| 126 | return; | |||
| 127 | } | |||
| 128 | AnnualTimeZoneRule* atzrule = dynamic_cast<AnnualTimeZoneRule*>(rule); | |||
| 129 | if (atzrule != nullptr && atzrule->getEndYear() == AnnualTimeZoneRule::MAX_YEAR) { | |||
| 130 | // A final rule | |||
| 131 | if (fFinalRules == nullptr) { | |||
| 132 | LocalPointer<UVector> lpFinalRules(new UVector(uprv_deleteUObjectuprv_deleteUObject_71, nullptr, status), status); | |||
| 133 | if (U_FAILURE(status)) { | |||
| 134 | return; | |||
| 135 | } | |||
| 136 | fFinalRules = lpFinalRules.orphan(); | |||
| 137 | } else if (fFinalRules->size() >= 2) { | |||
| 138 | // Cannot handle more than two final rules | |||
| 139 | status = U_INVALID_STATE_ERROR; | |||
| 140 | return; | |||
| 141 | } | |||
| 142 | fFinalRules->adoptElement(lpRule.orphan(), status); | |||
| 143 | } else { | |||
| 144 | // Non-final rule | |||
| 145 | if (fHistoricRules == nullptr) { | |||
| 146 | LocalPointer<UVector> lpHistoricRules(new UVector(uprv_deleteUObjectuprv_deleteUObject_71, nullptr, status), status); | |||
| 147 | if (U_FAILURE(status)) { | |||
| 148 | return; | |||
| 149 | } | |||
| 150 | fHistoricRules = lpHistoricRules.orphan(); | |||
| 151 | } | |||
| 152 | fHistoricRules->adoptElement(lpRule.orphan(), status); | |||
| 153 | } | |||
| 154 | // Mark dirty, so transitions are recalculated at next complete() call | |||
| 155 | fUpToDate = FALSE0; | |||
| 156 | } | |||
| 157 | ||||
| 158 | ||||
| 159 | void | |||
| 160 | RuleBasedTimeZone::completeConst(UErrorCode& status) const { | |||
| 161 | static UMutex gLock; | |||
| 162 | if (U_FAILURE(status)) { | |||
| 163 | return; | |||
| 164 | } | |||
| 165 | umtx_lockumtx_lock_71(&gLock); | |||
| 166 | if (!fUpToDate) { | |||
| 167 | RuleBasedTimeZone *ncThis = const_cast<RuleBasedTimeZone*>(this); | |||
| 168 | ncThis->complete(status); | |||
| 169 | } | |||
| 170 | umtx_unlockumtx_unlock_71(&gLock); | |||
| 171 | } | |||
| 172 | ||||
| 173 | void | |||
| 174 | RuleBasedTimeZone::complete(UErrorCode& status) { | |||
| 175 | if (U_FAILURE(status)) { | |||
| 176 | return; | |||
| 177 | } | |||
| 178 | if (fUpToDate) { | |||
| 179 | return; | |||
| 180 | } | |||
| 181 | // Make sure either no final rules or a pair of AnnualTimeZoneRules | |||
| 182 | // are available. | |||
| 183 | if (fFinalRules != NULL__null && fFinalRules->size() != 2) { | |||
| 184 | status = U_INVALID_STATE_ERROR; | |||
| 185 | return; | |||
| 186 | } | |||
| 187 | ||||
| 188 | // Create a TimezoneTransition and add to the list | |||
| 189 | if (fHistoricRules != NULL__null || fFinalRules != NULL__null) { | |||
| 190 | TimeZoneRule *curRule = fInitialRule; | |||
| 191 | UDate lastTransitionTime = MIN_MILLIS(((-0x7F000000) - 2440588) * (1.0 * (86400000))); | |||
| 192 | ||||
| 193 | // Build the transition array which represents historical time zone | |||
| 194 | // transitions. | |||
| 195 | if (fHistoricRules != NULL__null && fHistoricRules->size() > 0) { | |||
| 196 | int32_t i; | |||
| 197 | int32_t historicCount = fHistoricRules->size(); | |||
| 198 | LocalMemory<bool> done((bool *)uprv_mallocuprv_malloc_71(sizeof(bool) * historicCount)); | |||
| 199 | if (done == NULL__null) { | |||
| 200 | status = U_MEMORY_ALLOCATION_ERROR; | |||
| 201 | goto cleanup; | |||
| 202 | } | |||
| 203 | for (i = 0; i < historicCount; i++) { | |||
| 204 | done[i] = false; | |||
| 205 | } | |||
| 206 | while (TRUE1) { | |||
| 207 | int32_t curStdOffset = curRule->getRawOffset(); | |||
| 208 | int32_t curDstSavings = curRule->getDSTSavings(); | |||
| 209 | UDate nextTransitionTime = MAX_MILLIS(((+0x7F000000) - 2440588) * (1.0 * (86400000))); | |||
| 210 | TimeZoneRule *nextRule = NULL__null; | |||
| 211 | TimeZoneRule *r = NULL__null; | |||
| 212 | UBool avail; | |||
| 213 | UDate tt; | |||
| 214 | UnicodeString curName, name; | |||
| 215 | curRule->getName(curName); | |||
| 216 | ||||
| 217 | for (i = 0; i < historicCount; i++) { | |||
| 218 | if (done[i]) { | |||
| 219 | continue; | |||
| 220 | } | |||
| 221 | r = (TimeZoneRule*)fHistoricRules->elementAt(i); | |||
| 222 | avail = r->getNextStart(lastTransitionTime, curStdOffset, curDstSavings, false, tt); | |||
| 223 | if (!avail) { | |||
| 224 | // No more transitions from this rule - skip this rule next time | |||
| 225 | done[i] = true; | |||
| 226 | } else { | |||
| 227 | r->getName(name); | |||
| 228 | if (*r == *curRule || | |||
| 229 | (name == curName && r->getRawOffset() == curRule->getRawOffset() | |||
| 230 | && r->getDSTSavings() == curRule->getDSTSavings())) { | |||
| 231 | continue; | |||
| 232 | } | |||
| 233 | if (tt < nextTransitionTime) { | |||
| 234 | nextTransitionTime = tt; | |||
| 235 | nextRule = r; | |||
| 236 | } | |||
| 237 | } | |||
| 238 | } | |||
| 239 | ||||
| 240 | if (nextRule == NULL__null) { | |||
| 241 | // Check if all historic rules are done | |||
| 242 | UBool bDoneAll = TRUE1; | |||
| 243 | for (int32_t j = 0; j < historicCount; j++) { | |||
| 244 | if (!done[j]) { | |||
| 245 | bDoneAll = FALSE0; | |||
| 246 | break; | |||
| 247 | } | |||
| 248 | } | |||
| 249 | if (bDoneAll) { | |||
| 250 | break; | |||
| 251 | } | |||
| 252 | } | |||
| 253 | ||||
| 254 | if (fFinalRules != NULL__null) { | |||
| 255 | // Check if one of final rules has earlier transition date | |||
| 256 | for (i = 0; i < 2 /* fFinalRules->size() */; i++) { | |||
| 257 | TimeZoneRule *fr = (TimeZoneRule*)fFinalRules->elementAt(i); | |||
| 258 | if (*fr == *curRule) { | |||
| 259 | continue; | |||
| 260 | } | |||
| 261 | r = (TimeZoneRule*)fFinalRules->elementAt(i); | |||
| 262 | avail = r->getNextStart(lastTransitionTime, curStdOffset, curDstSavings, false, tt); | |||
| 263 | if (avail) { | |||
| 264 | if (tt < nextTransitionTime) { | |||
| 265 | nextTransitionTime = tt; | |||
| 266 | nextRule = r; | |||
| 267 | } | |||
| 268 | } | |||
| 269 | } | |||
| 270 | } | |||
| 271 | ||||
| 272 | if (nextRule == NULL__null) { | |||
| 273 | // Nothing more | |||
| 274 | break; | |||
| 275 | } | |||
| 276 | ||||
| 277 | if (fHistoricTransitions == NULL__null) { | |||
| 278 | LocalPointer<UVector> lpHistoricTransitions( | |||
| 279 | new UVector(deleteTransition, nullptr, status), status); | |||
| 280 | if (U_FAILURE(status)) { | |||
| 281 | goto cleanup; | |||
| 282 | } | |||
| 283 | fHistoricTransitions = lpHistoricTransitions.orphan(); | |||
| 284 | } | |||
| 285 | LocalPointer<Transition> trst(new Transition, status); | |||
| 286 | if (U_FAILURE(status)) { | |||
| 287 | goto cleanup; | |||
| 288 | } | |||
| 289 | trst->time = nextTransitionTime; | |||
| 290 | trst->from = curRule; | |||
| 291 | trst->to = nextRule; | |||
| 292 | fHistoricTransitions->adoptElement(trst.orphan(), status); | |||
| 293 | if (U_FAILURE(status)) { | |||
| 294 | goto cleanup; | |||
| 295 | } | |||
| 296 | lastTransitionTime = nextTransitionTime; | |||
| 297 | curRule = nextRule; | |||
| 298 | } | |||
| 299 | } | |||
| 300 | if (fFinalRules != NULL__null) { | |||
| 301 | if (fHistoricTransitions == NULL__null) { | |||
| 302 | LocalPointer<UVector> lpHistoricTransitions( | |||
| 303 | new UVector(deleteTransition, nullptr, status), status); | |||
| 304 | if (U_FAILURE(status)) { | |||
| 305 | goto cleanup; | |||
| 306 | } | |||
| 307 | fHistoricTransitions = lpHistoricTransitions.orphan(); | |||
| 308 | } | |||
| 309 | // Append the first transition for each | |||
| 310 | TimeZoneRule *rule0 = (TimeZoneRule*)fFinalRules->elementAt(0); | |||
| 311 | TimeZoneRule *rule1 = (TimeZoneRule*)fFinalRules->elementAt(1); | |||
| 312 | UDate tt0, tt1; | |||
| 313 | UBool avail0 = rule0->getNextStart(lastTransitionTime, curRule->getRawOffset(), curRule->getDSTSavings(), false, tt0); | |||
| 314 | UBool avail1 = rule1->getNextStart(lastTransitionTime, curRule->getRawOffset(), curRule->getDSTSavings(), false, tt1); | |||
| 315 | if (!avail0 || !avail1) { | |||
| 316 | // Should not happen, because both rules are permanent | |||
| 317 | status = U_INVALID_STATE_ERROR; | |||
| 318 | goto cleanup; | |||
| 319 | } | |||
| 320 | LocalPointer<Transition> final0(new Transition, status); | |||
| 321 | LocalPointer<Transition> final1(new Transition, status); | |||
| 322 | if (U_FAILURE(status)) { | |||
| 323 | goto cleanup; | |||
| 324 | } | |||
| 325 | if (tt0 < tt1) { | |||
| 326 | final0->time = tt0; | |||
| 327 | final0->from = curRule; | |||
| 328 | final0->to = rule0; | |||
| 329 | rule1->getNextStart(tt0, rule0->getRawOffset(), rule0->getDSTSavings(), false, final1->time); | |||
| 330 | final1->from = rule0; | |||
| 331 | final1->to = rule1; | |||
| 332 | } else { | |||
| 333 | final0->time = tt1; | |||
| 334 | final0->from = curRule; | |||
| 335 | final0->to = rule1; | |||
| 336 | rule0->getNextStart(tt1, rule1->getRawOffset(), rule1->getDSTSavings(), false, final1->time); | |||
| 337 | final1->from = rule1; | |||
| 338 | final1->to = rule0; | |||
| 339 | } | |||
| 340 | fHistoricTransitions->adoptElement(final0.orphan(), status); | |||
| 341 | fHistoricTransitions->adoptElement(final1.orphan(), status); | |||
| 342 | if (U_FAILURE(status)) { | |||
| 343 | goto cleanup; | |||
| 344 | } | |||
| 345 | } | |||
| 346 | } | |||
| 347 | fUpToDate = TRUE1; | |||
| 348 | return; | |||
| 349 | ||||
| 350 | cleanup: | |||
| 351 | deleteTransitions(); | |||
| 352 | fUpToDate = FALSE0; | |||
| 353 | } | |||
| 354 | ||||
| 355 | RuleBasedTimeZone* | |||
| 356 | RuleBasedTimeZone::clone() const { | |||
| 357 | return new RuleBasedTimeZone(*this); | |||
| 358 | } | |||
| 359 | ||||
| 360 | int32_t | |||
| 361 | RuleBasedTimeZone::getOffset(uint8_t era, int32_t year, int32_t month, int32_t day, | |||
| 362 | uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const { | |||
| 363 | if (U_FAILURE(status)) { | |||
| 364 | return 0; | |||
| 365 | } | |||
| 366 | if (month < UCAL_JANUARY || month > UCAL_DECEMBER) { | |||
| 367 | status = U_ILLEGAL_ARGUMENT_ERROR; | |||
| 368 | return 0; | |||
| 369 | } else { | |||
| 370 | return getOffset(era, year, month, day, dayOfWeek, millis, | |||
| 371 | Grego::monthLength(year, month), status); | |||
| 372 | } | |||
| 373 | } | |||
| 374 | ||||
| 375 | int32_t | |||
| 376 | RuleBasedTimeZone::getOffset(uint8_t era, int32_t year, int32_t month, int32_t day, | |||
| 377 | uint8_t /*dayOfWeek*/, int32_t millis, | |||
| 378 | int32_t /*monthLength*/, UErrorCode& status) const { | |||
| 379 | // dayOfWeek and monthLength are unused | |||
| 380 | if (U_FAILURE(status)) { | |||
| 381 | return 0; | |||
| 382 | } | |||
| 383 | if (era == GregorianCalendar::BC) { | |||
| 384 | // Convert to extended year | |||
| 385 | year = 1 - year; | |||
| 386 | } | |||
| 387 | int32_t rawOffset, dstOffset; | |||
| 388 | UDate time = (UDate)Grego::fieldsToDay(year, month, day) * U_MILLIS_PER_DAY(86400000) + millis; | |||
| 389 | getOffsetInternal(time, TRUE1, kDaylight, kStandard, rawOffset, dstOffset, status); | |||
| 390 | if (U_FAILURE(status)) { | |||
| 391 | return 0; | |||
| 392 | } | |||
| 393 | return (rawOffset + dstOffset); | |||
| 394 | } | |||
| 395 | ||||
| 396 | void | |||
| 397 | RuleBasedTimeZone::getOffset(UDate date, UBool local, int32_t& rawOffset, | |||
| 398 | int32_t& dstOffset, UErrorCode& status) const { | |||
| 399 | getOffsetInternal(date, local, kFormer, kLatter, rawOffset, dstOffset, status); | |||
| 400 | } | |||
| 401 | ||||
| 402 | void RuleBasedTimeZone::getOffsetFromLocal(UDate date, UTimeZoneLocalOption nonExistingTimeOpt, | |||
| 403 | UTimeZoneLocalOption duplicatedTimeOpt, | |||
| 404 | int32_t& rawOffset, int32_t& dstOffset, UErrorCode& status) const { | |||
| 405 | getOffsetInternal(date, TRUE1, nonExistingTimeOpt, duplicatedTimeOpt, rawOffset, dstOffset, status); | |||
| 406 | } | |||
| 407 | ||||
| 408 | ||||
| 409 | /* | |||
| 410 | * The internal getOffset implementation | |||
| 411 | */ | |||
| 412 | void | |||
| 413 | RuleBasedTimeZone::getOffsetInternal(UDate date, UBool local, | |||
| 414 | int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt, | |||
| 415 | int32_t& rawOffset, int32_t& dstOffset, | |||
| 416 | UErrorCode& status) const { | |||
| 417 | rawOffset = 0; | |||
| 418 | dstOffset = 0; | |||
| 419 | ||||
| 420 | if (U_FAILURE(status)) { | |||
| 421 | return; | |||
| 422 | } | |||
| 423 | if (!fUpToDate) { | |||
| 424 | // Transitions are not yet resolved. We cannot do it here | |||
| 425 | // because this method is const. Thus, do nothing and return | |||
| 426 | // error status. | |||
| 427 | status = U_INVALID_STATE_ERROR; | |||
| 428 | return; | |||
| 429 | } | |||
| 430 | const TimeZoneRule *rule = NULL__null; | |||
| 431 | if (fHistoricTransitions == NULL__null) { | |||
| 432 | rule = fInitialRule; | |||
| 433 | } else { | |||
| 434 | UDate tstart = getTransitionTime((Transition*)fHistoricTransitions->elementAt(0), | |||
| 435 | local, NonExistingTimeOpt, DuplicatedTimeOpt); | |||
| 436 | if (date < tstart) { | |||
| 437 | rule = fInitialRule; | |||
| 438 | } else { | |||
| 439 | int32_t idx = fHistoricTransitions->size() - 1; | |||
| 440 | UDate tend = getTransitionTime((Transition*)fHistoricTransitions->elementAt(idx), | |||
| 441 | local, NonExistingTimeOpt, DuplicatedTimeOpt); | |||
| 442 | if (date > tend) { | |||
| 443 | if (fFinalRules != NULL__null) { | |||
| 444 | rule = findRuleInFinal(date, local, NonExistingTimeOpt, DuplicatedTimeOpt); | |||
| 445 | } | |||
| 446 | if (rule == NULL__null) { | |||
| 447 | // no final rules or the given time is before the first transition | |||
| 448 | // specified by the final rules -> use the last rule | |||
| 449 | rule = ((Transition*)fHistoricTransitions->elementAt(idx))->to; | |||
| 450 | } | |||
| 451 | } else { | |||
| 452 | // Find a historical transition | |||
| 453 | while (idx >= 0) { | |||
| 454 | if (date >= getTransitionTime((Transition*)fHistoricTransitions->elementAt(idx), | |||
| 455 | local, NonExistingTimeOpt, DuplicatedTimeOpt)) { | |||
| 456 | break; | |||
| 457 | } | |||
| 458 | idx--; | |||
| 459 | } | |||
| 460 | rule = ((Transition*)fHistoricTransitions->elementAt(idx))->to; | |||
| 461 | } | |||
| 462 | } | |||
| 463 | } | |||
| 464 | if (rule != NULL__null) { | |||
| 465 | rawOffset = rule->getRawOffset(); | |||
| 466 | dstOffset = rule->getDSTSavings(); | |||
| 467 | } | |||
| 468 | } | |||
| 469 | ||||
| 470 | void | |||
| 471 | RuleBasedTimeZone::setRawOffset(int32_t /*offsetMillis*/) { | |||
| 472 | // We don't support this operation at this moment. | |||
| 473 | // Nothing to do! | |||
| 474 | } | |||
| 475 | ||||
| 476 | int32_t | |||
| 477 | RuleBasedTimeZone::getRawOffset(void) const { | |||
| 478 | // Note: This implementation returns standard GMT offset | |||
| 479 | // as of current time. | |||
| 480 | UErrorCode status = U_ZERO_ERROR; | |||
| 481 | int32_t raw, dst; | |||
| 482 | getOffset(uprv_getUTCtimeuprv_getUTCtime_71() * U_MILLIS_PER_SECOND(1000), | |||
| 483 | FALSE0, raw, dst, status); | |||
| 484 | return raw; | |||
| 485 | } | |||
| 486 | ||||
| 487 | UBool | |||
| 488 | RuleBasedTimeZone::useDaylightTime(void) const { | |||
| 489 | // Note: This implementation returns true when | |||
| 490 | // daylight saving time is used as of now or | |||
| 491 | // after the next transition. | |||
| 492 | UErrorCode status = U_ZERO_ERROR; | |||
| 493 | UDate now = uprv_getUTCtimeuprv_getUTCtime_71() * U_MILLIS_PER_SECOND(1000); | |||
| 494 | int32_t raw, dst; | |||
| 495 | getOffset(now, FALSE0, raw, dst, status); | |||
| 496 | if (dst != 0) { | |||
| 497 | return TRUE1; | |||
| 498 | } | |||
| 499 | // If DST is not used now, check if DST is used after the next transition | |||
| 500 | UDate time; | |||
| 501 | TimeZoneRule *from, *to; | |||
| 502 | UBool avail = findNext(now, FALSE0, time, from, to); | |||
| 503 | if (avail && to->getDSTSavings() != 0) { | |||
| 504 | return TRUE1; | |||
| 505 | } | |||
| 506 | return FALSE0; | |||
| 507 | } | |||
| 508 | ||||
| 509 | UBool | |||
| 510 | RuleBasedTimeZone::inDaylightTime(UDate date, UErrorCode& status) const { | |||
| 511 | if (U_FAILURE(status)) { | |||
| 512 | return FALSE0; | |||
| 513 | } | |||
| 514 | int32_t raw, dst; | |||
| 515 | getOffset(date, FALSE0, raw, dst, status); | |||
| 516 | if (dst != 0) { | |||
| 517 | return TRUE1; | |||
| 518 | } | |||
| 519 | return FALSE0; | |||
| 520 | } | |||
| 521 | ||||
| 522 | UBool | |||
| 523 | RuleBasedTimeZone::hasSameRules(const TimeZone& other) const { | |||
| 524 | if (this == &other) { | |||
| 525 | return TRUE1; | |||
| 526 | } | |||
| 527 | if (typeid(*this) != typeid(other)) { | |||
| 528 | return FALSE0; | |||
| 529 | } | |||
| 530 | const RuleBasedTimeZone& that = (const RuleBasedTimeZone&)other; | |||
| 531 | if (*fInitialRule != *(that.fInitialRule)) { | |||
| 532 | return FALSE0; | |||
| 533 | } | |||
| 534 | if (compareRules(fHistoricRules, that.fHistoricRules) | |||
| 535 | && compareRules(fFinalRules, that.fFinalRules)) { | |||
| 536 | return TRUE1; | |||
| 537 | } | |||
| 538 | return FALSE0; | |||
| 539 | } | |||
| 540 | ||||
| 541 | UBool | |||
| 542 | RuleBasedTimeZone::getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const { | |||
| 543 | UErrorCode status = U_ZERO_ERROR; | |||
| 544 | completeConst(status); | |||
| 545 | if (U_FAILURE(status)) { | |||
| 546 | return FALSE0; | |||
| 547 | } | |||
| 548 | UDate transitionTime; | |||
| 549 | TimeZoneRule *fromRule, *toRule; | |||
| 550 | UBool found = findNext(base, inclusive, transitionTime, fromRule, toRule); | |||
| 551 | if (found) { | |||
| 552 | result.setTime(transitionTime); | |||
| 553 | result.setFrom((const TimeZoneRule&)*fromRule); | |||
| 554 | result.setTo((const TimeZoneRule&)*toRule); | |||
| 555 | return TRUE1; | |||
| 556 | } | |||
| 557 | return FALSE0; | |||
| 558 | } | |||
| 559 | ||||
| 560 | UBool | |||
| 561 | RuleBasedTimeZone::getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const { | |||
| 562 | UErrorCode status = U_ZERO_ERROR; | |||
| 563 | completeConst(status); | |||
| 564 | if (U_FAILURE(status)) { | |||
| 565 | return FALSE0; | |||
| 566 | } | |||
| 567 | UDate transitionTime; | |||
| 568 | TimeZoneRule *fromRule, *toRule; | |||
| 569 | UBool found = findPrev(base, inclusive, transitionTime, fromRule, toRule); | |||
| 570 | if (found) { | |||
| 571 | result.setTime(transitionTime); | |||
| 572 | result.setFrom((const TimeZoneRule&)*fromRule); | |||
| 573 | result.setTo((const TimeZoneRule&)*toRule); | |||
| 574 | return TRUE1; | |||
| 575 | } | |||
| 576 | return FALSE0; | |||
| 577 | } | |||
| 578 | ||||
| 579 | int32_t | |||
| 580 | RuleBasedTimeZone::countTransitionRules(UErrorCode& /*status*/) const { | |||
| 581 | int32_t count = 0; | |||
| 582 | if (fHistoricRules != NULL__null) { | |||
| 583 | count += fHistoricRules->size(); | |||
| 584 | } | |||
| 585 | if (fFinalRules != NULL__null) { | |||
| 586 | count += fFinalRules->size(); | |||
| 587 | } | |||
| 588 | return count; | |||
| 589 | } | |||
| 590 | ||||
| 591 | void | |||
| 592 | RuleBasedTimeZone::getTimeZoneRules(const InitialTimeZoneRule*& initial, | |||
| 593 | const TimeZoneRule* trsrules[], | |||
| 594 | int32_t& trscount, | |||
| 595 | UErrorCode& status) const { | |||
| 596 | if (U_FAILURE(status)) { | |||
| 597 | return; | |||
| 598 | } | |||
| 599 | // Initial rule | |||
| 600 | initial = fInitialRule; | |||
| 601 | ||||
| 602 | // Transition rules | |||
| 603 | int32_t cnt = 0; | |||
| 604 | int32_t idx; | |||
| 605 | if (fHistoricRules != NULL__null && cnt < trscount) { | |||
| 606 | int32_t historicCount = fHistoricRules->size(); | |||
| 607 | idx = 0; | |||
| 608 | while (cnt < trscount && idx < historicCount) { | |||
| 609 | trsrules[cnt++] = (const TimeZoneRule*)fHistoricRules->elementAt(idx++); | |||
| 610 | } | |||
| 611 | } | |||
| 612 | if (fFinalRules != NULL__null && cnt < trscount) { | |||
| 613 | int32_t finalCount = fFinalRules->size(); | |||
| 614 | idx = 0; | |||
| 615 | while (cnt < trscount && idx < finalCount) { | |||
| 616 | trsrules[cnt++] = (const TimeZoneRule*)fFinalRules->elementAt(idx++); | |||
| 617 | } | |||
| 618 | } | |||
| 619 | // Set the result length | |||
| 620 | trscount = cnt; | |||
| 621 | } | |||
| 622 | ||||
| 623 | void | |||
| 624 | RuleBasedTimeZone::deleteRules(void) { | |||
| 625 | delete fInitialRule; | |||
| 626 | fInitialRule = NULL__null; | |||
| 627 | if (fHistoricRules != NULL__null) { | |||
| 628 | delete fHistoricRules; | |||
| 629 | fHistoricRules = NULL__null; | |||
| 630 | } | |||
| 631 | if (fFinalRules != NULL__null) { | |||
| 632 | delete fFinalRules; | |||
| 633 | fFinalRules = NULL__null; | |||
| 634 | } | |||
| 635 | } | |||
| 636 | ||||
| 637 | void | |||
| 638 | RuleBasedTimeZone::deleteTransitions(void) { | |||
| 639 | if (fHistoricTransitions != NULL__null) { | |||
| 640 | delete fHistoricTransitions; | |||
| 641 | } | |||
| 642 | fHistoricTransitions = NULL__null; | |||
| 643 | } | |||
| 644 | ||||
| 645 | UVector* | |||
| 646 | RuleBasedTimeZone::copyRules(UVector* source) { | |||
| 647 | if (source == nullptr) { | |||
| 648 | return nullptr; | |||
| 649 | } | |||
| 650 | UErrorCode ec = U_ZERO_ERROR; | |||
| 651 | int32_t size = source->size(); | |||
| 652 | LocalPointer<UVector> rules(new UVector(uprv_deleteUObjectuprv_deleteUObject_71, nullptr, size, ec), ec); | |||
| 653 | if (U_FAILURE(ec)) { | |||
| 654 | return nullptr; | |||
| 655 | } | |||
| 656 | int32_t i; | |||
| 657 | for (i = 0; i < size; i++) { | |||
| 658 | LocalPointer<TimeZoneRule> rule(((TimeZoneRule*)source->elementAt(i))->clone(), ec); | |||
| 659 | rules->adoptElement(rule.orphan(), ec); | |||
| 660 | if (U_FAILURE(ec)) { | |||
| 661 | return nullptr; | |||
| 662 | } | |||
| 663 | } | |||
| 664 | return rules.orphan(); | |||
| 665 | } | |||
| 666 | ||||
| 667 | TimeZoneRule* | |||
| 668 | RuleBasedTimeZone::findRuleInFinal(UDate date, UBool local, | |||
| 669 | int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const { | |||
| 670 | if (fFinalRules == NULL__null) { | |||
| 671 | return NULL__null; | |||
| 672 | } | |||
| 673 | ||||
| 674 | AnnualTimeZoneRule* fr0 = (AnnualTimeZoneRule*)fFinalRules->elementAt(0); | |||
| 675 | AnnualTimeZoneRule* fr1 = (AnnualTimeZoneRule*)fFinalRules->elementAt(1); | |||
| 676 | if (fr0 == NULL__null || fr1 == NULL__null) { | |||
| 677 | return NULL__null; | |||
| 678 | } | |||
| 679 | ||||
| 680 | UDate start0, start1; | |||
| 681 | UDate base; | |||
| 682 | int32_t localDelta; | |||
| 683 | ||||
| 684 | base = date; | |||
| 685 | if (local) { | |||
| 686 | localDelta = getLocalDelta(fr1->getRawOffset(), fr1->getDSTSavings(), | |||
| 687 | fr0->getRawOffset(), fr0->getDSTSavings(), | |||
| 688 | NonExistingTimeOpt, DuplicatedTimeOpt); | |||
| 689 | base -= localDelta; | |||
| 690 | } | |||
| 691 | UBool avail0 = fr0->getPreviousStart(base, fr1->getRawOffset(), fr1->getDSTSavings(), TRUE1, start0); | |||
| 692 | ||||
| 693 | base = date; | |||
| 694 | if (local) { | |||
| 695 | localDelta = getLocalDelta(fr0->getRawOffset(), fr0->getDSTSavings(), | |||
| 696 | fr1->getRawOffset(), fr1->getDSTSavings(), | |||
| 697 | NonExistingTimeOpt, DuplicatedTimeOpt); | |||
| 698 | base -= localDelta; | |||
| 699 | } | |||
| 700 | UBool avail1 = fr1->getPreviousStart(base, fr0->getRawOffset(), fr0->getDSTSavings(), TRUE1, start1); | |||
| 701 | ||||
| 702 | if (!avail0 || !avail1) { | |||
| 703 | if (avail0) { | |||
| 704 | return fr0; | |||
| 705 | } else if (avail1) { | |||
| 706 | return fr1; | |||
| 707 | } | |||
| 708 | // Both rules take effect after the given time | |||
| 709 | return NULL__null; | |||
| 710 | } | |||
| 711 | ||||
| 712 | return (start0 > start1) ? fr0 : fr1; | |||
| 713 | } | |||
| 714 | ||||
| 715 | UBool | |||
| 716 | RuleBasedTimeZone::findNext(UDate base, UBool inclusive, UDate& transitionTime, | |||
| 717 | TimeZoneRule*& fromRule, TimeZoneRule*& toRule) const { | |||
| 718 | if (fHistoricTransitions == NULL__null) { | |||
| 719 | return FALSE0; | |||
| 720 | } | |||
| 721 | UBool isFinal = FALSE0; | |||
| 722 | UBool found = FALSE0; | |||
| 723 | Transition result; | |||
| 724 | Transition *tzt = (Transition*)fHistoricTransitions->elementAt(0); | |||
| 725 | UDate tt = tzt->time; | |||
| 726 | if (tt > base || (inclusive && tt == base)) { | |||
| 727 | result = *tzt; | |||
| 728 | found = TRUE1; | |||
| 729 | } else { | |||
| 730 | int32_t idx = fHistoricTransitions->size() - 1; | |||
| 731 | tzt = (Transition*)fHistoricTransitions->elementAt(idx); | |||
| 732 | tt = tzt->time; | |||
| 733 | if (inclusive && tt == base) { | |||
| 734 | result = *tzt; | |||
| 735 | found = TRUE1; | |||
| 736 | } else if (tt <= base) { | |||
| 737 | if (fFinalRules != NULL__null) { | |||
| 738 | // Find a transion time with finalRules | |||
| 739 | TimeZoneRule *r0 = (TimeZoneRule*)fFinalRules->elementAt(0); | |||
| 740 | TimeZoneRule *r1 = (TimeZoneRule*)fFinalRules->elementAt(1); | |||
| 741 | UDate start0, start1; | |||
| 742 | UBool avail0 = r0->getNextStart(base, r1->getRawOffset(), r1->getDSTSavings(), inclusive, start0); | |||
| 743 | UBool avail1 = r1->getNextStart(base, r0->getRawOffset(), r0->getDSTSavings(), inclusive, start1); | |||
| 744 | // avail0/avail1 should be always TRUE | |||
| 745 | if (!avail0 && !avail1) { | |||
| 746 | return FALSE0; | |||
| 747 | } | |||
| 748 | if (!avail1 || start0 < start1) { | |||
| 749 | result.time = start0; | |||
| 750 | result.from = r1; | |||
| 751 | result.to = r0; | |||
| 752 | } else { | |||
| 753 | result.time = start1; | |||
| 754 | result.from = r0; | |||
| 755 | result.to = r1; | |||
| 756 | } | |||
| 757 | isFinal = TRUE1; | |||
| 758 | found = TRUE1; | |||
| 759 | } | |||
| 760 | } else { | |||
| 761 | // Find a transition within the historic transitions | |||
| 762 | idx--; | |||
| 763 | Transition *prev = tzt; | |||
| 764 | while (idx > 0) { | |||
| 765 | tzt = (Transition*)fHistoricTransitions->elementAt(idx); | |||
| 766 | tt = tzt->time; | |||
| 767 | if (tt < base || (!inclusive && tt == base)) { | |||
| 768 | break; | |||
| 769 | } | |||
| 770 | idx--; | |||
| 771 | prev = tzt; | |||
| 772 | } | |||
| 773 | result.time = prev->time; | |||
| 774 | result.from = prev->from; | |||
| 775 | result.to = prev->to; | |||
| 776 | found = TRUE1; | |||
| 777 | } | |||
| 778 | } | |||
| 779 | if (found) { | |||
| 780 | // For now, this implementation ignore transitions with only zone name changes. | |||
| 781 | if (result.from->getRawOffset() == result.to->getRawOffset() | |||
| 782 | && result.from->getDSTSavings() == result.to->getDSTSavings()) { | |||
| 783 | if (isFinal) { | |||
| 784 | return FALSE0; | |||
| 785 | } else { | |||
| 786 | // No offset changes. Try next one if not final | |||
| 787 | return findNext(result.time, FALSE0 /* always exclusive */, | |||
| 788 | transitionTime, fromRule, toRule); | |||
| 789 | } | |||
| 790 | } | |||
| 791 | transitionTime = result.time; | |||
| 792 | fromRule = result.from; | |||
| 793 | toRule = result.to; | |||
| 794 | return TRUE1; | |||
| 795 | } | |||
| 796 | return FALSE0; | |||
| 797 | } | |||
| 798 | ||||
| 799 | UBool | |||
| 800 | RuleBasedTimeZone::findPrev(UDate base, UBool inclusive, UDate& transitionTime, | |||
| 801 | TimeZoneRule*& fromRule, TimeZoneRule*& toRule) const { | |||
| 802 | if (fHistoricTransitions == NULL__null) { | |||
| 803 | return FALSE0; | |||
| 804 | } | |||
| 805 | UBool found = FALSE0; | |||
| 806 | Transition result; | |||
| 807 | Transition *tzt = (Transition*)fHistoricTransitions->elementAt(0); | |||
| 808 | UDate tt = tzt->time; | |||
| 809 | if (inclusive && tt == base) { | |||
| 810 | result = *tzt; | |||
| 811 | found = TRUE1; | |||
| 812 | } else if (tt < base) { | |||
| 813 | int32_t idx = fHistoricTransitions->size() - 1; | |||
| 814 | tzt = (Transition*)fHistoricTransitions->elementAt(idx); | |||
| 815 | tt = tzt->time; | |||
| 816 | if (inclusive && tt == base) { | |||
| 817 | result = *tzt; | |||
| 818 | found = TRUE1; | |||
| 819 | } else if (tt < base) { | |||
| 820 | if (fFinalRules != NULL__null) { | |||
| 821 | // Find a transion time with finalRules | |||
| 822 | TimeZoneRule *r0 = (TimeZoneRule*)fFinalRules->elementAt(0); | |||
| 823 | TimeZoneRule *r1 = (TimeZoneRule*)fFinalRules->elementAt(1); | |||
| 824 | UDate start0, start1; | |||
| 825 | UBool avail0 = r0->getPreviousStart(base, r1->getRawOffset(), r1->getDSTSavings(), inclusive, start0); | |||
| 826 | UBool avail1 = r1->getPreviousStart(base, r0->getRawOffset(), r0->getDSTSavings(), inclusive, start1); | |||
| 827 | // avail0/avail1 should be always TRUE | |||
| 828 | if (!avail0 && !avail1) { | |||
| 829 | return FALSE0; | |||
| 830 | } | |||
| 831 | if (!avail1 || start0 > start1) { | |||
| 832 | result.time = start0; | |||
| 833 | result.from = r1; | |||
| 834 | result.to = r0; | |||
| 835 | } else { | |||
| 836 | result.time = start1; | |||
| 837 | result.from = r0; | |||
| 838 | result.to = r1; | |||
| 839 | } | |||
| 840 | } else { | |||
| 841 | result = *tzt; | |||
| 842 | } | |||
| 843 | found = TRUE1; | |||
| 844 | } else { | |||
| 845 | // Find a transition within the historic transitions | |||
| 846 | idx--; | |||
| 847 | while (idx >= 0) { | |||
| 848 | tzt = (Transition*)fHistoricTransitions->elementAt(idx); | |||
| 849 | tt = tzt->time; | |||
| 850 | if (tt < base || (inclusive && tt == base)) { | |||
| 851 | break; | |||
| 852 | } | |||
| 853 | idx--; | |||
| 854 | } | |||
| 855 | result = *tzt; | |||
| 856 | found = TRUE1; | |||
| 857 | } | |||
| 858 | } | |||
| 859 | if (found) { | |||
| 860 | // For now, this implementation ignore transitions with only zone name changes. | |||
| 861 | if (result.from->getRawOffset() == result.to->getRawOffset() | |||
| 862 | && result.from->getDSTSavings() == result.to->getDSTSavings()) { | |||
| 863 | // No offset changes. Try next one if not final | |||
| 864 | return findPrev(result.time, FALSE0 /* always exclusive */, | |||
| 865 | transitionTime, fromRule, toRule); | |||
| 866 | } | |||
| 867 | transitionTime = result.time; | |||
| 868 | fromRule = result.from; | |||
| 869 | toRule = result.to; | |||
| 870 | return TRUE1; | |||
| 871 | } | |||
| 872 | return FALSE0; | |||
| 873 | } | |||
| 874 | ||||
| 875 | UDate | |||
| 876 | RuleBasedTimeZone::getTransitionTime(Transition* transition, UBool local, | |||
| 877 | int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const { | |||
| 878 | UDate time = transition->time; | |||
| 879 | if (local) { | |||
| 880 | time += getLocalDelta(transition->from->getRawOffset(), transition->from->getDSTSavings(), | |||
| 881 | transition->to->getRawOffset(), transition->to->getDSTSavings(), | |||
| 882 | NonExistingTimeOpt, DuplicatedTimeOpt); | |||
| 883 | } | |||
| 884 | return time; | |||
| 885 | } | |||
| 886 | ||||
| 887 | int32_t | |||
| 888 | RuleBasedTimeZone::getLocalDelta(int32_t rawBefore, int32_t dstBefore, int32_t rawAfter, int32_t dstAfter, | |||
| 889 | int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const { | |||
| 890 | int32_t delta = 0; | |||
| 891 | ||||
| 892 | int32_t offsetBefore = rawBefore + dstBefore; | |||
| 893 | int32_t offsetAfter = rawAfter + dstAfter; | |||
| 894 | ||||
| 895 | UBool dstToStd = (dstBefore != 0) && (dstAfter == 0); | |||
| 896 | UBool stdToDst = (dstBefore == 0) && (dstAfter != 0); | |||
| 897 | ||||
| 898 | if (offsetAfter - offsetBefore >= 0) { | |||
| 899 | // Positive transition, which makes a non-existing local time range | |||
| 900 | if (((NonExistingTimeOpt & kStdDstMask) == kStandard && dstToStd) | |||
| 901 | || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && stdToDst)) { | |||
| 902 | delta = offsetBefore; | |||
| 903 | } else if (((NonExistingTimeOpt & kStdDstMask) == kStandard && stdToDst) | |||
| 904 | || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && dstToStd)) { | |||
| 905 | delta = offsetAfter; | |||
| 906 | } else if ((NonExistingTimeOpt & kFormerLatterMask) == kLatter) { | |||
| 907 | delta = offsetBefore; | |||
| 908 | } else { | |||
| 909 | // Interprets the time with rule before the transition, | |||
| 910 | // default for non-existing time range | |||
| 911 | delta = offsetAfter; | |||
| 912 | } | |||
| 913 | } else { | |||
| 914 | // Negative transition, which makes a duplicated local time range | |||
| 915 | if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && dstToStd) | |||
| 916 | || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && stdToDst)) { | |||
| 917 | delta = offsetAfter; | |||
| 918 | } else if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && stdToDst) | |||
| 919 | || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && dstToStd)) { | |||
| 920 | delta = offsetBefore; | |||
| 921 | } else if ((DuplicatedTimeOpt & kFormerLatterMask) == kFormer) { | |||
| 922 | delta = offsetBefore; | |||
| 923 | } else { | |||
| 924 | // Interprets the time with rule after the transition, | |||
| 925 | // default for duplicated local time range | |||
| 926 | delta = offsetAfter; | |||
| 927 | } | |||
| 928 | } | |||
| 929 | return delta; | |||
| 930 | } | |||
| 931 | ||||
| 932 | U_NAMESPACE_END} | |||
| 933 | ||||
| 934 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |||
| 935 | ||||
| 936 | //eof | |||
| 937 |