1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/tools/tzcode/tz2icu.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1761 @@ 1.4 + 1.5 +/* 1.6 +********************************************************************** 1.7 +* Copyright (c) 2003-2010, International Business Machines 1.8 +* Corporation and others. All Rights Reserved. 1.9 +********************************************************************** 1.10 +* Author: Alan Liu 1.11 +* Created: July 10 2003 1.12 +* Since: ICU 2.8 1.13 +********************************************************************** 1.14 +*/ 1.15 +#include "tzfile.h" // from Olson tzcode archive, copied to this dir 1.16 + 1.17 +#ifdef WIN32 1.18 + 1.19 + #include <windows.h> 1.20 + #undef min // windows.h/STL conflict 1.21 + #undef max // windows.h/STL conflict 1.22 + // "identifier was truncated to 'number' characters" warning 1.23 + #pragma warning(disable: 4786) 1.24 + 1.25 +#else 1.26 + 1.27 + #include <unistd.h> 1.28 + #include <stdio.h> 1.29 + #include <dirent.h> 1.30 + #include <string.h> 1.31 + #include <sys/stat.h> 1.32 + 1.33 +#endif 1.34 + 1.35 +#include <algorithm> 1.36 +#include <cassert> 1.37 +#include <ctime> 1.38 +#include <fstream> 1.39 +#include <iomanip> 1.40 +#include <iostream> 1.41 +#include <iterator> 1.42 +#include <limits> 1.43 +#include <map> 1.44 +#include <set> 1.45 +#include <sstream> 1.46 +#include <sstream> 1.47 +#include <stdexcept> 1.48 +#include <string> 1.49 +#include <vector> 1.50 + 1.51 +#include "tz2icu.h" 1.52 +#include "unicode/uversion.h" 1.53 + 1.54 +using namespace std; 1.55 + 1.56 +bool ICU44PLUS = TRUE; 1.57 +string TZ_RESOURCE_NAME = ICU_TZ_RESOURCE; 1.58 + 1.59 +//-------------------------------------------------------------------- 1.60 +// Time utilities 1.61 +//-------------------------------------------------------------------- 1.62 + 1.63 +const int64_t SECS_PER_YEAR = 31536000; // 365 days 1.64 +const int64_t SECS_PER_LEAP_YEAR = 31622400; // 366 days 1.65 +const int64_t LOWEST_TIME32 = (int64_t)((int32_t)0x80000000); 1.66 +const int64_t HIGHEST_TIME32 = (int64_t)((int32_t)0x7fffffff); 1.67 + 1.68 +bool isLeap(int32_t y) { 1.69 + return (y%4 == 0) && ((y%100 != 0) || (y%400 == 0)); // Gregorian 1.70 +} 1.71 + 1.72 +int64_t secsPerYear(int32_t y) { 1.73 + return isLeap(y) ? SECS_PER_LEAP_YEAR : SECS_PER_YEAR; 1.74 +} 1.75 + 1.76 +/** 1.77 + * Given a calendar year, return the GMT epoch seconds for midnight 1.78 + * GMT of January 1 of that year. yearToSeconds(1970) == 0. 1.79 + */ 1.80 +int64_t yearToSeconds(int32_t year) { 1.81 + // inefficient but foolproof 1.82 + int64_t s = 0; 1.83 + int32_t y = 1970; 1.84 + while (y < year) { 1.85 + s += secsPerYear(y++); 1.86 + } 1.87 + while (y > year) { 1.88 + s -= secsPerYear(--y); 1.89 + } 1.90 + return s; 1.91 +} 1.92 + 1.93 +/** 1.94 + * Given 1970 GMT epoch seconds, return the calendar year containing 1.95 + * that time. secondsToYear(0) == 1970. 1.96 + */ 1.97 +int32_t secondsToYear(int64_t seconds) { 1.98 + // inefficient but foolproof 1.99 + int32_t y = 1970; 1.100 + int64_t s = 0; 1.101 + if (seconds >= 0) { 1.102 + for (;;) { 1.103 + s += secsPerYear(y++); 1.104 + if (s > seconds) break; 1.105 + } 1.106 + --y; 1.107 + } else { 1.108 + for (;;) { 1.109 + s -= secsPerYear(--y); 1.110 + if (s <= seconds) break; 1.111 + } 1.112 + } 1.113 + return y; 1.114 +} 1.115 + 1.116 +//-------------------------------------------------------------------- 1.117 +// Types 1.118 +//-------------------------------------------------------------------- 1.119 + 1.120 +struct FinalZone; 1.121 +struct FinalRule; 1.122 +struct SimplifiedZoneType; 1.123 + 1.124 +// A transition from one ZoneType to another 1.125 +// Minimal size = 5 bytes (4+1) 1.126 +struct Transition { 1.127 + int64_t time; // seconds, 1970 epoch 1.128 + int32_t type; // index into 'ZoneInfo.types' 0..255 1.129 + Transition(int64_t _time, int32_t _type) { 1.130 + time = _time; 1.131 + type = _type; 1.132 + } 1.133 +}; 1.134 + 1.135 +// A behavior mode (what zic calls a 'type') of a time zone. 1.136 +// Minimal size = 6 bytes (4+1+3bits) 1.137 +// SEE: SimplifiedZoneType 1.138 +struct ZoneType { 1.139 + int64_t rawoffset; // raw seconds offset from GMT 1.140 + int64_t dstoffset; // dst seconds offset from GMT 1.141 + 1.142 + // We don't really need any of the following, but they are 1.143 + // retained for possible future use. See SimplifiedZoneType. 1.144 + int32_t abbr; // index into ZoneInfo.abbrs 0..n-1 1.145 + bool isdst; 1.146 + bool isstd; 1.147 + bool isgmt; 1.148 + 1.149 + ZoneType(const SimplifiedZoneType&); // used by optimizeTypeList 1.150 + 1.151 + ZoneType() : rawoffset(-1), dstoffset(-1), abbr(-1) {} 1.152 + 1.153 + // A restricted equality, of just the raw and dst offset 1.154 + bool matches(const ZoneType& other) { 1.155 + return rawoffset == other.rawoffset && 1.156 + dstoffset == other.dstoffset; 1.157 + } 1.158 +}; 1.159 + 1.160 +// A collection of transitions from one ZoneType to another, together 1.161 +// with a list of the ZoneTypes. A ZoneInfo object may have a long 1.162 +// list of transitions between a smaller list of ZoneTypes. 1.163 +// 1.164 +// This object represents the contents of a single zic-created 1.165 +// zoneinfo file. 1.166 +struct ZoneInfo { 1.167 + vector<Transition> transitions; 1.168 + vector<ZoneType> types; 1.169 + vector<string> abbrs; 1.170 + 1.171 + string finalRuleID; 1.172 + int32_t finalOffset; 1.173 + int32_t finalYear; // -1 if none 1.174 + 1.175 + // If this is an alias, then all other fields are meaningless, and 1.176 + // this field will point to the "real" zone 0..n-1. 1.177 + int32_t aliasTo; // -1 if this is a "real" zone 1.178 + 1.179 + // If there are aliases TO this zone, then the following set will 1.180 + // contain their index numbers (each index >= 0). 1.181 + set<int32_t> aliases; 1.182 + 1.183 + ZoneInfo() : finalYear(-1), aliasTo(-1) {} 1.184 + 1.185 + void mergeFinalData(const FinalZone& fz); 1.186 + 1.187 + void optimizeTypeList(); 1.188 + 1.189 + // Set this zone to be an alias TO another zone. 1.190 + void setAliasTo(int32_t index); 1.191 + 1.192 + // Clear the list of aliases OF this zone. 1.193 + void clearAliases(); 1.194 + 1.195 + // Add an alias to the list of aliases OF this zone. 1.196 + void addAlias(int32_t index); 1.197 + 1.198 + // Is this an alias to another zone? 1.199 + bool isAlias() const { 1.200 + return aliasTo >= 0; 1.201 + } 1.202 + 1.203 + // Retrieve alias list 1.204 + const set<int32_t>& getAliases() const { 1.205 + return aliases; 1.206 + } 1.207 + 1.208 + void print(ostream& os, const string& id) const; 1.209 +}; 1.210 + 1.211 +void ZoneInfo::clearAliases() { 1.212 + assert(aliasTo < 0); 1.213 + aliases.clear(); 1.214 +} 1.215 + 1.216 +void ZoneInfo::addAlias(int32_t index) { 1.217 + assert(aliasTo < 0 && index >= 0 && aliases.find(index) == aliases.end()); 1.218 + aliases.insert(index); 1.219 +} 1.220 + 1.221 +void ZoneInfo::setAliasTo(int32_t index) { 1.222 + assert(index >= 0); 1.223 + assert(aliases.size() == 0); 1.224 + aliasTo = index; 1.225 +} 1.226 + 1.227 +typedef map<string, ZoneInfo> ZoneMap; 1.228 + 1.229 +typedef ZoneMap::const_iterator ZoneMapIter; 1.230 + 1.231 +//-------------------------------------------------------------------- 1.232 +// ZONEINFO 1.233 +//-------------------------------------------------------------------- 1.234 + 1.235 +// Global map holding all our ZoneInfo objects, indexed by id. 1.236 +ZoneMap ZONEINFO; 1.237 + 1.238 +//-------------------------------------------------------------------- 1.239 +// zoneinfo file parsing 1.240 +//-------------------------------------------------------------------- 1.241 + 1.242 +// Read zic-coded 32-bit integer from file 1.243 +int64_t readcoded(ifstream& file, int64_t minv=numeric_limits<int64_t>::min(), 1.244 + int64_t maxv=numeric_limits<int64_t>::max()) { 1.245 + unsigned char buf[4]; // must be UNSIGNED 1.246 + int64_t val=0; 1.247 + file.read((char*)buf, 4); 1.248 + for(int32_t i=0,shift=24;i<4;++i,shift-=8) { 1.249 + val |= buf[i] << shift; 1.250 + } 1.251 + if (val < minv || val > maxv) { 1.252 + ostringstream os; 1.253 + os << "coded value out-of-range: " << val << ", expected [" 1.254 + << minv << ", " << maxv << "]"; 1.255 + throw out_of_range(os.str()); 1.256 + } 1.257 + return val; 1.258 +} 1.259 + 1.260 +// Read zic-coded 64-bit integer from file 1.261 +int64_t readcoded64(ifstream& file, int64_t minv=numeric_limits<int64_t>::min(), 1.262 + int64_t maxv=numeric_limits<int64_t>::max()) { 1.263 + unsigned char buf[8]; // must be UNSIGNED 1.264 + int64_t val=0; 1.265 + file.read((char*)buf, 8); 1.266 + for(int32_t i=0,shift=56;i<8;++i,shift-=8) { 1.267 + val |= (int64_t)buf[i] << shift; 1.268 + } 1.269 + if (val < minv || val > maxv) { 1.270 + ostringstream os; 1.271 + os << "coded value out-of-range: " << val << ", expected [" 1.272 + << minv << ", " << maxv << "]"; 1.273 + throw out_of_range(os.str()); 1.274 + } 1.275 + return val; 1.276 +} 1.277 + 1.278 +// Read a boolean value 1.279 +bool readbool(ifstream& file) { 1.280 + char c; 1.281 + file.read(&c, 1); 1.282 + if (c!=0 && c!=1) { 1.283 + ostringstream os; 1.284 + os << "boolean value out-of-range: " << (int32_t)c; 1.285 + throw out_of_range(os.str()); 1.286 + } 1.287 + return (c!=0); 1.288 +} 1.289 + 1.290 +/** 1.291 + * Read the zoneinfo file structure (see tzfile.h) into a ZoneInfo 1.292 + * @param file an already-open file stream 1.293 + */ 1.294 +void readzoneinfo(ifstream& file, ZoneInfo& info, bool is64bitData) { 1.295 + int32_t i; 1.296 + 1.297 + // Check for TZ_ICU_MAGIC signature at file start. If we get a 1.298 + // signature mismatch, it means we're trying to read a file which 1.299 + // isn't a ICU-modified-zic-created zoneinfo file. Typically this 1.300 + // means the user is passing in a "normal" zoneinfo directory, or 1.301 + // a zoneinfo directory that is polluted with other files, or that 1.302 + // the user passed in the wrong directory. 1.303 + char buf[32]; 1.304 + file.read(buf, 4); 1.305 + if (strncmp(buf, TZ_ICU_MAGIC, 4) != 0) { 1.306 + throw invalid_argument("TZ_ICU_MAGIC signature missing"); 1.307 + } 1.308 + // skip additional Olson byte version 1.309 + file.read(buf, 1); 1.310 + // if '\0', we have just one copy of data, if '2', there is additional 1.311 + // 64 bit version at the end. 1.312 + if(buf[0]!=0 && buf[0]!='2') { 1.313 + throw invalid_argument("Bad Olson version info"); 1.314 + } 1.315 + 1.316 + // Read reserved bytes. The first of these will be a version byte. 1.317 + file.read(buf, 15); 1.318 + if (*(ICUZoneinfoVersion*)&buf != TZ_ICU_VERSION) { 1.319 + throw invalid_argument("File version mismatch"); 1.320 + } 1.321 + 1.322 + // Read array sizes 1.323 + int64_t isgmtcnt = readcoded(file, 0); 1.324 + int64_t isdstcnt = readcoded(file, 0); 1.325 + int64_t leapcnt = readcoded(file, 0); 1.326 + int64_t timecnt = readcoded(file, 0); 1.327 + int64_t typecnt = readcoded(file, 0); 1.328 + int64_t charcnt = readcoded(file, 0); 1.329 + 1.330 + // Confirm sizes that we assume to be equal. These assumptions 1.331 + // are drawn from a reading of the zic source (2003a), so they 1.332 + // should hold unless the zic source changes. 1.333 + if (isgmtcnt != typecnt || isdstcnt != typecnt) { 1.334 + throw invalid_argument("count mismatch between tzh_ttisgmtcnt, tzh_ttisdstcnt, tth_typecnt"); 1.335 + } 1.336 + 1.337 + // Used temporarily to store transition times and types. We need 1.338 + // to do this because the times and types are stored in two 1.339 + // separate arrays. 1.340 + vector<int64_t> transitionTimes(timecnt, -1); // temporary 1.341 + vector<int32_t> transitionTypes(timecnt, -1); // temporary 1.342 + 1.343 + // Read transition times 1.344 + for (i=0; i<timecnt; ++i) { 1.345 + if (is64bitData) { 1.346 + transitionTimes[i] = readcoded64(file); 1.347 + } else { 1.348 + transitionTimes[i] = readcoded(file); 1.349 + } 1.350 + } 1.351 + 1.352 + // Read transition types 1.353 + for (i=0; i<timecnt; ++i) { 1.354 + unsigned char c; 1.355 + file.read((char*) &c, 1); 1.356 + int32_t t = (int32_t) c; 1.357 + if (t < 0 || t >= typecnt) { 1.358 + ostringstream os; 1.359 + os << "illegal type: " << t << ", expected [0, " << (typecnt-1) << "]"; 1.360 + throw out_of_range(os.str()); 1.361 + } 1.362 + transitionTypes[i] = t; 1.363 + } 1.364 + 1.365 + // Build transitions vector out of corresponding times and types. 1.366 + bool insertInitial = false; 1.367 + if (is64bitData && !ICU44PLUS) { 1.368 + if (timecnt > 0) { 1.369 + int32_t minidx = -1; 1.370 + for (i=0; i<timecnt; ++i) { 1.371 + if (transitionTimes[i] < LOWEST_TIME32) { 1.372 + if (minidx == -1 || transitionTimes[i] > transitionTimes[minidx]) { 1.373 + // Preserve the latest transition before the 32bit minimum time 1.374 + minidx = i; 1.375 + } 1.376 + } else if (transitionTimes[i] > HIGHEST_TIME32) { 1.377 + // Skipping the rest of the transition data. We cannot put such 1.378 + // transitions into zoneinfo.res, because data is limited to singed 1.379 + // 32bit int by the ICU resource bundle. 1.380 + break; 1.381 + } else { 1.382 + info.transitions.push_back(Transition(transitionTimes[i], transitionTypes[i])); 1.383 + } 1.384 + } 1.385 + 1.386 + if (minidx != -1) { 1.387 + // If there are any transitions before the 32bit minimum time, 1.388 + // put the type information with the 32bit minimum time 1.389 + vector<Transition>::iterator itr = info.transitions.begin(); 1.390 + info.transitions.insert(itr, Transition(LOWEST_TIME32, transitionTypes[minidx])); 1.391 + } else { 1.392 + // Otherwise, we need insert the initial type later 1.393 + insertInitial = true; 1.394 + } 1.395 + } 1.396 + } else { 1.397 + for (i=0; i<timecnt; ++i) { 1.398 + info.transitions.push_back(Transition(transitionTimes[i], transitionTypes[i])); 1.399 + } 1.400 + } 1.401 + 1.402 + // Read types (except for the isdst and isgmt flags, which come later (why??)) 1.403 + for (i=0; i<typecnt; ++i) { 1.404 + ZoneType type; 1.405 + 1.406 + type.rawoffset = readcoded(file); 1.407 + type.dstoffset = readcoded(file); 1.408 + type.isdst = readbool(file); 1.409 + 1.410 + unsigned char c; 1.411 + file.read((char*) &c, 1); 1.412 + type.abbr = (int32_t) c; 1.413 + 1.414 + if (type.isdst != (type.dstoffset != 0)) { 1.415 + throw invalid_argument("isdst does not reflect dstoffset"); 1.416 + } 1.417 + 1.418 + info.types.push_back(type); 1.419 + } 1.420 + 1.421 + assert(info.types.size() == (unsigned) typecnt); 1.422 + 1.423 + if (insertInitial) { 1.424 + assert(timecnt > 0); 1.425 + assert(typecnt > 0); 1.426 + 1.427 + int32_t initialTypeIdx = -1; 1.428 + 1.429 + // Check if the first type is not dst 1.430 + if (info.types.at(0).dstoffset != 0) { 1.431 + // Initial type's rawoffset is same with the rawoffset after the 1.432 + // first transition, but no DST is observed. 1.433 + int64_t rawoffset0 = (info.types.at(info.transitions.at(0).type)).rawoffset; 1.434 + // Look for matching type 1.435 + for (i=0; i<(int32_t)info.types.size(); ++i) { 1.436 + if (info.types.at(i).rawoffset == rawoffset0 1.437 + && info.types.at(i).dstoffset == 0) { 1.438 + initialTypeIdx = i; 1.439 + break; 1.440 + } 1.441 + } 1.442 + } else { 1.443 + initialTypeIdx = 0; 1.444 + } 1.445 + assert(initialTypeIdx >= 0); 1.446 + // Add the initial type associated with the lowest int32 time 1.447 + vector<Transition>::iterator itr = info.transitions.begin(); 1.448 + info.transitions.insert(itr, Transition(LOWEST_TIME32, initialTypeIdx)); 1.449 + } 1.450 + 1.451 + 1.452 + // Read the abbreviation string 1.453 + if (charcnt) { 1.454 + // All abbreviations are concatenated together, with a 0 at 1.455 + // the end of each abbr. 1.456 + char* str = new char[charcnt + 8]; 1.457 + file.read(str, charcnt); 1.458 + 1.459 + // Split abbreviations apart into individual strings. Record 1.460 + // offset of each abbr in a vector. 1.461 + vector<int32_t> abbroffset; 1.462 + char *limit=str+charcnt; 1.463 + for (char* p=str; p<limit; ++p) { 1.464 + char* start = p; 1.465 + while (*p != 0) ++p; 1.466 + info.abbrs.push_back(string(start, p-start)); 1.467 + abbroffset.push_back(start-str); 1.468 + } 1.469 + 1.470 + // Remap all the abbrs. Old value is offset into concatenated 1.471 + // raw abbr strings. New value is index into vector of 1.472 + // strings. E.g., 0,5,10,14 => 0,1,2,3. 1.473 + 1.474 + // Keep track of which abbreviations get used. 1.475 + vector<bool> abbrseen(abbroffset.size(), false); 1.476 + 1.477 + for (vector<ZoneType>::iterator it=info.types.begin(); 1.478 + it!=info.types.end(); 1.479 + ++it) { 1.480 + vector<int32_t>::const_iterator x= 1.481 + find(abbroffset.begin(), abbroffset.end(), it->abbr); 1.482 + if (x==abbroffset.end()) { 1.483 + // TODO: Modify code to add a new string to the end of 1.484 + // the abbr list when a middle offset is given, e.g., 1.485 + // "abc*def*" where * == '\0', take offset of 1 and 1.486 + // make the array "abc", "def", "bc", and translate 1 1.487 + // => 2. NOT CRITICAL since we don't even use the 1.488 + // abbr at this time. 1.489 +#if 0 1.490 + // TODO: Re-enable this warning if we start using 1.491 + // the Olson abbr data, or if the above TODO is completed. 1.492 + ostringstream os; 1.493 + os << "Warning: unusual abbr offset " << it->abbr 1.494 + << ", expected one of"; 1.495 + for (vector<int32_t>::const_iterator y=abbroffset.begin(); 1.496 + y!=abbroffset.end(); ++y) { 1.497 + os << ' ' << *y; 1.498 + } 1.499 + cerr << os.str() << "; using 0" << endl; 1.500 +#endif 1.501 + it->abbr = 0; 1.502 + } else { 1.503 + int32_t index = x - abbroffset.begin(); 1.504 + it->abbr = index; 1.505 + abbrseen[index] = true; 1.506 + } 1.507 + } 1.508 + 1.509 + for (int32_t ii=0;ii<(int32_t) abbrseen.size();++ii) { 1.510 + if (!abbrseen[ii]) { 1.511 + cerr << "Warning: unused abbreviation: " << ii << endl; 1.512 + } 1.513 + } 1.514 + } 1.515 + 1.516 + // Read leap second info, if any. 1.517 + // *** We discard leap second data. *** 1.518 + for (i=0; i<leapcnt; ++i) { 1.519 + readcoded(file); // transition time 1.520 + readcoded(file); // total correction after above 1.521 + } 1.522 + 1.523 + // Read isstd flags 1.524 + for (i=0; i<typecnt; ++i) info.types[i].isstd = readbool(file); 1.525 + 1.526 + // Read isgmt flags 1.527 + for (i=0; i<typecnt; ++i) info.types[i].isgmt = readbool(file); 1.528 +} 1.529 + 1.530 +//-------------------------------------------------------------------- 1.531 +// Directory and file reading 1.532 +//-------------------------------------------------------------------- 1.533 + 1.534 +/** 1.535 + * Process a single zoneinfo file, adding the data to ZONEINFO 1.536 + * @param path the full path to the file, e.g., ".\zoneinfo\America\Los_Angeles" 1.537 + * @param id the zone ID, e.g., "America/Los_Angeles" 1.538 + */ 1.539 +void handleFile(string path, string id) { 1.540 + // Check for duplicate id 1.541 + if (ZONEINFO.find(id) != ZONEINFO.end()) { 1.542 + ostringstream os; 1.543 + os << "duplicate zone ID: " << id; 1.544 + throw invalid_argument(os.str()); 1.545 + } 1.546 + 1.547 + ifstream file(path.c_str(), ios::in | ios::binary); 1.548 + if (!file) { 1.549 + throw invalid_argument("can't open file"); 1.550 + } 1.551 + 1.552 + // eat 32bit data part 1.553 + ZoneInfo info; 1.554 + readzoneinfo(file, info, false); 1.555 + 1.556 + // Check for errors 1.557 + if (!file) { 1.558 + throw invalid_argument("read error"); 1.559 + } 1.560 + 1.561 + // we only use 64bit part 1.562 + ZoneInfo info64; 1.563 + readzoneinfo(file, info64, true); 1.564 + 1.565 + bool alldone = false; 1.566 + int64_t eofPos = (int64_t) file.tellg(); 1.567 + 1.568 + // '\n' + <envvar string> + '\n' after the 64bit version data 1.569 + char ch = file.get(); 1.570 + if (ch == 0x0a) { 1.571 + bool invalidchar = false; 1.572 + while (file.get(ch)) { 1.573 + if (ch == 0x0a) { 1.574 + break; 1.575 + } 1.576 + if (ch < 0x20) { 1.577 + // must be printable ascii 1.578 + invalidchar = true; 1.579 + break; 1.580 + } 1.581 + } 1.582 + if (!invalidchar) { 1.583 + eofPos = (int64_t) file.tellg(); 1.584 + file.seekg(0, ios::end); 1.585 + eofPos = eofPos - (int64_t) file.tellg(); 1.586 + if (eofPos == 0) { 1.587 + alldone = true; 1.588 + } 1.589 + } 1.590 + } 1.591 + if (!alldone) { 1.592 + ostringstream os; 1.593 + os << (-eofPos) << " unprocessed bytes at end"; 1.594 + throw invalid_argument(os.str()); 1.595 + } 1.596 + 1.597 + ZONEINFO[id] = info64; 1.598 +} 1.599 + 1.600 +/** 1.601 + * Recursively scan the given directory, calling handleFile() for each 1.602 + * file in the tree. The user should call with the root directory and 1.603 + * a prefix of "". The function will call itself with non-empty 1.604 + * prefix values. 1.605 + */ 1.606 +#ifdef WIN32 1.607 + 1.608 +void scandir(string dirname, string prefix="") { 1.609 + HANDLE hList; 1.610 + WIN32_FIND_DATA FileData; 1.611 + 1.612 + // Get the first file 1.613 + hList = FindFirstFile((dirname + "\\*").c_str(), &FileData); 1.614 + if (hList == INVALID_HANDLE_VALUE) { 1.615 + cerr << "Error: Invalid directory: " << dirname << endl; 1.616 + exit(1); 1.617 + } 1.618 + for (;;) { 1.619 + string name(FileData.cFileName); 1.620 + string path(dirname + "\\" + name); 1.621 + if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { 1.622 + if (name != "." && name != "..") { 1.623 + scandir(path, prefix + name + "/"); 1.624 + } 1.625 + } else { 1.626 + try { 1.627 + string id = prefix + name; 1.628 + handleFile(path, id); 1.629 + } catch (const exception& e) { 1.630 + cerr << "Error: While processing \"" << path << "\", " 1.631 + << e.what() << endl; 1.632 + exit(1); 1.633 + } 1.634 + } 1.635 + 1.636 + if (!FindNextFile(hList, &FileData)) { 1.637 + if (GetLastError() == ERROR_NO_MORE_FILES) { 1.638 + break; 1.639 + } // else...? 1.640 + } 1.641 + } 1.642 + FindClose(hList); 1.643 +} 1.644 + 1.645 +#else 1.646 + 1.647 +void scandir(string dir, string prefix="") { 1.648 + DIR *dp; 1.649 + struct dirent *dir_entry; 1.650 + struct stat stat_info; 1.651 + char pwd[512]; 1.652 + vector<string> subdirs; 1.653 + vector<string> subfiles; 1.654 + 1.655 + if ((dp = opendir(dir.c_str())) == NULL) { 1.656 + cerr << "Error: Invalid directory: " << dir << endl; 1.657 + exit(1); 1.658 + } 1.659 + if (!getcwd(pwd, sizeof(pwd))) { 1.660 + cerr << "Error: Directory name too long" << endl; 1.661 + exit(1); 1.662 + } 1.663 + chdir(dir.c_str()); 1.664 + while ((dir_entry = readdir(dp)) != NULL) { 1.665 + string name = dir_entry->d_name; 1.666 + string path = dir + "/" + name; 1.667 + lstat(dir_entry->d_name,&stat_info); 1.668 + if (S_ISDIR(stat_info.st_mode)) { 1.669 + if (name != "." && name != "..") { 1.670 + subdirs.push_back(path); 1.671 + subdirs.push_back(prefix + name + "/"); 1.672 + // scandir(path, prefix + name + "/"); 1.673 + } 1.674 + } else { 1.675 + try { 1.676 + string id = prefix + name; 1.677 + subfiles.push_back(path); 1.678 + subfiles.push_back(id); 1.679 + // handleFile(path, id); 1.680 + } catch (const exception& e) { 1.681 + cerr << "Error: While processing \"" << path << "\", " 1.682 + << e.what() << endl; 1.683 + exit(1); 1.684 + } 1.685 + } 1.686 + } 1.687 + closedir(dp); 1.688 + chdir(pwd); 1.689 + 1.690 + for(int32_t i=0;i<(int32_t)subfiles.size();i+=2) { 1.691 + try { 1.692 + handleFile(subfiles[i], subfiles[i+1]); 1.693 + } catch (const exception& e) { 1.694 + cerr << "Error: While processing \"" << subfiles[i] << "\", " 1.695 + << e.what() << endl; 1.696 + exit(1); 1.697 + } 1.698 + } 1.699 + for(int32_t i=0;i<(int32_t)subdirs.size();i+=2) { 1.700 + scandir(subdirs[i], subdirs[i+1]); 1.701 + } 1.702 +} 1.703 + 1.704 +#endif 1.705 + 1.706 +//-------------------------------------------------------------------- 1.707 +// Final zone and rule info 1.708 +//-------------------------------------------------------------------- 1.709 + 1.710 +/** 1.711 + * Read and discard the current line. 1.712 + */ 1.713 +void consumeLine(istream& in) { 1.714 + int32_t c; 1.715 + do { 1.716 + c = in.get(); 1.717 + } while (c != EOF && c != '\n'); 1.718 +} 1.719 + 1.720 +enum { 1.721 + DOM = 0, 1.722 + DOWGEQ = 1, 1.723 + DOWLEQ = 2 1.724 +}; 1.725 + 1.726 +const char* TIME_MODE[] = {"w", "s", "u"}; 1.727 + 1.728 +// Allow 29 days in February because zic outputs February 29 1.729 +// for rules like "last Sunday in February". 1.730 +const int32_t MONTH_LEN[] = {31,29,31,30,31,30,31,31,30,31,30,31}; 1.731 + 1.732 +const int32_t HOUR = 3600; 1.733 + 1.734 +struct FinalZone { 1.735 + int32_t offset; // raw offset 1.736 + int32_t year; // takes effect for y >= year 1.737 + string ruleid; 1.738 + set<string> aliases; 1.739 + FinalZone(int32_t _offset, int32_t _year, const string& _ruleid) : 1.740 + offset(_offset), year(_year), ruleid(_ruleid) { 1.741 + if (offset <= -16*HOUR || offset >= 16*HOUR) { 1.742 + ostringstream os; 1.743 + os << "Invalid input offset " << offset 1.744 + << " for year " << year 1.745 + << " and rule ID " << ruleid; 1.746 + throw invalid_argument(os.str()); 1.747 + } 1.748 + if (year < 1900 || year >= 2050) { 1.749 + ostringstream os; 1.750 + os << "Invalid input year " << year 1.751 + << " with offset " << offset 1.752 + << " and rule ID " << ruleid; 1.753 + throw invalid_argument(os.str()); 1.754 + } 1.755 + } 1.756 + FinalZone() : offset(-1), year(-1) {} 1.757 + void addLink(const string& alias) { 1.758 + if (aliases.find(alias) != aliases.end()) { 1.759 + ostringstream os; 1.760 + os << "Duplicate alias " << alias; 1.761 + throw invalid_argument(os.str()); 1.762 + } 1.763 + aliases.insert(alias); 1.764 + } 1.765 +}; 1.766 + 1.767 +struct FinalRulePart { 1.768 + int32_t mode; 1.769 + int32_t month; 1.770 + int32_t dom; 1.771 + int32_t dow; 1.772 + int32_t time; 1.773 + int32_t offset; // dst offset, usually either 0 or 1:00 1.774 + 1.775 + // Isstd and isgmt only have 3 valid states, corresponding to local 1.776 + // wall time, local standard time, and GMT standard time. 1.777 + // Here is how the isstd & isgmt flags are set by zic: 1.778 + //| case 's': /* Standard */ 1.779 + //| rp->r_todisstd = TRUE; 1.780 + //| rp->r_todisgmt = FALSE; 1.781 + //| case 'w': /* Wall */ 1.782 + //| rp->r_todisstd = FALSE; 1.783 + //| rp->r_todisgmt = FALSE; 1.784 + //| case 'g': /* Greenwich */ 1.785 + //| case 'u': /* Universal */ 1.786 + //| case 'z': /* Zulu */ 1.787 + //| rp->r_todisstd = TRUE; 1.788 + //| rp->r_todisgmt = TRUE; 1.789 + bool isstd; 1.790 + bool isgmt; 1.791 + 1.792 + bool isset; // used during building; later ignored 1.793 + 1.794 + FinalRulePart() : isset(false) {} 1.795 + void set(const string& id, 1.796 + const string& _mode, 1.797 + int32_t _month, 1.798 + int32_t _dom, 1.799 + int32_t _dow, 1.800 + int32_t _time, 1.801 + bool _isstd, 1.802 + bool _isgmt, 1.803 + int32_t _offset) { 1.804 + if (isset) { 1.805 + throw invalid_argument("FinalRulePart set twice"); 1.806 + } 1.807 + isset = true; 1.808 + if (_mode == "DOWLEQ") { 1.809 + mode = DOWLEQ; 1.810 + } else if (_mode == "DOWGEQ") { 1.811 + mode = DOWGEQ; 1.812 + } else if (_mode == "DOM") { 1.813 + mode = DOM; 1.814 + } else { 1.815 + throw invalid_argument("Unrecognized FinalRulePart mode"); 1.816 + } 1.817 + month = _month; 1.818 + dom = _dom; 1.819 + dow = _dow; 1.820 + time = _time; 1.821 + isstd = _isstd; 1.822 + isgmt = _isgmt; 1.823 + offset = _offset; 1.824 + 1.825 + ostringstream os; 1.826 + if (month < 0 || month >= 12) { 1.827 + os << "Invalid input month " << month; 1.828 + } 1.829 + if (dom < 1 || dom > MONTH_LEN[month]) { 1.830 + os << "Invalid input day of month " << dom; 1.831 + } 1.832 + if (mode != DOM && (dow < 0 || dow >= 7)) { 1.833 + os << "Invalid input day of week " << dow; 1.834 + } 1.835 + if (offset < 0 || offset > HOUR) { 1.836 + os << "Invalid input offset " << offset; 1.837 + } 1.838 + if (isgmt && !isstd) { 1.839 + os << "Invalid input isgmt && !isstd"; 1.840 + } 1.841 + if (!os.str().empty()) { 1.842 + os << " for rule " 1.843 + << id 1.844 + << _mode 1.845 + << month << dom << dow << time 1.846 + << isstd << isgmt 1.847 + << offset; 1.848 + throw invalid_argument(os.str()); 1.849 + } 1.850 + } 1.851 + 1.852 + /** 1.853 + * Return the time mode as an ICU SimpleTimeZone int from 0..2; 1.854 + * see simpletz.h. 1.855 + */ 1.856 + int32_t timemode() const { 1.857 + if (isgmt) { 1.858 + assert(isstd); 1.859 + return 2; // gmt standard 1.860 + } 1.861 + if (isstd) { 1.862 + return 1; // local standard 1.863 + } 1.864 + return 0; // local wall 1.865 + } 1.866 + 1.867 + // The SimpleTimeZone encoding method for rules is as follows: 1.868 + // stz_dowim stz_dow 1.869 + // DOM: dom 0 1.870 + // DOWGEQ: dom -(dow+1) 1.871 + // DOWLEQ: -dom -(dow+1) 1.872 + // E.g., to encode Mon>=7, use stz_dowim=7, stz_dow=-2 1.873 + // to encode Mon<=7, use stz_dowim=-7, stz_dow=-2 1.874 + // to encode 7, use stz_dowim=7, stz_dow=0 1.875 + // Note that for this program and for SimpleTimeZone, 0==Jan, 1.876 + // but for this program 0==Sun while for SimpleTimeZone 1==Sun. 1.877 + 1.878 + /** 1.879 + * Return a "dowim" param suitable for SimpleTimeZone. 1.880 + */ 1.881 + int32_t stz_dowim() const { 1.882 + return (mode == DOWLEQ) ? -dom : dom; 1.883 + } 1.884 + 1.885 + /** 1.886 + * Return a "dow" param suitable for SimpleTimeZone. 1.887 + */ 1.888 + int32_t stz_dow() const { 1.889 + return (mode == DOM) ? 0 : -(dow+1); 1.890 + } 1.891 +}; 1.892 + 1.893 +struct FinalRule { 1.894 + FinalRulePart part[2]; 1.895 + 1.896 + bool isset() const { 1.897 + return part[0].isset && part[1].isset; 1.898 + } 1.899 + 1.900 + void print(ostream& os) const; 1.901 +}; 1.902 + 1.903 +map<string,FinalZone> finalZones; 1.904 +map<string,FinalRule> finalRules; 1.905 + 1.906 +map<string, set<string> > links; 1.907 +map<string, string> reverseLinks; 1.908 +map<string, string> linkSource; // id => "Olson link" or "ICU alias" 1.909 + 1.910 +/** 1.911 + * Predicate used to find FinalRule objects that do not have both 1.912 + * sub-parts set (indicating an error in the input file). 1.913 + */ 1.914 +bool isNotSet(const pair<const string,FinalRule>& p) { 1.915 + return !p.second.isset(); 1.916 +} 1.917 + 1.918 +/** 1.919 + * Predicate used to find FinalZone objects that do not map to a known 1.920 + * rule (indicating an error in the input file). 1.921 + */ 1.922 +bool mapsToUnknownRule(const pair<const string,FinalZone>& p) { 1.923 + return finalRules.find(p.second.ruleid) == finalRules.end(); 1.924 +} 1.925 + 1.926 +/** 1.927 + * This set is used to make sure each rule in finalRules is used at 1.928 + * least once. First we populate it with all the rules from 1.929 + * finalRules; then we remove all the rules referred to in 1.930 + * finaleZones. 1.931 + */ 1.932 +set<string> ruleIDset; 1.933 + 1.934 +void insertRuleID(const pair<string,FinalRule>& p) { 1.935 + ruleIDset.insert(p.first); 1.936 +} 1.937 + 1.938 +void eraseRuleID(const pair<string,FinalZone>& p) { 1.939 + ruleIDset.erase(p.second.ruleid); 1.940 +} 1.941 + 1.942 +/** 1.943 + * Populate finalZones and finalRules from the given istream. 1.944 + */ 1.945 +void readFinalZonesAndRules(istream& in) { 1.946 + 1.947 + for (;;) { 1.948 + string token; 1.949 + in >> token; 1.950 + if (in.eof() || !in) { 1.951 + break; 1.952 + } else if (token == "zone") { 1.953 + // zone Africa/Cairo 7200 1995 Egypt # zone Africa/Cairo, offset 7200, year >= 1995, rule Egypt (0) 1.954 + string id, ruleid; 1.955 + int32_t offset, year; 1.956 + in >> id >> offset >> year >> ruleid; 1.957 + consumeLine(in); 1.958 + finalZones[id] = FinalZone(offset, year, ruleid); 1.959 + } else if (token == "rule") { 1.960 + // rule US DOWGEQ 3 1 0 7200 0 0 3600 # 52: US, file data/northamerica, line 119, mode DOWGEQ, April, dom 1, Sunday, time 7200, isstd 0, isgmt 0, offset 3600 1.961 + // rule US DOWLEQ 9 31 0 7200 0 0 0 # 53: US, file data/northamerica, line 114, mode DOWLEQ, October, dom 31, Sunday, time 7200, isstd 0, isgmt 0, offset 0 1.962 + string id, mode; 1.963 + int32_t month, dom, dow, time, offset; 1.964 + bool isstd, isgmt; 1.965 + in >> id >> mode >> month >> dom >> dow >> time >> isstd >> isgmt >> offset; 1.966 + consumeLine(in); 1.967 + FinalRule& fr = finalRules[id]; 1.968 + int32_t p = fr.part[0].isset ? 1 : 0; 1.969 + fr.part[p].set(id, mode, month, dom, dow, time, isstd, isgmt, offset); 1.970 + } else if (token == "link") { 1.971 + string fromid, toid; // fromid == "real" zone, toid == alias 1.972 + in >> fromid >> toid; 1.973 + // DO NOT consumeLine(in); 1.974 + if (finalZones.find(toid) != finalZones.end()) { 1.975 + throw invalid_argument("Bad link: `to' id is a \"real\" zone"); 1.976 + } 1.977 + 1.978 + links[fromid].insert(toid); 1.979 + reverseLinks[toid] = fromid; 1.980 + 1.981 + linkSource[fromid] = "Olson link"; 1.982 + linkSource[toid] = "Olson link"; 1.983 + } else if (token.length() > 0 && token[0] == '#') { 1.984 + consumeLine(in); 1.985 + } else { 1.986 + throw invalid_argument("Unrecognized keyword"); 1.987 + } 1.988 + } 1.989 + 1.990 + if (!in.eof() && !in) { 1.991 + throw invalid_argument("Parse failure"); 1.992 + } 1.993 + 1.994 + // Perform validity check: Each rule should have data for 2 parts. 1.995 + if (count_if(finalRules.begin(), finalRules.end(), isNotSet) != 0) { 1.996 + throw invalid_argument("One or more incomplete rule pairs"); 1.997 + } 1.998 + 1.999 + // Perform validity check: Each zone should map to a known rule. 1.1000 + if (count_if(finalZones.begin(), finalZones.end(), mapsToUnknownRule) != 0) { 1.1001 + throw invalid_argument("One or more zones refers to an unknown rule"); 1.1002 + } 1.1003 + 1.1004 + // Perform validity check: Each rule should be referred to by a zone. 1.1005 + ruleIDset.clear(); 1.1006 + for_each(finalRules.begin(), finalRules.end(), insertRuleID); 1.1007 + for_each(finalZones.begin(), finalZones.end(), eraseRuleID); 1.1008 + if (ruleIDset.size() != 0) { 1.1009 + throw invalid_argument("Unused rules"); 1.1010 + } 1.1011 +} 1.1012 + 1.1013 +//-------------------------------------------------------------------- 1.1014 +// Resource bundle output 1.1015 +//-------------------------------------------------------------------- 1.1016 + 1.1017 +// SEE olsontz.h FOR RESOURCE BUNDLE DATA LAYOUT 1.1018 + 1.1019 +void ZoneInfo::print(ostream& os, const string& id) const { 1.1020 + // Implement compressed format #2: 1.1021 + os << " /* " << id << " */ "; 1.1022 + 1.1023 + if (aliasTo >= 0) { 1.1024 + assert(aliases.size() == 0); 1.1025 + os << ":int { " << aliasTo << " } "; // No endl - save room for comment. 1.1026 + return; 1.1027 + } 1.1028 + 1.1029 + if (ICU44PLUS) { 1.1030 + os << ":table {" << endl; 1.1031 + } else { 1.1032 + os << ":array {" << endl; 1.1033 + } 1.1034 + 1.1035 + vector<Transition>::const_iterator trn; 1.1036 + vector<ZoneType>::const_iterator typ; 1.1037 + 1.1038 + bool first; 1.1039 + 1.1040 + if (ICU44PLUS) { 1.1041 + trn = transitions.begin(); 1.1042 + 1.1043 + // pre 32bit transitions 1.1044 + if (trn != transitions.end() && trn->time < LOWEST_TIME32) { 1.1045 + os << " transPre32:intvector { "; 1.1046 + for (first = true; trn != transitions.end() && trn->time < LOWEST_TIME32; ++trn) { 1.1047 + if (!first) { 1.1048 + os<< ", "; 1.1049 + } 1.1050 + first = false; 1.1051 + os << (int32_t)(trn->time >> 32) << ", " << (int32_t)(trn->time & 0x00000000ffffffff); 1.1052 + } 1.1053 + os << " }" << endl; 1.1054 + } 1.1055 + 1.1056 + // 32bit transtions 1.1057 + if (trn != transitions.end() && trn->time < HIGHEST_TIME32) { 1.1058 + os << " trans:intvector { "; 1.1059 + for (first = true; trn != transitions.end() && trn->time < HIGHEST_TIME32; ++trn) { 1.1060 + if (!first) { 1.1061 + os << ", "; 1.1062 + } 1.1063 + first = false; 1.1064 + os << trn->time; 1.1065 + } 1.1066 + os << " }" << endl; 1.1067 + } 1.1068 + 1.1069 + // post 32bit transitons 1.1070 + if (trn != transitions.end()) { 1.1071 + os << " transPost32:intvector { "; 1.1072 + for (first = true; trn != transitions.end(); ++trn) { 1.1073 + if (!first) { 1.1074 + os<< ", "; 1.1075 + } 1.1076 + first = false; 1.1077 + os << (int32_t)(trn->time >> 32) << ", " << (int32_t)(trn->time & 0x00000000ffffffff); 1.1078 + } 1.1079 + os << " }" << endl; 1.1080 + } 1.1081 + } else { 1.1082 + os << " :intvector { "; 1.1083 + for (trn = transitions.begin(), first = true; trn != transitions.end(); ++trn) { 1.1084 + if (!first) os << ", "; 1.1085 + first = false; 1.1086 + os << trn->time; 1.1087 + } 1.1088 + os << " }" << endl; 1.1089 + } 1.1090 + 1.1091 + 1.1092 + first=true; 1.1093 + if (ICU44PLUS) { 1.1094 + os << " typeOffsets:intvector { "; 1.1095 + } else { 1.1096 + os << " :intvector { "; 1.1097 + } 1.1098 + for (typ = types.begin(); typ != types.end(); ++typ) { 1.1099 + if (!first) os << ", "; 1.1100 + first = false; 1.1101 + os << typ->rawoffset << ", " << typ->dstoffset; 1.1102 + } 1.1103 + os << " }" << endl; 1.1104 + 1.1105 + if (ICU44PLUS) { 1.1106 + if (transitions.size() != 0) { 1.1107 + os << " typeMap:bin { \"" << hex << setfill('0'); 1.1108 + for (trn = transitions.begin(); trn != transitions.end(); ++trn) { 1.1109 + os << setw(2) << trn->type; 1.1110 + } 1.1111 + os << dec << "\" }" << endl; 1.1112 + } 1.1113 + } else { 1.1114 + os << " :bin { \"" << hex << setfill('0'); 1.1115 + for (trn = transitions.begin(); trn != transitions.end(); ++trn) { 1.1116 + os << setw(2) << trn->type; 1.1117 + } 1.1118 + os << dec << "\" }" << endl; 1.1119 + } 1.1120 + 1.1121 + // Final zone info, if any 1.1122 + if (finalYear != -1) { 1.1123 + if (ICU44PLUS) { 1.1124 + os << " finalRule { \"" << finalRuleID << "\" }" << endl; 1.1125 + os << " finalRaw:int { " << finalOffset << " }" << endl; 1.1126 + os << " finalYear:int { " << finalYear << " }" << endl; 1.1127 + } else { 1.1128 + os << " \"" << finalRuleID << "\"" << endl; 1.1129 + os << " :intvector { " << finalOffset << ", " 1.1130 + << finalYear << " }" << endl; 1.1131 + } 1.1132 + } 1.1133 + 1.1134 + // Alias list, if any 1.1135 + if (aliases.size() != 0) { 1.1136 + first = true; 1.1137 + if (ICU44PLUS) { 1.1138 + os << " links:intvector { "; 1.1139 + } else { 1.1140 + os << " :intvector { "; 1.1141 + } 1.1142 + for (set<int32_t>::const_iterator i=aliases.begin(); i!=aliases.end(); ++i) { 1.1143 + if (!first) os << ", "; 1.1144 + first = false; 1.1145 + os << *i; 1.1146 + } 1.1147 + os << " }" << endl; 1.1148 + } 1.1149 + 1.1150 + os << " } "; // no trailing 'endl', so comments can be placed. 1.1151 +} 1.1152 + 1.1153 +inline ostream& 1.1154 +operator<<(ostream& os, const ZoneMap& zoneinfo) { 1.1155 + int32_t c = 0; 1.1156 + for (ZoneMapIter it = zoneinfo.begin(); 1.1157 + it != zoneinfo.end(); 1.1158 + ++it) { 1.1159 + if(c && !ICU44PLUS) os << ","; 1.1160 + it->second.print(os, it->first); 1.1161 + os << "//Z#" << c++ << endl; 1.1162 + } 1.1163 + return os; 1.1164 +} 1.1165 + 1.1166 +// print the string list 1.1167 +ostream& printStringList( ostream& os, const ZoneMap& zoneinfo) { 1.1168 + int32_t n = 0; // count 1.1169 + int32_t col = 0; // column 1.1170 + os << " Names {" << endl 1.1171 + << " "; 1.1172 + for (ZoneMapIter it = zoneinfo.begin(); 1.1173 + it != zoneinfo.end(); 1.1174 + ++it) { 1.1175 + if(n) { 1.1176 + os << ","; 1.1177 + col ++; 1.1178 + } 1.1179 + const string& id = it->first; 1.1180 + os << "\"" << id << "\""; 1.1181 + col += id.length() + 2; 1.1182 + if(col >= 50) { 1.1183 + os << " // " << n << endl 1.1184 + << " "; 1.1185 + col = 0; 1.1186 + } 1.1187 + n++; 1.1188 + } 1.1189 + os << " // " << (n-1) << endl 1.1190 + << " }" << endl; 1.1191 + 1.1192 + return os; 1.1193 +} 1.1194 + 1.1195 +//-------------------------------------------------------------------- 1.1196 +// main 1.1197 +//-------------------------------------------------------------------- 1.1198 + 1.1199 +// Unary predicate for finding transitions after a given time 1.1200 +bool isAfter(const Transition t, int64_t thresh) { 1.1201 + return t.time >= thresh; 1.1202 +} 1.1203 + 1.1204 +/** 1.1205 + * A zone type that contains only the raw and dst offset. Used by the 1.1206 + * optimizeTypeList() method. 1.1207 + */ 1.1208 +struct SimplifiedZoneType { 1.1209 + int64_t rawoffset; 1.1210 + int64_t dstoffset; 1.1211 + SimplifiedZoneType() : rawoffset(-1), dstoffset(-1) {} 1.1212 + SimplifiedZoneType(const ZoneType& t) : rawoffset(t.rawoffset), 1.1213 + dstoffset(t.dstoffset) {} 1.1214 + bool operator<(const SimplifiedZoneType& t) const { 1.1215 + return rawoffset < t.rawoffset || 1.1216 + (rawoffset == t.rawoffset && 1.1217 + dstoffset < t.dstoffset); 1.1218 + } 1.1219 +}; 1.1220 + 1.1221 +/** 1.1222 + * Construct a ZoneType from a SimplifiedZoneType. Note that this 1.1223 + * discards information; the new ZoneType will have meaningless 1.1224 + * (empty) abbr, isdst, isstd, and isgmt flags; this is appropriate, 1.1225 + * since ignoring these is how we do optimization (we have no use for 1.1226 + * these in historical transitions). 1.1227 + */ 1.1228 +ZoneType::ZoneType(const SimplifiedZoneType& t) : 1.1229 + rawoffset(t.rawoffset), dstoffset(t.dstoffset), 1.1230 + abbr(-1), isdst(false), isstd(false), isgmt(false) {} 1.1231 + 1.1232 +/** 1.1233 + * Optimize the type list to remove excess entries. The type list may 1.1234 + * contain entries that are distinct only in terms of their dst, std, 1.1235 + * or gmt flags. Since we don't care about those flags, we can reduce 1.1236 + * the type list to a set of unique raw/dst offset pairs, and remap 1.1237 + * the type indices in the transition list, which stores, for each 1.1238 + * transition, a transition time and a type index. 1.1239 + */ 1.1240 +void ZoneInfo::optimizeTypeList() { 1.1241 + // Assemble set of unique types; only those in the `transitions' 1.1242 + // list, since there may be unused types in the `types' list 1.1243 + // corresponding to transitions that have been trimmed (during 1.1244 + // merging of final data). 1.1245 + 1.1246 + if (aliasTo >= 0) return; // Nothing to do for aliases 1.1247 + 1.1248 + if (!ICU44PLUS) { 1.1249 + // This is the old logic which has a bug, which occasionally removes 1.1250 + // the type before the first transition. The problem was fixed 1.1251 + // by inserting the dummy transition indirectly. 1.1252 + 1.1253 + // If there are zero transitions and one type, then leave that as-is. 1.1254 + if (transitions.size() == 0) { 1.1255 + if (types.size() != 1) { 1.1256 + cerr << "Error: transition count = 0, type count = " << types.size() << endl; 1.1257 + } 1.1258 + return; 1.1259 + } 1.1260 + 1.1261 + set<SimplifiedZoneType> simpleset; 1.1262 + for (vector<Transition>::const_iterator i=transitions.begin(); 1.1263 + i!=transitions.end(); ++i) { 1.1264 + assert(i->type < (int32_t)types.size()); 1.1265 + simpleset.insert(types[i->type]); 1.1266 + } 1.1267 + 1.1268 + // Map types to integer indices 1.1269 + map<SimplifiedZoneType,int32_t> simplemap; 1.1270 + int32_t n=0; 1.1271 + for (set<SimplifiedZoneType>::const_iterator i=simpleset.begin(); 1.1272 + i!=simpleset.end(); ++i) { 1.1273 + simplemap[*i] = n++; 1.1274 + } 1.1275 + 1.1276 + // Remap transitions 1.1277 + for (vector<Transition>::iterator i=transitions.begin(); 1.1278 + i!=transitions.end(); ++i) { 1.1279 + assert(i->type < (int32_t)types.size()); 1.1280 + ZoneType oldtype = types[i->type]; 1.1281 + SimplifiedZoneType newtype(oldtype); 1.1282 + assert(simplemap.find(newtype) != simplemap.end()); 1.1283 + i->type = simplemap[newtype]; 1.1284 + } 1.1285 + 1.1286 + // Replace type list 1.1287 + types.clear(); 1.1288 + copy(simpleset.begin(), simpleset.end(), back_inserter(types)); 1.1289 + 1.1290 + } else { 1.1291 + if (types.size() > 1) { 1.1292 + // Note: localtime uses the very first non-dst type as initial offsets. 1.1293 + // If all types are DSTs, the very first type is treated as the initial offsets. 1.1294 + 1.1295 + // Decide a type used as the initial offsets. ICU put the type at index 0. 1.1296 + ZoneType initialType = types[0]; 1.1297 + for (vector<ZoneType>::const_iterator i=types.begin(); i!=types.end(); ++i) { 1.1298 + if (i->dstoffset == 0) { 1.1299 + initialType = *i; 1.1300 + break; 1.1301 + } 1.1302 + } 1.1303 + 1.1304 + SimplifiedZoneType initialSimplifiedType(initialType); 1.1305 + 1.1306 + // create a set of unique types, but ignoring fields which we're not interested in 1.1307 + set<SimplifiedZoneType> simpleset; 1.1308 + simpleset.insert(initialSimplifiedType); 1.1309 + for (vector<Transition>::const_iterator i=transitions.begin(); i!=transitions.end(); ++i) { 1.1310 + assert(i->type < (int32_t)types.size()); 1.1311 + simpleset.insert(types[i->type]); 1.1312 + } 1.1313 + 1.1314 + // Map types to integer indices, however, keeping the first type at offset 0 1.1315 + map<SimplifiedZoneType,int32_t> simplemap; 1.1316 + simplemap[initialSimplifiedType] = 0; 1.1317 + int32_t n = 1; 1.1318 + for (set<SimplifiedZoneType>::const_iterator i=simpleset.begin(); i!=simpleset.end(); ++i) { 1.1319 + if (*i < initialSimplifiedType || initialSimplifiedType < *i) { 1.1320 + simplemap[*i] = n++; 1.1321 + } 1.1322 + } 1.1323 + 1.1324 + // Remap transitions 1.1325 + for (vector<Transition>::iterator i=transitions.begin(); 1.1326 + i!=transitions.end(); ++i) { 1.1327 + assert(i->type < (int32_t)types.size()); 1.1328 + ZoneType oldtype = types[i->type]; 1.1329 + SimplifiedZoneType newtype(oldtype); 1.1330 + assert(simplemap.find(newtype) != simplemap.end()); 1.1331 + i->type = simplemap[newtype]; 1.1332 + } 1.1333 + 1.1334 + // Replace type list 1.1335 + types.clear(); 1.1336 + types.push_back(initialSimplifiedType); 1.1337 + for (set<SimplifiedZoneType>::const_iterator i=simpleset.begin(); i!=simpleset.end(); ++i) { 1.1338 + if (*i < initialSimplifiedType || initialSimplifiedType < *i) { 1.1339 + types.push_back(*i); 1.1340 + } 1.1341 + } 1.1342 + 1.1343 + // Reiterating transitions to remove any transitions which 1.1344 + // do not actually change the raw/dst offsets 1.1345 + int32_t prevTypeIdx = 0; 1.1346 + for (vector<Transition>::iterator i=transitions.begin(); i!=transitions.end();) { 1.1347 + if (i->type == prevTypeIdx) { 1.1348 + // this is not a time transition, probably just name change 1.1349 + // e.g. America/Resolute after 2006 in 2010b 1.1350 + transitions.erase(i); 1.1351 + } else { 1.1352 + prevTypeIdx = i->type; 1.1353 + i++; 1.1354 + } 1.1355 + } 1.1356 + } 1.1357 + } 1.1358 + 1.1359 +} 1.1360 + 1.1361 +/** 1.1362 + * Merge final zone data into this zone. 1.1363 + */ 1.1364 +void ZoneInfo::mergeFinalData(const FinalZone& fz) { 1.1365 + int32_t year = fz.year; 1.1366 + int64_t seconds = yearToSeconds(year); 1.1367 + 1.1368 + if (!ICU44PLUS) { 1.1369 + if (seconds > HIGHEST_TIME32) { 1.1370 + // Avoid transitions beyond signed 32bit max second. 1.1371 + // This may result incorrect offset computation around 1.1372 + // HIGHEST_TIME32. This is a limitation of ICU 1.1373 + // before 4.4. 1.1374 + seconds = HIGHEST_TIME32; 1.1375 + } 1.1376 + } 1.1377 + 1.1378 + vector<Transition>::iterator it = 1.1379 + find_if(transitions.begin(), transitions.end(), 1.1380 + bind2nd(ptr_fun(isAfter), seconds)); 1.1381 + transitions.erase(it, transitions.end()); 1.1382 + 1.1383 + if (finalYear != -1) { 1.1384 + throw invalid_argument("Final zone already merged in"); 1.1385 + } 1.1386 + finalYear = fz.year; 1.1387 + finalOffset = fz.offset; 1.1388 + finalRuleID = fz.ruleid; 1.1389 +} 1.1390 + 1.1391 +/** 1.1392 + * Merge the data from the given final zone into the core zone data by 1.1393 + * calling the ZoneInfo member function mergeFinalData. 1.1394 + */ 1.1395 +void mergeOne(const string& zoneid, const FinalZone& fz) { 1.1396 + if (ZONEINFO.find(zoneid) == ZONEINFO.end()) { 1.1397 + throw invalid_argument("Unrecognized final zone ID"); 1.1398 + } 1.1399 + ZONEINFO[zoneid].mergeFinalData(fz); 1.1400 +} 1.1401 + 1.1402 +/** 1.1403 + * Visitor function that merges the final zone data into the main zone 1.1404 + * data structures. It calls mergeOne for each final zone and its 1.1405 + * list of aliases. 1.1406 + */ 1.1407 +void mergeFinalZone(const pair<string,FinalZone>& p) { 1.1408 + const string& id = p.first; 1.1409 + const FinalZone& fz = p.second; 1.1410 + 1.1411 + mergeOne(id, fz); 1.1412 +} 1.1413 + 1.1414 +/** 1.1415 + * Print this rule in resource bundle format to os. ID and enclosing 1.1416 + * braces handled elsewhere. 1.1417 + */ 1.1418 +void FinalRule::print(ostream& os) const { 1.1419 + // First print the rule part that enters DST; then the rule part 1.1420 + // that exits it. 1.1421 + int32_t whichpart = (part[0].offset != 0) ? 0 : 1; 1.1422 + assert(part[whichpart].offset != 0); 1.1423 + assert(part[1-whichpart].offset == 0); 1.1424 + 1.1425 + os << " "; 1.1426 + for (int32_t i=0; i<2; ++i) { 1.1427 + const FinalRulePart& p = part[whichpart]; 1.1428 + whichpart = 1-whichpart; 1.1429 + os << p.month << ", " << p.stz_dowim() << ", " << p.stz_dow() << ", " 1.1430 + << p.time << ", " << p.timemode() << ", "; 1.1431 + } 1.1432 + os << part[whichpart].offset << endl; 1.1433 +} 1.1434 + 1.1435 +int main(int argc, char *argv[]) { 1.1436 + string rootpath, zonetab, version; 1.1437 + bool validArgs = FALSE; 1.1438 + 1.1439 + if (argc == 4 || argc == 5) { 1.1440 + validArgs = TRUE; 1.1441 + rootpath = argv[1]; 1.1442 + zonetab = argv[2]; 1.1443 + version = argv[3]; 1.1444 + if (argc == 5) { 1.1445 + if (strcmp(argv[4], "--old") == 0) { 1.1446 + ICU44PLUS = FALSE; 1.1447 + TZ_RESOURCE_NAME = ICU_TZ_RESOURCE_OLD; 1.1448 + } else { 1.1449 + validArgs = FALSE; 1.1450 + } 1.1451 + } 1.1452 + } 1.1453 + if (!validArgs) { 1.1454 + cout << "Usage: tz2icu <dir> <cmap> <tzver> [--old]" << endl 1.1455 + << " <dir> path to zoneinfo file tree generated by" << endl 1.1456 + << " ICU-patched version of zic" << endl 1.1457 + << " <cmap> country map, from tzdata archive," << endl 1.1458 + << " typically named \"zone.tab\"" << endl 1.1459 + << " <tzver> version string, such as \"2003e\"" << endl 1.1460 + << " --old generating resource format before ICU4.4" << endl; 1.1461 + exit(1); 1.1462 + } 1.1463 + 1.1464 + cout << "Olson data version: " << version << endl; 1.1465 + cout << "ICU 4.4+ format: " << (ICU44PLUS ? "Yes" : "No") << endl; 1.1466 + 1.1467 + try { 1.1468 + ifstream finals(ICU_ZONE_FILE); 1.1469 + if (finals) { 1.1470 + readFinalZonesAndRules(finals); 1.1471 + 1.1472 + cout << "Finished reading " << finalZones.size() 1.1473 + << " final zones and " << finalRules.size() 1.1474 + << " final rules from " ICU_ZONE_FILE << endl; 1.1475 + } else { 1.1476 + cerr << "Error: Unable to open " ICU_ZONE_FILE << endl; 1.1477 + return 1; 1.1478 + } 1.1479 + } catch (const exception& error) { 1.1480 + cerr << "Error: While reading " ICU_ZONE_FILE ": " << error.what() << endl; 1.1481 + return 1; 1.1482 + } 1.1483 + 1.1484 + try { 1.1485 + // Recursively scan all files below the given path, accumulating 1.1486 + // their data into ZONEINFO. All files must be TZif files. Any 1.1487 + // failure along the way will result in a call to exit(1). 1.1488 + scandir(rootpath); 1.1489 + } catch (const exception& error) { 1.1490 + cerr << "Error: While scanning " << rootpath << ": " << error.what() << endl; 1.1491 + return 1; 1.1492 + } 1.1493 + 1.1494 + cout << "Finished reading " << ZONEINFO.size() << " zoneinfo files [" 1.1495 + << (ZONEINFO.begin())->first << ".." 1.1496 + << (--ZONEINFO.end())->first << "]" << endl; 1.1497 + 1.1498 + try { 1.1499 + for_each(finalZones.begin(), finalZones.end(), mergeFinalZone); 1.1500 + } catch (const exception& error) { 1.1501 + cerr << "Error: While merging final zone data: " << error.what() << endl; 1.1502 + return 1; 1.1503 + } 1.1504 + 1.1505 + // Process links (including ICU aliases). For each link set we have 1.1506 + // a canonical ID (e.g., America/Los_Angeles) and a set of one or more 1.1507 + // aliases (e.g., PST, PST8PDT, ...). 1.1508 + 1.1509 + // 1. Add all aliases as zone objects in ZONEINFO 1.1510 + for (map<string,set<string> >::const_iterator i = links.begin(); 1.1511 + i!=links.end(); ++i) { 1.1512 + const string& olson = i->first; 1.1513 + const set<string>& aliases = i->second; 1.1514 + if (ZONEINFO.find(olson) == ZONEINFO.end()) { 1.1515 + cerr << "Error: Invalid " << linkSource[olson] << " to non-existent \"" 1.1516 + << olson << "\"" << endl; 1.1517 + return 1; 1.1518 + } 1.1519 + for (set<string>::const_iterator j=aliases.begin(); 1.1520 + j!=aliases.end(); ++j) { 1.1521 + ZONEINFO[*j] = ZoneInfo(); 1.1522 + } 1.1523 + } 1.1524 + 1.1525 + // 2. Create a mapping from zones to index numbers 0..n-1. 1.1526 + map<string,int32_t> zoneIDs; 1.1527 + vector<string> zoneIDlist; 1.1528 + int32_t z=0; 1.1529 + for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) { 1.1530 + zoneIDs[i->first] = z++; 1.1531 + zoneIDlist.push_back(i->first); 1.1532 + } 1.1533 + assert(z == (int32_t) ZONEINFO.size()); 1.1534 + 1.1535 + // 3. Merge aliases. Sometimes aliases link to other aliases; we 1.1536 + // resolve these into simplest possible sets. 1.1537 + map<string,set<string> > links2; 1.1538 + map<string,string> reverse2; 1.1539 + for (map<string,set<string> >::const_iterator i = links.begin(); 1.1540 + i!=links.end(); ++i) { 1.1541 + string olson = i->first; 1.1542 + while (reverseLinks.find(olson) != reverseLinks.end()) { 1.1543 + olson = reverseLinks[olson]; 1.1544 + } 1.1545 + for (set<string>::const_iterator j=i->second.begin(); j!=i->second.end(); ++j) { 1.1546 + links2[olson].insert(*j); 1.1547 + reverse2[*j] = olson; 1.1548 + } 1.1549 + } 1.1550 + links = links2; 1.1551 + reverseLinks = reverse2; 1.1552 + 1.1553 + if (false) { // Debugging: Emit link map 1.1554 + for (map<string,set<string> >::const_iterator i = links.begin(); 1.1555 + i!=links.end(); ++i) { 1.1556 + cout << i->first << ": "; 1.1557 + for (set<string>::const_iterator j=i->second.begin(); j!=i->second.end(); ++j) { 1.1558 + cout << *j << ", "; 1.1559 + } 1.1560 + cout << endl; 1.1561 + } 1.1562 + } 1.1563 + 1.1564 + // 4. Update aliases 1.1565 + for (map<string,set<string> >::const_iterator i = links.begin(); 1.1566 + i!=links.end(); ++i) { 1.1567 + const string& olson = i->first; 1.1568 + const set<string>& aliases = i->second; 1.1569 + ZONEINFO[olson].clearAliases(); 1.1570 + ZONEINFO[olson].addAlias(zoneIDs[olson]); 1.1571 + for (set<string>::const_iterator j=aliases.begin(); 1.1572 + j!=aliases.end(); ++j) { 1.1573 + assert(zoneIDs.find(olson) != zoneIDs.end()); 1.1574 + assert(zoneIDs.find(*j) != zoneIDs.end()); 1.1575 + assert(ZONEINFO.find(*j) != ZONEINFO.end()); 1.1576 + ZONEINFO[*j].setAliasTo(zoneIDs[olson]); 1.1577 + ZONEINFO[olson].addAlias(zoneIDs[*j]); 1.1578 + } 1.1579 + } 1.1580 + 1.1581 + // Once merging of final data is complete, we can optimize the type list 1.1582 + for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) { 1.1583 + i->second.optimizeTypeList(); 1.1584 + } 1.1585 + 1.1586 + // Create the country map 1.1587 + map<string, set<string> > countryMap; // country -> set of zones 1.1588 + map<string, string> reverseCountryMap; // zone -> country 1.1589 + try { 1.1590 + ifstream f(zonetab.c_str()); 1.1591 + if (!f) { 1.1592 + cerr << "Error: Unable to open " << zonetab << endl; 1.1593 + return 1; 1.1594 + } 1.1595 + int32_t n = 0; 1.1596 + string line; 1.1597 + while (getline(f, line)) { 1.1598 + string::size_type lb = line.find('#'); 1.1599 + if (lb != string::npos) { 1.1600 + line.resize(lb); // trim comments 1.1601 + } 1.1602 + string country, coord, zone; 1.1603 + istringstream is(line); 1.1604 + is >> country >> coord >> zone; 1.1605 + if (country.size() == 0) continue; 1.1606 + if (country.size() != 2 || zone.size() < 1) { 1.1607 + cerr << "Error: Can't parse " << line << " in " << zonetab << endl; 1.1608 + return 1; 1.1609 + } 1.1610 + if (ZONEINFO.find(zone) == ZONEINFO.end()) { 1.1611 + cerr << "Error: Country maps to invalid zone " << zone 1.1612 + << " in " << zonetab << endl; 1.1613 + return 1; 1.1614 + } 1.1615 + countryMap[country].insert(zone); 1.1616 + reverseCountryMap[zone] = country; 1.1617 + //cerr << (n+1) << ": " << country << " <=> " << zone << endl; 1.1618 + ++n; 1.1619 + } 1.1620 + cout << "Finished reading " << n 1.1621 + << " country entries from " << zonetab << endl; 1.1622 + } catch (const exception& error) { 1.1623 + cerr << "Error: While reading " << zonetab << ": " << error.what() << endl; 1.1624 + return 1; 1.1625 + } 1.1626 + 1.1627 + // Merge ICU aliases into country map. Don't merge any alias 1.1628 + // that already has a country map, since that doesn't make sense. 1.1629 + // E.g. "Link Europe/Oslo Arctic/Longyearbyen" doesn't mean we 1.1630 + // should cross-map the countries between these two zones. 1.1631 + for (map<string,set<string> >::const_iterator i = links.begin(); 1.1632 + i!=links.end(); ++i) { 1.1633 + const string& olson(i->first); 1.1634 + if (reverseCountryMap.find(olson) == reverseCountryMap.end()) { 1.1635 + continue; 1.1636 + } 1.1637 + string c = reverseCountryMap[olson]; 1.1638 + const set<string>& aliases(i->second); 1.1639 + for (set<string>::const_iterator j=aliases.begin(); 1.1640 + j != aliases.end(); ++j) { 1.1641 + if (reverseCountryMap.find(*j) == reverseCountryMap.end()) { 1.1642 + countryMap[c].insert(*j); 1.1643 + reverseCountryMap[*j] = c; 1.1644 + //cerr << "Aliased country: " << c << " <=> " << *j << endl; 1.1645 + } 1.1646 + } 1.1647 + } 1.1648 + 1.1649 + // Create a pseudo-country containing all zones belonging to no country 1.1650 + set<string> nocountry; 1.1651 + for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) { 1.1652 + if (reverseCountryMap.find(i->first) == reverseCountryMap.end()) { 1.1653 + nocountry.insert(i->first); 1.1654 + } 1.1655 + } 1.1656 + countryMap[""] = nocountry; 1.1657 + 1.1658 + // Get local time & year for below 1.1659 + time_t sec; 1.1660 + time(&sec); 1.1661 + struct tm* now = localtime(&sec); 1.1662 + int32_t thisYear = now->tm_year + 1900; 1.1663 + 1.1664 + string filename = TZ_RESOURCE_NAME + ".txt"; 1.1665 + // Write out a resource-bundle source file containing data for 1.1666 + // all zones. 1.1667 + ofstream file(filename.c_str()); 1.1668 + if (file) { 1.1669 + file << "//---------------------------------------------------------" << endl 1.1670 + << "// Copyright (C) 2003"; 1.1671 + if (thisYear > 2003) { 1.1672 + file << "-" << thisYear; 1.1673 + } 1.1674 + file << ", International Business Machines" << endl 1.1675 + << "// Corporation and others. All Rights Reserved." << endl 1.1676 + << "//---------------------------------------------------------" << endl 1.1677 + << "// Build tool: tz2icu" << endl 1.1678 + << "// Build date: " << asctime(now) /* << endl -- asctime emits CR */ 1.1679 + << "// Olson source: ftp://elsie.nci.nih.gov/pub/" << endl 1.1680 + << "// Olson version: " << version << endl 1.1681 + << "// ICU version: " << U_ICU_VERSION << endl 1.1682 + << "//---------------------------------------------------------" << endl 1.1683 + << "// >> !!! >> THIS IS A MACHINE-GENERATED FILE << !!! <<" << endl 1.1684 + << "// >> !!! >>> DO NOT EDIT <<< !!! <<" << endl 1.1685 + << "//---------------------------------------------------------" << endl 1.1686 + << endl 1.1687 + << TZ_RESOURCE_NAME << ":table(nofallback) {" << endl 1.1688 + << " TZVersion { \"" << version << "\" }" << endl 1.1689 + << " Zones:array { " << endl 1.1690 + << ZONEINFO // Zones (the actual data) 1.1691 + << " }" << endl; 1.1692 + 1.1693 + // Names correspond to the Zones list, used for binary searching. 1.1694 + printStringList ( file, ZONEINFO ); // print the Names list 1.1695 + 1.1696 + // Final Rules are used if requested by the zone 1.1697 + file << " Rules { " << endl; 1.1698 + // Emit final rules 1.1699 + int32_t frc = 0; 1.1700 + for(map<string,FinalRule>::iterator i=finalRules.begin(); 1.1701 + i!=finalRules.end(); ++i) { 1.1702 + const string& id = i->first; 1.1703 + const FinalRule& r = i->second; 1.1704 + file << " " << id << ":intvector {" << endl; 1.1705 + r.print(file); 1.1706 + file << " } //_#" << frc++ << endl; 1.1707 + } 1.1708 + file << " }" << endl; 1.1709 + 1.1710 + // Emit country (region) map. 1.1711 + if (ICU44PLUS) { 1.1712 + file << " Regions:array {" << endl; 1.1713 + int32_t zn = 0; 1.1714 + for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) { 1.1715 + map<string, string>::iterator cit = reverseCountryMap.find(i->first); 1.1716 + if (cit == reverseCountryMap.end()) { 1.1717 + file << " \"001\","; 1.1718 + } else { 1.1719 + file << " \"" << cit->second << "\", "; 1.1720 + } 1.1721 + file << "//Z#" << zn++ << " " << i->first << endl; 1.1722 + } 1.1723 + file << " }" << endl; 1.1724 + } else { 1.1725 + file << " Regions { " << endl; 1.1726 + int32_t rc = 0; 1.1727 + for (map<string, set<string> >::const_iterator i=countryMap.begin(); 1.1728 + i != countryMap.end(); ++i) { 1.1729 + string country = i->first; 1.1730 + const set<string>& zones(i->second); 1.1731 + file << " "; 1.1732 + if(country[0]==0) { 1.1733 + file << "Default"; 1.1734 + } 1.1735 + file << country << ":intvector { "; 1.1736 + bool first = true; 1.1737 + for (set<string>::const_iterator j=zones.begin(); 1.1738 + j != zones.end(); ++j) { 1.1739 + if (!first) file << ", "; 1.1740 + first = false; 1.1741 + if (zoneIDs.find(*j) == zoneIDs.end()) { 1.1742 + cerr << "Error: Nonexistent zone in country map: " << *j << endl; 1.1743 + return 1; 1.1744 + } 1.1745 + file << zoneIDs[*j]; // emit the zone's index number 1.1746 + } 1.1747 + file << " } //R#" << rc++ << endl; 1.1748 + } 1.1749 + file << " }" << endl; 1.1750 + } 1.1751 + 1.1752 + file << "}" << endl; 1.1753 + } 1.1754 + 1.1755 + file.close(); 1.1756 + 1.1757 + if (file) { // recheck error bit 1.1758 + cout << "Finished writing " << TZ_RESOURCE_NAME << ".txt" << endl; 1.1759 + } else { 1.1760 + cerr << "Error: Unable to open/write to " << TZ_RESOURCE_NAME << ".txt" << endl; 1.1761 + return 1; 1.1762 + } 1.1763 +} 1.1764 +//eof