]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/olsontz.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / i18n / olsontz.cpp
index 244876b065b2409d25946ed83023667788fe8eae..4873caaee6d45724afa85fd92b8b283767d6392b 100644 (file)
@@ -1,3 +1,5 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 **********************************************************************
 * Copyright (c) 2003-2013, International Business Machines
@@ -23,7 +25,7 @@
 #include "uassert.h"
 #include "uvector.h"
 #include <float.h> // DBL_MAX
-#include "uresimp.h" // struct UResourceBundle
+#include "uresimp.h"
 #include "zonemeta.h"
 #include "umutex.h"
 
@@ -132,13 +134,12 @@ OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
         //        setID(ures_getKey((UResourceBundle*) res)); // cast away const
 
         int32_t len;
-        UResourceBundle r;
-        ures_initStackObject(&r);
+        StackUResourceBundle r;
 
         // Pre-32bit second transitions
-        ures_getByKey(res, kTRANSPRE32, &r, &ec);
-        transitionTimesPre32 = ures_getIntVector(&r, &len, &ec);
-        transitionCountPre32 = len >> 1;
+        ures_getByKey(res, kTRANSPRE32, r.getAlias(), &ec);
+        transitionTimesPre32 = ures_getIntVector(r.getAlias(), &len, &ec);
+        transitionCountPre32 = static_cast<int16_t>(len >> 1);
         if (ec == U_MISSING_RESOURCE_ERROR) {
             // No pre-32bit transitions
             transitionTimesPre32 = NULL;
@@ -149,9 +150,9 @@ OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
         }
 
         // 32bit second transitions
-        ures_getByKey(res, kTRANS, &r, &ec);
-        transitionTimes32 = ures_getIntVector(&r, &len, &ec);
-        transitionCount32 = len;
+        ures_getByKey(res, kTRANS, r.getAlias(), &ec);
+        transitionTimes32 = ures_getIntVector(r.getAlias(), &len, &ec);
+        transitionCount32 = static_cast<int16_t>(len);
         if (ec == U_MISSING_RESOURCE_ERROR) {
             // No 32bit transitions
             transitionTimes32 = NULL;
@@ -162,9 +163,9 @@ OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
         }
 
         // Post-32bit second transitions
-        ures_getByKey(res, kTRANSPOST32, &r, &ec);
-        transitionTimesPost32 = ures_getIntVector(&r, &len, &ec);
-        transitionCountPost32 = len >> 1;
+        ures_getByKey(res, kTRANSPOST32, r.getAlias(), &ec);
+        transitionTimesPost32 = ures_getIntVector(r.getAlias(), &len, &ec);
+        transitionCountPost32 = static_cast<int16_t>(len >> 1);
         if (ec == U_MISSING_RESOURCE_ERROR) {
             // No pre-32bit transitions
             transitionTimesPost32 = NULL;
@@ -175,8 +176,8 @@ OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
         }
 
         // Type offsets list must be of even size, with size >= 2
-        ures_getByKey(res, kTYPEOFFSETS, &r, &ec);
-        typeOffsets = ures_getIntVector(&r, &len, &ec);
+        ures_getByKey(res, kTYPEOFFSETS, r.getAlias(), &ec);
+        typeOffsets = ures_getIntVector(r.getAlias(), &len, &ec);
         if (U_SUCCESS(ec) && (len < 2 || len > 0x7FFE || (len & 1) != 0)) {
             ec = U_INVALID_FORMAT_ERROR;
         }
@@ -185,8 +186,8 @@ OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
         // 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);
+            ures_getByKey(res, kTYPEMAP, r.getAlias(), &ec);
+            typeMapData = ures_getBinary(r.getAlias(), &len, &ec);
             if (ec == U_MISSING_RESOURCE_ERROR) {
                 // no type mapping data
                 ec = U_INVALID_FORMAT_ERROR;
@@ -196,60 +197,61 @@ OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
         }
 
         // Process final rule and data, if any
-        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 {
-                    finalStartYear = ruleYear;
+            const UChar *ruleIdUStr = ures_getStringByKey(res, kFINALRULE, &len, &ec);
+            ures_getByKey(res, kFINALRAW, r.getAlias(), &ec);
+            int32_t ruleRaw = ures_getInt(r.getAlias(), &ec);
+            ures_getByKey(res, kFINALYEAR, r.getAlias(), &ec);
+            int32_t ruleYear = ures_getInt(r.getAlias(), &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 {
+                        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.
+                        // 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);
+                        // finalZone->setStartYear(finalStartYear);
 
 
-                    // Compute the millis for Jan 1, 0:00 GMT of the finalYear
+                        // 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;
+                        // 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;
                 }
-            } 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(rule);
-        } else if (ec == U_MISSING_RESOURCE_ERROR) {
-            // No final zone
-            ec = U_ZERO_ERROR;
         }
-        ures_close(&r);
 
         // initialize canonical ID
         canonicalID = ZoneMeta::getCanonicalCLDRID(tzid, ec);
@@ -287,8 +289,7 @@ OlsonTimeZone& OlsonTimeZone::operator=(const OlsonTimeZone& other) {
     typeMapData = other.typeMapData;
 
     delete finalZone;
-    finalZone = (other.finalZone != 0) ?
-        (SimpleTimeZone*) other.finalZone->clone() : 0;
+    finalZone = (other.finalZone != 0) ? other.finalZone->clone() : 0;
 
     finalStartYear = other.finalStartYear;
     finalStartMillis = other.finalStartMillis;
@@ -319,7 +320,7 @@ UBool OlsonTimeZone::operator==(const TimeZone& other) const {
 /**
  * TimeZone API.
  */
-TimeZone* OlsonTimeZone::clone() const {
+OlsonTimeZone* OlsonTimeZone::clone() const {
     return new OlsonTimeZone(*this);
 }
 
@@ -816,7 +817,7 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
              * 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();
+            finalZoneWithStartYear = finalZone->clone();
             // Check to make sure finalZone was actually cloned.
             if (finalZoneWithStartYear == NULL) {
                 status = U_MEMORY_ALLOCATION_ERROR;
@@ -837,7 +838,7 @@ OlsonTimeZone::initTransitionRules(UErrorCode& status) {
             startTime = tzt.getTime();
         } else {
             // final rule with no transitions
-            finalZoneWithStartYear = (SimpleTimeZone*)finalZone->clone();
+            finalZoneWithStartYear = finalZone->clone();
             // Check to make sure finalZone was actually cloned.
             if (finalZoneWithStartYear == NULL) {
                 status = U_MEMORY_ALLOCATION_ERROR;