Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ********************************************************************** |
michael@0 | 3 | * Copyright (c) 2003-2013, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ********************************************************************** |
michael@0 | 6 | * Author: Alan Liu |
michael@0 | 7 | * Created: July 21 2003 |
michael@0 | 8 | * Since: ICU 2.8 |
michael@0 | 9 | ********************************************************************** |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #include "utypeinfo.h" // for 'typeid' to work |
michael@0 | 13 | |
michael@0 | 14 | #include "olsontz.h" |
michael@0 | 15 | |
michael@0 | 16 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 17 | |
michael@0 | 18 | #include "unicode/ures.h" |
michael@0 | 19 | #include "unicode/simpletz.h" |
michael@0 | 20 | #include "unicode/gregocal.h" |
michael@0 | 21 | #include "gregoimp.h" |
michael@0 | 22 | #include "cmemory.h" |
michael@0 | 23 | #include "uassert.h" |
michael@0 | 24 | #include "uvector.h" |
michael@0 | 25 | #include <float.h> // DBL_MAX |
michael@0 | 26 | #include "uresimp.h" // struct UResourceBundle |
michael@0 | 27 | #include "zonemeta.h" |
michael@0 | 28 | #include "umutex.h" |
michael@0 | 29 | |
michael@0 | 30 | #ifdef U_DEBUG_TZ |
michael@0 | 31 | # include <stdio.h> |
michael@0 | 32 | # include "uresimp.h" // for debugging |
michael@0 | 33 | |
michael@0 | 34 | static void debug_tz_loc(const char *f, int32_t l) |
michael@0 | 35 | { |
michael@0 | 36 | fprintf(stderr, "%s:%d: ", f, l); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | static void debug_tz_msg(const char *pat, ...) |
michael@0 | 40 | { |
michael@0 | 41 | va_list ap; |
michael@0 | 42 | va_start(ap, pat); |
michael@0 | 43 | vfprintf(stderr, pat, ap); |
michael@0 | 44 | fflush(stderr); |
michael@0 | 45 | } |
michael@0 | 46 | // must use double parens, i.e.: U_DEBUG_TZ_MSG(("four is: %d",4)); |
michael@0 | 47 | #define U_DEBUG_TZ_MSG(x) {debug_tz_loc(__FILE__,__LINE__);debug_tz_msg x;} |
michael@0 | 48 | #else |
michael@0 | 49 | #define U_DEBUG_TZ_MSG(x) |
michael@0 | 50 | #endif |
michael@0 | 51 | |
michael@0 | 52 | static UBool arrayEqual(const void *a1, const void *a2, int32_t size) { |
michael@0 | 53 | if (a1 == NULL && a2 == NULL) { |
michael@0 | 54 | return TRUE; |
michael@0 | 55 | } |
michael@0 | 56 | if ((a1 != NULL && a2 == NULL) || (a1 == NULL && a2 != NULL)) { |
michael@0 | 57 | return FALSE; |
michael@0 | 58 | } |
michael@0 | 59 | if (a1 == a2) { |
michael@0 | 60 | return TRUE; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | return (uprv_memcmp(a1, a2, size) == 0); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | U_NAMESPACE_BEGIN |
michael@0 | 67 | |
michael@0 | 68 | #define kTRANS "trans" |
michael@0 | 69 | #define kTRANSPRE32 "transPre32" |
michael@0 | 70 | #define kTRANSPOST32 "transPost32" |
michael@0 | 71 | #define kTYPEOFFSETS "typeOffsets" |
michael@0 | 72 | #define kTYPEMAP "typeMap" |
michael@0 | 73 | #define kLINKS "links" |
michael@0 | 74 | #define kFINALRULE "finalRule" |
michael@0 | 75 | #define kFINALRAW "finalRaw" |
michael@0 | 76 | #define kFINALYEAR "finalYear" |
michael@0 | 77 | |
michael@0 | 78 | #define SECONDS_PER_DAY (24*60*60) |
michael@0 | 79 | |
michael@0 | 80 | static const int32_t ZEROS[] = {0,0}; |
michael@0 | 81 | |
michael@0 | 82 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(OlsonTimeZone) |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Default constructor. Creates a time zone with an empty ID and |
michael@0 | 86 | * a fixed GMT offset of zero. |
michael@0 | 87 | */ |
michael@0 | 88 | /*OlsonTimeZone::OlsonTimeZone() : finalYear(INT32_MAX), finalMillis(DBL_MAX), finalZone(0), transitionRulesInitialized(FALSE) { |
michael@0 | 89 | clearTransitionRules(); |
michael@0 | 90 | constructEmpty(); |
michael@0 | 91 | }*/ |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Construct a GMT+0 zone with no transitions. This is done when a |
michael@0 | 95 | * constructor fails so the resultant object is well-behaved. |
michael@0 | 96 | */ |
michael@0 | 97 | void OlsonTimeZone::constructEmpty() { |
michael@0 | 98 | canonicalID = NULL; |
michael@0 | 99 | |
michael@0 | 100 | transitionCountPre32 = transitionCount32 = transitionCountPost32 = 0; |
michael@0 | 101 | transitionTimesPre32 = transitionTimes32 = transitionTimesPost32 = NULL; |
michael@0 | 102 | |
michael@0 | 103 | typeMapData = NULL; |
michael@0 | 104 | |
michael@0 | 105 | typeCount = 1; |
michael@0 | 106 | typeOffsets = ZEROS; |
michael@0 | 107 | |
michael@0 | 108 | finalZone = NULL; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | /** |
michael@0 | 112 | * Construct from a resource bundle |
michael@0 | 113 | * @param top the top-level zoneinfo resource bundle. This is used |
michael@0 | 114 | * to lookup the rule that `res' may refer to, if there is one. |
michael@0 | 115 | * @param res the resource bundle of the zone to be constructed |
michael@0 | 116 | * @param ec input-output error code |
michael@0 | 117 | */ |
michael@0 | 118 | OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top, |
michael@0 | 119 | const UResourceBundle* res, |
michael@0 | 120 | const UnicodeString& tzid, |
michael@0 | 121 | UErrorCode& ec) : |
michael@0 | 122 | BasicTimeZone(tzid), finalZone(NULL) |
michael@0 | 123 | { |
michael@0 | 124 | clearTransitionRules(); |
michael@0 | 125 | U_DEBUG_TZ_MSG(("OlsonTimeZone(%s)\n", ures_getKey((UResourceBundle*)res))); |
michael@0 | 126 | if ((top == NULL || res == NULL) && U_SUCCESS(ec)) { |
michael@0 | 127 | ec = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 128 | } |
michael@0 | 129 | if (U_SUCCESS(ec)) { |
michael@0 | 130 | // TODO -- clean up -- Doesn't work if res points to an alias |
michael@0 | 131 | // // TODO remove nonconst casts below when ures_* API is fixed |
michael@0 | 132 | // setID(ures_getKey((UResourceBundle*) res)); // cast away const |
michael@0 | 133 | |
michael@0 | 134 | int32_t len; |
michael@0 | 135 | UResourceBundle r; |
michael@0 | 136 | ures_initStackObject(&r); |
michael@0 | 137 | |
michael@0 | 138 | // Pre-32bit second transitions |
michael@0 | 139 | ures_getByKey(res, kTRANSPRE32, &r, &ec); |
michael@0 | 140 | transitionTimesPre32 = ures_getIntVector(&r, &len, &ec); |
michael@0 | 141 | transitionCountPre32 = len >> 1; |
michael@0 | 142 | if (ec == U_MISSING_RESOURCE_ERROR) { |
michael@0 | 143 | // No pre-32bit transitions |
michael@0 | 144 | transitionTimesPre32 = NULL; |
michael@0 | 145 | transitionCountPre32 = 0; |
michael@0 | 146 | ec = U_ZERO_ERROR; |
michael@0 | 147 | } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF || (len & 1) != 0) /* len must be even */) { |
michael@0 | 148 | ec = U_INVALID_FORMAT_ERROR; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | // 32bit second transitions |
michael@0 | 152 | ures_getByKey(res, kTRANS, &r, &ec); |
michael@0 | 153 | transitionTimes32 = ures_getIntVector(&r, &len, &ec); |
michael@0 | 154 | transitionCount32 = len; |
michael@0 | 155 | if (ec == U_MISSING_RESOURCE_ERROR) { |
michael@0 | 156 | // No 32bit transitions |
michael@0 | 157 | transitionTimes32 = NULL; |
michael@0 | 158 | transitionCount32 = 0; |
michael@0 | 159 | ec = U_ZERO_ERROR; |
michael@0 | 160 | } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF)) { |
michael@0 | 161 | ec = U_INVALID_FORMAT_ERROR; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | // Post-32bit second transitions |
michael@0 | 165 | ures_getByKey(res, kTRANSPOST32, &r, &ec); |
michael@0 | 166 | transitionTimesPost32 = ures_getIntVector(&r, &len, &ec); |
michael@0 | 167 | transitionCountPost32 = len >> 1; |
michael@0 | 168 | if (ec == U_MISSING_RESOURCE_ERROR) { |
michael@0 | 169 | // No pre-32bit transitions |
michael@0 | 170 | transitionTimesPost32 = NULL; |
michael@0 | 171 | transitionCountPost32 = 0; |
michael@0 | 172 | ec = U_ZERO_ERROR; |
michael@0 | 173 | } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF || (len & 1) != 0) /* len must be even */) { |
michael@0 | 174 | ec = U_INVALID_FORMAT_ERROR; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | // Type offsets list must be of even size, with size >= 2 |
michael@0 | 178 | ures_getByKey(res, kTYPEOFFSETS, &r, &ec); |
michael@0 | 179 | typeOffsets = ures_getIntVector(&r, &len, &ec); |
michael@0 | 180 | if (U_SUCCESS(ec) && (len < 2 || len > 0x7FFE || (len & 1) != 0)) { |
michael@0 | 181 | ec = U_INVALID_FORMAT_ERROR; |
michael@0 | 182 | } |
michael@0 | 183 | typeCount = (int16_t) len >> 1; |
michael@0 | 184 | |
michael@0 | 185 | // Type map data must be of the same size as the transition count |
michael@0 | 186 | typeMapData = NULL; |
michael@0 | 187 | if (transitionCount() > 0) { |
michael@0 | 188 | ures_getByKey(res, kTYPEMAP, &r, &ec); |
michael@0 | 189 | typeMapData = ures_getBinary(&r, &len, &ec); |
michael@0 | 190 | if (ec == U_MISSING_RESOURCE_ERROR) { |
michael@0 | 191 | // no type mapping data |
michael@0 | 192 | ec = U_INVALID_FORMAT_ERROR; |
michael@0 | 193 | } else if (U_SUCCESS(ec) && len != transitionCount()) { |
michael@0 | 194 | ec = U_INVALID_FORMAT_ERROR; |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | // Process final rule and data, if any |
michael@0 | 199 | const UChar *ruleIdUStr = ures_getStringByKey(res, kFINALRULE, &len, &ec); |
michael@0 | 200 | ures_getByKey(res, kFINALRAW, &r, &ec); |
michael@0 | 201 | int32_t ruleRaw = ures_getInt(&r, &ec); |
michael@0 | 202 | ures_getByKey(res, kFINALYEAR, &r, &ec); |
michael@0 | 203 | int32_t ruleYear = ures_getInt(&r, &ec); |
michael@0 | 204 | if (U_SUCCESS(ec)) { |
michael@0 | 205 | UnicodeString ruleID(TRUE, ruleIdUStr, len); |
michael@0 | 206 | UResourceBundle *rule = TimeZone::loadRule(top, ruleID, NULL, ec); |
michael@0 | 207 | const int32_t *ruleData = ures_getIntVector(rule, &len, &ec); |
michael@0 | 208 | if (U_SUCCESS(ec) && len == 11) { |
michael@0 | 209 | UnicodeString emptyStr; |
michael@0 | 210 | finalZone = new SimpleTimeZone( |
michael@0 | 211 | ruleRaw * U_MILLIS_PER_SECOND, |
michael@0 | 212 | emptyStr, |
michael@0 | 213 | (int8_t)ruleData[0], (int8_t)ruleData[1], (int8_t)ruleData[2], |
michael@0 | 214 | ruleData[3] * U_MILLIS_PER_SECOND, |
michael@0 | 215 | (SimpleTimeZone::TimeMode) ruleData[4], |
michael@0 | 216 | (int8_t)ruleData[5], (int8_t)ruleData[6], (int8_t)ruleData[7], |
michael@0 | 217 | ruleData[8] * U_MILLIS_PER_SECOND, |
michael@0 | 218 | (SimpleTimeZone::TimeMode) ruleData[9], |
michael@0 | 219 | ruleData[10] * U_MILLIS_PER_SECOND, ec); |
michael@0 | 220 | if (finalZone == NULL) { |
michael@0 | 221 | ec = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 222 | } else { |
michael@0 | 223 | finalStartYear = ruleYear; |
michael@0 | 224 | |
michael@0 | 225 | // Note: Setting finalStartYear to the finalZone is problematic. When a date is around |
michael@0 | 226 | // year boundary, SimpleTimeZone may return false result when DST is observed at the |
michael@0 | 227 | // beginning of year. We could apply safe margin (day or two), but when one of recurrent |
michael@0 | 228 | // rules falls around year boundary, it could return false result. Without setting the |
michael@0 | 229 | // start year, finalZone works fine around the year boundary of the start year. |
michael@0 | 230 | |
michael@0 | 231 | // finalZone->setStartYear(finalStartYear); |
michael@0 | 232 | |
michael@0 | 233 | |
michael@0 | 234 | // Compute the millis for Jan 1, 0:00 GMT of the finalYear |
michael@0 | 235 | |
michael@0 | 236 | // Note: finalStartMillis is used for detecting either if |
michael@0 | 237 | // historic transition data or finalZone to be used. In an |
michael@0 | 238 | // extreme edge case - for example, two transitions fall into |
michael@0 | 239 | // small windows of time around the year boundary, this may |
michael@0 | 240 | // result incorrect offset computation. But I think it will |
michael@0 | 241 | // never happen practically. Yoshito - Feb 20, 2010 |
michael@0 | 242 | finalStartMillis = Grego::fieldsToDay(finalStartYear, 0, 1) * U_MILLIS_PER_DAY; |
michael@0 | 243 | } |
michael@0 | 244 | } else { |
michael@0 | 245 | ec = U_INVALID_FORMAT_ERROR; |
michael@0 | 246 | } |
michael@0 | 247 | ures_close(rule); |
michael@0 | 248 | } else if (ec == U_MISSING_RESOURCE_ERROR) { |
michael@0 | 249 | // No final zone |
michael@0 | 250 | ec = U_ZERO_ERROR; |
michael@0 | 251 | } |
michael@0 | 252 | ures_close(&r); |
michael@0 | 253 | |
michael@0 | 254 | // initialize canonical ID |
michael@0 | 255 | canonicalID = ZoneMeta::getCanonicalCLDRID(tzid, ec); |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | if (U_FAILURE(ec)) { |
michael@0 | 259 | constructEmpty(); |
michael@0 | 260 | } |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | /** |
michael@0 | 264 | * Copy constructor |
michael@0 | 265 | */ |
michael@0 | 266 | OlsonTimeZone::OlsonTimeZone(const OlsonTimeZone& other) : |
michael@0 | 267 | BasicTimeZone(other), finalZone(0) { |
michael@0 | 268 | *this = other; |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | /** |
michael@0 | 272 | * Assignment operator |
michael@0 | 273 | */ |
michael@0 | 274 | OlsonTimeZone& OlsonTimeZone::operator=(const OlsonTimeZone& other) { |
michael@0 | 275 | canonicalID = other.canonicalID; |
michael@0 | 276 | |
michael@0 | 277 | transitionTimesPre32 = other.transitionTimesPre32; |
michael@0 | 278 | transitionTimes32 = other.transitionTimes32; |
michael@0 | 279 | transitionTimesPost32 = other.transitionTimesPost32; |
michael@0 | 280 | |
michael@0 | 281 | transitionCountPre32 = other.transitionCountPre32; |
michael@0 | 282 | transitionCount32 = other.transitionCount32; |
michael@0 | 283 | transitionCountPost32 = other.transitionCountPost32; |
michael@0 | 284 | |
michael@0 | 285 | typeCount = other.typeCount; |
michael@0 | 286 | typeOffsets = other.typeOffsets; |
michael@0 | 287 | typeMapData = other.typeMapData; |
michael@0 | 288 | |
michael@0 | 289 | delete finalZone; |
michael@0 | 290 | finalZone = (other.finalZone != 0) ? |
michael@0 | 291 | (SimpleTimeZone*) other.finalZone->clone() : 0; |
michael@0 | 292 | |
michael@0 | 293 | finalStartYear = other.finalStartYear; |
michael@0 | 294 | finalStartMillis = other.finalStartMillis; |
michael@0 | 295 | |
michael@0 | 296 | clearTransitionRules(); |
michael@0 | 297 | |
michael@0 | 298 | return *this; |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | /** |
michael@0 | 302 | * Destructor |
michael@0 | 303 | */ |
michael@0 | 304 | OlsonTimeZone::~OlsonTimeZone() { |
michael@0 | 305 | deleteTransitionRules(); |
michael@0 | 306 | delete finalZone; |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | /** |
michael@0 | 310 | * Returns true if the two TimeZone objects are equal. |
michael@0 | 311 | */ |
michael@0 | 312 | UBool OlsonTimeZone::operator==(const TimeZone& other) const { |
michael@0 | 313 | return ((this == &other) || |
michael@0 | 314 | (typeid(*this) == typeid(other) && |
michael@0 | 315 | TimeZone::operator==(other) && |
michael@0 | 316 | hasSameRules(other))); |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | /** |
michael@0 | 320 | * TimeZone API. |
michael@0 | 321 | */ |
michael@0 | 322 | TimeZone* OlsonTimeZone::clone() const { |
michael@0 | 323 | return new OlsonTimeZone(*this); |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | /** |
michael@0 | 327 | * TimeZone API. |
michael@0 | 328 | */ |
michael@0 | 329 | int32_t OlsonTimeZone::getOffset(uint8_t era, int32_t year, int32_t month, |
michael@0 | 330 | int32_t dom, uint8_t dow, |
michael@0 | 331 | int32_t millis, UErrorCode& ec) const { |
michael@0 | 332 | if (month < UCAL_JANUARY || month > UCAL_DECEMBER) { |
michael@0 | 333 | if (U_SUCCESS(ec)) { |
michael@0 | 334 | ec = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 335 | } |
michael@0 | 336 | return 0; |
michael@0 | 337 | } else { |
michael@0 | 338 | return getOffset(era, year, month, dom, dow, millis, |
michael@0 | 339 | Grego::monthLength(year, month), |
michael@0 | 340 | ec); |
michael@0 | 341 | } |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | /** |
michael@0 | 345 | * TimeZone API. |
michael@0 | 346 | */ |
michael@0 | 347 | int32_t OlsonTimeZone::getOffset(uint8_t era, int32_t year, int32_t month, |
michael@0 | 348 | int32_t dom, uint8_t dow, |
michael@0 | 349 | int32_t millis, int32_t monthLength, |
michael@0 | 350 | UErrorCode& ec) const { |
michael@0 | 351 | if (U_FAILURE(ec)) { |
michael@0 | 352 | return 0; |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | if ((era != GregorianCalendar::AD && era != GregorianCalendar::BC) |
michael@0 | 356 | || month < UCAL_JANUARY |
michael@0 | 357 | || month > UCAL_DECEMBER |
michael@0 | 358 | || dom < 1 |
michael@0 | 359 | || dom > monthLength |
michael@0 | 360 | || dow < UCAL_SUNDAY |
michael@0 | 361 | || dow > UCAL_SATURDAY |
michael@0 | 362 | || millis < 0 |
michael@0 | 363 | || millis >= U_MILLIS_PER_DAY |
michael@0 | 364 | || monthLength < 28 |
michael@0 | 365 | || monthLength > 31) { |
michael@0 | 366 | ec = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 367 | return 0; |
michael@0 | 368 | } |
michael@0 | 369 | |
michael@0 | 370 | if (era == GregorianCalendar::BC) { |
michael@0 | 371 | year = -year; |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | if (finalZone != NULL && year >= finalStartYear) { |
michael@0 | 375 | return finalZone->getOffset(era, year, month, dom, dow, |
michael@0 | 376 | millis, monthLength, ec); |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | // Compute local epoch millis from input fields |
michael@0 | 380 | UDate date = (UDate)(Grego::fieldsToDay(year, month, dom) * U_MILLIS_PER_DAY + millis); |
michael@0 | 381 | int32_t rawoff, dstoff; |
michael@0 | 382 | getHistoricalOffset(date, TRUE, kDaylight, kStandard, rawoff, dstoff); |
michael@0 | 383 | return rawoff + dstoff; |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | /** |
michael@0 | 387 | * TimeZone API. |
michael@0 | 388 | */ |
michael@0 | 389 | void OlsonTimeZone::getOffset(UDate date, UBool local, int32_t& rawoff, |
michael@0 | 390 | int32_t& dstoff, UErrorCode& ec) const { |
michael@0 | 391 | if (U_FAILURE(ec)) { |
michael@0 | 392 | return; |
michael@0 | 393 | } |
michael@0 | 394 | if (finalZone != NULL && date >= finalStartMillis) { |
michael@0 | 395 | finalZone->getOffset(date, local, rawoff, dstoff, ec); |
michael@0 | 396 | } else { |
michael@0 | 397 | getHistoricalOffset(date, local, kFormer, kLatter, rawoff, dstoff); |
michael@0 | 398 | } |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | void |
michael@0 | 402 | OlsonTimeZone::getOffsetFromLocal(UDate date, int32_t nonExistingTimeOpt, int32_t duplicatedTimeOpt, |
michael@0 | 403 | int32_t& rawoff, int32_t& dstoff, UErrorCode& ec) const { |
michael@0 | 404 | if (U_FAILURE(ec)) { |
michael@0 | 405 | return; |
michael@0 | 406 | } |
michael@0 | 407 | if (finalZone != NULL && date >= finalStartMillis) { |
michael@0 | 408 | finalZone->getOffsetFromLocal(date, nonExistingTimeOpt, duplicatedTimeOpt, rawoff, dstoff, ec); |
michael@0 | 409 | } else { |
michael@0 | 410 | getHistoricalOffset(date, TRUE, nonExistingTimeOpt, duplicatedTimeOpt, rawoff, dstoff); |
michael@0 | 411 | } |
michael@0 | 412 | } |
michael@0 | 413 | |
michael@0 | 414 | |
michael@0 | 415 | /** |
michael@0 | 416 | * TimeZone API. |
michael@0 | 417 | */ |
michael@0 | 418 | void OlsonTimeZone::setRawOffset(int32_t /*offsetMillis*/) { |
michael@0 | 419 | // We don't support this operation, since OlsonTimeZones are |
michael@0 | 420 | // immutable (except for the ID, which is in the base class). |
michael@0 | 421 | |
michael@0 | 422 | // Nothing to do! |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | /** |
michael@0 | 426 | * TimeZone API. |
michael@0 | 427 | */ |
michael@0 | 428 | int32_t OlsonTimeZone::getRawOffset() const { |
michael@0 | 429 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 430 | int32_t raw, dst; |
michael@0 | 431 | getOffset((double) uprv_getUTCtime() * U_MILLIS_PER_SECOND, |
michael@0 | 432 | FALSE, raw, dst, ec); |
michael@0 | 433 | return raw; |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | #if defined U_DEBUG_TZ |
michael@0 | 437 | void printTime(double ms) { |
michael@0 | 438 | int32_t year, month, dom, dow; |
michael@0 | 439 | double millis=0; |
michael@0 | 440 | double days = ClockMath::floorDivide(((double)ms), (double)U_MILLIS_PER_DAY, millis); |
michael@0 | 441 | |
michael@0 | 442 | Grego::dayToFields(days, year, month, dom, dow); |
michael@0 | 443 | U_DEBUG_TZ_MSG((" getHistoricalOffset: time %.1f (%04d.%02d.%02d+%.1fh)\n", ms, |
michael@0 | 444 | year, month+1, dom, (millis/kOneHour))); |
michael@0 | 445 | } |
michael@0 | 446 | #endif |
michael@0 | 447 | |
michael@0 | 448 | int64_t |
michael@0 | 449 | OlsonTimeZone::transitionTimeInSeconds(int16_t transIdx) const { |
michael@0 | 450 | U_ASSERT(transIdx >= 0 && transIdx < transitionCount()); |
michael@0 | 451 | |
michael@0 | 452 | if (transIdx < transitionCountPre32) { |
michael@0 | 453 | return (((int64_t)((uint32_t)transitionTimesPre32[transIdx << 1])) << 32) |
michael@0 | 454 | | ((int64_t)((uint32_t)transitionTimesPre32[(transIdx << 1) + 1])); |
michael@0 | 455 | } |
michael@0 | 456 | |
michael@0 | 457 | transIdx -= transitionCountPre32; |
michael@0 | 458 | if (transIdx < transitionCount32) { |
michael@0 | 459 | return (int64_t)transitionTimes32[transIdx]; |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | transIdx -= transitionCount32; |
michael@0 | 463 | return (((int64_t)((uint32_t)transitionTimesPost32[transIdx << 1])) << 32) |
michael@0 | 464 | | ((int64_t)((uint32_t)transitionTimesPost32[(transIdx << 1) + 1])); |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | // Maximum absolute offset in seconds (86400 seconds = 1 day) |
michael@0 | 468 | // getHistoricalOffset uses this constant as safety margin of |
michael@0 | 469 | // quick zone transition checking. |
michael@0 | 470 | #define MAX_OFFSET_SECONDS 86400 |
michael@0 | 471 | |
michael@0 | 472 | void |
michael@0 | 473 | OlsonTimeZone::getHistoricalOffset(UDate date, UBool local, |
michael@0 | 474 | int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt, |
michael@0 | 475 | int32_t& rawoff, int32_t& dstoff) const { |
michael@0 | 476 | U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst)\n", |
michael@0 | 477 | date, local?"T":"F", NonExistingTimeOpt, DuplicatedTimeOpt)); |
michael@0 | 478 | #if defined U_DEBUG_TZ |
michael@0 | 479 | printTime(date*1000.0); |
michael@0 | 480 | #endif |
michael@0 | 481 | int16_t transCount = transitionCount(); |
michael@0 | 482 | |
michael@0 | 483 | if (transCount > 0) { |
michael@0 | 484 | double sec = uprv_floor(date / U_MILLIS_PER_SECOND); |
michael@0 | 485 | if (!local && sec < transitionTimeInSeconds(0)) { |
michael@0 | 486 | // Before the first transition time |
michael@0 | 487 | rawoff = initialRawOffset() * U_MILLIS_PER_SECOND; |
michael@0 | 488 | dstoff = initialDstOffset() * U_MILLIS_PER_SECOND; |
michael@0 | 489 | } else { |
michael@0 | 490 | // Linear search from the end is the fastest approach, since |
michael@0 | 491 | // most lookups will happen at/near the end. |
michael@0 | 492 | int16_t transIdx; |
michael@0 | 493 | for (transIdx = transCount - 1; transIdx >= 0; transIdx--) { |
michael@0 | 494 | int64_t transition = transitionTimeInSeconds(transIdx); |
michael@0 | 495 | |
michael@0 | 496 | if (local && (sec >= (transition - MAX_OFFSET_SECONDS))) { |
michael@0 | 497 | int32_t offsetBefore = zoneOffsetAt(transIdx - 1); |
michael@0 | 498 | UBool dstBefore = dstOffsetAt(transIdx - 1) != 0; |
michael@0 | 499 | |
michael@0 | 500 | int32_t offsetAfter = zoneOffsetAt(transIdx); |
michael@0 | 501 | UBool dstAfter = dstOffsetAt(transIdx) != 0; |
michael@0 | 502 | |
michael@0 | 503 | UBool dstToStd = dstBefore && !dstAfter; |
michael@0 | 504 | UBool stdToDst = !dstBefore && dstAfter; |
michael@0 | 505 | |
michael@0 | 506 | if (offsetAfter - offsetBefore >= 0) { |
michael@0 | 507 | // Positive transition, which makes a non-existing local time range |
michael@0 | 508 | if (((NonExistingTimeOpt & kStdDstMask) == kStandard && dstToStd) |
michael@0 | 509 | || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && stdToDst)) { |
michael@0 | 510 | transition += offsetBefore; |
michael@0 | 511 | } else if (((NonExistingTimeOpt & kStdDstMask) == kStandard && stdToDst) |
michael@0 | 512 | || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && dstToStd)) { |
michael@0 | 513 | transition += offsetAfter; |
michael@0 | 514 | } else if ((NonExistingTimeOpt & kFormerLatterMask) == kLatter) { |
michael@0 | 515 | transition += offsetBefore; |
michael@0 | 516 | } else { |
michael@0 | 517 | // Interprets the time with rule before the transition, |
michael@0 | 518 | // default for non-existing time range |
michael@0 | 519 | transition += offsetAfter; |
michael@0 | 520 | } |
michael@0 | 521 | } else { |
michael@0 | 522 | // Negative transition, which makes a duplicated local time range |
michael@0 | 523 | if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && dstToStd) |
michael@0 | 524 | || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && stdToDst)) { |
michael@0 | 525 | transition += offsetAfter; |
michael@0 | 526 | } else if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && stdToDst) |
michael@0 | 527 | || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && dstToStd)) { |
michael@0 | 528 | transition += offsetBefore; |
michael@0 | 529 | } else if ((DuplicatedTimeOpt & kFormerLatterMask) == kFormer) { |
michael@0 | 530 | transition += offsetBefore; |
michael@0 | 531 | } else { |
michael@0 | 532 | // Interprets the time with rule after the transition, |
michael@0 | 533 | // default for duplicated local time range |
michael@0 | 534 | transition += offsetAfter; |
michael@0 | 535 | } |
michael@0 | 536 | } |
michael@0 | 537 | } |
michael@0 | 538 | if (sec >= transition) { |
michael@0 | 539 | break; |
michael@0 | 540 | } |
michael@0 | 541 | } |
michael@0 | 542 | // transIdx could be -1 when local=true |
michael@0 | 543 | rawoff = rawOffsetAt(transIdx) * U_MILLIS_PER_SECOND; |
michael@0 | 544 | dstoff = dstOffsetAt(transIdx) * U_MILLIS_PER_SECOND; |
michael@0 | 545 | } |
michael@0 | 546 | } else { |
michael@0 | 547 | // No transitions, single pair of offsets only |
michael@0 | 548 | rawoff = initialRawOffset() * U_MILLIS_PER_SECOND; |
michael@0 | 549 | dstoff = initialDstOffset() * U_MILLIS_PER_SECOND; |
michael@0 | 550 | } |
michael@0 | 551 | U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst) - raw=%d, dst=%d\n", |
michael@0 | 552 | date, local?"T":"F", NonExistingTimeOpt, DuplicatedTimeOpt, rawoff, dstoff)); |
michael@0 | 553 | } |
michael@0 | 554 | |
michael@0 | 555 | /** |
michael@0 | 556 | * TimeZone API. |
michael@0 | 557 | */ |
michael@0 | 558 | UBool OlsonTimeZone::useDaylightTime() const { |
michael@0 | 559 | // If DST was observed in 1942 (for example) but has never been |
michael@0 | 560 | // observed from 1943 to the present, most clients will expect |
michael@0 | 561 | // this method to return FALSE. This method determines whether |
michael@0 | 562 | // DST is in use in the current year (at any point in the year) |
michael@0 | 563 | // and returns TRUE if so. |
michael@0 | 564 | |
michael@0 | 565 | UDate current = uprv_getUTCtime(); |
michael@0 | 566 | if (finalZone != NULL && current >= finalStartMillis) { |
michael@0 | 567 | return finalZone->useDaylightTime(); |
michael@0 | 568 | } |
michael@0 | 569 | |
michael@0 | 570 | int32_t year, month, dom, dow, doy, mid; |
michael@0 | 571 | Grego::timeToFields(current, year, month, dom, dow, doy, mid); |
michael@0 | 572 | |
michael@0 | 573 | // Find start of this year, and start of next year |
michael@0 | 574 | double start = Grego::fieldsToDay(year, 0, 1) * SECONDS_PER_DAY; |
michael@0 | 575 | double limit = Grego::fieldsToDay(year+1, 0, 1) * SECONDS_PER_DAY; |
michael@0 | 576 | |
michael@0 | 577 | // Return TRUE if DST is observed at any time during the current |
michael@0 | 578 | // year. |
michael@0 | 579 | for (int16_t i = 0; i < transitionCount(); ++i) { |
michael@0 | 580 | double transition = (double)transitionTimeInSeconds(i); |
michael@0 | 581 | if (transition >= limit) { |
michael@0 | 582 | break; |
michael@0 | 583 | } |
michael@0 | 584 | if ((transition >= start && dstOffsetAt(i) != 0) |
michael@0 | 585 | || (transition > start && dstOffsetAt(i - 1) != 0)) { |
michael@0 | 586 | return TRUE; |
michael@0 | 587 | } |
michael@0 | 588 | } |
michael@0 | 589 | return FALSE; |
michael@0 | 590 | } |
michael@0 | 591 | int32_t |
michael@0 | 592 | OlsonTimeZone::getDSTSavings() const{ |
michael@0 | 593 | if (finalZone != NULL){ |
michael@0 | 594 | return finalZone->getDSTSavings(); |
michael@0 | 595 | } |
michael@0 | 596 | return TimeZone::getDSTSavings(); |
michael@0 | 597 | } |
michael@0 | 598 | /** |
michael@0 | 599 | * TimeZone API. |
michael@0 | 600 | */ |
michael@0 | 601 | UBool OlsonTimeZone::inDaylightTime(UDate date, UErrorCode& ec) const { |
michael@0 | 602 | int32_t raw, dst; |
michael@0 | 603 | getOffset(date, FALSE, raw, dst, ec); |
michael@0 | 604 | return dst != 0; |
michael@0 | 605 | } |
michael@0 | 606 | |
michael@0 | 607 | UBool |
michael@0 | 608 | OlsonTimeZone::hasSameRules(const TimeZone &other) const { |
michael@0 | 609 | if (this == &other) { |
michael@0 | 610 | return TRUE; |
michael@0 | 611 | } |
michael@0 | 612 | const OlsonTimeZone* z = dynamic_cast<const OlsonTimeZone*>(&other); |
michael@0 | 613 | if (z == NULL) { |
michael@0 | 614 | return FALSE; |
michael@0 | 615 | } |
michael@0 | 616 | |
michael@0 | 617 | // [sic] pointer comparison: typeMapData points into |
michael@0 | 618 | // memory-mapped or DLL space, so if two zones have the same |
michael@0 | 619 | // pointer, they are equal. |
michael@0 | 620 | if (typeMapData == z->typeMapData) { |
michael@0 | 621 | return TRUE; |
michael@0 | 622 | } |
michael@0 | 623 | |
michael@0 | 624 | // If the pointers are not equal, the zones may still |
michael@0 | 625 | // be equal if their rules and transitions are equal |
michael@0 | 626 | if ((finalZone == NULL && z->finalZone != NULL) |
michael@0 | 627 | || (finalZone != NULL && z->finalZone == NULL) |
michael@0 | 628 | || (finalZone != NULL && z->finalZone != NULL && *finalZone != *z->finalZone)) { |
michael@0 | 629 | return FALSE; |
michael@0 | 630 | } |
michael@0 | 631 | |
michael@0 | 632 | if (finalZone != NULL) { |
michael@0 | 633 | if (finalStartYear != z->finalStartYear || finalStartMillis != z->finalStartMillis) { |
michael@0 | 634 | return FALSE; |
michael@0 | 635 | } |
michael@0 | 636 | } |
michael@0 | 637 | if (typeCount != z->typeCount |
michael@0 | 638 | || transitionCountPre32 != z->transitionCountPre32 |
michael@0 | 639 | || transitionCount32 != z->transitionCount32 |
michael@0 | 640 | || transitionCountPost32 != z->transitionCountPost32) { |
michael@0 | 641 | return FALSE; |
michael@0 | 642 | } |
michael@0 | 643 | |
michael@0 | 644 | return |
michael@0 | 645 | arrayEqual(transitionTimesPre32, z->transitionTimesPre32, sizeof(transitionTimesPre32[0]) * transitionCountPre32 << 1) |
michael@0 | 646 | && arrayEqual(transitionTimes32, z->transitionTimes32, sizeof(transitionTimes32[0]) * transitionCount32) |
michael@0 | 647 | && arrayEqual(transitionTimesPost32, z->transitionTimesPost32, sizeof(transitionTimesPost32[0]) * transitionCountPost32 << 1) |
michael@0 | 648 | && arrayEqual(typeOffsets, z->typeOffsets, sizeof(typeOffsets[0]) * typeCount << 1) |
michael@0 | 649 | && arrayEqual(typeMapData, z->typeMapData, sizeof(typeMapData[0]) * transitionCount()); |
michael@0 | 650 | } |
michael@0 | 651 | |
michael@0 | 652 | void |
michael@0 | 653 | OlsonTimeZone::clearTransitionRules(void) { |
michael@0 | 654 | initialRule = NULL; |
michael@0 | 655 | firstTZTransition = NULL; |
michael@0 | 656 | firstFinalTZTransition = NULL; |
michael@0 | 657 | historicRules = NULL; |
michael@0 | 658 | historicRuleCount = 0; |
michael@0 | 659 | finalZoneWithStartYear = NULL; |
michael@0 | 660 | firstTZTransitionIdx = 0; |
michael@0 | 661 | transitionRulesInitOnce.reset(); |
michael@0 | 662 | } |
michael@0 | 663 | |
michael@0 | 664 | void |
michael@0 | 665 | OlsonTimeZone::deleteTransitionRules(void) { |
michael@0 | 666 | if (initialRule != NULL) { |
michael@0 | 667 | delete initialRule; |
michael@0 | 668 | } |
michael@0 | 669 | if (firstTZTransition != NULL) { |
michael@0 | 670 | delete firstTZTransition; |
michael@0 | 671 | } |
michael@0 | 672 | if (firstFinalTZTransition != NULL) { |
michael@0 | 673 | delete firstFinalTZTransition; |
michael@0 | 674 | } |
michael@0 | 675 | if (finalZoneWithStartYear != NULL) { |
michael@0 | 676 | delete finalZoneWithStartYear; |
michael@0 | 677 | } |
michael@0 | 678 | if (historicRules != NULL) { |
michael@0 | 679 | for (int i = 0; i < historicRuleCount; i++) { |
michael@0 | 680 | if (historicRules[i] != NULL) { |
michael@0 | 681 | delete historicRules[i]; |
michael@0 | 682 | } |
michael@0 | 683 | } |
michael@0 | 684 | uprv_free(historicRules); |
michael@0 | 685 | } |
michael@0 | 686 | clearTransitionRules(); |
michael@0 | 687 | } |
michael@0 | 688 | |
michael@0 | 689 | /* |
michael@0 | 690 | * Lazy transition rules initializer |
michael@0 | 691 | */ |
michael@0 | 692 | |
michael@0 | 693 | static void U_CALLCONV initRules(OlsonTimeZone *This, UErrorCode &status) { |
michael@0 | 694 | This->initTransitionRules(status); |
michael@0 | 695 | } |
michael@0 | 696 | |
michael@0 | 697 | void |
michael@0 | 698 | OlsonTimeZone::checkTransitionRules(UErrorCode& status) const { |
michael@0 | 699 | OlsonTimeZone *ncThis = const_cast<OlsonTimeZone *>(this); |
michael@0 | 700 | umtx_initOnce(ncThis->transitionRulesInitOnce, &initRules, ncThis, status); |
michael@0 | 701 | } |
michael@0 | 702 | |
michael@0 | 703 | void |
michael@0 | 704 | OlsonTimeZone::initTransitionRules(UErrorCode& status) { |
michael@0 | 705 | if(U_FAILURE(status)) { |
michael@0 | 706 | return; |
michael@0 | 707 | } |
michael@0 | 708 | deleteTransitionRules(); |
michael@0 | 709 | UnicodeString tzid; |
michael@0 | 710 | getID(tzid); |
michael@0 | 711 | |
michael@0 | 712 | UnicodeString stdName = tzid + UNICODE_STRING_SIMPLE("(STD)"); |
michael@0 | 713 | UnicodeString dstName = tzid + UNICODE_STRING_SIMPLE("(DST)"); |
michael@0 | 714 | |
michael@0 | 715 | int32_t raw, dst; |
michael@0 | 716 | |
michael@0 | 717 | // Create initial rule |
michael@0 | 718 | raw = initialRawOffset() * U_MILLIS_PER_SECOND; |
michael@0 | 719 | dst = initialDstOffset() * U_MILLIS_PER_SECOND; |
michael@0 | 720 | initialRule = new InitialTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst); |
michael@0 | 721 | // Check to make sure initialRule was created |
michael@0 | 722 | if (initialRule == NULL) { |
michael@0 | 723 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 724 | deleteTransitionRules(); |
michael@0 | 725 | return; |
michael@0 | 726 | } |
michael@0 | 727 | |
michael@0 | 728 | int32_t transCount = transitionCount(); |
michael@0 | 729 | if (transCount > 0) { |
michael@0 | 730 | int16_t transitionIdx, typeIdx; |
michael@0 | 731 | |
michael@0 | 732 | // We probably no longer need to check the first "real" transition |
michael@0 | 733 | // here, because the new tzcode remove such transitions already. |
michael@0 | 734 | // For now, keeping this code for just in case. Feb 19, 2010 Yoshito |
michael@0 | 735 | firstTZTransitionIdx = 0; |
michael@0 | 736 | for (transitionIdx = 0; transitionIdx < transCount; transitionIdx++) { |
michael@0 | 737 | if (typeMapData[transitionIdx] != 0) { // type 0 is the initial type |
michael@0 | 738 | break; |
michael@0 | 739 | } |
michael@0 | 740 | firstTZTransitionIdx++; |
michael@0 | 741 | } |
michael@0 | 742 | if (transitionIdx == transCount) { |
michael@0 | 743 | // Actually no transitions... |
michael@0 | 744 | } else { |
michael@0 | 745 | // Build historic rule array |
michael@0 | 746 | UDate* times = (UDate*)uprv_malloc(sizeof(UDate)*transCount); /* large enough to store all transition times */ |
michael@0 | 747 | if (times == NULL) { |
michael@0 | 748 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 749 | deleteTransitionRules(); |
michael@0 | 750 | return; |
michael@0 | 751 | } |
michael@0 | 752 | for (typeIdx = 0; typeIdx < typeCount; typeIdx++) { |
michael@0 | 753 | // Gather all start times for each pair of offsets |
michael@0 | 754 | int32_t nTimes = 0; |
michael@0 | 755 | for (transitionIdx = firstTZTransitionIdx; transitionIdx < transCount; transitionIdx++) { |
michael@0 | 756 | if (typeIdx == (int16_t)typeMapData[transitionIdx]) { |
michael@0 | 757 | UDate tt = (UDate)transitionTime(transitionIdx); |
michael@0 | 758 | if (finalZone == NULL || tt <= finalStartMillis) { |
michael@0 | 759 | // Exclude transitions after finalMillis |
michael@0 | 760 | times[nTimes++] = tt; |
michael@0 | 761 | } |
michael@0 | 762 | } |
michael@0 | 763 | } |
michael@0 | 764 | if (nTimes > 0) { |
michael@0 | 765 | // Create a TimeArrayTimeZoneRule |
michael@0 | 766 | raw = typeOffsets[typeIdx << 1] * U_MILLIS_PER_SECOND; |
michael@0 | 767 | dst = typeOffsets[(typeIdx << 1) + 1] * U_MILLIS_PER_SECOND; |
michael@0 | 768 | if (historicRules == NULL) { |
michael@0 | 769 | historicRuleCount = typeCount; |
michael@0 | 770 | historicRules = (TimeArrayTimeZoneRule**)uprv_malloc(sizeof(TimeArrayTimeZoneRule*)*historicRuleCount); |
michael@0 | 771 | if (historicRules == NULL) { |
michael@0 | 772 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 773 | deleteTransitionRules(); |
michael@0 | 774 | uprv_free(times); |
michael@0 | 775 | return; |
michael@0 | 776 | } |
michael@0 | 777 | for (int i = 0; i < historicRuleCount; i++) { |
michael@0 | 778 | // Initialize TimeArrayTimeZoneRule pointers as NULL |
michael@0 | 779 | historicRules[i] = NULL; |
michael@0 | 780 | } |
michael@0 | 781 | } |
michael@0 | 782 | historicRules[typeIdx] = new TimeArrayTimeZoneRule((dst == 0 ? stdName : dstName), |
michael@0 | 783 | raw, dst, times, nTimes, DateTimeRule::UTC_TIME); |
michael@0 | 784 | // Check for memory allocation error |
michael@0 | 785 | if (historicRules[typeIdx] == NULL) { |
michael@0 | 786 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 787 | deleteTransitionRules(); |
michael@0 | 788 | return; |
michael@0 | 789 | } |
michael@0 | 790 | } |
michael@0 | 791 | } |
michael@0 | 792 | uprv_free(times); |
michael@0 | 793 | |
michael@0 | 794 | // Create initial transition |
michael@0 | 795 | typeIdx = (int16_t)typeMapData[firstTZTransitionIdx]; |
michael@0 | 796 | firstTZTransition = new TimeZoneTransition((UDate)transitionTime(firstTZTransitionIdx), |
michael@0 | 797 | *initialRule, *historicRules[typeIdx]); |
michael@0 | 798 | // Check to make sure firstTZTransition was created. |
michael@0 | 799 | if (firstTZTransition == NULL) { |
michael@0 | 800 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 801 | deleteTransitionRules(); |
michael@0 | 802 | return; |
michael@0 | 803 | } |
michael@0 | 804 | } |
michael@0 | 805 | } |
michael@0 | 806 | if (finalZone != NULL) { |
michael@0 | 807 | // Get the first occurence of final rule starts |
michael@0 | 808 | UDate startTime = (UDate)finalStartMillis; |
michael@0 | 809 | TimeZoneRule *firstFinalRule = NULL; |
michael@0 | 810 | |
michael@0 | 811 | if (finalZone->useDaylightTime()) { |
michael@0 | 812 | /* |
michael@0 | 813 | * Note: When an OlsonTimeZone is constructed, we should set the final year |
michael@0 | 814 | * as the start year of finalZone. However, the bounday condition used for |
michael@0 | 815 | * getting offset from finalZone has some problems. |
michael@0 | 816 | * For now, we do not set the valid start year when the construction time |
michael@0 | 817 | * and create a clone and set the start year when extracting rules. |
michael@0 | 818 | */ |
michael@0 | 819 | finalZoneWithStartYear = (SimpleTimeZone*)finalZone->clone(); |
michael@0 | 820 | // Check to make sure finalZone was actually cloned. |
michael@0 | 821 | if (finalZoneWithStartYear == NULL) { |
michael@0 | 822 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 823 | deleteTransitionRules(); |
michael@0 | 824 | return; |
michael@0 | 825 | } |
michael@0 | 826 | finalZoneWithStartYear->setStartYear(finalStartYear); |
michael@0 | 827 | |
michael@0 | 828 | TimeZoneTransition tzt; |
michael@0 | 829 | finalZoneWithStartYear->getNextTransition(startTime, false, tzt); |
michael@0 | 830 | firstFinalRule = tzt.getTo()->clone(); |
michael@0 | 831 | // Check to make sure firstFinalRule received proper clone. |
michael@0 | 832 | if (firstFinalRule == NULL) { |
michael@0 | 833 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 834 | deleteTransitionRules(); |
michael@0 | 835 | return; |
michael@0 | 836 | } |
michael@0 | 837 | startTime = tzt.getTime(); |
michael@0 | 838 | } else { |
michael@0 | 839 | // final rule with no transitions |
michael@0 | 840 | finalZoneWithStartYear = (SimpleTimeZone*)finalZone->clone(); |
michael@0 | 841 | // Check to make sure finalZone was actually cloned. |
michael@0 | 842 | if (finalZoneWithStartYear == NULL) { |
michael@0 | 843 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 844 | deleteTransitionRules(); |
michael@0 | 845 | return; |
michael@0 | 846 | } |
michael@0 | 847 | finalZone->getID(tzid); |
michael@0 | 848 | firstFinalRule = new TimeArrayTimeZoneRule(tzid, |
michael@0 | 849 | finalZone->getRawOffset(), 0, &startTime, 1, DateTimeRule::UTC_TIME); |
michael@0 | 850 | // Check firstFinalRule was properly created. |
michael@0 | 851 | if (firstFinalRule == NULL) { |
michael@0 | 852 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 853 | deleteTransitionRules(); |
michael@0 | 854 | return; |
michael@0 | 855 | } |
michael@0 | 856 | } |
michael@0 | 857 | TimeZoneRule *prevRule = NULL; |
michael@0 | 858 | if (transCount > 0) { |
michael@0 | 859 | prevRule = historicRules[typeMapData[transCount - 1]]; |
michael@0 | 860 | } |
michael@0 | 861 | if (prevRule == NULL) { |
michael@0 | 862 | // No historic transitions, but only finalZone available |
michael@0 | 863 | prevRule = initialRule; |
michael@0 | 864 | } |
michael@0 | 865 | firstFinalTZTransition = new TimeZoneTransition(); |
michael@0 | 866 | // Check to make sure firstFinalTZTransition was created before dereferencing |
michael@0 | 867 | if (firstFinalTZTransition == NULL) { |
michael@0 | 868 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 869 | deleteTransitionRules(); |
michael@0 | 870 | return; |
michael@0 | 871 | } |
michael@0 | 872 | firstFinalTZTransition->setTime(startTime); |
michael@0 | 873 | firstFinalTZTransition->adoptFrom(prevRule->clone()); |
michael@0 | 874 | firstFinalTZTransition->adoptTo(firstFinalRule); |
michael@0 | 875 | } |
michael@0 | 876 | } |
michael@0 | 877 | |
michael@0 | 878 | UBool |
michael@0 | 879 | OlsonTimeZone::getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const { |
michael@0 | 880 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 881 | checkTransitionRules(status); |
michael@0 | 882 | if (U_FAILURE(status)) { |
michael@0 | 883 | return FALSE; |
michael@0 | 884 | } |
michael@0 | 885 | |
michael@0 | 886 | if (finalZone != NULL) { |
michael@0 | 887 | if (inclusive && base == firstFinalTZTransition->getTime()) { |
michael@0 | 888 | result = *firstFinalTZTransition; |
michael@0 | 889 | return TRUE; |
michael@0 | 890 | } else if (base >= firstFinalTZTransition->getTime()) { |
michael@0 | 891 | if (finalZone->useDaylightTime()) { |
michael@0 | 892 | //return finalZone->getNextTransition(base, inclusive, result); |
michael@0 | 893 | return finalZoneWithStartYear->getNextTransition(base, inclusive, result); |
michael@0 | 894 | } else { |
michael@0 | 895 | // No more transitions |
michael@0 | 896 | return FALSE; |
michael@0 | 897 | } |
michael@0 | 898 | } |
michael@0 | 899 | } |
michael@0 | 900 | if (historicRules != NULL) { |
michael@0 | 901 | // Find a historical transition |
michael@0 | 902 | int16_t transCount = transitionCount(); |
michael@0 | 903 | int16_t ttidx = transCount - 1; |
michael@0 | 904 | for (; ttidx >= firstTZTransitionIdx; ttidx--) { |
michael@0 | 905 | UDate t = (UDate)transitionTime(ttidx); |
michael@0 | 906 | if (base > t || (!inclusive && base == t)) { |
michael@0 | 907 | break; |
michael@0 | 908 | } |
michael@0 | 909 | } |
michael@0 | 910 | if (ttidx == transCount - 1) { |
michael@0 | 911 | if (firstFinalTZTransition != NULL) { |
michael@0 | 912 | result = *firstFinalTZTransition; |
michael@0 | 913 | return TRUE; |
michael@0 | 914 | } else { |
michael@0 | 915 | return FALSE; |
michael@0 | 916 | } |
michael@0 | 917 | } else if (ttidx < firstTZTransitionIdx) { |
michael@0 | 918 | result = *firstTZTransition; |
michael@0 | 919 | return TRUE; |
michael@0 | 920 | } else { |
michael@0 | 921 | // Create a TimeZoneTransition |
michael@0 | 922 | TimeZoneRule *to = historicRules[typeMapData[ttidx + 1]]; |
michael@0 | 923 | TimeZoneRule *from = historicRules[typeMapData[ttidx]]; |
michael@0 | 924 | UDate startTime = (UDate)transitionTime(ttidx+1); |
michael@0 | 925 | |
michael@0 | 926 | // The transitions loaded from zoneinfo.res may contain non-transition data |
michael@0 | 927 | UnicodeString fromName, toName; |
michael@0 | 928 | from->getName(fromName); |
michael@0 | 929 | to->getName(toName); |
michael@0 | 930 | if (fromName == toName && from->getRawOffset() == to->getRawOffset() |
michael@0 | 931 | && from->getDSTSavings() == to->getDSTSavings()) { |
michael@0 | 932 | return getNextTransition(startTime, false, result); |
michael@0 | 933 | } |
michael@0 | 934 | result.setTime(startTime); |
michael@0 | 935 | result.adoptFrom(from->clone()); |
michael@0 | 936 | result.adoptTo(to->clone()); |
michael@0 | 937 | return TRUE; |
michael@0 | 938 | } |
michael@0 | 939 | } |
michael@0 | 940 | return FALSE; |
michael@0 | 941 | } |
michael@0 | 942 | |
michael@0 | 943 | UBool |
michael@0 | 944 | OlsonTimeZone::getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const { |
michael@0 | 945 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 946 | checkTransitionRules(status); |
michael@0 | 947 | if (U_FAILURE(status)) { |
michael@0 | 948 | return FALSE; |
michael@0 | 949 | } |
michael@0 | 950 | |
michael@0 | 951 | if (finalZone != NULL) { |
michael@0 | 952 | if (inclusive && base == firstFinalTZTransition->getTime()) { |
michael@0 | 953 | result = *firstFinalTZTransition; |
michael@0 | 954 | return TRUE; |
michael@0 | 955 | } else if (base > firstFinalTZTransition->getTime()) { |
michael@0 | 956 | if (finalZone->useDaylightTime()) { |
michael@0 | 957 | //return finalZone->getPreviousTransition(base, inclusive, result); |
michael@0 | 958 | return finalZoneWithStartYear->getPreviousTransition(base, inclusive, result); |
michael@0 | 959 | } else { |
michael@0 | 960 | result = *firstFinalTZTransition; |
michael@0 | 961 | return TRUE; |
michael@0 | 962 | } |
michael@0 | 963 | } |
michael@0 | 964 | } |
michael@0 | 965 | |
michael@0 | 966 | if (historicRules != NULL) { |
michael@0 | 967 | // Find a historical transition |
michael@0 | 968 | int16_t ttidx = transitionCount() - 1; |
michael@0 | 969 | for (; ttidx >= firstTZTransitionIdx; ttidx--) { |
michael@0 | 970 | UDate t = (UDate)transitionTime(ttidx); |
michael@0 | 971 | if (base > t || (inclusive && base == t)) { |
michael@0 | 972 | break; |
michael@0 | 973 | } |
michael@0 | 974 | } |
michael@0 | 975 | if (ttidx < firstTZTransitionIdx) { |
michael@0 | 976 | // No more transitions |
michael@0 | 977 | return FALSE; |
michael@0 | 978 | } else if (ttidx == firstTZTransitionIdx) { |
michael@0 | 979 | result = *firstTZTransition; |
michael@0 | 980 | return TRUE; |
michael@0 | 981 | } else { |
michael@0 | 982 | // Create a TimeZoneTransition |
michael@0 | 983 | TimeZoneRule *to = historicRules[typeMapData[ttidx]]; |
michael@0 | 984 | TimeZoneRule *from = historicRules[typeMapData[ttidx-1]]; |
michael@0 | 985 | UDate startTime = (UDate)transitionTime(ttidx); |
michael@0 | 986 | |
michael@0 | 987 | // The transitions loaded from zoneinfo.res may contain non-transition data |
michael@0 | 988 | UnicodeString fromName, toName; |
michael@0 | 989 | from->getName(fromName); |
michael@0 | 990 | to->getName(toName); |
michael@0 | 991 | if (fromName == toName && from->getRawOffset() == to->getRawOffset() |
michael@0 | 992 | && from->getDSTSavings() == to->getDSTSavings()) { |
michael@0 | 993 | return getPreviousTransition(startTime, false, result); |
michael@0 | 994 | } |
michael@0 | 995 | result.setTime(startTime); |
michael@0 | 996 | result.adoptFrom(from->clone()); |
michael@0 | 997 | result.adoptTo(to->clone()); |
michael@0 | 998 | return TRUE; |
michael@0 | 999 | } |
michael@0 | 1000 | } |
michael@0 | 1001 | return FALSE; |
michael@0 | 1002 | } |
michael@0 | 1003 | |
michael@0 | 1004 | int32_t |
michael@0 | 1005 | OlsonTimeZone::countTransitionRules(UErrorCode& status) const { |
michael@0 | 1006 | if (U_FAILURE(status)) { |
michael@0 | 1007 | return 0; |
michael@0 | 1008 | } |
michael@0 | 1009 | checkTransitionRules(status); |
michael@0 | 1010 | if (U_FAILURE(status)) { |
michael@0 | 1011 | return 0; |
michael@0 | 1012 | } |
michael@0 | 1013 | |
michael@0 | 1014 | int32_t count = 0; |
michael@0 | 1015 | if (historicRules != NULL) { |
michael@0 | 1016 | // historicRules may contain null entries when original zoneinfo data |
michael@0 | 1017 | // includes non transition data. |
michael@0 | 1018 | for (int32_t i = 0; i < historicRuleCount; i++) { |
michael@0 | 1019 | if (historicRules[i] != NULL) { |
michael@0 | 1020 | count++; |
michael@0 | 1021 | } |
michael@0 | 1022 | } |
michael@0 | 1023 | } |
michael@0 | 1024 | if (finalZone != NULL) { |
michael@0 | 1025 | if (finalZone->useDaylightTime()) { |
michael@0 | 1026 | count += 2; |
michael@0 | 1027 | } else { |
michael@0 | 1028 | count++; |
michael@0 | 1029 | } |
michael@0 | 1030 | } |
michael@0 | 1031 | return count; |
michael@0 | 1032 | } |
michael@0 | 1033 | |
michael@0 | 1034 | void |
michael@0 | 1035 | OlsonTimeZone::getTimeZoneRules(const InitialTimeZoneRule*& initial, |
michael@0 | 1036 | const TimeZoneRule* trsrules[], |
michael@0 | 1037 | int32_t& trscount, |
michael@0 | 1038 | UErrorCode& status) const { |
michael@0 | 1039 | if (U_FAILURE(status)) { |
michael@0 | 1040 | return; |
michael@0 | 1041 | } |
michael@0 | 1042 | checkTransitionRules(status); |
michael@0 | 1043 | if (U_FAILURE(status)) { |
michael@0 | 1044 | return; |
michael@0 | 1045 | } |
michael@0 | 1046 | |
michael@0 | 1047 | // Initial rule |
michael@0 | 1048 | initial = initialRule; |
michael@0 | 1049 | |
michael@0 | 1050 | // Transition rules |
michael@0 | 1051 | int32_t cnt = 0; |
michael@0 | 1052 | if (historicRules != NULL && trscount > cnt) { |
michael@0 | 1053 | // historicRules may contain null entries when original zoneinfo data |
michael@0 | 1054 | // includes non transition data. |
michael@0 | 1055 | for (int32_t i = 0; i < historicRuleCount; i++) { |
michael@0 | 1056 | if (historicRules[i] != NULL) { |
michael@0 | 1057 | trsrules[cnt++] = historicRules[i]; |
michael@0 | 1058 | if (cnt >= trscount) { |
michael@0 | 1059 | break; |
michael@0 | 1060 | } |
michael@0 | 1061 | } |
michael@0 | 1062 | } |
michael@0 | 1063 | } |
michael@0 | 1064 | if (finalZoneWithStartYear != NULL && trscount > cnt) { |
michael@0 | 1065 | const InitialTimeZoneRule *tmpini; |
michael@0 | 1066 | int32_t tmpcnt = trscount - cnt; |
michael@0 | 1067 | finalZoneWithStartYear->getTimeZoneRules(tmpini, &trsrules[cnt], tmpcnt, status); |
michael@0 | 1068 | if (U_FAILURE(status)) { |
michael@0 | 1069 | return; |
michael@0 | 1070 | } |
michael@0 | 1071 | cnt += tmpcnt; |
michael@0 | 1072 | } |
michael@0 | 1073 | // Set the result length |
michael@0 | 1074 | trscount = cnt; |
michael@0 | 1075 | } |
michael@0 | 1076 | |
michael@0 | 1077 | U_NAMESPACE_END |
michael@0 | 1078 | |
michael@0 | 1079 | #endif // !UCONFIG_NO_FORMATTING |
michael@0 | 1080 | |
michael@0 | 1081 | //eof |