]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/olsontz.cpp
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / i18n / olsontz.cpp
index 67be07b99e3012b652ffc44ae03b9fcb10a0bd41..b9b2e0507d8be2f556728807bfdda2b346f3bf6f 100644 (file)
@@ -1,6 +1,6 @@
 /*
 **********************************************************************
-* Copyright (c) 2003-2009, International Business Machines
+* Copyright (c) 2003-2010, International Business Machines
 * Corporation and others.  All Rights Reserved.
 **********************************************************************
 * Author: Alan Liu
@@ -9,6 +9,8 @@
 **********************************************************************
 */
 
+#include <typeinfo>  // for 'typeid' to work
+
 #include "olsontz.h"
 
 #if !UCONFIG_NO_FORMATTING
@@ -21,6 +23,7 @@
 #include "uassert.h"
 #include "uvector.h"
 #include <float.h> // DBL_MAX
+#include "uresimp.h" // struct UResourceBundle
 
 #ifdef U_DEBUG_TZ
 # include <stdio.h>
@@ -44,8 +47,32 @@ static void debug_tz_msg(const char *pat, ...)
 #define U_DEBUG_TZ_MSG(x)
 #endif
 
+static UBool arrayEqual(const void *a1, const void *a2, int32_t size) {
+    if (a1 == NULL && a2 == NULL) {
+        return TRUE;
+    }
+    if ((a1 != NULL && a2 == NULL) || (a1 == NULL && a2 != NULL)) {
+        return FALSE;
+    }
+    if (a1 == a2) {
+        return TRUE;
+    }
+
+    return (uprv_memcmp(a1, a2, size) == 0);
+}
+
 U_NAMESPACE_BEGIN
 
+#define kTRANS          "trans"
+#define kTRANSPRE32     "transPre32"
+#define kTRANSPOST32    "transPost32"
+#define kTYPEOFFSETS    "typeOffsets"
+#define kTYPEMAP        "typeMap"
+#define kLINKS          "links"
+#define kFINALRULE      "finalRule"
+#define kFINALRAW       "finalRaw"
+#define kFINALYEAR      "finalYear"
+
 #define SECONDS_PER_DAY (24*60*60)
 
 static const int32_t ZEROS[] = {0,0};
@@ -66,10 +93,15 @@ UOBJECT_DEFINE_RTTI_IMPLEMENTATION(OlsonTimeZone)
  * constructor fails so the resultant object is well-behaved.
  */
 void OlsonTimeZone::constructEmpty() {
-    transitionCount = 0;
+    transitionCountPre32 = transitionCount32 = transitionCountPost32 = 0;
+    transitionTimesPre32 = transitionTimes32 = transitionTimesPost32 = NULL;
+
+    typeMapData = NULL;
+
     typeCount = 1;
-    transitionTimes = typeOffsets = ZEROS;
-    typeData = (const uint8_t*) ZEROS;
+    typeOffsets = ZEROS;
+
+    finalZone = NULL;
 }
 
 /**
@@ -82,7 +114,7 @@ void OlsonTimeZone::constructEmpty() {
 OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
                              const UResourceBundle* res,
                              UErrorCode& ec) :
-  finalYear(INT32_MAX), finalMillis(DBL_MAX), finalZone(0), transitionRulesInitialized(FALSE)
+  finalZone(NULL), transitionRulesInitialized(FALSE)
 {
     clearTransitionRules();
     U_DEBUG_TZ_MSG(("OlsonTimeZone(%s)\n", ures_getKey((UResourceBundle*)res)));
@@ -94,121 +126,125 @@ OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
         //        // TODO remove nonconst casts below when ures_* API is fixed
         //        setID(ures_getKey((UResourceBundle*) res)); // cast away const
 
-        // Size 1 is an alias TO another zone (int)
-        // HOWEVER, the caller should dereference this and never pass it in to us
-        // Size 3 is a purely historical zone (no final rules)
-        // Size 4 is like size 3, but with an alias list at the end
-        // Size 5 is a hybrid zone, with historical and final elements
-        // Size 6 is like size 5, but with an alias list at the end
-        int32_t size = ures_getSize(res);
-        if (size < 3 || size > 6) {
+        int32_t len;
+        UResourceBundle r;
+        ures_initStackObject(&r);
+
+        // Pre-32bit second transitions
+        ures_getByKey(res, kTRANSPRE32, &r, &ec);
+        transitionTimesPre32 = ures_getIntVector(&r, &len, &ec);
+        transitionCountPre32 = len >> 1;
+        if (ec == U_MISSING_RESOURCE_ERROR) {
+            // No pre-32bit transitions
+            transitionTimesPre32 = NULL;
+            transitionCountPre32 = 0;
+            ec = U_ZERO_ERROR;
+        } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF || (len & 1) != 0) /* len must be even */) {
             ec = U_INVALID_FORMAT_ERROR;
         }
 
-        // Transitions list may be empty
-        int32_t i;
-        UResourceBundle* r = ures_getByIndex(res, 0, NULL, &ec);
-        transitionTimes = ures_getIntVector(r, &i, &ec);
-        if ((i<0 || i>0x7FFF) && U_SUCCESS(ec)) {
+        // 32bit second transitions
+        ures_getByKey(res, kTRANS, &r, &ec);
+        transitionTimes32 = ures_getIntVector(&r, &len, &ec);
+        transitionCount32 = len;
+        if (ec == U_MISSING_RESOURCE_ERROR) {
+            // No 32bit transitions
+            transitionTimes32 = NULL;
+            transitionCount32 = 0;
+            ec = U_ZERO_ERROR;
+        } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF)) {
             ec = U_INVALID_FORMAT_ERROR;
         }
-        transitionCount = (int16_t) i;
-        
-        // Type offsets list must be of even size, with size >= 2
-        r = ures_getByIndex(res, 1, r, &ec);
-        typeOffsets = ures_getIntVector(r, &i, &ec);
-        if ((i<2 || i>0x7FFE || ((i&1)!=0)) && U_SUCCESS(ec)) {
+
+        // Post-32bit second transitions
+        ures_getByKey(res, kTRANSPOST32, &r, &ec);
+        transitionTimesPost32 = ures_getIntVector(&r, &len, &ec);
+        transitionCountPost32 = len >> 1;
+        if (ec == U_MISSING_RESOURCE_ERROR) {
+            // No pre-32bit transitions
+            transitionTimesPost32 = NULL;
+            transitionCountPost32 = 0;
+            ec = U_ZERO_ERROR;
+        } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF || (len & 1) != 0) /* len must be even */) {
             ec = U_INVALID_FORMAT_ERROR;
         }
-        typeCount = (int16_t) i >> 1;
 
-        // Type data must be of the same size as the transitions list        
-        r = ures_getByIndex(res, 2, r, &ec);
-        int32_t len;
-        typeData = ures_getBinary(r, &len, &ec);
-        ures_close(r);
-        if (len != transitionCount && U_SUCCESS(ec)) {
+        // Type offsets list must be of even size, with size >= 2
+        ures_getByKey(res, kTYPEOFFSETS, &r, &ec);
+        typeOffsets = ures_getIntVector(&r, &len, &ec);
+        if (U_SUCCESS(ec) && (len < 2 || len > 0x7FFE || (len & 1) != 0)) {
             ec = U_INVALID_FORMAT_ERROR;
         }
-
-#if defined (U_DEBUG_TZ)
-        U_DEBUG_TZ_MSG(("OlsonTimeZone(%s) - size = %d, typecount %d transitioncount %d - err %s\n", ures_getKey((UResourceBundle*)res), size, typeCount,  transitionCount, u_errorName(ec)));
-        if(U_SUCCESS(ec)) {
-          int32_t jj;
-          for(jj=0;jj<transitionCount;jj++) {
-            int32_t year, month, dom, dow;
-            double millis=0;
-            double days = Math::floorDivide(((double)transitionTimes[jj])*1000.0, (double)U_MILLIS_PER_DAY, millis);
-            
-            Grego::dayToFields(days, year, month, dom, dow);
-            U_DEBUG_TZ_MSG(("   Transition %d:  time %d (%04d.%02d.%02d+%.1fh), typedata%d\n", jj, transitionTimes[jj],
-                            year, month+1, dom, (millis/kOneHour), typeData[jj]));
-//            U_DEBUG_TZ_MSG(("     offset%d\n", typeOffsets[jj]));
-            int16_t f = jj;
-            f <<= 1;
-            U_DEBUG_TZ_MSG(("     offsets[%d+%d]=(%d+%d)=(%d==%d)\n", (int)f,(int)f+1,(int)typeOffsets[f],(int)typeOffsets[f+1],(int)zoneOffset(jj),
-                (int)typeOffsets[f]+(int)typeOffsets[f+1]));
-          }
+        typeCount = (int16_t) len >> 1;
+
+        // Type map data must be of the same size as the transition count
+        typeMapData =  NULL;
+        if (transitionCount() > 0) {
+            ures_getByKey(res, kTYPEMAP, &r, &ec);
+            typeMapData = ures_getBinary(&r, &len, &ec);
+            if (ec == U_MISSING_RESOURCE_ERROR) {
+                // no type mapping data
+                ec = U_INVALID_FORMAT_ERROR;
+            } else if (U_SUCCESS(ec) && len != transitionCount()) {
+                ec = U_INVALID_FORMAT_ERROR;
+            }
         }
-#endif
 
         // Process final rule and data, if any
-        if (size >= 5) {
-            int32_t ruleidLen = 0;
-            const UChar* idUStr = ures_getStringByIndex(res, 3, &ruleidLen, &ec);
-            UnicodeString ruleid(TRUE, idUStr, ruleidLen);
-            r = ures_getByIndex(res, 4, NULL, &ec);
-            const int32_t* data = ures_getIntVector(r, &len, &ec);
-#if defined U_DEBUG_TZ
-            const char *rKey = ures_getKey(r);
-            const char *zKey = ures_getKey((UResourceBundle*)res);
-#endif
-            ures_close(r);
-            if (U_SUCCESS(ec)) {
-                if (data != 0 && len == 2) {
-                    int32_t rawOffset = data[0] * U_MILLIS_PER_SECOND;
-                    // Subtract one from the actual final year; we
-                    // actually store final year - 1, and compare
-                    // using > rather than >=.  This allows us to use
-                    // INT32_MAX as an exclusive upper limit for all
-                    // years, including INT32_MAX.
-                    U_ASSERT(data[1] > INT32_MIN);
-                    finalYear = data[1] - 1;
-                    // Also compute the millis for Jan 1, 0:00 GMT of the
-                    // finalYear.  This reduces runtime computations.
-                    finalMillis = Grego::fieldsToDay(data[1], 0, 1) * U_MILLIS_PER_DAY;
-                    U_DEBUG_TZ_MSG(("zone%s|%s: {%d,%d}, finalYear%d, finalMillis%.1lf\n",
-                                    zKey,rKey, data[0], data[1], finalYear, finalMillis));
-                    r = TimeZone::loadRule(top, ruleid, NULL, ec);
-                    if (U_SUCCESS(ec)) {
-                        // 3, 1, -1, 7200, 0, 9, -31, -1, 7200, 0, 3600
-                        data = ures_getIntVector(r, &len, &ec);
-                        if (U_SUCCESS(ec) && len == 11) {
-                            UnicodeString emptyStr;
-                            U_DEBUG_TZ_MSG(("zone%s, rule%s: {%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d}\n", zKey, ures_getKey(r), 
-                                          data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10]));
-                            finalZone = new SimpleTimeZone(rawOffset, emptyStr,
-                                (int8_t)data[0], (int8_t)data[1], (int8_t)data[2],
-                                data[3] * U_MILLIS_PER_SECOND,
-                                (SimpleTimeZone::TimeMode) data[4],
-                                (int8_t)data[5], (int8_t)data[6], (int8_t)data[7],
-                                data[8] * U_MILLIS_PER_SECOND,
-                                (SimpleTimeZone::TimeMode) data[9],
-                                data[10] * U_MILLIS_PER_SECOND, ec);
-                            // Make sure finalZone was created
-                            if (finalZone == NULL) {
-                                ec = U_MEMORY_ALLOCATION_ERROR;
-                            }
-                        } else {
-                            ec = U_INVALID_FORMAT_ERROR;
-                        }
-                    }
-                    ures_close(r);
+        const UChar *ruleIdUStr = ures_getStringByKey(res, kFINALRULE, &len, &ec);
+        ures_getByKey(res, kFINALRAW, &r, &ec);
+        int32_t ruleRaw = ures_getInt(&r, &ec);
+        ures_getByKey(res, kFINALYEAR, &r, &ec);
+        int32_t ruleYear = ures_getInt(&r, &ec);
+        if (U_SUCCESS(ec)) {
+            UnicodeString ruleID(TRUE, ruleIdUStr, len);
+            UResourceBundle *rule = TimeZone::loadRule(top, ruleID, NULL, ec);
+            const int32_t *ruleData = ures_getIntVector(rule, &len, &ec); 
+            if (U_SUCCESS(ec) && len == 11) {
+                UnicodeString emptyStr;
+                finalZone = new SimpleTimeZone(
+                    ruleRaw * U_MILLIS_PER_SECOND,
+                    emptyStr,
+                    (int8_t)ruleData[0], (int8_t)ruleData[1], (int8_t)ruleData[2],
+                    ruleData[3] * U_MILLIS_PER_SECOND,
+                    (SimpleTimeZone::TimeMode) ruleData[4],
+                    (int8_t)ruleData[5], (int8_t)ruleData[6], (int8_t)ruleData[7],
+                    ruleData[8] * U_MILLIS_PER_SECOND,
+                    (SimpleTimeZone::TimeMode) ruleData[9],
+                    ruleData[10] * U_MILLIS_PER_SECOND, ec);
+                if (finalZone == NULL) {
+                    ec = U_MEMORY_ALLOCATION_ERROR;
                 } else {
-                    ec = U_INVALID_FORMAT_ERROR;
+                    finalStartYear = ruleYear;
+
+                    // Note: Setting finalStartYear to the finalZone is problematic.  When a date is around
+                    // year boundary, SimpleTimeZone may return false result when DST is observed at the 
+                    // beginning of year.  We could apply safe margin (day or two), but when one of recurrent
+                    // rules falls around year boundary, it could return false result.  Without setting the
+                    // start year, finalZone works fine around the year boundary of the start year.
+
+                    // finalZone->setStartYear(finalStartYear);
+
+
+                    // Compute the millis for Jan 1, 0:00 GMT of the finalYear
+
+                    // Note: finalStartMillis is used for detecting either if
+                    // historic transition data or finalZone to be used.  In an
+                    // extreme edge case - for example, two transitions fall into
+                    // small windows of time around the year boundary, this may
+                    // result incorrect offset computation.  But I think it will
+                    // never happen practically.  Yoshito - Feb 20, 2010
+                    finalStartMillis = Grego::fieldsToDay(finalStartYear, 0, 1) * U_MILLIS_PER_DAY;
                 }
+            } else {
+                ec = U_INVALID_FORMAT_ERROR;
             }
+            ures_close(rule);
+        } else if (ec == U_MISSING_RESOURCE_ERROR) {
+            // No final zone
+            ec = U_ZERO_ERROR;
         }
+        ures_close(&r);
     }
 
     if (U_FAILURE(ec)) {
@@ -228,17 +264,27 @@ OlsonTimeZone::OlsonTimeZone(const OlsonTimeZone& other) :
  * Assignment operator
  */
 OlsonTimeZone& OlsonTimeZone::operator=(const OlsonTimeZone& other) {
-    transitionCount = other.transitionCount;
+    transitionTimesPre32 = other.transitionTimesPre32;
+    transitionTimes32 = other.transitionTimes32;
+    transitionTimesPost32 = other.transitionTimesPost32;
+
+    transitionCountPre32 = other.transitionCountPre32;
+    transitionCount32 = other.transitionCount32;
+    transitionCountPost32 = other.transitionCountPost32;
+
     typeCount = other.typeCount;
-    transitionTimes = other.transitionTimes;
     typeOffsets = other.typeOffsets;
-    typeData = other.typeData;
-    finalYear = other.finalYear;
-    finalMillis = other.finalMillis;
+    typeMapData = other.typeMapData;
+
     delete finalZone;
     finalZone = (other.finalZone != 0) ?
         (SimpleTimeZone*) other.finalZone->clone() : 0;
+
+    finalStartYear = other.finalStartYear;
+    finalStartMillis = other.finalStartMillis;
+
     clearTransitionRules();
+
     return *this;
 }
 
@@ -255,7 +301,7 @@ OlsonTimeZone::~OlsonTimeZone() {
  */
 UBool OlsonTimeZone::operator==(const TimeZone& other) const {
     return ((this == &other) ||
-            (getDynamicClassID() == other.getDynamicClassID() &&
+            (typeid(*this) == typeid(other) &&
             TimeZone::operator==(other) &&
             hasSameRules(other)));
 }
@@ -315,8 +361,7 @@ int32_t OlsonTimeZone::getOffset(uint8_t era, int32_t year, int32_t month,
         year = -year;
     }
 
-    if (year > finalYear) { // [sic] >, not >=; see above
-        U_ASSERT(finalZone != 0);
+    if (finalZone != NULL && year >= finalStartYear) {
         return finalZone->getOffset(era, year, month, dom, dow,
                                     millis, monthLength, ec);
     }
@@ -336,10 +381,7 @@ void OlsonTimeZone::getOffset(UDate date, UBool local, int32_t& rawoff,
     if (U_FAILURE(ec)) {
         return;
     }
-    // The check against finalMillis will suffice most of the time, except
-    // for the case in which finalMillis == DBL_MAX, date == DBL_MAX,
-    // and finalZone == 0.  For this case we add "&& finalZone != 0".
-    if (date >= finalMillis && finalZone != 0) {
+    if (finalZone != NULL && date >= finalStartMillis) {
         finalZone->getOffset(date, local, rawoff, dstoff, ec);
     } else {
         getHistoricalOffset(date, local, kFormer, kLatter, rawoff, dstoff);
@@ -352,7 +394,7 @@ OlsonTimeZone::getOffsetFromLocal(UDate date, int32_t nonExistingTimeOpt, int32_
     if (U_FAILURE(ec)) {
         return;
     }
-    if (date >= finalMillis && finalZone != 0) {
+    if (finalZone != NULL && date >= finalStartMillis) {
         finalZone->getOffsetFromLocal(date, nonExistingTimeOpt, duplicatedTimeOpt, rawoff, dstoff, ec);
     } else {
         getHistoricalOffset(date, TRUE, nonExistingTimeOpt, duplicatedTimeOpt, rawoff, dstoff);
@@ -385,7 +427,7 @@ int32_t OlsonTimeZone::getRawOffset() const {
 void printTime(double ms) {
             int32_t year, month, dom, dow;
             double millis=0;
-            double days = Math::floorDivide(((double)ms), (double)U_MILLIS_PER_DAY, millis);
+            double days = ClockMath::floorDivide(((double)ms), (double)U_MILLIS_PER_DAY, millis);
             
             Grego::dayToFields(days, year, month, dom, dow);
             U_DEBUG_TZ_MSG(("   getHistoricalOffset:  time %.1f (%04d.%02d.%02d+%.1fh)\n", ms,
@@ -393,6 +435,25 @@ void printTime(double ms) {
     }
 #endif
 
+int64_t
+OlsonTimeZone::transitionTimeInSeconds(int16_t transIdx) const {
+    U_ASSERT(transIdx >= 0 && transIdx < transitionCount()); 
+
+    if (transIdx < transitionCountPre32) {
+        return (((int64_t)((uint32_t)transitionTimesPre32[transIdx << 1])) << 32)
+            | ((int64_t)((uint32_t)transitionTimesPre32[(transIdx << 1) + 1]));
+    }
+
+    transIdx -= transitionCountPre32;
+    if (transIdx < transitionCount32) {
+        return (int64_t)transitionTimes32[transIdx];
+    }
+
+    transIdx -= transitionCount32;
+    return (((int64_t)((uint32_t)transitionTimesPost32[transIdx << 1])) << 32)
+        | ((int64_t)((uint32_t)transitionTimesPost32[(transIdx << 1) + 1]));
+}
+
 void
 OlsonTimeZone::getHistoricalOffset(UDate date, UBool local,
                                    int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt,
@@ -402,94 +463,75 @@ OlsonTimeZone::getHistoricalOffset(UDate date, UBool local,
 #if defined U_DEBUG_TZ
         printTime(date*1000.0);
 #endif
-    if (transitionCount != 0) {
+    int16_t transCount = transitionCount();
+
+    if (transCount > 0) {
         double sec = uprv_floor(date / U_MILLIS_PER_SECOND);
-        // Linear search from the end is the fastest approach, since
-        // most lookups will happen at/near the end.
-        int16_t i;
-        for (i = transitionCount - 1; i > 0; --i) {
-            int32_t transition = transitionTimes[i];
-
-            if (local) {
-                int32_t offsetBefore = zoneOffset(typeData[i-1]);
-                UBool dstBefore = dstOffset(typeData[i-1]) != 0;
-
-                int32_t offsetAfter = zoneOffset(typeData[i]);
-                UBool dstAfter = dstOffset(typeData[i]) != 0;
-
-                UBool dstToStd = dstBefore && !dstAfter;
-                UBool stdToDst = !dstBefore && dstAfter;
-                
-                if (offsetAfter - offsetBefore >= 0) {
-                    // Positive transition, which makes a non-existing local time range
-                    if (((NonExistingTimeOpt & kStdDstMask) == kStandard && dstToStd)
-                            || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && stdToDst)) {
-                        transition += offsetBefore;
-                    } else if (((NonExistingTimeOpt & kStdDstMask) == kStandard && stdToDst)
-                            || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && dstToStd)) {
-                        transition += offsetAfter;
-                    } else if ((NonExistingTimeOpt & kFormerLatterMask) == kLatter) {
-                        transition += offsetBefore;
-                    } else {
-                        // Interprets the time with rule before the transition,
-                        // default for non-existing time range
-                        transition += offsetAfter;
-                    }
-                } else {
-                    // Negative transition, which makes a duplicated local time range
-                    if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && dstToStd)
-                            || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && stdToDst)) {
-                        transition += offsetAfter;
-                    } else if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && stdToDst)
-                            || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && dstToStd)) {
-                        transition += offsetBefore;
-                    } else if ((DuplicatedTimeOpt & kFormerLatterMask) == kFormer) {
-                        transition += offsetBefore;
+        if (!local && sec < transitionTimeInSeconds(0)) {
+            // Before the first transition time
+            rawoff = initialRawOffset() * U_MILLIS_PER_SECOND;
+            dstoff = initialDstOffset() * U_MILLIS_PER_SECOND;
+        } else {
+            // Linear search from the end is the fastest approach, since
+            // most lookups will happen at/near the end.
+            int16_t transIdx;
+            for (transIdx = transCount - 1; transIdx >= 0; transIdx--) {
+                int64_t transition = transitionTimeInSeconds(transIdx);
+
+                if (local) {
+                    int32_t offsetBefore = zoneOffsetAt(transIdx - 1);
+                    UBool dstBefore = dstOffsetAt(transIdx - 1) != 0;
+
+                    int32_t offsetAfter = zoneOffsetAt(transIdx);
+                    UBool dstAfter = dstOffsetAt(transIdx) != 0;
+
+                    UBool dstToStd = dstBefore && !dstAfter;
+                    UBool stdToDst = !dstBefore && dstAfter;
+                    
+                    if (offsetAfter - offsetBefore >= 0) {
+                        // Positive transition, which makes a non-existing local time range
+                        if (((NonExistingTimeOpt & kStdDstMask) == kStandard && dstToStd)
+                                || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && stdToDst)) {
+                            transition += offsetBefore;
+                        } else if (((NonExistingTimeOpt & kStdDstMask) == kStandard && stdToDst)
+                                || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && dstToStd)) {
+                            transition += offsetAfter;
+                        } else if ((NonExistingTimeOpt & kFormerLatterMask) == kLatter) {
+                            transition += offsetBefore;
+                        } else {
+                            // Interprets the time with rule before the transition,
+                            // default for non-existing time range
+                            transition += offsetAfter;
+                        }
                     } else {
-                        // Interprets the time with rule after the transition,
-                        // default for duplicated local time range
-                        transition += offsetAfter;
+                        // Negative transition, which makes a duplicated local time range
+                        if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && dstToStd)
+                                || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && stdToDst)) {
+                            transition += offsetAfter;
+                        } else if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && stdToDst)
+                                || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && dstToStd)) {
+                            transition += offsetBefore;
+                        } else if ((DuplicatedTimeOpt & kFormerLatterMask) == kFormer) {
+                            transition += offsetBefore;
+                        } else {
+                            // Interprets the time with rule after the transition,
+                            // default for duplicated local time range
+                            transition += offsetAfter;
+                        }
                     }
                 }
+                if (sec >= transition) {
+                    break;
+                }
             }
-            if (sec >= transition) {
-                U_DEBUG_TZ_MSG(("Found@%d: time=%.1f, localtransition=%d (orig %d) dz %d\n", i, sec, transition, transitionTimes[i],
-                    zoneOffset(typeData[i-1])));
-#if defined U_DEBUG_TZ
-                printTime(transition*1000.0);
-                printTime(transitionTimes[i]*1000.0);
-#endif
-                break;
-            } else {
-                U_DEBUG_TZ_MSG(("miss@%d: time=%.1f, localtransition=%d (orig %d) dz %d\n", i, sec, transition, transitionTimes[i],
-                    zoneOffset(typeData[i-1])));
-#if defined U_DEBUG_TZ
-                printTime(transition*1000.0);
-                printTime(transitionTimes[i]*1000.0);
-#endif
-            }
+            // transIdx could be -1 when local=true
+            rawoff = rawOffsetAt(transIdx) * U_MILLIS_PER_SECOND;
+            dstoff = dstOffsetAt(transIdx) * U_MILLIS_PER_SECOND;
         }
-
-        U_ASSERT(i>=0 && i<transitionCount);
-
-        // Check invariants for GMT times; if these pass for GMT times
-        // the local logic should be working too.
-        U_ASSERT(local || sec < transitionTimes[0] || sec >= transitionTimes[i]);
-        U_ASSERT(local || i == transitionCount-1 || sec < transitionTimes[i+1]);
-
-        U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst) - trans %d\n",
-            date, local?"T":"F", NonExistingTimeOpt, DuplicatedTimeOpt, i));
-
-        // Since ICU tzdata 2007c, the first transition data is actually not a
-        // transition, but used for representing the initial offset.  So the code
-        // below works even if i == 0.
-        int16_t index = typeData[i];
-        rawoff = rawOffset(index) * U_MILLIS_PER_SECOND;
-        dstoff = dstOffset(index) * U_MILLIS_PER_SECOND;
     } else {
         // No transitions, single pair of offsets only
-        rawoff = rawOffset(0) * U_MILLIS_PER_SECOND;
-        dstoff = dstOffset(0) * U_MILLIS_PER_SECOND;
+        rawoff = initialRawOffset() * U_MILLIS_PER_SECOND;
+        dstoff = initialDstOffset() * U_MILLIS_PER_SECOND;
     }
     U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst) - raw=%d, dst=%d\n",
         date, local?"T":"F", NonExistingTimeOpt, DuplicatedTimeOpt, rawoff, dstoff));
@@ -505,29 +547,27 @@ UBool OlsonTimeZone::useDaylightTime() const {
     // DST is in use in the current year (at any point in the year)
     // and returns TRUE if so.
 
-    int32_t days = (int32_t)Math::floorDivide(uprv_getUTCtime(), (double)U_MILLIS_PER_DAY); // epoch days
-
-    int32_t year, month, dom, dow;
-    
-    Grego::dayToFields(days, year, month, dom, dow);
-
-    if (year > finalYear) { // [sic] >, not >=; see above
-        U_ASSERT(finalZone != 0 && finalZone->useDaylightTime());
-        return TRUE;
+    UDate current = uprv_getUTCtime();
+    if (finalZone != NULL && current >= finalStartMillis) {
+        return finalZone->useDaylightTime();
     }
 
+    int32_t year, month, dom, dow, doy, mid;
+    Grego::timeToFields(current, year, month, dom, dow, doy, mid);
+
     // Find start of this year, and start of next year
-    int32_t start = (int32_t) Grego::fieldsToDay(year, 0, 1) * SECONDS_PER_DAY;    
-    int32_t limit = (int32_t) Grego::fieldsToDay(year+1, 0, 1) * SECONDS_PER_DAY;    
+    double start = Grego::fieldsToDay(year, 0, 1) * SECONDS_PER_DAY;
+    double limit = Grego::fieldsToDay(year+1, 0, 1) * SECONDS_PER_DAY;
 
     // Return TRUE if DST is observed at any time during the current
     // year.
-    for (int16_t i=0; i<transitionCount; ++i) {
-        if (transitionTimes[i] >= limit) {
+    for (int16_t i = 0; i < transitionCount(); ++i) {
+        double transition = transitionTime(i);
+        if (transition >= limit) {
             break;
         }
-        if ((transitionTimes[i] >= start && dstOffset(typeData[i]) != 0)
-                || (transitionTimes[i] > start && i > 0 && dstOffset(typeData[i - 1]) != 0)) {
+        if ((transition >= start && dstOffsetAt(i) != 0)
+                || (transition > start && dstOffsetAt(i - 1) != 0)) {
             return TRUE;
         }
     }
@@ -535,7 +575,7 @@ UBool OlsonTimeZone::useDaylightTime() const {
 }
 int32_t 
 OlsonTimeZone::getDSTSavings() const{
-    if(finalZone!=NULL){
+    if (finalZone != NULL){
         return finalZone->getDSTSavings();
     }
     return TimeZone::getDSTSavings();
@@ -554,34 +594,44 @@ OlsonTimeZone::hasSameRules(const TimeZone &other) const {
     if (this == &other) {
         return TRUE;
     }
-    if (other.getDynamicClassID() != OlsonTimeZone::getStaticClassID()) {
+    const OlsonTimeZone* z = dynamic_cast<const OlsonTimeZone*>(&other);
+    if (z == NULL) {
         return FALSE;
     }
-    const OlsonTimeZone* z = (const OlsonTimeZone*) &other;
 
-    // [sic] pointer comparison: typeData points into
+    // [sic] pointer comparison: typeMapData points into
     // memory-mapped or DLL space, so if two zones have the same
     // pointer, they are equal.
-    if (typeData == z->typeData) {
+    if (typeMapData == z->typeMapData) {
         return TRUE;
     }
     
-     // If the pointers are not equal, the zones may still
-     // be equal if their rules and transitions are equal
+    // If the pointers are not equal, the zones may still
+    // be equal if their rules and transitions are equal
+    if ((finalZone == NULL && z->finalZone != NULL)
+        || (finalZone != NULL && z->finalZone == NULL)
+        || (finalZone != NULL && z->finalZone != NULL && *finalZone != *z->finalZone)) {
+        return FALSE;
+    }
+
+    if (finalZone != NULL) {
+        if (finalStartYear != z->finalStartYear || finalStartMillis != z->finalStartMillis) {
+            return FALSE;
+        }
+    }
+    if (typeCount != z->typeCount
+        || transitionCountPre32 != z->transitionCountPre32
+        || transitionCount32 != z->transitionCount32
+        || transitionCountPost32 != z->transitionCountPost32) {
+        return FALSE;
+    }
+
     return
-        (finalYear == z->finalYear &&
-          // Don't compare finalMillis; if finalYear is ==, so is finalMillis
-          ((finalZone == 0 && z->finalZone == 0) ||
-            (finalZone != 0 && z->finalZone != 0 && *finalZone == *z->finalZone)) &&
-          
-          transitionCount == z->transitionCount &&
-          typeCount == z->typeCount &&
-          uprv_memcmp(transitionTimes, z->transitionTimes,
-                      sizeof(transitionTimes[0]) * transitionCount) == 0 &&
-          uprv_memcmp(typeOffsets, z->typeOffsets,
-                      (sizeof(typeOffsets[0]) * typeCount) << 1) == 0 &&
-          uprv_memcmp(typeData, z->typeData,
-                      (sizeof(typeData[0]) * typeCount)) == 0);
+        arrayEqual(transitionTimesPre32, z->transitionTimesPre32, sizeof(transitionTimesPre32[0]) * transitionCountPre32 << 1)
+        && arrayEqual(transitionTimes32, z->transitionTimes32, sizeof(transitionTimes32[0]) * transitionCount32)
+        && arrayEqual(transitionTimesPost32, z->transitionTimesPost32, sizeof(transitionTimesPost32[0]) * transitionCountPost32 << 1)
+        && arrayEqual(typeOffsets, z->typeOffsets, sizeof(typeOffsets[0]) * typeCount << 1)
+        && arrayEqual(typeMapData, z->typeMapData, sizeof(typeMapData[0]) * transitionCount());
 }
 
 void
@@ -637,36 +687,37 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
     UnicodeString dstName = tzid + UNICODE_STRING_SIMPLE("(DST)");
 
     int32_t raw, dst;
-    if (transitionCount > 0) {
-        int16_t transitionIdx, typeIdx;
 
-        // Note: Since 2007c, the very first transition data is a dummy entry
-        //       added for resolving a offset calculation problem.
+    // Create initial rule
+    raw = initialRawOffset() * U_MILLIS_PER_SECOND;
+    dst = initialDstOffset() * U_MILLIS_PER_SECOND;
+    initialRule = new InitialTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst);
+    // Check to make sure initialRule was created
+    if (initialRule == NULL) {
+        status = U_MEMORY_ALLOCATION_ERROR;
+        deleteTransitionRules();
+        return;
+    }
 
-        // Create initial rule
-        typeIdx = (int16_t)typeData[0]; // initial type
-        raw = rawOffset(typeIdx) * U_MILLIS_PER_SECOND;
-        dst = dstOffset(typeIdx) * U_MILLIS_PER_SECOND;
-        initialRule = new InitialTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst);
-        // Check to make sure initialRule was created
-        if (initialRule == NULL) {
-            status = U_MEMORY_ALLOCATION_ERROR;
-            deleteTransitionRules();
-            return;
-        }
+    int32_t transCount = transitionCount();
+    if (transCount > 0) {
+        int16_t transitionIdx, typeIdx;
 
+        // We probably no longer need to check the first "real" transition
+        // here, because the new tzcode remove such transitions already.
+        // For now, keeping this code for just in case. Feb 19, 2010 Yoshito
         firstTZTransitionIdx = 0;
-        for (transitionIdx = 1; transitionIdx < transitionCount; transitionIdx++) {
-            firstTZTransitionIdx++;
-            if (typeIdx != (int16_t)typeData[transitionIdx]) {
+        for (transitionIdx = 0; transitionIdx < transCount; transitionIdx++) {
+            if (typeMapData[transitionIdx] != 0) { // type 0 is the initial type
                 break;
             }
+            firstTZTransitionIdx++;
         }
-        if (transitionIdx == transitionCount) {
+        if (transitionIdx == transCount) {
             // Actually no transitions...
         } else {
             // Build historic rule array
-            UDate* times = (UDate*)uprv_malloc(sizeof(UDate)*transitionCount); /* large enough to store all transition times */
+            UDate* times = (UDate*)uprv_malloc(sizeof(UDate)*transCount); /* large enough to store all transition times */
             if (times == NULL) {
                 status = U_MEMORY_ALLOCATION_ERROR;
                 deleteTransitionRules();
@@ -675,10 +726,10 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
             for (typeIdx = 0; typeIdx < typeCount; typeIdx++) {
                 // Gather all start times for each pair of offsets
                 int32_t nTimes = 0;
-                for (transitionIdx = firstTZTransitionIdx; transitionIdx < transitionCount; transitionIdx++) {
-                    if (typeIdx == (int16_t)typeData[transitionIdx]) {
-                        UDate tt = ((UDate)transitionTimes[transitionIdx]) * U_MILLIS_PER_SECOND;
-                        if (tt < finalMillis) {
+                for (transitionIdx = firstTZTransitionIdx; transitionIdx < transCount; transitionIdx++) {
+                    if (typeIdx == (int16_t)typeMapData[transitionIdx]) {
+                        UDate tt = (UDate)transitionTime(transitionIdx);
+                        if (finalZone == NULL || tt <= finalStartMillis) {
                             // Exclude transitions after finalMillis
                             times[nTimes++] = tt;
                         }
@@ -686,8 +737,8 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
                 }
                 if (nTimes > 0) {
                     // Create a TimeArrayTimeZoneRule
-                    raw = rawOffset(typeIdx) * U_MILLIS_PER_SECOND;
-                    dst = dstOffset(typeIdx) * U_MILLIS_PER_SECOND;
+                    raw = typeOffsets[typeIdx << 1] * U_MILLIS_PER_SECOND;
+                    dst = typeOffsets[(typeIdx << 1) + 1] * U_MILLIS_PER_SECOND;
                     if (historicRules == NULL) {
                         historicRuleCount = typeCount;
                         historicRules = (TimeArrayTimeZoneRule**)uprv_malloc(sizeof(TimeArrayTimeZoneRule*)*historicRuleCount);
@@ -715,8 +766,8 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
             uprv_free(times);
 
             // Create initial transition
-            typeIdx = (int16_t)typeData[firstTZTransitionIdx];
-            firstTZTransition = new TimeZoneTransition(((UDate)transitionTimes[firstTZTransitionIdx]) * U_MILLIS_PER_SECOND,
+            typeIdx = (int16_t)typeMapData[firstTZTransitionIdx];
+            firstTZTransition = new TimeZoneTransition((UDate)transitionTime(firstTZTransitionIdx),
                     *initialRule, *historicRules[typeIdx]);
             // Check to make sure firstTZTransition was created.
             if (firstTZTransition == NULL) {
@@ -726,30 +777,18 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
             }
         }
     }
-    if (initialRule == NULL) {
-        // No historic transitions
-        raw = rawOffset(0) * U_MILLIS_PER_SECOND;
-        dst = dstOffset(0) * U_MILLIS_PER_SECOND;
-        initialRule = new InitialTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst);
-        // Check to make sure initialRule was created.
-        if (initialRule == NULL) {
-            status = U_MEMORY_ALLOCATION_ERROR;
-            deleteTransitionRules();
-            return;
-        }
-    }
     if (finalZone != NULL) {
         // Get the first occurence of final rule starts
-        UDate startTime = (UDate)finalMillis;
+        UDate startTime = (UDate)finalStartMillis;
         TimeZoneRule *firstFinalRule = NULL;
+
         if (finalZone->useDaylightTime()) {
             /*
              * Note: When an OlsonTimeZone is constructed, we should set the final year
              * as the start year of finalZone.  However, the bounday condition used for
-             * getting offset from finalZone has some problems.  So setting the start year
-             * in the finalZone will cause a problem.  For now, we do not set the valid
-             * start year when the construction time and create a clone and set the
-             * start year when extracting rules.
+             * getting offset from finalZone has some problems.
+             * For now, we do not set the valid start year when the construction time
+             * and create a clone and set the start year when extracting rules.
              */
             finalZoneWithStartYear = (SimpleTimeZone*)finalZone->clone();
             // Check to make sure finalZone was actually cloned.
@@ -758,9 +797,7 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
                 deleteTransitionRules();
                 return;
             }
-            // finalYear is 1 year before the actual final year.
-            // See the comment in the construction method.
-            finalZoneWithStartYear->setStartYear(finalYear + 1);
+            finalZoneWithStartYear->setStartYear(finalStartYear);
 
             TimeZoneTransition tzt;
             finalZoneWithStartYear->getNextTransition(startTime, false, tzt);
@@ -773,8 +810,9 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
             }
             startTime = tzt.getTime();
         } else {
+            // final rule with no transitions
             finalZoneWithStartYear = (SimpleTimeZone*)finalZone->clone();
-            // Check to make sure finalZoneWithStartYear received proper clone before dereference.
+            // Check to make sure finalZone was actually cloned.
             if (finalZoneWithStartYear == NULL) {
                 status = U_MEMORY_ALLOCATION_ERROR;
                 deleteTransitionRules();
@@ -791,8 +829,8 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
             }
         }
         TimeZoneRule *prevRule = NULL;
-        if (transitionCount > 0) {
-            prevRule = historicRules[typeData[transitionCount - 1]];
+        if (transCount > 0) {
+            prevRule = historicRules[typeMapData[transCount - 1]];
         }
         if (prevRule == NULL) {
             // No historic transitions, but only finalZone available
@@ -836,14 +874,15 @@ OlsonTimeZone::getNextTransition(UDate base, UBool inclusive, TimeZoneTransition
     }
     if (historicRules != NULL) {
         // Find a historical transition
-        int16_t ttidx = transitionCount - 1;
+        int16_t transCount = transitionCount();
+        int16_t ttidx = transCount - 1;
         for (; ttidx >= firstTZTransitionIdx; ttidx--) {
-            UDate t = ((UDate)transitionTimes[ttidx]) * U_MILLIS_PER_SECOND;
+            UDate t = (UDate)transitionTime(ttidx);
             if (base > t || (!inclusive && base == t)) {
                 break;
             }
         }
-        if (ttidx == transitionCount - 1)  {
+        if (ttidx == transCount - 1)  {
             if (firstFinalTZTransition != NULL) {
                 result = *firstFinalTZTransition;
                 return TRUE;
@@ -855,9 +894,9 @@ OlsonTimeZone::getNextTransition(UDate base, UBool inclusive, TimeZoneTransition
             return TRUE;
         } else {
             // Create a TimeZoneTransition
-            TimeZoneRule *to = historicRules[typeData[ttidx + 1]];
-            TimeZoneRule *from = historicRules[typeData[ttidx]];
-            UDate startTime = ((UDate)transitionTimes[ttidx+1]) * U_MILLIS_PER_SECOND;
+            TimeZoneRule *to = historicRules[typeMapData[ttidx + 1]];
+            TimeZoneRule *from = historicRules[typeMapData[ttidx]];
+            UDate startTime = (UDate)transitionTime(ttidx+1);
 
             // The transitions loaded from zoneinfo.res may contain non-transition data
             UnicodeString fromName, toName;
@@ -895,15 +934,15 @@ OlsonTimeZone::getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransi
             } else {
                 result = *firstFinalTZTransition;
                 return TRUE;
-            }                
+            }
         }
     }
 
     if (historicRules != NULL) {
         // Find a historical transition
-        int16_t ttidx = transitionCount - 1;
+        int16_t ttidx = transitionCount() - 1;
         for (; ttidx >= firstTZTransitionIdx; ttidx--) {
-            UDate t = ((UDate)transitionTimes[ttidx]) * U_MILLIS_PER_SECOND;
+            UDate t = (UDate)transitionTime(ttidx);
             if (base > t || (inclusive && base == t)) {
                 break;
             }
@@ -916,9 +955,9 @@ OlsonTimeZone::getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransi
             return TRUE;
         } else {
             // Create a TimeZoneTransition
-            TimeZoneRule *to = historicRules[typeData[ttidx]];
-            TimeZoneRule *from = historicRules[typeData[ttidx-1]];
-            UDate startTime = ((UDate)transitionTimes[ttidx]) * U_MILLIS_PER_SECOND;
+            TimeZoneRule *to = historicRules[typeMapData[ttidx]];
+            TimeZoneRule *from = historicRules[typeMapData[ttidx-1]];
+            UDate startTime = (UDate)transitionTime(ttidx);
 
             // The transitions loaded from zoneinfo.res may contain non-transition data
             UnicodeString fromName, toName;