intl/icu/source/tools/tzcode/tz2icu.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 **********************************************************************
michael@0 4 * Copyright (c) 2003-2010, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 **********************************************************************
michael@0 7 * Author: Alan Liu
michael@0 8 * Created: July 10 2003
michael@0 9 * Since: ICU 2.8
michael@0 10 **********************************************************************
michael@0 11 */
michael@0 12 #include "tzfile.h" // from Olson tzcode archive, copied to this dir
michael@0 13
michael@0 14 #ifdef WIN32
michael@0 15
michael@0 16 #include <windows.h>
michael@0 17 #undef min // windows.h/STL conflict
michael@0 18 #undef max // windows.h/STL conflict
michael@0 19 // "identifier was truncated to 'number' characters" warning
michael@0 20 #pragma warning(disable: 4786)
michael@0 21
michael@0 22 #else
michael@0 23
michael@0 24 #include <unistd.h>
michael@0 25 #include <stdio.h>
michael@0 26 #include <dirent.h>
michael@0 27 #include <string.h>
michael@0 28 #include <sys/stat.h>
michael@0 29
michael@0 30 #endif
michael@0 31
michael@0 32 #include <algorithm>
michael@0 33 #include <cassert>
michael@0 34 #include <ctime>
michael@0 35 #include <fstream>
michael@0 36 #include <iomanip>
michael@0 37 #include <iostream>
michael@0 38 #include <iterator>
michael@0 39 #include <limits>
michael@0 40 #include <map>
michael@0 41 #include <set>
michael@0 42 #include <sstream>
michael@0 43 #include <sstream>
michael@0 44 #include <stdexcept>
michael@0 45 #include <string>
michael@0 46 #include <vector>
michael@0 47
michael@0 48 #include "tz2icu.h"
michael@0 49 #include "unicode/uversion.h"
michael@0 50
michael@0 51 using namespace std;
michael@0 52
michael@0 53 bool ICU44PLUS = TRUE;
michael@0 54 string TZ_RESOURCE_NAME = ICU_TZ_RESOURCE;
michael@0 55
michael@0 56 //--------------------------------------------------------------------
michael@0 57 // Time utilities
michael@0 58 //--------------------------------------------------------------------
michael@0 59
michael@0 60 const int64_t SECS_PER_YEAR = 31536000; // 365 days
michael@0 61 const int64_t SECS_PER_LEAP_YEAR = 31622400; // 366 days
michael@0 62 const int64_t LOWEST_TIME32 = (int64_t)((int32_t)0x80000000);
michael@0 63 const int64_t HIGHEST_TIME32 = (int64_t)((int32_t)0x7fffffff);
michael@0 64
michael@0 65 bool isLeap(int32_t y) {
michael@0 66 return (y%4 == 0) && ((y%100 != 0) || (y%400 == 0)); // Gregorian
michael@0 67 }
michael@0 68
michael@0 69 int64_t secsPerYear(int32_t y) {
michael@0 70 return isLeap(y) ? SECS_PER_LEAP_YEAR : SECS_PER_YEAR;
michael@0 71 }
michael@0 72
michael@0 73 /**
michael@0 74 * Given a calendar year, return the GMT epoch seconds for midnight
michael@0 75 * GMT of January 1 of that year. yearToSeconds(1970) == 0.
michael@0 76 */
michael@0 77 int64_t yearToSeconds(int32_t year) {
michael@0 78 // inefficient but foolproof
michael@0 79 int64_t s = 0;
michael@0 80 int32_t y = 1970;
michael@0 81 while (y < year) {
michael@0 82 s += secsPerYear(y++);
michael@0 83 }
michael@0 84 while (y > year) {
michael@0 85 s -= secsPerYear(--y);
michael@0 86 }
michael@0 87 return s;
michael@0 88 }
michael@0 89
michael@0 90 /**
michael@0 91 * Given 1970 GMT epoch seconds, return the calendar year containing
michael@0 92 * that time. secondsToYear(0) == 1970.
michael@0 93 */
michael@0 94 int32_t secondsToYear(int64_t seconds) {
michael@0 95 // inefficient but foolproof
michael@0 96 int32_t y = 1970;
michael@0 97 int64_t s = 0;
michael@0 98 if (seconds >= 0) {
michael@0 99 for (;;) {
michael@0 100 s += secsPerYear(y++);
michael@0 101 if (s > seconds) break;
michael@0 102 }
michael@0 103 --y;
michael@0 104 } else {
michael@0 105 for (;;) {
michael@0 106 s -= secsPerYear(--y);
michael@0 107 if (s <= seconds) break;
michael@0 108 }
michael@0 109 }
michael@0 110 return y;
michael@0 111 }
michael@0 112
michael@0 113 //--------------------------------------------------------------------
michael@0 114 // Types
michael@0 115 //--------------------------------------------------------------------
michael@0 116
michael@0 117 struct FinalZone;
michael@0 118 struct FinalRule;
michael@0 119 struct SimplifiedZoneType;
michael@0 120
michael@0 121 // A transition from one ZoneType to another
michael@0 122 // Minimal size = 5 bytes (4+1)
michael@0 123 struct Transition {
michael@0 124 int64_t time; // seconds, 1970 epoch
michael@0 125 int32_t type; // index into 'ZoneInfo.types' 0..255
michael@0 126 Transition(int64_t _time, int32_t _type) {
michael@0 127 time = _time;
michael@0 128 type = _type;
michael@0 129 }
michael@0 130 };
michael@0 131
michael@0 132 // A behavior mode (what zic calls a 'type') of a time zone.
michael@0 133 // Minimal size = 6 bytes (4+1+3bits)
michael@0 134 // SEE: SimplifiedZoneType
michael@0 135 struct ZoneType {
michael@0 136 int64_t rawoffset; // raw seconds offset from GMT
michael@0 137 int64_t dstoffset; // dst seconds offset from GMT
michael@0 138
michael@0 139 // We don't really need any of the following, but they are
michael@0 140 // retained for possible future use. See SimplifiedZoneType.
michael@0 141 int32_t abbr; // index into ZoneInfo.abbrs 0..n-1
michael@0 142 bool isdst;
michael@0 143 bool isstd;
michael@0 144 bool isgmt;
michael@0 145
michael@0 146 ZoneType(const SimplifiedZoneType&); // used by optimizeTypeList
michael@0 147
michael@0 148 ZoneType() : rawoffset(-1), dstoffset(-1), abbr(-1) {}
michael@0 149
michael@0 150 // A restricted equality, of just the raw and dst offset
michael@0 151 bool matches(const ZoneType& other) {
michael@0 152 return rawoffset == other.rawoffset &&
michael@0 153 dstoffset == other.dstoffset;
michael@0 154 }
michael@0 155 };
michael@0 156
michael@0 157 // A collection of transitions from one ZoneType to another, together
michael@0 158 // with a list of the ZoneTypes. A ZoneInfo object may have a long
michael@0 159 // list of transitions between a smaller list of ZoneTypes.
michael@0 160 //
michael@0 161 // This object represents the contents of a single zic-created
michael@0 162 // zoneinfo file.
michael@0 163 struct ZoneInfo {
michael@0 164 vector<Transition> transitions;
michael@0 165 vector<ZoneType> types;
michael@0 166 vector<string> abbrs;
michael@0 167
michael@0 168 string finalRuleID;
michael@0 169 int32_t finalOffset;
michael@0 170 int32_t finalYear; // -1 if none
michael@0 171
michael@0 172 // If this is an alias, then all other fields are meaningless, and
michael@0 173 // this field will point to the "real" zone 0..n-1.
michael@0 174 int32_t aliasTo; // -1 if this is a "real" zone
michael@0 175
michael@0 176 // If there are aliases TO this zone, then the following set will
michael@0 177 // contain their index numbers (each index >= 0).
michael@0 178 set<int32_t> aliases;
michael@0 179
michael@0 180 ZoneInfo() : finalYear(-1), aliasTo(-1) {}
michael@0 181
michael@0 182 void mergeFinalData(const FinalZone& fz);
michael@0 183
michael@0 184 void optimizeTypeList();
michael@0 185
michael@0 186 // Set this zone to be an alias TO another zone.
michael@0 187 void setAliasTo(int32_t index);
michael@0 188
michael@0 189 // Clear the list of aliases OF this zone.
michael@0 190 void clearAliases();
michael@0 191
michael@0 192 // Add an alias to the list of aliases OF this zone.
michael@0 193 void addAlias(int32_t index);
michael@0 194
michael@0 195 // Is this an alias to another zone?
michael@0 196 bool isAlias() const {
michael@0 197 return aliasTo >= 0;
michael@0 198 }
michael@0 199
michael@0 200 // Retrieve alias list
michael@0 201 const set<int32_t>& getAliases() const {
michael@0 202 return aliases;
michael@0 203 }
michael@0 204
michael@0 205 void print(ostream& os, const string& id) const;
michael@0 206 };
michael@0 207
michael@0 208 void ZoneInfo::clearAliases() {
michael@0 209 assert(aliasTo < 0);
michael@0 210 aliases.clear();
michael@0 211 }
michael@0 212
michael@0 213 void ZoneInfo::addAlias(int32_t index) {
michael@0 214 assert(aliasTo < 0 && index >= 0 && aliases.find(index) == aliases.end());
michael@0 215 aliases.insert(index);
michael@0 216 }
michael@0 217
michael@0 218 void ZoneInfo::setAliasTo(int32_t index) {
michael@0 219 assert(index >= 0);
michael@0 220 assert(aliases.size() == 0);
michael@0 221 aliasTo = index;
michael@0 222 }
michael@0 223
michael@0 224 typedef map<string, ZoneInfo> ZoneMap;
michael@0 225
michael@0 226 typedef ZoneMap::const_iterator ZoneMapIter;
michael@0 227
michael@0 228 //--------------------------------------------------------------------
michael@0 229 // ZONEINFO
michael@0 230 //--------------------------------------------------------------------
michael@0 231
michael@0 232 // Global map holding all our ZoneInfo objects, indexed by id.
michael@0 233 ZoneMap ZONEINFO;
michael@0 234
michael@0 235 //--------------------------------------------------------------------
michael@0 236 // zoneinfo file parsing
michael@0 237 //--------------------------------------------------------------------
michael@0 238
michael@0 239 // Read zic-coded 32-bit integer from file
michael@0 240 int64_t readcoded(ifstream& file, int64_t minv=numeric_limits<int64_t>::min(),
michael@0 241 int64_t maxv=numeric_limits<int64_t>::max()) {
michael@0 242 unsigned char buf[4]; // must be UNSIGNED
michael@0 243 int64_t val=0;
michael@0 244 file.read((char*)buf, 4);
michael@0 245 for(int32_t i=0,shift=24;i<4;++i,shift-=8) {
michael@0 246 val |= buf[i] << shift;
michael@0 247 }
michael@0 248 if (val < minv || val > maxv) {
michael@0 249 ostringstream os;
michael@0 250 os << "coded value out-of-range: " << val << ", expected ["
michael@0 251 << minv << ", " << maxv << "]";
michael@0 252 throw out_of_range(os.str());
michael@0 253 }
michael@0 254 return val;
michael@0 255 }
michael@0 256
michael@0 257 // Read zic-coded 64-bit integer from file
michael@0 258 int64_t readcoded64(ifstream& file, int64_t minv=numeric_limits<int64_t>::min(),
michael@0 259 int64_t maxv=numeric_limits<int64_t>::max()) {
michael@0 260 unsigned char buf[8]; // must be UNSIGNED
michael@0 261 int64_t val=0;
michael@0 262 file.read((char*)buf, 8);
michael@0 263 for(int32_t i=0,shift=56;i<8;++i,shift-=8) {
michael@0 264 val |= (int64_t)buf[i] << shift;
michael@0 265 }
michael@0 266 if (val < minv || val > maxv) {
michael@0 267 ostringstream os;
michael@0 268 os << "coded value out-of-range: " << val << ", expected ["
michael@0 269 << minv << ", " << maxv << "]";
michael@0 270 throw out_of_range(os.str());
michael@0 271 }
michael@0 272 return val;
michael@0 273 }
michael@0 274
michael@0 275 // Read a boolean value
michael@0 276 bool readbool(ifstream& file) {
michael@0 277 char c;
michael@0 278 file.read(&c, 1);
michael@0 279 if (c!=0 && c!=1) {
michael@0 280 ostringstream os;
michael@0 281 os << "boolean value out-of-range: " << (int32_t)c;
michael@0 282 throw out_of_range(os.str());
michael@0 283 }
michael@0 284 return (c!=0);
michael@0 285 }
michael@0 286
michael@0 287 /**
michael@0 288 * Read the zoneinfo file structure (see tzfile.h) into a ZoneInfo
michael@0 289 * @param file an already-open file stream
michael@0 290 */
michael@0 291 void readzoneinfo(ifstream& file, ZoneInfo& info, bool is64bitData) {
michael@0 292 int32_t i;
michael@0 293
michael@0 294 // Check for TZ_ICU_MAGIC signature at file start. If we get a
michael@0 295 // signature mismatch, it means we're trying to read a file which
michael@0 296 // isn't a ICU-modified-zic-created zoneinfo file. Typically this
michael@0 297 // means the user is passing in a "normal" zoneinfo directory, or
michael@0 298 // a zoneinfo directory that is polluted with other files, or that
michael@0 299 // the user passed in the wrong directory.
michael@0 300 char buf[32];
michael@0 301 file.read(buf, 4);
michael@0 302 if (strncmp(buf, TZ_ICU_MAGIC, 4) != 0) {
michael@0 303 throw invalid_argument("TZ_ICU_MAGIC signature missing");
michael@0 304 }
michael@0 305 // skip additional Olson byte version
michael@0 306 file.read(buf, 1);
michael@0 307 // if '\0', we have just one copy of data, if '2', there is additional
michael@0 308 // 64 bit version at the end.
michael@0 309 if(buf[0]!=0 && buf[0]!='2') {
michael@0 310 throw invalid_argument("Bad Olson version info");
michael@0 311 }
michael@0 312
michael@0 313 // Read reserved bytes. The first of these will be a version byte.
michael@0 314 file.read(buf, 15);
michael@0 315 if (*(ICUZoneinfoVersion*)&buf != TZ_ICU_VERSION) {
michael@0 316 throw invalid_argument("File version mismatch");
michael@0 317 }
michael@0 318
michael@0 319 // Read array sizes
michael@0 320 int64_t isgmtcnt = readcoded(file, 0);
michael@0 321 int64_t isdstcnt = readcoded(file, 0);
michael@0 322 int64_t leapcnt = readcoded(file, 0);
michael@0 323 int64_t timecnt = readcoded(file, 0);
michael@0 324 int64_t typecnt = readcoded(file, 0);
michael@0 325 int64_t charcnt = readcoded(file, 0);
michael@0 326
michael@0 327 // Confirm sizes that we assume to be equal. These assumptions
michael@0 328 // are drawn from a reading of the zic source (2003a), so they
michael@0 329 // should hold unless the zic source changes.
michael@0 330 if (isgmtcnt != typecnt || isdstcnt != typecnt) {
michael@0 331 throw invalid_argument("count mismatch between tzh_ttisgmtcnt, tzh_ttisdstcnt, tth_typecnt");
michael@0 332 }
michael@0 333
michael@0 334 // Used temporarily to store transition times and types. We need
michael@0 335 // to do this because the times and types are stored in two
michael@0 336 // separate arrays.
michael@0 337 vector<int64_t> transitionTimes(timecnt, -1); // temporary
michael@0 338 vector<int32_t> transitionTypes(timecnt, -1); // temporary
michael@0 339
michael@0 340 // Read transition times
michael@0 341 for (i=0; i<timecnt; ++i) {
michael@0 342 if (is64bitData) {
michael@0 343 transitionTimes[i] = readcoded64(file);
michael@0 344 } else {
michael@0 345 transitionTimes[i] = readcoded(file);
michael@0 346 }
michael@0 347 }
michael@0 348
michael@0 349 // Read transition types
michael@0 350 for (i=0; i<timecnt; ++i) {
michael@0 351 unsigned char c;
michael@0 352 file.read((char*) &c, 1);
michael@0 353 int32_t t = (int32_t) c;
michael@0 354 if (t < 0 || t >= typecnt) {
michael@0 355 ostringstream os;
michael@0 356 os << "illegal type: " << t << ", expected [0, " << (typecnt-1) << "]";
michael@0 357 throw out_of_range(os.str());
michael@0 358 }
michael@0 359 transitionTypes[i] = t;
michael@0 360 }
michael@0 361
michael@0 362 // Build transitions vector out of corresponding times and types.
michael@0 363 bool insertInitial = false;
michael@0 364 if (is64bitData && !ICU44PLUS) {
michael@0 365 if (timecnt > 0) {
michael@0 366 int32_t minidx = -1;
michael@0 367 for (i=0; i<timecnt; ++i) {
michael@0 368 if (transitionTimes[i] < LOWEST_TIME32) {
michael@0 369 if (minidx == -1 || transitionTimes[i] > transitionTimes[minidx]) {
michael@0 370 // Preserve the latest transition before the 32bit minimum time
michael@0 371 minidx = i;
michael@0 372 }
michael@0 373 } else if (transitionTimes[i] > HIGHEST_TIME32) {
michael@0 374 // Skipping the rest of the transition data. We cannot put such
michael@0 375 // transitions into zoneinfo.res, because data is limited to singed
michael@0 376 // 32bit int by the ICU resource bundle.
michael@0 377 break;
michael@0 378 } else {
michael@0 379 info.transitions.push_back(Transition(transitionTimes[i], transitionTypes[i]));
michael@0 380 }
michael@0 381 }
michael@0 382
michael@0 383 if (minidx != -1) {
michael@0 384 // If there are any transitions before the 32bit minimum time,
michael@0 385 // put the type information with the 32bit minimum time
michael@0 386 vector<Transition>::iterator itr = info.transitions.begin();
michael@0 387 info.transitions.insert(itr, Transition(LOWEST_TIME32, transitionTypes[minidx]));
michael@0 388 } else {
michael@0 389 // Otherwise, we need insert the initial type later
michael@0 390 insertInitial = true;
michael@0 391 }
michael@0 392 }
michael@0 393 } else {
michael@0 394 for (i=0; i<timecnt; ++i) {
michael@0 395 info.transitions.push_back(Transition(transitionTimes[i], transitionTypes[i]));
michael@0 396 }
michael@0 397 }
michael@0 398
michael@0 399 // Read types (except for the isdst and isgmt flags, which come later (why??))
michael@0 400 for (i=0; i<typecnt; ++i) {
michael@0 401 ZoneType type;
michael@0 402
michael@0 403 type.rawoffset = readcoded(file);
michael@0 404 type.dstoffset = readcoded(file);
michael@0 405 type.isdst = readbool(file);
michael@0 406
michael@0 407 unsigned char c;
michael@0 408 file.read((char*) &c, 1);
michael@0 409 type.abbr = (int32_t) c;
michael@0 410
michael@0 411 if (type.isdst != (type.dstoffset != 0)) {
michael@0 412 throw invalid_argument("isdst does not reflect dstoffset");
michael@0 413 }
michael@0 414
michael@0 415 info.types.push_back(type);
michael@0 416 }
michael@0 417
michael@0 418 assert(info.types.size() == (unsigned) typecnt);
michael@0 419
michael@0 420 if (insertInitial) {
michael@0 421 assert(timecnt > 0);
michael@0 422 assert(typecnt > 0);
michael@0 423
michael@0 424 int32_t initialTypeIdx = -1;
michael@0 425
michael@0 426 // Check if the first type is not dst
michael@0 427 if (info.types.at(0).dstoffset != 0) {
michael@0 428 // Initial type's rawoffset is same with the rawoffset after the
michael@0 429 // first transition, but no DST is observed.
michael@0 430 int64_t rawoffset0 = (info.types.at(info.transitions.at(0).type)).rawoffset;
michael@0 431 // Look for matching type
michael@0 432 for (i=0; i<(int32_t)info.types.size(); ++i) {
michael@0 433 if (info.types.at(i).rawoffset == rawoffset0
michael@0 434 && info.types.at(i).dstoffset == 0) {
michael@0 435 initialTypeIdx = i;
michael@0 436 break;
michael@0 437 }
michael@0 438 }
michael@0 439 } else {
michael@0 440 initialTypeIdx = 0;
michael@0 441 }
michael@0 442 assert(initialTypeIdx >= 0);
michael@0 443 // Add the initial type associated with the lowest int32 time
michael@0 444 vector<Transition>::iterator itr = info.transitions.begin();
michael@0 445 info.transitions.insert(itr, Transition(LOWEST_TIME32, initialTypeIdx));
michael@0 446 }
michael@0 447
michael@0 448
michael@0 449 // Read the abbreviation string
michael@0 450 if (charcnt) {
michael@0 451 // All abbreviations are concatenated together, with a 0 at
michael@0 452 // the end of each abbr.
michael@0 453 char* str = new char[charcnt + 8];
michael@0 454 file.read(str, charcnt);
michael@0 455
michael@0 456 // Split abbreviations apart into individual strings. Record
michael@0 457 // offset of each abbr in a vector.
michael@0 458 vector<int32_t> abbroffset;
michael@0 459 char *limit=str+charcnt;
michael@0 460 for (char* p=str; p<limit; ++p) {
michael@0 461 char* start = p;
michael@0 462 while (*p != 0) ++p;
michael@0 463 info.abbrs.push_back(string(start, p-start));
michael@0 464 abbroffset.push_back(start-str);
michael@0 465 }
michael@0 466
michael@0 467 // Remap all the abbrs. Old value is offset into concatenated
michael@0 468 // raw abbr strings. New value is index into vector of
michael@0 469 // strings. E.g., 0,5,10,14 => 0,1,2,3.
michael@0 470
michael@0 471 // Keep track of which abbreviations get used.
michael@0 472 vector<bool> abbrseen(abbroffset.size(), false);
michael@0 473
michael@0 474 for (vector<ZoneType>::iterator it=info.types.begin();
michael@0 475 it!=info.types.end();
michael@0 476 ++it) {
michael@0 477 vector<int32_t>::const_iterator x=
michael@0 478 find(abbroffset.begin(), abbroffset.end(), it->abbr);
michael@0 479 if (x==abbroffset.end()) {
michael@0 480 // TODO: Modify code to add a new string to the end of
michael@0 481 // the abbr list when a middle offset is given, e.g.,
michael@0 482 // "abc*def*" where * == '\0', take offset of 1 and
michael@0 483 // make the array "abc", "def", "bc", and translate 1
michael@0 484 // => 2. NOT CRITICAL since we don't even use the
michael@0 485 // abbr at this time.
michael@0 486 #if 0
michael@0 487 // TODO: Re-enable this warning if we start using
michael@0 488 // the Olson abbr data, or if the above TODO is completed.
michael@0 489 ostringstream os;
michael@0 490 os << "Warning: unusual abbr offset " << it->abbr
michael@0 491 << ", expected one of";
michael@0 492 for (vector<int32_t>::const_iterator y=abbroffset.begin();
michael@0 493 y!=abbroffset.end(); ++y) {
michael@0 494 os << ' ' << *y;
michael@0 495 }
michael@0 496 cerr << os.str() << "; using 0" << endl;
michael@0 497 #endif
michael@0 498 it->abbr = 0;
michael@0 499 } else {
michael@0 500 int32_t index = x - abbroffset.begin();
michael@0 501 it->abbr = index;
michael@0 502 abbrseen[index] = true;
michael@0 503 }
michael@0 504 }
michael@0 505
michael@0 506 for (int32_t ii=0;ii<(int32_t) abbrseen.size();++ii) {
michael@0 507 if (!abbrseen[ii]) {
michael@0 508 cerr << "Warning: unused abbreviation: " << ii << endl;
michael@0 509 }
michael@0 510 }
michael@0 511 }
michael@0 512
michael@0 513 // Read leap second info, if any.
michael@0 514 // *** We discard leap second data. ***
michael@0 515 for (i=0; i<leapcnt; ++i) {
michael@0 516 readcoded(file); // transition time
michael@0 517 readcoded(file); // total correction after above
michael@0 518 }
michael@0 519
michael@0 520 // Read isstd flags
michael@0 521 for (i=0; i<typecnt; ++i) info.types[i].isstd = readbool(file);
michael@0 522
michael@0 523 // Read isgmt flags
michael@0 524 for (i=0; i<typecnt; ++i) info.types[i].isgmt = readbool(file);
michael@0 525 }
michael@0 526
michael@0 527 //--------------------------------------------------------------------
michael@0 528 // Directory and file reading
michael@0 529 //--------------------------------------------------------------------
michael@0 530
michael@0 531 /**
michael@0 532 * Process a single zoneinfo file, adding the data to ZONEINFO
michael@0 533 * @param path the full path to the file, e.g., ".\zoneinfo\America\Los_Angeles"
michael@0 534 * @param id the zone ID, e.g., "America/Los_Angeles"
michael@0 535 */
michael@0 536 void handleFile(string path, string id) {
michael@0 537 // Check for duplicate id
michael@0 538 if (ZONEINFO.find(id) != ZONEINFO.end()) {
michael@0 539 ostringstream os;
michael@0 540 os << "duplicate zone ID: " << id;
michael@0 541 throw invalid_argument(os.str());
michael@0 542 }
michael@0 543
michael@0 544 ifstream file(path.c_str(), ios::in | ios::binary);
michael@0 545 if (!file) {
michael@0 546 throw invalid_argument("can't open file");
michael@0 547 }
michael@0 548
michael@0 549 // eat 32bit data part
michael@0 550 ZoneInfo info;
michael@0 551 readzoneinfo(file, info, false);
michael@0 552
michael@0 553 // Check for errors
michael@0 554 if (!file) {
michael@0 555 throw invalid_argument("read error");
michael@0 556 }
michael@0 557
michael@0 558 // we only use 64bit part
michael@0 559 ZoneInfo info64;
michael@0 560 readzoneinfo(file, info64, true);
michael@0 561
michael@0 562 bool alldone = false;
michael@0 563 int64_t eofPos = (int64_t) file.tellg();
michael@0 564
michael@0 565 // '\n' + <envvar string> + '\n' after the 64bit version data
michael@0 566 char ch = file.get();
michael@0 567 if (ch == 0x0a) {
michael@0 568 bool invalidchar = false;
michael@0 569 while (file.get(ch)) {
michael@0 570 if (ch == 0x0a) {
michael@0 571 break;
michael@0 572 }
michael@0 573 if (ch < 0x20) {
michael@0 574 // must be printable ascii
michael@0 575 invalidchar = true;
michael@0 576 break;
michael@0 577 }
michael@0 578 }
michael@0 579 if (!invalidchar) {
michael@0 580 eofPos = (int64_t) file.tellg();
michael@0 581 file.seekg(0, ios::end);
michael@0 582 eofPos = eofPos - (int64_t) file.tellg();
michael@0 583 if (eofPos == 0) {
michael@0 584 alldone = true;
michael@0 585 }
michael@0 586 }
michael@0 587 }
michael@0 588 if (!alldone) {
michael@0 589 ostringstream os;
michael@0 590 os << (-eofPos) << " unprocessed bytes at end";
michael@0 591 throw invalid_argument(os.str());
michael@0 592 }
michael@0 593
michael@0 594 ZONEINFO[id] = info64;
michael@0 595 }
michael@0 596
michael@0 597 /**
michael@0 598 * Recursively scan the given directory, calling handleFile() for each
michael@0 599 * file in the tree. The user should call with the root directory and
michael@0 600 * a prefix of "". The function will call itself with non-empty
michael@0 601 * prefix values.
michael@0 602 */
michael@0 603 #ifdef WIN32
michael@0 604
michael@0 605 void scandir(string dirname, string prefix="") {
michael@0 606 HANDLE hList;
michael@0 607 WIN32_FIND_DATA FileData;
michael@0 608
michael@0 609 // Get the first file
michael@0 610 hList = FindFirstFile((dirname + "\\*").c_str(), &FileData);
michael@0 611 if (hList == INVALID_HANDLE_VALUE) {
michael@0 612 cerr << "Error: Invalid directory: " << dirname << endl;
michael@0 613 exit(1);
michael@0 614 }
michael@0 615 for (;;) {
michael@0 616 string name(FileData.cFileName);
michael@0 617 string path(dirname + "\\" + name);
michael@0 618 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
michael@0 619 if (name != "." && name != "..") {
michael@0 620 scandir(path, prefix + name + "/");
michael@0 621 }
michael@0 622 } else {
michael@0 623 try {
michael@0 624 string id = prefix + name;
michael@0 625 handleFile(path, id);
michael@0 626 } catch (const exception& e) {
michael@0 627 cerr << "Error: While processing \"" << path << "\", "
michael@0 628 << e.what() << endl;
michael@0 629 exit(1);
michael@0 630 }
michael@0 631 }
michael@0 632
michael@0 633 if (!FindNextFile(hList, &FileData)) {
michael@0 634 if (GetLastError() == ERROR_NO_MORE_FILES) {
michael@0 635 break;
michael@0 636 } // else...?
michael@0 637 }
michael@0 638 }
michael@0 639 FindClose(hList);
michael@0 640 }
michael@0 641
michael@0 642 #else
michael@0 643
michael@0 644 void scandir(string dir, string prefix="") {
michael@0 645 DIR *dp;
michael@0 646 struct dirent *dir_entry;
michael@0 647 struct stat stat_info;
michael@0 648 char pwd[512];
michael@0 649 vector<string> subdirs;
michael@0 650 vector<string> subfiles;
michael@0 651
michael@0 652 if ((dp = opendir(dir.c_str())) == NULL) {
michael@0 653 cerr << "Error: Invalid directory: " << dir << endl;
michael@0 654 exit(1);
michael@0 655 }
michael@0 656 if (!getcwd(pwd, sizeof(pwd))) {
michael@0 657 cerr << "Error: Directory name too long" << endl;
michael@0 658 exit(1);
michael@0 659 }
michael@0 660 chdir(dir.c_str());
michael@0 661 while ((dir_entry = readdir(dp)) != NULL) {
michael@0 662 string name = dir_entry->d_name;
michael@0 663 string path = dir + "/" + name;
michael@0 664 lstat(dir_entry->d_name,&stat_info);
michael@0 665 if (S_ISDIR(stat_info.st_mode)) {
michael@0 666 if (name != "." && name != "..") {
michael@0 667 subdirs.push_back(path);
michael@0 668 subdirs.push_back(prefix + name + "/");
michael@0 669 // scandir(path, prefix + name + "/");
michael@0 670 }
michael@0 671 } else {
michael@0 672 try {
michael@0 673 string id = prefix + name;
michael@0 674 subfiles.push_back(path);
michael@0 675 subfiles.push_back(id);
michael@0 676 // handleFile(path, id);
michael@0 677 } catch (const exception& e) {
michael@0 678 cerr << "Error: While processing \"" << path << "\", "
michael@0 679 << e.what() << endl;
michael@0 680 exit(1);
michael@0 681 }
michael@0 682 }
michael@0 683 }
michael@0 684 closedir(dp);
michael@0 685 chdir(pwd);
michael@0 686
michael@0 687 for(int32_t i=0;i<(int32_t)subfiles.size();i+=2) {
michael@0 688 try {
michael@0 689 handleFile(subfiles[i], subfiles[i+1]);
michael@0 690 } catch (const exception& e) {
michael@0 691 cerr << "Error: While processing \"" << subfiles[i] << "\", "
michael@0 692 << e.what() << endl;
michael@0 693 exit(1);
michael@0 694 }
michael@0 695 }
michael@0 696 for(int32_t i=0;i<(int32_t)subdirs.size();i+=2) {
michael@0 697 scandir(subdirs[i], subdirs[i+1]);
michael@0 698 }
michael@0 699 }
michael@0 700
michael@0 701 #endif
michael@0 702
michael@0 703 //--------------------------------------------------------------------
michael@0 704 // Final zone and rule info
michael@0 705 //--------------------------------------------------------------------
michael@0 706
michael@0 707 /**
michael@0 708 * Read and discard the current line.
michael@0 709 */
michael@0 710 void consumeLine(istream& in) {
michael@0 711 int32_t c;
michael@0 712 do {
michael@0 713 c = in.get();
michael@0 714 } while (c != EOF && c != '\n');
michael@0 715 }
michael@0 716
michael@0 717 enum {
michael@0 718 DOM = 0,
michael@0 719 DOWGEQ = 1,
michael@0 720 DOWLEQ = 2
michael@0 721 };
michael@0 722
michael@0 723 const char* TIME_MODE[] = {"w", "s", "u"};
michael@0 724
michael@0 725 // Allow 29 days in February because zic outputs February 29
michael@0 726 // for rules like "last Sunday in February".
michael@0 727 const int32_t MONTH_LEN[] = {31,29,31,30,31,30,31,31,30,31,30,31};
michael@0 728
michael@0 729 const int32_t HOUR = 3600;
michael@0 730
michael@0 731 struct FinalZone {
michael@0 732 int32_t offset; // raw offset
michael@0 733 int32_t year; // takes effect for y >= year
michael@0 734 string ruleid;
michael@0 735 set<string> aliases;
michael@0 736 FinalZone(int32_t _offset, int32_t _year, const string& _ruleid) :
michael@0 737 offset(_offset), year(_year), ruleid(_ruleid) {
michael@0 738 if (offset <= -16*HOUR || offset >= 16*HOUR) {
michael@0 739 ostringstream os;
michael@0 740 os << "Invalid input offset " << offset
michael@0 741 << " for year " << year
michael@0 742 << " and rule ID " << ruleid;
michael@0 743 throw invalid_argument(os.str());
michael@0 744 }
michael@0 745 if (year < 1900 || year >= 2050) {
michael@0 746 ostringstream os;
michael@0 747 os << "Invalid input year " << year
michael@0 748 << " with offset " << offset
michael@0 749 << " and rule ID " << ruleid;
michael@0 750 throw invalid_argument(os.str());
michael@0 751 }
michael@0 752 }
michael@0 753 FinalZone() : offset(-1), year(-1) {}
michael@0 754 void addLink(const string& alias) {
michael@0 755 if (aliases.find(alias) != aliases.end()) {
michael@0 756 ostringstream os;
michael@0 757 os << "Duplicate alias " << alias;
michael@0 758 throw invalid_argument(os.str());
michael@0 759 }
michael@0 760 aliases.insert(alias);
michael@0 761 }
michael@0 762 };
michael@0 763
michael@0 764 struct FinalRulePart {
michael@0 765 int32_t mode;
michael@0 766 int32_t month;
michael@0 767 int32_t dom;
michael@0 768 int32_t dow;
michael@0 769 int32_t time;
michael@0 770 int32_t offset; // dst offset, usually either 0 or 1:00
michael@0 771
michael@0 772 // Isstd and isgmt only have 3 valid states, corresponding to local
michael@0 773 // wall time, local standard time, and GMT standard time.
michael@0 774 // Here is how the isstd & isgmt flags are set by zic:
michael@0 775 //| case 's': /* Standard */
michael@0 776 //| rp->r_todisstd = TRUE;
michael@0 777 //| rp->r_todisgmt = FALSE;
michael@0 778 //| case 'w': /* Wall */
michael@0 779 //| rp->r_todisstd = FALSE;
michael@0 780 //| rp->r_todisgmt = FALSE;
michael@0 781 //| case 'g': /* Greenwich */
michael@0 782 //| case 'u': /* Universal */
michael@0 783 //| case 'z': /* Zulu */
michael@0 784 //| rp->r_todisstd = TRUE;
michael@0 785 //| rp->r_todisgmt = TRUE;
michael@0 786 bool isstd;
michael@0 787 bool isgmt;
michael@0 788
michael@0 789 bool isset; // used during building; later ignored
michael@0 790
michael@0 791 FinalRulePart() : isset(false) {}
michael@0 792 void set(const string& id,
michael@0 793 const string& _mode,
michael@0 794 int32_t _month,
michael@0 795 int32_t _dom,
michael@0 796 int32_t _dow,
michael@0 797 int32_t _time,
michael@0 798 bool _isstd,
michael@0 799 bool _isgmt,
michael@0 800 int32_t _offset) {
michael@0 801 if (isset) {
michael@0 802 throw invalid_argument("FinalRulePart set twice");
michael@0 803 }
michael@0 804 isset = true;
michael@0 805 if (_mode == "DOWLEQ") {
michael@0 806 mode = DOWLEQ;
michael@0 807 } else if (_mode == "DOWGEQ") {
michael@0 808 mode = DOWGEQ;
michael@0 809 } else if (_mode == "DOM") {
michael@0 810 mode = DOM;
michael@0 811 } else {
michael@0 812 throw invalid_argument("Unrecognized FinalRulePart mode");
michael@0 813 }
michael@0 814 month = _month;
michael@0 815 dom = _dom;
michael@0 816 dow = _dow;
michael@0 817 time = _time;
michael@0 818 isstd = _isstd;
michael@0 819 isgmt = _isgmt;
michael@0 820 offset = _offset;
michael@0 821
michael@0 822 ostringstream os;
michael@0 823 if (month < 0 || month >= 12) {
michael@0 824 os << "Invalid input month " << month;
michael@0 825 }
michael@0 826 if (dom < 1 || dom > MONTH_LEN[month]) {
michael@0 827 os << "Invalid input day of month " << dom;
michael@0 828 }
michael@0 829 if (mode != DOM && (dow < 0 || dow >= 7)) {
michael@0 830 os << "Invalid input day of week " << dow;
michael@0 831 }
michael@0 832 if (offset < 0 || offset > HOUR) {
michael@0 833 os << "Invalid input offset " << offset;
michael@0 834 }
michael@0 835 if (isgmt && !isstd) {
michael@0 836 os << "Invalid input isgmt && !isstd";
michael@0 837 }
michael@0 838 if (!os.str().empty()) {
michael@0 839 os << " for rule "
michael@0 840 << id
michael@0 841 << _mode
michael@0 842 << month << dom << dow << time
michael@0 843 << isstd << isgmt
michael@0 844 << offset;
michael@0 845 throw invalid_argument(os.str());
michael@0 846 }
michael@0 847 }
michael@0 848
michael@0 849 /**
michael@0 850 * Return the time mode as an ICU SimpleTimeZone int from 0..2;
michael@0 851 * see simpletz.h.
michael@0 852 */
michael@0 853 int32_t timemode() const {
michael@0 854 if (isgmt) {
michael@0 855 assert(isstd);
michael@0 856 return 2; // gmt standard
michael@0 857 }
michael@0 858 if (isstd) {
michael@0 859 return 1; // local standard
michael@0 860 }
michael@0 861 return 0; // local wall
michael@0 862 }
michael@0 863
michael@0 864 // The SimpleTimeZone encoding method for rules is as follows:
michael@0 865 // stz_dowim stz_dow
michael@0 866 // DOM: dom 0
michael@0 867 // DOWGEQ: dom -(dow+1)
michael@0 868 // DOWLEQ: -dom -(dow+1)
michael@0 869 // E.g., to encode Mon>=7, use stz_dowim=7, stz_dow=-2
michael@0 870 // to encode Mon<=7, use stz_dowim=-7, stz_dow=-2
michael@0 871 // to encode 7, use stz_dowim=7, stz_dow=0
michael@0 872 // Note that for this program and for SimpleTimeZone, 0==Jan,
michael@0 873 // but for this program 0==Sun while for SimpleTimeZone 1==Sun.
michael@0 874
michael@0 875 /**
michael@0 876 * Return a "dowim" param suitable for SimpleTimeZone.
michael@0 877 */
michael@0 878 int32_t stz_dowim() const {
michael@0 879 return (mode == DOWLEQ) ? -dom : dom;
michael@0 880 }
michael@0 881
michael@0 882 /**
michael@0 883 * Return a "dow" param suitable for SimpleTimeZone.
michael@0 884 */
michael@0 885 int32_t stz_dow() const {
michael@0 886 return (mode == DOM) ? 0 : -(dow+1);
michael@0 887 }
michael@0 888 };
michael@0 889
michael@0 890 struct FinalRule {
michael@0 891 FinalRulePart part[2];
michael@0 892
michael@0 893 bool isset() const {
michael@0 894 return part[0].isset && part[1].isset;
michael@0 895 }
michael@0 896
michael@0 897 void print(ostream& os) const;
michael@0 898 };
michael@0 899
michael@0 900 map<string,FinalZone> finalZones;
michael@0 901 map<string,FinalRule> finalRules;
michael@0 902
michael@0 903 map<string, set<string> > links;
michael@0 904 map<string, string> reverseLinks;
michael@0 905 map<string, string> linkSource; // id => "Olson link" or "ICU alias"
michael@0 906
michael@0 907 /**
michael@0 908 * Predicate used to find FinalRule objects that do not have both
michael@0 909 * sub-parts set (indicating an error in the input file).
michael@0 910 */
michael@0 911 bool isNotSet(const pair<const string,FinalRule>& p) {
michael@0 912 return !p.second.isset();
michael@0 913 }
michael@0 914
michael@0 915 /**
michael@0 916 * Predicate used to find FinalZone objects that do not map to a known
michael@0 917 * rule (indicating an error in the input file).
michael@0 918 */
michael@0 919 bool mapsToUnknownRule(const pair<const string,FinalZone>& p) {
michael@0 920 return finalRules.find(p.second.ruleid) == finalRules.end();
michael@0 921 }
michael@0 922
michael@0 923 /**
michael@0 924 * This set is used to make sure each rule in finalRules is used at
michael@0 925 * least once. First we populate it with all the rules from
michael@0 926 * finalRules; then we remove all the rules referred to in
michael@0 927 * finaleZones.
michael@0 928 */
michael@0 929 set<string> ruleIDset;
michael@0 930
michael@0 931 void insertRuleID(const pair<string,FinalRule>& p) {
michael@0 932 ruleIDset.insert(p.first);
michael@0 933 }
michael@0 934
michael@0 935 void eraseRuleID(const pair<string,FinalZone>& p) {
michael@0 936 ruleIDset.erase(p.second.ruleid);
michael@0 937 }
michael@0 938
michael@0 939 /**
michael@0 940 * Populate finalZones and finalRules from the given istream.
michael@0 941 */
michael@0 942 void readFinalZonesAndRules(istream& in) {
michael@0 943
michael@0 944 for (;;) {
michael@0 945 string token;
michael@0 946 in >> token;
michael@0 947 if (in.eof() || !in) {
michael@0 948 break;
michael@0 949 } else if (token == "zone") {
michael@0 950 // zone Africa/Cairo 7200 1995 Egypt # zone Africa/Cairo, offset 7200, year >= 1995, rule Egypt (0)
michael@0 951 string id, ruleid;
michael@0 952 int32_t offset, year;
michael@0 953 in >> id >> offset >> year >> ruleid;
michael@0 954 consumeLine(in);
michael@0 955 finalZones[id] = FinalZone(offset, year, ruleid);
michael@0 956 } else if (token == "rule") {
michael@0 957 // 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
michael@0 958 // 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
michael@0 959 string id, mode;
michael@0 960 int32_t month, dom, dow, time, offset;
michael@0 961 bool isstd, isgmt;
michael@0 962 in >> id >> mode >> month >> dom >> dow >> time >> isstd >> isgmt >> offset;
michael@0 963 consumeLine(in);
michael@0 964 FinalRule& fr = finalRules[id];
michael@0 965 int32_t p = fr.part[0].isset ? 1 : 0;
michael@0 966 fr.part[p].set(id, mode, month, dom, dow, time, isstd, isgmt, offset);
michael@0 967 } else if (token == "link") {
michael@0 968 string fromid, toid; // fromid == "real" zone, toid == alias
michael@0 969 in >> fromid >> toid;
michael@0 970 // DO NOT consumeLine(in);
michael@0 971 if (finalZones.find(toid) != finalZones.end()) {
michael@0 972 throw invalid_argument("Bad link: `to' id is a \"real\" zone");
michael@0 973 }
michael@0 974
michael@0 975 links[fromid].insert(toid);
michael@0 976 reverseLinks[toid] = fromid;
michael@0 977
michael@0 978 linkSource[fromid] = "Olson link";
michael@0 979 linkSource[toid] = "Olson link";
michael@0 980 } else if (token.length() > 0 && token[0] == '#') {
michael@0 981 consumeLine(in);
michael@0 982 } else {
michael@0 983 throw invalid_argument("Unrecognized keyword");
michael@0 984 }
michael@0 985 }
michael@0 986
michael@0 987 if (!in.eof() && !in) {
michael@0 988 throw invalid_argument("Parse failure");
michael@0 989 }
michael@0 990
michael@0 991 // Perform validity check: Each rule should have data for 2 parts.
michael@0 992 if (count_if(finalRules.begin(), finalRules.end(), isNotSet) != 0) {
michael@0 993 throw invalid_argument("One or more incomplete rule pairs");
michael@0 994 }
michael@0 995
michael@0 996 // Perform validity check: Each zone should map to a known rule.
michael@0 997 if (count_if(finalZones.begin(), finalZones.end(), mapsToUnknownRule) != 0) {
michael@0 998 throw invalid_argument("One or more zones refers to an unknown rule");
michael@0 999 }
michael@0 1000
michael@0 1001 // Perform validity check: Each rule should be referred to by a zone.
michael@0 1002 ruleIDset.clear();
michael@0 1003 for_each(finalRules.begin(), finalRules.end(), insertRuleID);
michael@0 1004 for_each(finalZones.begin(), finalZones.end(), eraseRuleID);
michael@0 1005 if (ruleIDset.size() != 0) {
michael@0 1006 throw invalid_argument("Unused rules");
michael@0 1007 }
michael@0 1008 }
michael@0 1009
michael@0 1010 //--------------------------------------------------------------------
michael@0 1011 // Resource bundle output
michael@0 1012 //--------------------------------------------------------------------
michael@0 1013
michael@0 1014 // SEE olsontz.h FOR RESOURCE BUNDLE DATA LAYOUT
michael@0 1015
michael@0 1016 void ZoneInfo::print(ostream& os, const string& id) const {
michael@0 1017 // Implement compressed format #2:
michael@0 1018 os << " /* " << id << " */ ";
michael@0 1019
michael@0 1020 if (aliasTo >= 0) {
michael@0 1021 assert(aliases.size() == 0);
michael@0 1022 os << ":int { " << aliasTo << " } "; // No endl - save room for comment.
michael@0 1023 return;
michael@0 1024 }
michael@0 1025
michael@0 1026 if (ICU44PLUS) {
michael@0 1027 os << ":table {" << endl;
michael@0 1028 } else {
michael@0 1029 os << ":array {" << endl;
michael@0 1030 }
michael@0 1031
michael@0 1032 vector<Transition>::const_iterator trn;
michael@0 1033 vector<ZoneType>::const_iterator typ;
michael@0 1034
michael@0 1035 bool first;
michael@0 1036
michael@0 1037 if (ICU44PLUS) {
michael@0 1038 trn = transitions.begin();
michael@0 1039
michael@0 1040 // pre 32bit transitions
michael@0 1041 if (trn != transitions.end() && trn->time < LOWEST_TIME32) {
michael@0 1042 os << " transPre32:intvector { ";
michael@0 1043 for (first = true; trn != transitions.end() && trn->time < LOWEST_TIME32; ++trn) {
michael@0 1044 if (!first) {
michael@0 1045 os<< ", ";
michael@0 1046 }
michael@0 1047 first = false;
michael@0 1048 os << (int32_t)(trn->time >> 32) << ", " << (int32_t)(trn->time & 0x00000000ffffffff);
michael@0 1049 }
michael@0 1050 os << " }" << endl;
michael@0 1051 }
michael@0 1052
michael@0 1053 // 32bit transtions
michael@0 1054 if (trn != transitions.end() && trn->time < HIGHEST_TIME32) {
michael@0 1055 os << " trans:intvector { ";
michael@0 1056 for (first = true; trn != transitions.end() && trn->time < HIGHEST_TIME32; ++trn) {
michael@0 1057 if (!first) {
michael@0 1058 os << ", ";
michael@0 1059 }
michael@0 1060 first = false;
michael@0 1061 os << trn->time;
michael@0 1062 }
michael@0 1063 os << " }" << endl;
michael@0 1064 }
michael@0 1065
michael@0 1066 // post 32bit transitons
michael@0 1067 if (trn != transitions.end()) {
michael@0 1068 os << " transPost32:intvector { ";
michael@0 1069 for (first = true; trn != transitions.end(); ++trn) {
michael@0 1070 if (!first) {
michael@0 1071 os<< ", ";
michael@0 1072 }
michael@0 1073 first = false;
michael@0 1074 os << (int32_t)(trn->time >> 32) << ", " << (int32_t)(trn->time & 0x00000000ffffffff);
michael@0 1075 }
michael@0 1076 os << " }" << endl;
michael@0 1077 }
michael@0 1078 } else {
michael@0 1079 os << " :intvector { ";
michael@0 1080 for (trn = transitions.begin(), first = true; trn != transitions.end(); ++trn) {
michael@0 1081 if (!first) os << ", ";
michael@0 1082 first = false;
michael@0 1083 os << trn->time;
michael@0 1084 }
michael@0 1085 os << " }" << endl;
michael@0 1086 }
michael@0 1087
michael@0 1088
michael@0 1089 first=true;
michael@0 1090 if (ICU44PLUS) {
michael@0 1091 os << " typeOffsets:intvector { ";
michael@0 1092 } else {
michael@0 1093 os << " :intvector { ";
michael@0 1094 }
michael@0 1095 for (typ = types.begin(); typ != types.end(); ++typ) {
michael@0 1096 if (!first) os << ", ";
michael@0 1097 first = false;
michael@0 1098 os << typ->rawoffset << ", " << typ->dstoffset;
michael@0 1099 }
michael@0 1100 os << " }" << endl;
michael@0 1101
michael@0 1102 if (ICU44PLUS) {
michael@0 1103 if (transitions.size() != 0) {
michael@0 1104 os << " typeMap:bin { \"" << hex << setfill('0');
michael@0 1105 for (trn = transitions.begin(); trn != transitions.end(); ++trn) {
michael@0 1106 os << setw(2) << trn->type;
michael@0 1107 }
michael@0 1108 os << dec << "\" }" << endl;
michael@0 1109 }
michael@0 1110 } else {
michael@0 1111 os << " :bin { \"" << hex << setfill('0');
michael@0 1112 for (trn = transitions.begin(); trn != transitions.end(); ++trn) {
michael@0 1113 os << setw(2) << trn->type;
michael@0 1114 }
michael@0 1115 os << dec << "\" }" << endl;
michael@0 1116 }
michael@0 1117
michael@0 1118 // Final zone info, if any
michael@0 1119 if (finalYear != -1) {
michael@0 1120 if (ICU44PLUS) {
michael@0 1121 os << " finalRule { \"" << finalRuleID << "\" }" << endl;
michael@0 1122 os << " finalRaw:int { " << finalOffset << " }" << endl;
michael@0 1123 os << " finalYear:int { " << finalYear << " }" << endl;
michael@0 1124 } else {
michael@0 1125 os << " \"" << finalRuleID << "\"" << endl;
michael@0 1126 os << " :intvector { " << finalOffset << ", "
michael@0 1127 << finalYear << " }" << endl;
michael@0 1128 }
michael@0 1129 }
michael@0 1130
michael@0 1131 // Alias list, if any
michael@0 1132 if (aliases.size() != 0) {
michael@0 1133 first = true;
michael@0 1134 if (ICU44PLUS) {
michael@0 1135 os << " links:intvector { ";
michael@0 1136 } else {
michael@0 1137 os << " :intvector { ";
michael@0 1138 }
michael@0 1139 for (set<int32_t>::const_iterator i=aliases.begin(); i!=aliases.end(); ++i) {
michael@0 1140 if (!first) os << ", ";
michael@0 1141 first = false;
michael@0 1142 os << *i;
michael@0 1143 }
michael@0 1144 os << " }" << endl;
michael@0 1145 }
michael@0 1146
michael@0 1147 os << " } "; // no trailing 'endl', so comments can be placed.
michael@0 1148 }
michael@0 1149
michael@0 1150 inline ostream&
michael@0 1151 operator<<(ostream& os, const ZoneMap& zoneinfo) {
michael@0 1152 int32_t c = 0;
michael@0 1153 for (ZoneMapIter it = zoneinfo.begin();
michael@0 1154 it != zoneinfo.end();
michael@0 1155 ++it) {
michael@0 1156 if(c && !ICU44PLUS) os << ",";
michael@0 1157 it->second.print(os, it->first);
michael@0 1158 os << "//Z#" << c++ << endl;
michael@0 1159 }
michael@0 1160 return os;
michael@0 1161 }
michael@0 1162
michael@0 1163 // print the string list
michael@0 1164 ostream& printStringList( ostream& os, const ZoneMap& zoneinfo) {
michael@0 1165 int32_t n = 0; // count
michael@0 1166 int32_t col = 0; // column
michael@0 1167 os << " Names {" << endl
michael@0 1168 << " ";
michael@0 1169 for (ZoneMapIter it = zoneinfo.begin();
michael@0 1170 it != zoneinfo.end();
michael@0 1171 ++it) {
michael@0 1172 if(n) {
michael@0 1173 os << ",";
michael@0 1174 col ++;
michael@0 1175 }
michael@0 1176 const string& id = it->first;
michael@0 1177 os << "\"" << id << "\"";
michael@0 1178 col += id.length() + 2;
michael@0 1179 if(col >= 50) {
michael@0 1180 os << " // " << n << endl
michael@0 1181 << " ";
michael@0 1182 col = 0;
michael@0 1183 }
michael@0 1184 n++;
michael@0 1185 }
michael@0 1186 os << " // " << (n-1) << endl
michael@0 1187 << " }" << endl;
michael@0 1188
michael@0 1189 return os;
michael@0 1190 }
michael@0 1191
michael@0 1192 //--------------------------------------------------------------------
michael@0 1193 // main
michael@0 1194 //--------------------------------------------------------------------
michael@0 1195
michael@0 1196 // Unary predicate for finding transitions after a given time
michael@0 1197 bool isAfter(const Transition t, int64_t thresh) {
michael@0 1198 return t.time >= thresh;
michael@0 1199 }
michael@0 1200
michael@0 1201 /**
michael@0 1202 * A zone type that contains only the raw and dst offset. Used by the
michael@0 1203 * optimizeTypeList() method.
michael@0 1204 */
michael@0 1205 struct SimplifiedZoneType {
michael@0 1206 int64_t rawoffset;
michael@0 1207 int64_t dstoffset;
michael@0 1208 SimplifiedZoneType() : rawoffset(-1), dstoffset(-1) {}
michael@0 1209 SimplifiedZoneType(const ZoneType& t) : rawoffset(t.rawoffset),
michael@0 1210 dstoffset(t.dstoffset) {}
michael@0 1211 bool operator<(const SimplifiedZoneType& t) const {
michael@0 1212 return rawoffset < t.rawoffset ||
michael@0 1213 (rawoffset == t.rawoffset &&
michael@0 1214 dstoffset < t.dstoffset);
michael@0 1215 }
michael@0 1216 };
michael@0 1217
michael@0 1218 /**
michael@0 1219 * Construct a ZoneType from a SimplifiedZoneType. Note that this
michael@0 1220 * discards information; the new ZoneType will have meaningless
michael@0 1221 * (empty) abbr, isdst, isstd, and isgmt flags; this is appropriate,
michael@0 1222 * since ignoring these is how we do optimization (we have no use for
michael@0 1223 * these in historical transitions).
michael@0 1224 */
michael@0 1225 ZoneType::ZoneType(const SimplifiedZoneType& t) :
michael@0 1226 rawoffset(t.rawoffset), dstoffset(t.dstoffset),
michael@0 1227 abbr(-1), isdst(false), isstd(false), isgmt(false) {}
michael@0 1228
michael@0 1229 /**
michael@0 1230 * Optimize the type list to remove excess entries. The type list may
michael@0 1231 * contain entries that are distinct only in terms of their dst, std,
michael@0 1232 * or gmt flags. Since we don't care about those flags, we can reduce
michael@0 1233 * the type list to a set of unique raw/dst offset pairs, and remap
michael@0 1234 * the type indices in the transition list, which stores, for each
michael@0 1235 * transition, a transition time and a type index.
michael@0 1236 */
michael@0 1237 void ZoneInfo::optimizeTypeList() {
michael@0 1238 // Assemble set of unique types; only those in the `transitions'
michael@0 1239 // list, since there may be unused types in the `types' list
michael@0 1240 // corresponding to transitions that have been trimmed (during
michael@0 1241 // merging of final data).
michael@0 1242
michael@0 1243 if (aliasTo >= 0) return; // Nothing to do for aliases
michael@0 1244
michael@0 1245 if (!ICU44PLUS) {
michael@0 1246 // This is the old logic which has a bug, which occasionally removes
michael@0 1247 // the type before the first transition. The problem was fixed
michael@0 1248 // by inserting the dummy transition indirectly.
michael@0 1249
michael@0 1250 // If there are zero transitions and one type, then leave that as-is.
michael@0 1251 if (transitions.size() == 0) {
michael@0 1252 if (types.size() != 1) {
michael@0 1253 cerr << "Error: transition count = 0, type count = " << types.size() << endl;
michael@0 1254 }
michael@0 1255 return;
michael@0 1256 }
michael@0 1257
michael@0 1258 set<SimplifiedZoneType> simpleset;
michael@0 1259 for (vector<Transition>::const_iterator i=transitions.begin();
michael@0 1260 i!=transitions.end(); ++i) {
michael@0 1261 assert(i->type < (int32_t)types.size());
michael@0 1262 simpleset.insert(types[i->type]);
michael@0 1263 }
michael@0 1264
michael@0 1265 // Map types to integer indices
michael@0 1266 map<SimplifiedZoneType,int32_t> simplemap;
michael@0 1267 int32_t n=0;
michael@0 1268 for (set<SimplifiedZoneType>::const_iterator i=simpleset.begin();
michael@0 1269 i!=simpleset.end(); ++i) {
michael@0 1270 simplemap[*i] = n++;
michael@0 1271 }
michael@0 1272
michael@0 1273 // Remap transitions
michael@0 1274 for (vector<Transition>::iterator i=transitions.begin();
michael@0 1275 i!=transitions.end(); ++i) {
michael@0 1276 assert(i->type < (int32_t)types.size());
michael@0 1277 ZoneType oldtype = types[i->type];
michael@0 1278 SimplifiedZoneType newtype(oldtype);
michael@0 1279 assert(simplemap.find(newtype) != simplemap.end());
michael@0 1280 i->type = simplemap[newtype];
michael@0 1281 }
michael@0 1282
michael@0 1283 // Replace type list
michael@0 1284 types.clear();
michael@0 1285 copy(simpleset.begin(), simpleset.end(), back_inserter(types));
michael@0 1286
michael@0 1287 } else {
michael@0 1288 if (types.size() > 1) {
michael@0 1289 // Note: localtime uses the very first non-dst type as initial offsets.
michael@0 1290 // If all types are DSTs, the very first type is treated as the initial offsets.
michael@0 1291
michael@0 1292 // Decide a type used as the initial offsets. ICU put the type at index 0.
michael@0 1293 ZoneType initialType = types[0];
michael@0 1294 for (vector<ZoneType>::const_iterator i=types.begin(); i!=types.end(); ++i) {
michael@0 1295 if (i->dstoffset == 0) {
michael@0 1296 initialType = *i;
michael@0 1297 break;
michael@0 1298 }
michael@0 1299 }
michael@0 1300
michael@0 1301 SimplifiedZoneType initialSimplifiedType(initialType);
michael@0 1302
michael@0 1303 // create a set of unique types, but ignoring fields which we're not interested in
michael@0 1304 set<SimplifiedZoneType> simpleset;
michael@0 1305 simpleset.insert(initialSimplifiedType);
michael@0 1306 for (vector<Transition>::const_iterator i=transitions.begin(); i!=transitions.end(); ++i) {
michael@0 1307 assert(i->type < (int32_t)types.size());
michael@0 1308 simpleset.insert(types[i->type]);
michael@0 1309 }
michael@0 1310
michael@0 1311 // Map types to integer indices, however, keeping the first type at offset 0
michael@0 1312 map<SimplifiedZoneType,int32_t> simplemap;
michael@0 1313 simplemap[initialSimplifiedType] = 0;
michael@0 1314 int32_t n = 1;
michael@0 1315 for (set<SimplifiedZoneType>::const_iterator i=simpleset.begin(); i!=simpleset.end(); ++i) {
michael@0 1316 if (*i < initialSimplifiedType || initialSimplifiedType < *i) {
michael@0 1317 simplemap[*i] = n++;
michael@0 1318 }
michael@0 1319 }
michael@0 1320
michael@0 1321 // Remap transitions
michael@0 1322 for (vector<Transition>::iterator i=transitions.begin();
michael@0 1323 i!=transitions.end(); ++i) {
michael@0 1324 assert(i->type < (int32_t)types.size());
michael@0 1325 ZoneType oldtype = types[i->type];
michael@0 1326 SimplifiedZoneType newtype(oldtype);
michael@0 1327 assert(simplemap.find(newtype) != simplemap.end());
michael@0 1328 i->type = simplemap[newtype];
michael@0 1329 }
michael@0 1330
michael@0 1331 // Replace type list
michael@0 1332 types.clear();
michael@0 1333 types.push_back(initialSimplifiedType);
michael@0 1334 for (set<SimplifiedZoneType>::const_iterator i=simpleset.begin(); i!=simpleset.end(); ++i) {
michael@0 1335 if (*i < initialSimplifiedType || initialSimplifiedType < *i) {
michael@0 1336 types.push_back(*i);
michael@0 1337 }
michael@0 1338 }
michael@0 1339
michael@0 1340 // Reiterating transitions to remove any transitions which
michael@0 1341 // do not actually change the raw/dst offsets
michael@0 1342 int32_t prevTypeIdx = 0;
michael@0 1343 for (vector<Transition>::iterator i=transitions.begin(); i!=transitions.end();) {
michael@0 1344 if (i->type == prevTypeIdx) {
michael@0 1345 // this is not a time transition, probably just name change
michael@0 1346 // e.g. America/Resolute after 2006 in 2010b
michael@0 1347 transitions.erase(i);
michael@0 1348 } else {
michael@0 1349 prevTypeIdx = i->type;
michael@0 1350 i++;
michael@0 1351 }
michael@0 1352 }
michael@0 1353 }
michael@0 1354 }
michael@0 1355
michael@0 1356 }
michael@0 1357
michael@0 1358 /**
michael@0 1359 * Merge final zone data into this zone.
michael@0 1360 */
michael@0 1361 void ZoneInfo::mergeFinalData(const FinalZone& fz) {
michael@0 1362 int32_t year = fz.year;
michael@0 1363 int64_t seconds = yearToSeconds(year);
michael@0 1364
michael@0 1365 if (!ICU44PLUS) {
michael@0 1366 if (seconds > HIGHEST_TIME32) {
michael@0 1367 // Avoid transitions beyond signed 32bit max second.
michael@0 1368 // This may result incorrect offset computation around
michael@0 1369 // HIGHEST_TIME32. This is a limitation of ICU
michael@0 1370 // before 4.4.
michael@0 1371 seconds = HIGHEST_TIME32;
michael@0 1372 }
michael@0 1373 }
michael@0 1374
michael@0 1375 vector<Transition>::iterator it =
michael@0 1376 find_if(transitions.begin(), transitions.end(),
michael@0 1377 bind2nd(ptr_fun(isAfter), seconds));
michael@0 1378 transitions.erase(it, transitions.end());
michael@0 1379
michael@0 1380 if (finalYear != -1) {
michael@0 1381 throw invalid_argument("Final zone already merged in");
michael@0 1382 }
michael@0 1383 finalYear = fz.year;
michael@0 1384 finalOffset = fz.offset;
michael@0 1385 finalRuleID = fz.ruleid;
michael@0 1386 }
michael@0 1387
michael@0 1388 /**
michael@0 1389 * Merge the data from the given final zone into the core zone data by
michael@0 1390 * calling the ZoneInfo member function mergeFinalData.
michael@0 1391 */
michael@0 1392 void mergeOne(const string& zoneid, const FinalZone& fz) {
michael@0 1393 if (ZONEINFO.find(zoneid) == ZONEINFO.end()) {
michael@0 1394 throw invalid_argument("Unrecognized final zone ID");
michael@0 1395 }
michael@0 1396 ZONEINFO[zoneid].mergeFinalData(fz);
michael@0 1397 }
michael@0 1398
michael@0 1399 /**
michael@0 1400 * Visitor function that merges the final zone data into the main zone
michael@0 1401 * data structures. It calls mergeOne for each final zone and its
michael@0 1402 * list of aliases.
michael@0 1403 */
michael@0 1404 void mergeFinalZone(const pair<string,FinalZone>& p) {
michael@0 1405 const string& id = p.first;
michael@0 1406 const FinalZone& fz = p.second;
michael@0 1407
michael@0 1408 mergeOne(id, fz);
michael@0 1409 }
michael@0 1410
michael@0 1411 /**
michael@0 1412 * Print this rule in resource bundle format to os. ID and enclosing
michael@0 1413 * braces handled elsewhere.
michael@0 1414 */
michael@0 1415 void FinalRule::print(ostream& os) const {
michael@0 1416 // First print the rule part that enters DST; then the rule part
michael@0 1417 // that exits it.
michael@0 1418 int32_t whichpart = (part[0].offset != 0) ? 0 : 1;
michael@0 1419 assert(part[whichpart].offset != 0);
michael@0 1420 assert(part[1-whichpart].offset == 0);
michael@0 1421
michael@0 1422 os << " ";
michael@0 1423 for (int32_t i=0; i<2; ++i) {
michael@0 1424 const FinalRulePart& p = part[whichpart];
michael@0 1425 whichpart = 1-whichpart;
michael@0 1426 os << p.month << ", " << p.stz_dowim() << ", " << p.stz_dow() << ", "
michael@0 1427 << p.time << ", " << p.timemode() << ", ";
michael@0 1428 }
michael@0 1429 os << part[whichpart].offset << endl;
michael@0 1430 }
michael@0 1431
michael@0 1432 int main(int argc, char *argv[]) {
michael@0 1433 string rootpath, zonetab, version;
michael@0 1434 bool validArgs = FALSE;
michael@0 1435
michael@0 1436 if (argc == 4 || argc == 5) {
michael@0 1437 validArgs = TRUE;
michael@0 1438 rootpath = argv[1];
michael@0 1439 zonetab = argv[2];
michael@0 1440 version = argv[3];
michael@0 1441 if (argc == 5) {
michael@0 1442 if (strcmp(argv[4], "--old") == 0) {
michael@0 1443 ICU44PLUS = FALSE;
michael@0 1444 TZ_RESOURCE_NAME = ICU_TZ_RESOURCE_OLD;
michael@0 1445 } else {
michael@0 1446 validArgs = FALSE;
michael@0 1447 }
michael@0 1448 }
michael@0 1449 }
michael@0 1450 if (!validArgs) {
michael@0 1451 cout << "Usage: tz2icu <dir> <cmap> <tzver> [--old]" << endl
michael@0 1452 << " <dir> path to zoneinfo file tree generated by" << endl
michael@0 1453 << " ICU-patched version of zic" << endl
michael@0 1454 << " <cmap> country map, from tzdata archive," << endl
michael@0 1455 << " typically named \"zone.tab\"" << endl
michael@0 1456 << " <tzver> version string, such as \"2003e\"" << endl
michael@0 1457 << " --old generating resource format before ICU4.4" << endl;
michael@0 1458 exit(1);
michael@0 1459 }
michael@0 1460
michael@0 1461 cout << "Olson data version: " << version << endl;
michael@0 1462 cout << "ICU 4.4+ format: " << (ICU44PLUS ? "Yes" : "No") << endl;
michael@0 1463
michael@0 1464 try {
michael@0 1465 ifstream finals(ICU_ZONE_FILE);
michael@0 1466 if (finals) {
michael@0 1467 readFinalZonesAndRules(finals);
michael@0 1468
michael@0 1469 cout << "Finished reading " << finalZones.size()
michael@0 1470 << " final zones and " << finalRules.size()
michael@0 1471 << " final rules from " ICU_ZONE_FILE << endl;
michael@0 1472 } else {
michael@0 1473 cerr << "Error: Unable to open " ICU_ZONE_FILE << endl;
michael@0 1474 return 1;
michael@0 1475 }
michael@0 1476 } catch (const exception& error) {
michael@0 1477 cerr << "Error: While reading " ICU_ZONE_FILE ": " << error.what() << endl;
michael@0 1478 return 1;
michael@0 1479 }
michael@0 1480
michael@0 1481 try {
michael@0 1482 // Recursively scan all files below the given path, accumulating
michael@0 1483 // their data into ZONEINFO. All files must be TZif files. Any
michael@0 1484 // failure along the way will result in a call to exit(1).
michael@0 1485 scandir(rootpath);
michael@0 1486 } catch (const exception& error) {
michael@0 1487 cerr << "Error: While scanning " << rootpath << ": " << error.what() << endl;
michael@0 1488 return 1;
michael@0 1489 }
michael@0 1490
michael@0 1491 cout << "Finished reading " << ZONEINFO.size() << " zoneinfo files ["
michael@0 1492 << (ZONEINFO.begin())->first << ".."
michael@0 1493 << (--ZONEINFO.end())->first << "]" << endl;
michael@0 1494
michael@0 1495 try {
michael@0 1496 for_each(finalZones.begin(), finalZones.end(), mergeFinalZone);
michael@0 1497 } catch (const exception& error) {
michael@0 1498 cerr << "Error: While merging final zone data: " << error.what() << endl;
michael@0 1499 return 1;
michael@0 1500 }
michael@0 1501
michael@0 1502 // Process links (including ICU aliases). For each link set we have
michael@0 1503 // a canonical ID (e.g., America/Los_Angeles) and a set of one or more
michael@0 1504 // aliases (e.g., PST, PST8PDT, ...).
michael@0 1505
michael@0 1506 // 1. Add all aliases as zone objects in ZONEINFO
michael@0 1507 for (map<string,set<string> >::const_iterator i = links.begin();
michael@0 1508 i!=links.end(); ++i) {
michael@0 1509 const string& olson = i->first;
michael@0 1510 const set<string>& aliases = i->second;
michael@0 1511 if (ZONEINFO.find(olson) == ZONEINFO.end()) {
michael@0 1512 cerr << "Error: Invalid " << linkSource[olson] << " to non-existent \""
michael@0 1513 << olson << "\"" << endl;
michael@0 1514 return 1;
michael@0 1515 }
michael@0 1516 for (set<string>::const_iterator j=aliases.begin();
michael@0 1517 j!=aliases.end(); ++j) {
michael@0 1518 ZONEINFO[*j] = ZoneInfo();
michael@0 1519 }
michael@0 1520 }
michael@0 1521
michael@0 1522 // 2. Create a mapping from zones to index numbers 0..n-1.
michael@0 1523 map<string,int32_t> zoneIDs;
michael@0 1524 vector<string> zoneIDlist;
michael@0 1525 int32_t z=0;
michael@0 1526 for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) {
michael@0 1527 zoneIDs[i->first] = z++;
michael@0 1528 zoneIDlist.push_back(i->first);
michael@0 1529 }
michael@0 1530 assert(z == (int32_t) ZONEINFO.size());
michael@0 1531
michael@0 1532 // 3. Merge aliases. Sometimes aliases link to other aliases; we
michael@0 1533 // resolve these into simplest possible sets.
michael@0 1534 map<string,set<string> > links2;
michael@0 1535 map<string,string> reverse2;
michael@0 1536 for (map<string,set<string> >::const_iterator i = links.begin();
michael@0 1537 i!=links.end(); ++i) {
michael@0 1538 string olson = i->first;
michael@0 1539 while (reverseLinks.find(olson) != reverseLinks.end()) {
michael@0 1540 olson = reverseLinks[olson];
michael@0 1541 }
michael@0 1542 for (set<string>::const_iterator j=i->second.begin(); j!=i->second.end(); ++j) {
michael@0 1543 links2[olson].insert(*j);
michael@0 1544 reverse2[*j] = olson;
michael@0 1545 }
michael@0 1546 }
michael@0 1547 links = links2;
michael@0 1548 reverseLinks = reverse2;
michael@0 1549
michael@0 1550 if (false) { // Debugging: Emit link map
michael@0 1551 for (map<string,set<string> >::const_iterator i = links.begin();
michael@0 1552 i!=links.end(); ++i) {
michael@0 1553 cout << i->first << ": ";
michael@0 1554 for (set<string>::const_iterator j=i->second.begin(); j!=i->second.end(); ++j) {
michael@0 1555 cout << *j << ", ";
michael@0 1556 }
michael@0 1557 cout << endl;
michael@0 1558 }
michael@0 1559 }
michael@0 1560
michael@0 1561 // 4. Update aliases
michael@0 1562 for (map<string,set<string> >::const_iterator i = links.begin();
michael@0 1563 i!=links.end(); ++i) {
michael@0 1564 const string& olson = i->first;
michael@0 1565 const set<string>& aliases = i->second;
michael@0 1566 ZONEINFO[olson].clearAliases();
michael@0 1567 ZONEINFO[olson].addAlias(zoneIDs[olson]);
michael@0 1568 for (set<string>::const_iterator j=aliases.begin();
michael@0 1569 j!=aliases.end(); ++j) {
michael@0 1570 assert(zoneIDs.find(olson) != zoneIDs.end());
michael@0 1571 assert(zoneIDs.find(*j) != zoneIDs.end());
michael@0 1572 assert(ZONEINFO.find(*j) != ZONEINFO.end());
michael@0 1573 ZONEINFO[*j].setAliasTo(zoneIDs[olson]);
michael@0 1574 ZONEINFO[olson].addAlias(zoneIDs[*j]);
michael@0 1575 }
michael@0 1576 }
michael@0 1577
michael@0 1578 // Once merging of final data is complete, we can optimize the type list
michael@0 1579 for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) {
michael@0 1580 i->second.optimizeTypeList();
michael@0 1581 }
michael@0 1582
michael@0 1583 // Create the country map
michael@0 1584 map<string, set<string> > countryMap; // country -> set of zones
michael@0 1585 map<string, string> reverseCountryMap; // zone -> country
michael@0 1586 try {
michael@0 1587 ifstream f(zonetab.c_str());
michael@0 1588 if (!f) {
michael@0 1589 cerr << "Error: Unable to open " << zonetab << endl;
michael@0 1590 return 1;
michael@0 1591 }
michael@0 1592 int32_t n = 0;
michael@0 1593 string line;
michael@0 1594 while (getline(f, line)) {
michael@0 1595 string::size_type lb = line.find('#');
michael@0 1596 if (lb != string::npos) {
michael@0 1597 line.resize(lb); // trim comments
michael@0 1598 }
michael@0 1599 string country, coord, zone;
michael@0 1600 istringstream is(line);
michael@0 1601 is >> country >> coord >> zone;
michael@0 1602 if (country.size() == 0) continue;
michael@0 1603 if (country.size() != 2 || zone.size() < 1) {
michael@0 1604 cerr << "Error: Can't parse " << line << " in " << zonetab << endl;
michael@0 1605 return 1;
michael@0 1606 }
michael@0 1607 if (ZONEINFO.find(zone) == ZONEINFO.end()) {
michael@0 1608 cerr << "Error: Country maps to invalid zone " << zone
michael@0 1609 << " in " << zonetab << endl;
michael@0 1610 return 1;
michael@0 1611 }
michael@0 1612 countryMap[country].insert(zone);
michael@0 1613 reverseCountryMap[zone] = country;
michael@0 1614 //cerr << (n+1) << ": " << country << " <=> " << zone << endl;
michael@0 1615 ++n;
michael@0 1616 }
michael@0 1617 cout << "Finished reading " << n
michael@0 1618 << " country entries from " << zonetab << endl;
michael@0 1619 } catch (const exception& error) {
michael@0 1620 cerr << "Error: While reading " << zonetab << ": " << error.what() << endl;
michael@0 1621 return 1;
michael@0 1622 }
michael@0 1623
michael@0 1624 // Merge ICU aliases into country map. Don't merge any alias
michael@0 1625 // that already has a country map, since that doesn't make sense.
michael@0 1626 // E.g. "Link Europe/Oslo Arctic/Longyearbyen" doesn't mean we
michael@0 1627 // should cross-map the countries between these two zones.
michael@0 1628 for (map<string,set<string> >::const_iterator i = links.begin();
michael@0 1629 i!=links.end(); ++i) {
michael@0 1630 const string& olson(i->first);
michael@0 1631 if (reverseCountryMap.find(olson) == reverseCountryMap.end()) {
michael@0 1632 continue;
michael@0 1633 }
michael@0 1634 string c = reverseCountryMap[olson];
michael@0 1635 const set<string>& aliases(i->second);
michael@0 1636 for (set<string>::const_iterator j=aliases.begin();
michael@0 1637 j != aliases.end(); ++j) {
michael@0 1638 if (reverseCountryMap.find(*j) == reverseCountryMap.end()) {
michael@0 1639 countryMap[c].insert(*j);
michael@0 1640 reverseCountryMap[*j] = c;
michael@0 1641 //cerr << "Aliased country: " << c << " <=> " << *j << endl;
michael@0 1642 }
michael@0 1643 }
michael@0 1644 }
michael@0 1645
michael@0 1646 // Create a pseudo-country containing all zones belonging to no country
michael@0 1647 set<string> nocountry;
michael@0 1648 for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) {
michael@0 1649 if (reverseCountryMap.find(i->first) == reverseCountryMap.end()) {
michael@0 1650 nocountry.insert(i->first);
michael@0 1651 }
michael@0 1652 }
michael@0 1653 countryMap[""] = nocountry;
michael@0 1654
michael@0 1655 // Get local time & year for below
michael@0 1656 time_t sec;
michael@0 1657 time(&sec);
michael@0 1658 struct tm* now = localtime(&sec);
michael@0 1659 int32_t thisYear = now->tm_year + 1900;
michael@0 1660
michael@0 1661 string filename = TZ_RESOURCE_NAME + ".txt";
michael@0 1662 // Write out a resource-bundle source file containing data for
michael@0 1663 // all zones.
michael@0 1664 ofstream file(filename.c_str());
michael@0 1665 if (file) {
michael@0 1666 file << "//---------------------------------------------------------" << endl
michael@0 1667 << "// Copyright (C) 2003";
michael@0 1668 if (thisYear > 2003) {
michael@0 1669 file << "-" << thisYear;
michael@0 1670 }
michael@0 1671 file << ", International Business Machines" << endl
michael@0 1672 << "// Corporation and others. All Rights Reserved." << endl
michael@0 1673 << "//---------------------------------------------------------" << endl
michael@0 1674 << "// Build tool: tz2icu" << endl
michael@0 1675 << "// Build date: " << asctime(now) /* << endl -- asctime emits CR */
michael@0 1676 << "// Olson source: ftp://elsie.nci.nih.gov/pub/" << endl
michael@0 1677 << "// Olson version: " << version << endl
michael@0 1678 << "// ICU version: " << U_ICU_VERSION << endl
michael@0 1679 << "//---------------------------------------------------------" << endl
michael@0 1680 << "// >> !!! >> THIS IS A MACHINE-GENERATED FILE << !!! <<" << endl
michael@0 1681 << "// >> !!! >>> DO NOT EDIT <<< !!! <<" << endl
michael@0 1682 << "//---------------------------------------------------------" << endl
michael@0 1683 << endl
michael@0 1684 << TZ_RESOURCE_NAME << ":table(nofallback) {" << endl
michael@0 1685 << " TZVersion { \"" << version << "\" }" << endl
michael@0 1686 << " Zones:array { " << endl
michael@0 1687 << ZONEINFO // Zones (the actual data)
michael@0 1688 << " }" << endl;
michael@0 1689
michael@0 1690 // Names correspond to the Zones list, used for binary searching.
michael@0 1691 printStringList ( file, ZONEINFO ); // print the Names list
michael@0 1692
michael@0 1693 // Final Rules are used if requested by the zone
michael@0 1694 file << " Rules { " << endl;
michael@0 1695 // Emit final rules
michael@0 1696 int32_t frc = 0;
michael@0 1697 for(map<string,FinalRule>::iterator i=finalRules.begin();
michael@0 1698 i!=finalRules.end(); ++i) {
michael@0 1699 const string& id = i->first;
michael@0 1700 const FinalRule& r = i->second;
michael@0 1701 file << " " << id << ":intvector {" << endl;
michael@0 1702 r.print(file);
michael@0 1703 file << " } //_#" << frc++ << endl;
michael@0 1704 }
michael@0 1705 file << " }" << endl;
michael@0 1706
michael@0 1707 // Emit country (region) map.
michael@0 1708 if (ICU44PLUS) {
michael@0 1709 file << " Regions:array {" << endl;
michael@0 1710 int32_t zn = 0;
michael@0 1711 for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) {
michael@0 1712 map<string, string>::iterator cit = reverseCountryMap.find(i->first);
michael@0 1713 if (cit == reverseCountryMap.end()) {
michael@0 1714 file << " \"001\",";
michael@0 1715 } else {
michael@0 1716 file << " \"" << cit->second << "\", ";
michael@0 1717 }
michael@0 1718 file << "//Z#" << zn++ << " " << i->first << endl;
michael@0 1719 }
michael@0 1720 file << " }" << endl;
michael@0 1721 } else {
michael@0 1722 file << " Regions { " << endl;
michael@0 1723 int32_t rc = 0;
michael@0 1724 for (map<string, set<string> >::const_iterator i=countryMap.begin();
michael@0 1725 i != countryMap.end(); ++i) {
michael@0 1726 string country = i->first;
michael@0 1727 const set<string>& zones(i->second);
michael@0 1728 file << " ";
michael@0 1729 if(country[0]==0) {
michael@0 1730 file << "Default";
michael@0 1731 }
michael@0 1732 file << country << ":intvector { ";
michael@0 1733 bool first = true;
michael@0 1734 for (set<string>::const_iterator j=zones.begin();
michael@0 1735 j != zones.end(); ++j) {
michael@0 1736 if (!first) file << ", ";
michael@0 1737 first = false;
michael@0 1738 if (zoneIDs.find(*j) == zoneIDs.end()) {
michael@0 1739 cerr << "Error: Nonexistent zone in country map: " << *j << endl;
michael@0 1740 return 1;
michael@0 1741 }
michael@0 1742 file << zoneIDs[*j]; // emit the zone's index number
michael@0 1743 }
michael@0 1744 file << " } //R#" << rc++ << endl;
michael@0 1745 }
michael@0 1746 file << " }" << endl;
michael@0 1747 }
michael@0 1748
michael@0 1749 file << "}" << endl;
michael@0 1750 }
michael@0 1751
michael@0 1752 file.close();
michael@0 1753
michael@0 1754 if (file) { // recheck error bit
michael@0 1755 cout << "Finished writing " << TZ_RESOURCE_NAME << ".txt" << endl;
michael@0 1756 } else {
michael@0 1757 cerr << "Error: Unable to open/write to " << TZ_RESOURCE_NAME << ".txt" << endl;
michael@0 1758 return 1;
michael@0 1759 }
michael@0 1760 }
michael@0 1761 //eof

mercurial