/* ********************************************************************** * Copyright (c) 2003, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu * Created: July 10 2003 * Since: ICU 2.8 ********************************************************************** */ #include "tzfile.h" // from Olson tzcode archive, copied to this dir #ifdef WIN32 #include #undef min // windows.h/STL conflict #undef max // windows.h/STL conflict // "identifier was truncated to 'number' characters" warning #pragma warning(disable: 4786) #else #include #include #include #include #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "tz2icu.h" // Excerpt from tzfile.h: //|#define TZ_MAGIC "TZif" //| //|struct tzhead { //| char tzh_magic[4]; /* TZ_MAGIC */ //| char tzh_reserved[16]; /* reserved for future use */ //| char tzh_ttisgmtcnt[4]; /* coded number of trans. time flags */ //| char tzh_ttisstdcnt[4]; /* coded number of trans. time flags */ //| char tzh_leapcnt[4]; /* coded number of leap seconds */ //| char tzh_timecnt[4]; /* coded number of transition times */ //| char tzh_typecnt[4]; /* coded number of local time types */ //| char tzh_charcnt[4]; /* coded number of abbr. chars */ //|}; //| //|/* //|** . . .followed by. . . //|** //|** tzh_timecnt (char [4])s coded transition times a la time(2) //|** tzh_timecnt (unsigned char)s types of local time starting at above //|** tzh_typecnt repetitions of //|** one (char [4]) coded UTC offset in seconds //|** one (unsigned char) used to set tm_isdst //|** one (unsigned char) that's an abbreviation list index //|** tzh_charcnt (char)s '\0'-terminated zone abbreviations //|** tzh_leapcnt repetitions of //|** one (char [4]) coded leap second transition times //|** one (char [4]) total correction after above //|** tzh_ttisstdcnt (char)s indexed by type; if TRUE, transition //|** time is standard time, if FALSE, //|** transition time is wall clock time //|** if absent, transition times are //|** assumed to be wall clock time //|** tzh_ttisgmtcnt (char)s indexed by type; if TRUE, transition //|** time is UTC, if FALSE, //|** transition time is local time //|** if absent, transition times are //|** assumed to be local time //|*/ // But we know from zic.c that ttisgmtcnt == ttisstdcnt == typecnt using namespace std; //-------------------------------------------------------------------- // Time utilities //-------------------------------------------------------------------- const long SECS_PER_YEAR = 31536000; // 365 days const long SECS_PER_LEAP_YEAR = 31622400; // 366 days bool isLeap(int y) { return (y%4 == 0) && ((y%100 != 0) || (y%400 == 0)); // Gregorian } long secsPerYear(int y) { return isLeap(y) ? SECS_PER_LEAP_YEAR : SECS_PER_YEAR; } /** * Given a calendar year, return the GMT epoch seconds for midnight * GMT of January 1 of that year. yearToSeconds(1970) == 0. */ long yearToSeconds(int year) { // inefficient but foolproof long s = 0; int y = 1970; while (y < year) { s += secsPerYear(y++); } while (y > year) { s -= secsPerYear(--y); } return s; } /** * Given 1970 GMT epoch seconds, return the calendar year containing * that time. secondsToYear(0) == 1970. */ int secondsToYear(long seconds) { // inefficient but foolproof int y = 1970; long s = 0; if (seconds >= 0) { for (;;) { s += secsPerYear(y++); if (s > seconds) break; } --y; } else { for (;;) { s -= secsPerYear(--y); if (s <= seconds) break; } } return y; } //-------------------------------------------------------------------- // Types //-------------------------------------------------------------------- struct FinalZone; struct FinalRule; struct SimplifiedZoneType; // A transition from one ZoneType to another // Minimal size = 5 bytes (4+1) struct Transition { long time; // seconds, 1970 epoch int type; // index into 'ZoneInfo.types' 0..255 Transition(long _time, int _type) { time = _time; type = _type; } }; // A behavior mode (what zic calls a 'type') of a time zone. // Minimal size = 6 bytes (4+1+3bits) // SEE: SimplifiedZoneType struct ZoneType { long rawoffset; // raw seconds offset from GMT long dstoffset; // dst seconds offset from GMT // We don't really need any of the following, but they are // retained for possible future use. See SimplifiedZoneType. int abbr; // index into ZoneInfo.abbrs 0..n-1 bool isdst; bool isstd; bool isgmt; ZoneType(const SimplifiedZoneType&); // used by optimizeTypeList ZoneType() : rawoffset(-1), dstoffset(-1), abbr(-1) {} // A restricted equality, of just the raw and dst offset bool matches(const ZoneType& other) { return rawoffset == other.rawoffset && dstoffset == other.dstoffset; } }; // A collection of transitions from one ZoneType to another, together // with a list of the ZoneTypes. A ZoneInfo object may have a long // list of transitions between a smaller list of ZoneTypes. // // This object represents the contents of a single zic-created // zoneinfo file. struct ZoneInfo { vector transitions; vector types; vector abbrs; string finalRuleID; int finalOffset; int finalYear; // -1 if none // If this is an alias, then all other fields are meaningless, and // this field will point to the "real" zone 0..n-1. int aliasTo; // -1 if this is a "real" zone // If there are aliases TO this zone, then the following set will // contain their index numbers (each index >= 0). set aliases; ZoneInfo() : finalYear(-1), aliasTo(-1) {} void mergeFinalData(const FinalZone& fz); void optimizeTypeList(); // Set this zone to be an alias TO another zone. void setAliasTo(int index); // Clear the list of aliases OF this zone. void clearAliases(); // Add an alias to the list of aliases OF this zone. void addAlias(int index); void print(ostream& os, const string& id) const; }; void ZoneInfo::clearAliases() { assert(aliasTo < 0); aliases.clear(); } void ZoneInfo::addAlias(int index) { assert(aliasTo < 0 && index >= 0 && aliases.find(index) == aliases.end()); aliases.insert(index); } void ZoneInfo::setAliasTo(int index) { assert(index >= 0); assert(aliases.size() == 0); aliasTo = index; } typedef map ZoneMap; typedef ZoneMap::const_iterator ZoneMapIter; //-------------------------------------------------------------------- // ZONEINFO //-------------------------------------------------------------------- // Global map holding all our ZoneInfo objects, indexed by id. ZoneMap ZONEINFO; //-------------------------------------------------------------------- // zoneinfo file parsing //-------------------------------------------------------------------- // Read zic-coded 32-bit integer from file long readcoded(ifstream& file, long minv=numeric_limits::min(), long maxv=numeric_limits::max()) { unsigned char buf[4]; // must be UNSIGNED long val=0; file.read((char*)buf, 4); for(int i=0,shift=24;i<4;++i,shift-=8) { val |= buf[i] << shift; } if (val < minv || val > maxv) { ostringstream os; os << "coded value out-of-range: " << val << ", expected [" << minv << ", " << maxv << "]"; throw out_of_range(os.str()); } return val; } // Read a boolean value bool readbool(ifstream& file) { char c; file.read(&c, 1); if (c!=0 && c!=1) { ostringstream os; os << "boolean value out-of-range: " << (int)c; throw out_of_range(os.str()); } return (c!=0); } /** * Read the zoneinfo file structure (see tzfile.h) into a ZoneInfo * @param file an already-open file stream */ void readzoneinfo(ifstream& file, ZoneInfo& info) { int i; // Check for TZ_ICU_MAGIC signature at file start. If we get a // signature mismatch, it means we're trying to read a file which // isn't a ICU-modified-zic-created zoneinfo file. Typically this // means the user is passing in a "normal" zoneinfo directory, or // a zoneinfo directory that is polluted with other files, or that // the user passed in the wrong directory. char buf[32]; file.read(buf, 4); if (strncmp(buf, TZ_ICU_MAGIC, 4) != 0) { throw invalid_argument("TZ_ICU_MAGIC signature missing"); } // Read reserved bytes. The first of these will be a version byte. file.read(buf, 16); if (*(ICUZoneinfoVersion*)&buf != TZ_ICU_VERSION) { throw invalid_argument("File version mismatch"); } // Read array sizes long isgmtcnt = readcoded(file, 0); long isdstcnt = readcoded(file, 0); long leapcnt = readcoded(file, 0); long timecnt = readcoded(file, 0); long typecnt = readcoded(file, 0); long charcnt = readcoded(file, 0); // Confirm sizes that we assume to be equal. These assumptions // are drawn from a reading of the zic source (2003a), so they // should hold unless the zic source changes. if (isgmtcnt != typecnt || isdstcnt != typecnt) { throw invalid_argument("count mismatch between tzh_ttisgmtcnt, tzh_ttisdstcnt, tth_typecnt"); } // Used temporarily to store transition times and types. We need // to do this because the times and types are stored in two // separate arrays. vector transitionTimes(timecnt, -1); // temporary vector transitionTypes(timecnt, -1); // temporary // Read transition times for (i=0; i= typecnt) { ostringstream os; os << "illegal type: " << t << ", expected [0, " << (typecnt-1) << "]"; throw out_of_range(os.str()); } transitionTypes[i] = t; } // Build transitions vector out of corresponding times and types. for (i=0; i abbroffset; char *limit=str+charcnt; for (char* p=str; p 0,1,2,3. // Keep track of which abbreviations get used. vector abbrseen(abbroffset.size(), false); for (vector::iterator it=info.types.begin(); it!=info.types.end(); ++it) { vector::const_iterator x= find(abbroffset.begin(), abbroffset.end(), it->abbr); if (x==abbroffset.end()) { // TODO: Modify code to add a new string to the end of // the abbr list when a middle offset is given, e.g., // "abc*def*" where * == '\0', take offset of 1 and // make the array "abc", "def", "bc", and translate 1 // => 2. NOT CRITICAL since we don't even use the // abbr at this time. #if 0 // TODO: Re-enable this warning if we start using // the Olson abbr data, or if the above TODO is completed. ostringstream os; os << "Warning: unusual abbr offset " << it->abbr << ", expected one of"; for (vector::const_iterator y=abbroffset.begin(); y!=abbroffset.end(); ++y) { os << ' ' << *y; } cerr << os.str() << "; using 0" << endl; #endif it->abbr = 0; } else { int index = x - abbroffset.begin(); it->abbr = index; abbrseen[index] = true; } } for (int ii=0;ii<(int) abbrseen.size();++ii) { if (!abbrseen[ii]) { cerr << "Warning: unused abbreviation: " << ii << endl; } } } // Read leap second info, if any. // *** We discard leap second data. *** for (i=0; i subdirs; vector subfiles; if ((dp = opendir(dir.c_str())) == NULL) { cerr << "Error: Invalid directory: " << dir << endl; exit(1); } if (!getcwd(pwd, sizeof(pwd))) { cerr << "Error: Directory name too long" << endl; exit(1); } chdir(dir.c_str()); while ((dir_entry = readdir(dp)) != NULL) { string name = dir_entry->d_name; string path = dir + "/" + name; lstat(dir_entry->d_name,&stat_info); if (S_ISDIR(stat_info.st_mode)) { if (name != "." && name != "..") { subdirs.push_back(path); subdirs.push_back(prefix + name + "/"); // scandir(path, prefix + name + "/"); } } else { try { string id = prefix + name; subfiles.push_back(path); subfiles.push_back(id); // handleFile(path, id); } catch (const exception& e) { cerr << "Error: While processing \"" << path << "\", " << e.what() << endl; exit(1); } } } closedir(dp); chdir(pwd); for(int i=0;i<(int)subfiles.size();i+=2) { try { handleFile(subfiles[i], subfiles[i+1]); } catch (const exception& e) { cerr << "Error: While processing \"" << subfiles[i] << "\", " << e.what() << endl; exit(1); } } for(int i=0;i<(int)subdirs.size();i+=2) { scandir(subdirs[i], subdirs[i+1]); } } #endif //-------------------------------------------------------------------- // Final zone and rule info //-------------------------------------------------------------------- /** * Read and discard the current line. */ void consumeLine(istream& in) { int c; do { c = in.get(); } while (c != EOF && c != '\n'); } enum { DOM = 0, DOWGEQ = 1, DOWLEQ = 2 }; const char* TIME_MODE[] = {"w", "s", "u"}; const int MONTH_LEN[] = {31,28,31,30,31,30,31,31,30,31,30,31}; const int HOUR = 3600; struct FinalZone { int offset; // raw offset int year; // takes effect for y >= year string ruleid; set aliases; FinalZone(int _offset, int _year, const string& _ruleid) : offset(_offset), year(_year), ruleid(_ruleid) { if (offset <= -16*HOUR || offset >= 16*HOUR || year < 1900 || year >= 2050) { throw invalid_argument("Invalid input arguments"); } } FinalZone() : offset(-1), year(-1) {} void addLink(const string& alias) { if (aliases.find(alias) != aliases.end()) { throw invalid_argument("Duplicate alias"); } aliases.insert(alias); } }; struct FinalRulePart { int mode; int month; int dom; int dow; int time; int offset; // dst offset, usually either 0 or 1:00 // Isstd and isgmt only have 3 valid states, corresponding to local // wall time, local standard time, and GMT standard time. // Here is how the isstd & isgmt flags are set by zic: //| case 's': /* Standard */ //| rp->r_todisstd = TRUE; //| rp->r_todisgmt = FALSE; //| case 'w': /* Wall */ //| rp->r_todisstd = FALSE; //| rp->r_todisgmt = FALSE; //| case 'g': /* Greenwich */ //| case 'u': /* Universal */ //| case 'z': /* Zulu */ //| rp->r_todisstd = TRUE; //| rp->r_todisgmt = TRUE; bool isstd; bool isgmt; bool isset; // used during building; later ignored FinalRulePart() : isset(false) {} void set(const string& _mode, int _month, int _dom, int _dow, int _time, bool _isstd, bool _isgmt, int _offset) { if (isset) { throw invalid_argument("FinalRulePart set twice"); } isset = true; if (_mode == "DOWLEQ") { mode = DOWLEQ; } else if (_mode == "DOWGEQ") { mode = DOWGEQ; } else if (_mode == "DOM") { mode = DOM; } else { throw invalid_argument("Unrecognized FinalRulePart mode"); } month = _month; dom = _dom; dow = _dow; time = _time; isstd = _isstd; isgmt = _isgmt; offset = _offset; if (month < 0 || month >= 12 || dom < 1 || dom > MONTH_LEN[month] || (mode != DOM && (dow < 0 || dow >= 7)) || offset < 0 || offset > HOUR || (isgmt && !isstd)) { throw invalid_argument("Invalid input arguments"); } } /** * Return the time mode as an ICU SimpleTimeZone int from 0..2; * see simpletz.h. */ int timemode() const { if (isgmt) { assert(isstd); return 2; // gmt standard } if (isstd) { return 1; // local standard } return 0; // local wall } // The SimpleTimeZone encoding method for rules is as follows: // stz_dowim stz_dow // DOM: dom 0 // DOWGEQ: dom -(dow+1) // DOWLEQ: -dom -(dow+1) // E.g., to encode Mon>=7, use stz_dowim=7, stz_dow=-2 // to encode Mon<=7, use stz_dowim=-7, stz_dow=-2 // to encode 7, use stz_dowim=7, stz_dow=0 // Note that for this program and for SimpleTimeZone, 0==Jan, // but for this program 0==Sun while for SimpleTimeZone 1==Sun. /** * Return a "dowim" param suitable for SimpleTimeZone. */ int stz_dowim() const { return (mode == DOWLEQ) ? -dom : dom; } /** * Return a "dow" param suitable for SimpleTimeZone. */ int stz_dow() const { return (mode == DOM) ? 0 : -(dow+1); } }; struct FinalRule { FinalRulePart part[2]; bool isset() const { return part[0].isset && part[1].isset; } void print(ostream& os) const; }; map finalZones; map finalRules; map > links; map reverseLinks; map linkSource; // id => "Olson link" or "ICU alias" /** * Predicate used to find FinalRule objects that do not have both * sub-parts set (indicating an error in the input file). */ bool isNotSet(const pair& p) { return !p.second.isset(); } /** * Predicate used to find FinalZone objects that do not map to a known * rule (indicating an error in the input file). */ bool mapsToUnknownRule(const pair& p) { return finalRules.find(p.second.ruleid) == finalRules.end(); } /** * This set is used to make sure each rule in finalRules is used at * least once. First we populate it with all the rules from * finalRules; then we remove all the rules referred to in * finaleZones. */ set ruleIDset; void insertRuleID(const pair& p) { ruleIDset.insert(p.first); } void eraseRuleID(const pair& p) { ruleIDset.erase(p.second.ruleid); } /** * Populate finalZones and finalRules from the given istream. */ void readFinalZonesAndRules(istream& in) { for (;;) { string token; in >> token; if (in.eof() || !in) { break; } else if (token == "zone") { // zone Africa/Cairo 7200 1995 Egypt # zone Africa/Cairo, offset 7200, year >= 1995, rule Egypt (0) string id, ruleid; int offset, year; in >> id >> offset >> year >> ruleid; consumeLine(in); finalZones[id] = FinalZone(offset, year, ruleid); } else if (token == "rule") { // 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 // 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 string id, mode; int month, dom, dow, time, offset; bool isstd, isgmt; in >> id >> mode >> month >> dom >> dow >> time >> isstd >> isgmt >> offset; consumeLine(in); FinalRule& fr = finalRules[id]; int p = fr.part[0].isset ? 1 : 0; fr.part[p].set(mode, month, dom, dow, time, isstd, isgmt, offset); } else if (token == "link") { string fromid, toid; // fromid == "real" zone, toid == alias in >> fromid >> toid; // DO NOT consumeLine(in); if (finalZones.find(toid) != finalZones.end()) { throw invalid_argument("Bad link: `to' id is a \"real\" zone"); } // TODO remove // // Not all links refer to final zones; need to check // if (finalZones.find(fromid) != finalZones.end()) { // finalZones[fromid].addLink(toid); // //cout << fromid << ": alias is " << toid << endl; // } links[fromid].insert(toid); reverseLinks[toid] = fromid; linkSource[fromid] = "Olson link"; linkSource[toid] = "Olson link"; } else if (token.length() > 0 && token[0] == '#') { consumeLine(in); } else { throw invalid_argument("Unrecognized keyword"); } } if (!in.eof() && !in) { throw invalid_argument("Parse failure"); } // Perform validity check: Each rule should have data for 2 parts. if (count_if(finalRules.begin(), finalRules.end(), isNotSet) != 0) { throw invalid_argument("One or more incomplete rule pairs"); } // Perform validity check: Each zone should map to a known rule. if (count_if(finalZones.begin(), finalZones.end(), mapsToUnknownRule) != 0) { throw invalid_argument("One or more zones refers to an unknown rule"); } // Perform validity check: Each rule should be referred to by a zone. ruleIDset.clear(); for_each(finalRules.begin(), finalRules.end(), insertRuleID); for_each(finalZones.begin(), finalZones.end(), eraseRuleID); if (ruleIDset.size() != 0) { throw invalid_argument("Unused rules"); } } //-------------------------------------------------------------------- // Resource bundle output //-------------------------------------------------------------------- /* Assuming we don't need the leap seconds info or the abbreviations, we have: N is ZoneInfo.transition.size(); may be zero. "tzfile.h" states that N <= 370 M is ZoneInfo.types.size(); 1..255 For each transition: - 32 bit time - 8 bit type index For each type: - 32 bit offset - 3 bit flags Total storage in bytes is 5 N + ~4.5 M per zone. Compressed format #1 (153 KB): - binary: 2 bytes N 1 byte M N bytes type index for each transition ~M/2 bytes flags for each type (ceil(M/2.0)) - intvector: N ints times for each transition M ints offsets for each type vvv *** Here's the one we use *** vvv Compressed format #2 (155 KB): - intvector: N ints times for each transition - intvector: M ints offsets for each type - binary: N bytes type index for each transition ~M/2 bytes flags for each type (ceil(M/2.0)) Compressed format #3 (165 KB): - binary: N bytes type index for each transition - binary: ~M/2 bytes flags for each type (ceil(M/2.0)) - intvector: N ints times for each transition - intvector: M ints offsets for each type // TODO update format docs */ void ZoneInfo::print(ostream& os, const string& id) const { // Implement compressed format #2: os << " " << id; if (aliasTo >= 0) { assert(aliases.size() == 0); os << ":int { " << aliasTo << " }" << endl; return; } os << ":array {" << endl; vector::const_iterator trn; vector::const_iterator typ; bool first=true; os << " :intvector { "; for (trn = transitions.begin(); trn != transitions.end(); ++trn) { if (!first) os << ", "; first = false; os << trn->time; } os << " }" << endl; first=true; os << " :intvector { "; for (typ = types.begin(); typ != types.end(); ++typ) { if (!first) os << ", "; first = false; os << typ->rawoffset << ", " << typ->dstoffset; } os << " }" << endl; os << " :bin { \"" << hex << setfill('0'); for (trn = transitions.begin(); trn != transitions.end(); ++trn) { os << setw(2) << trn->type; } os << dec << "\" }" << endl; // Final zone info, if any if (finalYear != -1) { os << " \"" << finalRuleID << "\"" << endl; os << " :intvector { " << finalOffset << ", " << finalYear << " }" << endl; } // Alias list, if any if (aliases.size() != 0) { first = true; os << " :intvector { "; for (set::const_iterator i=aliases.begin(); i!=aliases.end(); ++i) { if (!first) os << ", "; first = false; os << *i; } os << " }" << endl; } os << " }" << endl; } inline ostream& operator<<(ostream& os, const ZoneMap& zoneinfo) { for (ZoneMapIter it = zoneinfo.begin(); it != zoneinfo.end(); ++it) { it->second.print(os, it->first); } return os; } //-------------------------------------------------------------------- // main //-------------------------------------------------------------------- // Unary predicate for finding transitions after a given time bool isAfter(const Transition t, long thresh) { return t.time >= thresh; } /** * A zone type that contains only the raw and dst offset. Used by the * optimizeTypeList() method. */ struct SimplifiedZoneType { long rawoffset; long dstoffset; SimplifiedZoneType() : rawoffset(-1), dstoffset(-1) {} SimplifiedZoneType(const ZoneType& t) : rawoffset(t.rawoffset), dstoffset(t.dstoffset) {} bool operator<(const SimplifiedZoneType& t) const { return rawoffset < t.rawoffset || (rawoffset == t.rawoffset && dstoffset < t.dstoffset); } }; /** * Construct a ZoneType from a SimplifiedZoneType. Note that this * discards information; the new ZoneType will have meaningless * (empty) abbr, isdst, isstd, and isgmt flags; this is appropriate, * since ignoring these is how we do optimization (we have no use for * these in historical transitions). */ ZoneType::ZoneType(const SimplifiedZoneType& t) : rawoffset(t.rawoffset), dstoffset(t.dstoffset), abbr(-1), isdst(false), isstd(false), isgmt(false) {} /** * Optimize the type list to remove excess entries. The type list may * contain entries that are distinct only in terms of their dst, std, * or gmt flags. Since we don't care about those flags, we can reduce * the type list to a set of unique raw/dst offset pairs, and remap * the type indices in the transition list, which stores, for each * transition, a transition time and a type index. */ void ZoneInfo::optimizeTypeList() { // Assemble set of unique types; only those in the `transitions' // list, since there may be unused types in the `types' list // corresponding to transitions that have been trimmed (during // merging of final data). if (aliasTo >= 0) return; // Nothing to do for aliases // If there are zero transitions and one type, then leave that as-is. if (transitions.size() == 0) { if (types.size() != 1) { cerr << "Error: transition count = 0, type count = " << types.size() << endl; } return; } set simpleset; for (vector::const_iterator i=transitions.begin(); i!=transitions.end(); ++i) { assert(i->type < (int)types.size()); simpleset.insert(types[i->type]); } // Map types to integer indices map simplemap; int n=0; for (set::const_iterator i=simpleset.begin(); i!=simpleset.end(); ++i) { simplemap[*i] = n++; } // Remap transitions for (vector::iterator i=transitions.begin(); i!=transitions.end(); ++i) { assert(i->type < (int)types.size()); ZoneType oldtype = types[i->type]; SimplifiedZoneType newtype(oldtype); assert(simplemap.find(newtype) != simplemap.end()); i->type = simplemap[newtype]; } // Replace type list types.clear(); copy(simpleset.begin(), simpleset.end(), back_inserter(types)); } /** * Merge final zone data into this zone. */ void ZoneInfo::mergeFinalData(const FinalZone& fz) { int year = fz.year; long seconds = yearToSeconds(year); vector::iterator it = find_if(transitions.begin(), transitions.end(), bind2nd(ptr_fun(isAfter), seconds)); transitions.erase(it, transitions.end()); if (finalYear != -1) { throw invalid_argument("Final zone already merged in"); } finalYear = fz.year; finalOffset = fz.offset; finalRuleID = fz.ruleid; } /** * Merge the data from the given final zone into the core zone data by * calling the ZoneInfo member function mergeFinalData. */ void mergeOne(const string& zoneid, const FinalZone& fz) { if (ZONEINFO.find(zoneid) == ZONEINFO.end()) { throw invalid_argument("Unrecognized final zone ID"); } ZONEINFO[zoneid].mergeFinalData(fz); } /** * Visitor function that merges the final zone data into the main zone * data structures. It calls mergeOne for each final zone and its * list of aliases. */ void mergeFinalZone(const pair& p) { const string& id = p.first; const FinalZone& fz = p.second; mergeOne(id, fz); // TODO remoev // for (set::const_iterator i=fz.aliases.begin(); i!=fz.aliases.end(); ++i) { // mergeOne(*i, fz); // } } /** * Print this rule in resource bundle format to os. ID and enclosing * braces handled elsewhere. */ void FinalRule::print(ostream& os) const { // First print the rule part that enters DST; then the rule part // that exits it. int whichpart = (part[0].offset != 0) ? 0 : 1; assert(part[whichpart].offset != 0); assert(part[1-whichpart].offset == 0); os << " "; for (int i=0; i<2; ++i) { const FinalRulePart& p = part[whichpart]; whichpart = 1-whichpart; os << p.month << ", " << p.stz_dowim() << ", " << p.stz_dow() << ", " << p.time << ", " << p.timemode() << ", "; } os << part[whichpart].offset << endl; } int main(int argc, char *argv[]) { string rootpath, zonetab; if (argc != 3) { cout << "Usage: tz2icu " << endl << " path to zoneinfo file tree generated by" << endl << " ICU-patched version of zic" << endl << " country map, from tzdata archive," << endl << " typically named \"zone.tab\"" << endl; exit(1); } else { rootpath = argv[1]; zonetab = argv[2]; } try { ifstream finals(ICU_ZONE_FILE); if (finals) { readFinalZonesAndRules(finals); cout << "Finished reading " << finalZones.size() << " final zones and " << finalRules.size() << " final rules from " ICU_ZONE_FILE << endl; } else { cerr << "Error: Unable to open " ICU_ZONE_FILE << endl; return 1; } } catch (const exception& error) { cerr << "Error: While reading " ICU_ZONE_FILE ": " << error.what() << endl; return 1; } // Read the legacy alias list and process it. Treat the legacy mappings // like links, but also record them in the "legacy" hash. try { ifstream aliases(ICU_TZ_ALIAS); if (!aliases) { cerr << "Error: Unable to open " ICU_TZ_ALIAS << endl; return 1; } int n = 0; string line; while (getline(aliases, line)) { string::size_type lb = line.find('#'); if (lb != string::npos) { line.resize(lb); // trim comments } vector a; istringstream is(line); copy(istream_iterator(is),istream_iterator(), back_inserter(a)); if (a.size() == 0) continue; // blank line if (a.size() != 2) { cerr << "Error: Can't parse \"" << line << "\" in " ICU_TZ_ALIAS << endl; exit(1); } ++n; string alias(a[0]), olson(a[1]); if (links.find(alias) != links.end()) { cerr << "Error: Alias \"" << alias << "\" is an Olson zone in " ICU_TZ_ALIAS << endl; return 1; } if (reverseLinks.find(alias) != reverseLinks.end()) { cerr << "Error: Alias \"" << alias << "\" is an Olson link to \"" << reverseLinks[olson] << "\" in " << ICU_TZ_ALIAS << endl; return 1; } // Record source for error reporting if (linkSource.find(olson) == linkSource.end()) { linkSource[olson] = "ICU alias"; } assert(linkSource.find(alias) == linkSource.end()); linkSource[alias] = "ICU alias"; links[olson].insert(alias); reverseLinks[alias] = olson; } cout << "Finished reading " << n << " aliases from " ICU_TZ_ALIAS << endl; } catch (const exception& error) { cerr << "Error: While reading " ICU_TZ_ALIAS ": " << error.what() << endl; return 1; } try { // Recursively scan all files below the given path, accumulating // their data into ZONEINFO. All files must be TZif files. Any // failure along the way will result in a call to exit(1). scandir(rootpath); } catch (const exception& error) { cerr << "Error: While scanning " << rootpath << ": " << error.what() << endl; return 1; } cout << "Finished reading " << ZONEINFO.size() << " zoneinfo files [" << (ZONEINFO.begin())->first << ".." << (--ZONEINFO.end())->first << "]" << endl; try { for_each(finalZones.begin(), finalZones.end(), mergeFinalZone); } catch (const exception& error) { cerr << "Error: While merging final zone data: " << error.what() << endl; return 1; } // Process links (including ICU aliases). For each link set we have // a canonical ID (e.g., America/Los_Angeles) and a set of one or more // aliases (e.g., PST, PST8PDT, ...). // 1. Add all aliases as zone objects in ZONEINFO for (map >::const_iterator i = links.begin(); i!=links.end(); ++i) { const string& olson = i->first; const set& aliases = i->second; if (ZONEINFO.find(olson) == ZONEINFO.end()) { cerr << "Error: Invalid " << linkSource[olson] << " to non-existent \"" << olson << "\"" << endl; return 1; } for (set::const_iterator j=aliases.begin(); j!=aliases.end(); ++j) { ZONEINFO[*j] = ZoneInfo(); } } // 2. Create a mapping from zones to index numbers 0..n-1. map zoneIDs; vector zoneIDlist; int z=0; for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) { zoneIDs[i->first] = z++; zoneIDlist.push_back(i->first); } assert(z == (int) ZONEINFO.size()); // 3. Merge aliases. Sometimes aliases link to other aliases; we // resolve these into simplest possible sets. map > links2; map reverse2; for (map >::const_iterator i = links.begin(); i!=links.end(); ++i) { string olson = i->first; while (reverseLinks.find(olson) != reverseLinks.end()) { olson = reverseLinks[olson]; } for (set::const_iterator j=i->second.begin(); j!=i->second.end(); ++j) { links2[olson].insert(*j); reverse2[*j] = olson; } } links = links2; reverseLinks = reverse2; if (false) { // Debugging: Emit link map for (map >::const_iterator i = links.begin(); i!=links.end(); ++i) { cout << i->first << ": "; for (set::const_iterator j=i->second.begin(); j!=i->second.end(); ++j) { cout << *j << ", "; } cout << endl; } } // 4. Update aliases for (map >::const_iterator i = links.begin(); i!=links.end(); ++i) { const string& olson = i->first; const set& aliases = i->second; ZONEINFO[olson].clearAliases(); for (set::const_iterator j=aliases.begin(); j!=aliases.end(); ++j) { assert(zoneIDs.find(olson) != zoneIDs.end()); assert(zoneIDs.find(*j) != zoneIDs.end()); assert(ZONEINFO.find(*j) != ZONEINFO.end()); ZONEINFO[*j].setAliasTo(zoneIDs[olson]); ZONEINFO[olson].addAlias(zoneIDs[*j]); } } // Once merging of final data is complete, we can optimize the type list for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) { i->second.optimizeTypeList(); } // Create the country map map > countryMap; // country -> set of zones map reverseCountryMap; // zone -> country try { ifstream f(zonetab.c_str()); if (!f) { cerr << "Error: Unable to open " << zonetab << endl; return 1; } int n = 0; string line; while (getline(f, line)) { string::size_type lb = line.find('#'); if (lb != string::npos) { line.resize(lb); // trim comments } string country, coord, zone; istringstream is(line); is >> country >> coord >> zone; if (country.size() == 0) continue; if (country.size() != 2 || zone.size() < 1) { cerr << "Error: Can't parse " << line << " in " << zonetab << endl; return 1; } if (ZONEINFO.find(zone) == ZONEINFO.end()) { cerr << "Error: Country maps to invalid zone " << zone << " in " << zonetab << endl; return 1; } countryMap[country].insert(zone); reverseCountryMap[zone] = country; ++n; } cout << "Finished reading " << n << " country entries from " << zonetab << endl; } catch (const exception& error) { cerr << "Error: While reading " << zonetab << ": " << error.what() << endl; return 1; } // Merge links into country map for (map >::const_iterator i = links.begin(); i!=links.end(); ++i) { const string& olson(i->first); if (reverseCountryMap.find(olson) == reverseCountryMap.end()) { continue; } string c = reverseCountryMap[olson]; const set& aliases(i->second); for (set::const_iterator j=aliases.begin(); j != aliases.end(); ++j) { countryMap[c].insert(*j); reverseCountryMap[*j] = c; } } // Create a pseudo-country containing all zones belonging to no country set nocountry; for (ZoneMap::iterator i=ZONEINFO.begin(); i!=ZONEINFO.end(); ++i) { if (reverseCountryMap.find(i->first) == reverseCountryMap.end()) { nocountry.insert(i->first); } } countryMap[""] = nocountry; // NOTE: The following code assumes that genrb reorders the keys // using a strcmp comparison (true in ICU 2.8). // Find the starting positions of zones, rules ("_..."), and // countries ("%..."). There is an empty country "%" that // contains all non-country zones. That counts as an ordinary // country. There is also a meta-data entry named "_" which does // NOT count as a rule. int32_t zoneStart=0, zoneCount=ZONEINFO.size(), ruleStart=0, ruleCount=finalRules.size() + 1, // include meta '_' for now countryStart=0, countryCount=countryMap.size(); // Try to figure out how strcmp in genrb is going to sort things. char zoneCh='A', ruleCh='_', countryCh='%'; if (zoneCh < ruleCh) ruleStart += zoneCount; else zoneStart += ruleCount; if (zoneCh < countryCh) countryStart += zoneCount; else zoneStart += countryCount; if (ruleCh < countryCh) countryStart += ruleCount; else ruleStart += countryCount; // Remove the meta element, '_', which will be at the start of the // real rules. ruleStart++; ruleCount--; // Get local time & year for below time_t sec; time(&sec); struct tm* now = localtime(&sec); int thisYear = now->tm_year + 1900; // Write out a resource-bundle source file containing data for // all zones. ofstream file(ICU_TZ_RESOURCE ".txt"); if (file) { file << "//---------------------------------------------------------" << endl << "// Copyright (C) 2003"; if (thisYear > 2003) { file << "-" << thisYear; } file << ", International Business Machines" << endl << "// Corporation and others. All Rights Reserved." << endl << "//---------------------------------------------------------" << endl << "// Build tool: tz2icu" << endl << "// Build date: " << asctime(now) /* << endl -- asctime emits CR */ << "// Olson source: ftp://elsie.nci.nih.gov/pub/" << endl << "//---------------------------------------------------------" << endl << "// >> !!! >> THIS IS A MACHINE-GENERATED FILE << !!! <<" << endl << "// >> !!! >>> DO NOT EDIT <<< !!! <<" << endl << "//---------------------------------------------------------" << endl << endl << ICU_TZ_RESOURCE " {" << endl << ZONEINFO; // Emit final rules for(map::iterator i=finalRules.begin(); i!=finalRules.end(); ++i) { const string& id = i->first; const FinalRule& r = i->second; file << " _" << id << ":intvector {" << endl; r.print(file); file << " }" << endl; } // Emit country map. Emitting the string zone IDs results // in a 188 kb binary resource; emitting the zone index numbers // trims this to 171 kb. More work for the runtime code, but // a smaller data footprint. for (map >::const_iterator i=countryMap.begin(); i != countryMap.end(); ++i) { string country = i->first; const set& zones(i->second); file << " %" << country << ":intvector { "; bool first = true; for (set::const_iterator j=zones.begin(); j != zones.end(); ++j) { if (!first) file << ", "; first = false; if (zoneIDs.find(*j) == zoneIDs.end()) { cerr << "Error: Nonexistent zone in country map: " << *j << endl; return 1; } file << zoneIDs[*j]; // emit the zone's index number } file << " }" << endl; } // Emit meta-data. file << " _:intvector {" << endl << " " << zoneStart << ", " << zoneCount << ", // zone start,count" << endl << " " << ruleStart << ", " << ruleCount << ", // rule start,count" << endl << " " << countryStart << ", " << countryCount << " // country start,count" << endl << " }" << endl; file << "}" << endl; } if (file) { // recheck error bit cout << "Finished writing " ICU_TZ_RESOURCE ".txt" << endl; return 0; } else { cerr << "Error: Unable to open/write to " ICU_TZ_RESOURCE ".txt" << endl; return 1; } } //eof