1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 2007-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: icuzdump.cpp
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 2007-04-02
16 * created by: Yoshito Umaoka
17 *
18 * This tool write out timezone transitions for ICU timezone. This tool
19 * is used as a part of tzdata update process to check if ICU timezone
20 * code works as well as the corresponding Olson stock localtime/zdump.
21 */
22
23 #include <cstdlib>
24 #include <cstring>
25 #include <fstream>
26 #include <sstream>
27 #include <iostream>
28
29 #include "unicode/utypes.h"
30 #include "unicode/ustring.h"
31 #include "unicode/timezone.h"
32 #include "unicode/simpletz.h"
33 #include "unicode/smpdtfmt.h"
34 #include "unicode/decimfmt.h"
35 #include "unicode/gregocal.h"
36 #include "unicode/ustream.h"
37 #include "unicode/putil.h"
38
39 #include "cmemory.h"
40 #include "uoptions.h"
41
42 using namespace std;
43 using namespace icu;
44
45 class DumpFormatter {
46 public:
DumpFormatter()47 DumpFormatter() {
48 UErrorCode status = U_ZERO_ERROR;
49 stz = new SimpleTimeZone(0, "");
50 sdf = new SimpleDateFormat((UnicodeString)"yyyy-MM-dd EEE HH:mm:ss", Locale::getEnglish(), status);
51 DecimalFormatSymbols *symbols = new DecimalFormatSymbols(Locale::getEnglish(), status);
52 decf = new DecimalFormat("00", symbols, status);
53 }
~DumpFormatter()54 ~DumpFormatter() {
55 }
56
format(UDate time,int32_t offset,UBool isDst,UnicodeString & appendTo)57 UnicodeString& format(UDate time, int32_t offset, UBool isDst, UnicodeString& appendTo) {
58 stz->setRawOffset(offset);
59 sdf->setTimeZone(*stz);
60 UnicodeString str = sdf->format(time, appendTo);
61 if (offset < 0) {
62 appendTo += "-";
63 offset = -offset;
64 } else {
65 appendTo += "+";
66 }
67
68 int32_t hour, min, sec;
69
70 offset /= 1000;
71 sec = offset % 60;
72 offset = (offset - sec) / 60;
73 min = offset % 60;
74 hour = offset / 60;
75
76 decf->format(hour, appendTo);
77 decf->format(min, appendTo);
78 decf->format(sec, appendTo);
79 appendTo += "[DST=";
80 if (isDst) {
81 appendTo += "1";
82 } else {
83 appendTo += "0";
84 }
85 appendTo += "]";
86 return appendTo;
87 }
88 private:
89 SimpleTimeZone* stz;
90 SimpleDateFormat* sdf;
91 DecimalFormat* decf;
92 };
93
94 class ICUZDump {
95 public:
ICUZDump()96 ICUZDump() {
97 formatter = new DumpFormatter();
98 loyear = 1902;
99 hiyear = 2050;
100 tick = 1000;
101 linesep = NULL;
102 }
103
~ICUZDump()104 ~ICUZDump() {
105 }
106
setLowYear(int32_t lo)107 void setLowYear(int32_t lo) {
108 loyear = lo;
109 }
110
setHighYear(int32_t hi)111 void setHighYear(int32_t hi) {
112 hiyear = hi;
113 }
114
setTick(int32_t t)115 void setTick(int32_t t) {
116 tick = t;
117 }
118
setTimeZone(TimeZone * tz)119 void setTimeZone(TimeZone* tz) {
120 timezone = tz;
121 }
122
setDumpFormatter(DumpFormatter * fmt)123 void setDumpFormatter(DumpFormatter* fmt) {
124 formatter = fmt;
125 }
126
setLineSeparator(const char * sep)127 void setLineSeparator(const char* sep) {
128 linesep = sep;
129 }
130
dump(ostream & out)131 void dump(ostream& out) {
132 UErrorCode status = U_ZERO_ERROR;
133 UDate SEARCH_INCREMENT = 12 * 60 * 60 * 1000; // half day
134 UDate t, cutlo, cuthi;
135 int32_t rawOffset, dstOffset;
136 UnicodeString str;
137
138 getCutOverTimes(cutlo, cuthi);
139 t = cutlo;
140 timezone->getOffset(t, false, rawOffset, dstOffset, status);
141 while (t < cuthi) {
142 int32_t newRawOffset, newDstOffset;
143 UDate newt = t + SEARCH_INCREMENT;
144
145 timezone->getOffset(newt, false, newRawOffset, newDstOffset, status);
146
147 UBool bSameOffset = (rawOffset + dstOffset) == (newRawOffset + newDstOffset);
148 UBool bSameDst = ((dstOffset != 0) && (newDstOffset != 0)) || ((dstOffset == 0) && (newDstOffset == 0));
149
150 if (!bSameOffset || !bSameDst) {
151 // find the boundary
152 UDate lot = t;
153 UDate hit = newt;
154 while (true) {
155 int32_t diff = (int32_t)(hit - lot);
156 if (diff <= tick) {
157 break;
158 }
159 UDate medt = lot + ((diff / 2) / tick) * tick;
160 int32_t medRawOffset, medDstOffset;
161 timezone->getOffset(medt, false, medRawOffset, medDstOffset, status);
162
163 bSameOffset = (rawOffset + dstOffset) == (medRawOffset + medDstOffset);
164 bSameDst = ((dstOffset != 0) && (medDstOffset != 0)) || ((dstOffset == 0) && (medDstOffset == 0));
165
166 if (!bSameOffset || !bSameDst) {
167 hit = medt;
168 } else {
169 lot = medt;
170 }
171 }
172 // write out the boundary
173 str.remove();
174 formatter->format(lot, rawOffset + dstOffset, (dstOffset == 0 ? false : true), str);
175 out << str << " > ";
176 str.remove();
177 formatter->format(hit, newRawOffset + newDstOffset, (newDstOffset == 0 ? false : true), str);
178 out << str;
179 if (linesep != NULL) {
180 out << linesep;
181 } else {
182 out << endl;
183 }
184
185 rawOffset = newRawOffset;
186 dstOffset = newDstOffset;
187 }
188 t = newt;
189 }
190 }
191
192 private:
getCutOverTimes(UDate & lo,UDate & hi)193 void getCutOverTimes(UDate& lo, UDate& hi) {
194 UErrorCode status = U_ZERO_ERROR;
195 GregorianCalendar* gcal = new GregorianCalendar(timezone, Locale::getEnglish(), status);
196 gcal->clear();
197 gcal->set(loyear, 0, 1, 0, 0, 0);
198 lo = gcal->getTime(status);
199 gcal->set(hiyear, 0, 1, 0, 0, 0);
200 hi = gcal->getTime(status);
201 }
202
203 TimeZone* timezone;
204 int32_t loyear;
205 int32_t hiyear;
206 int32_t tick;
207
208 DumpFormatter* formatter;
209 const char* linesep;
210 };
211
212 class ZoneIterator {
213 public:
ZoneIterator(UBool bAll=false)214 ZoneIterator(UBool bAll = false) {
215 if (bAll) {
216 UErrorCode status = U_ZERO_ERROR;
217 zenum = TimeZone::createEnumeration(status);
218 // TODO: Add error case handling later.
219 }
220 else {
221 zenum = NULL;
222 zids = NULL;
223 idx = 0;
224 numids = 1;
225 }
226 }
227
ZoneIterator(const char ** ids,int32_t num)228 ZoneIterator(const char** ids, int32_t num) {
229 zenum = NULL;
230 zids = ids;
231 idx = 0;
232 numids = num;
233 }
234
~ZoneIterator()235 ~ZoneIterator() {
236 if (zenum != NULL) {
237 delete zenum;
238 }
239 }
240
next()241 TimeZone* next() {
242 TimeZone* tz = NULL;
243 if (zenum != NULL) {
244 UErrorCode status = U_ZERO_ERROR;
245 const UnicodeString* zid = zenum->snext(status);
246 if (zid != NULL) {
247 tz = TimeZone::createTimeZone(*zid);
248 }
249 }
250 else {
251 if (idx < numids) {
252 if (zids != NULL) {
253 tz = TimeZone::createTimeZone((const UnicodeString&)zids[idx]);
254 }
255 else {
256 tz = TimeZone::createDefault();
257 }
258 idx++;
259 }
260 }
261 return tz;
262 }
263
264 private:
265 const char** zids;
266 StringEnumeration* zenum;
267 int32_t idx;
268 int32_t numids;
269 };
270
271 enum {
272 kOptHelpH = 0,
273 kOptHelpQuestionMark,
274 kOptAllZones,
275 kOptCutover,
276 kOptDestDir,
277 kOptLineSep
278 };
279
280 static UOption options[]={
281 UOPTION_HELP_H,
282 UOPTION_HELP_QUESTION_MARK,
283 UOPTION_DEF("allzones", 'a', UOPT_NO_ARG),
284 UOPTION_DEF("cutover", 'c', UOPT_REQUIRES_ARG),
285 UOPTION_DEF("destdir", 'd', UOPT_REQUIRES_ARG),
286 UOPTION_DEF("linesep", 'l', UOPT_REQUIRES_ARG)
287 };
288
289 extern int
main(int argc,char * argv[])290 main(int argc, char *argv[]) {
291 int32_t low = 1902;
292 int32_t high = 2038;
293 UBool bAll = false;
294 const char *dir = NULL;
295 const char *linesep = NULL;
296
297 U_MAIN_INIT_ARGS(argc, argv);
298 argc = u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
299
300 if (argc < 0) {
301 cerr << "Illegal command line argument(s)" << endl << endl;
302 }
303
304 if (argc < 0 || options[kOptHelpH].doesOccur || options[kOptHelpQuestionMark].doesOccur) {
305 cerr
306 << "Usage: icuzdump [-options] [zoneid1 zoneid2 ...]" << endl
307 << endl
308 << "\tDump all offset transitions for the specified zones." << endl
309 << endl
310 << "Options:" << endl
311 << "\t-a : Dump all available zones." << endl
312 << "\t-d <dir> : When specified, write transitions in a file under" << endl
313 << "\t the directory for each zone." << endl
314 << "\t-l <sep> : New line code type used in file outputs. CR or LF (default)"
315 << "\t or CRLF." << endl
316 << "\t-c [<low_year>,]<high_year>" << endl
317 << "\t : When specified, dump transitions starting <low_year>" << endl
318 << "\t (inclusive) up to <high_year> (exclusive). The default" << endl
319 << "\t values are 1902(low) and 2038(high)." << endl;
320 return argc < 0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
321 }
322
323 bAll = options[kOptAllZones].doesOccur;
324
325 if (options[kOptDestDir].doesOccur) {
326 dir = options[kOptDestDir].value;
327 }
328
329 if (options[kOptLineSep].doesOccur) {
330 if (strcmp(options[kOptLineSep].value, "CR") == 0) {
331 linesep = "\r";
332 } else if (strcmp(options[kOptLineSep].value, "CRLF") == 0) {
333 linesep = "\r\n";
334 } else if (strcmp(options[kOptLineSep].value, "LF") == 0) {
335 linesep = "\n";
336 }
337 }
338
339 if (options[kOptCutover].doesOccur) {
340 char* comma = (char*)strchr(options[kOptCutover].value, ',');
341 if (comma == NULL) {
342 high = atoi(options[kOptCutover].value);
343 } else {
344 *comma = 0;
345 low = atoi(options[kOptCutover].value);
346 high = atoi(comma + 1);
347 }
348 }
349
350 ICUZDump dumper;
351 dumper.setLowYear(low);
352 dumper.setHighYear(high);
353 if (dir != NULL && linesep != NULL) {
354 // use the specified line separator only for file output
355 dumper.setLineSeparator((const char*)linesep);
356 }
357
358 ZoneIterator* zit;
359 if (bAll) {
360 zit = new ZoneIterator(true);
361 } else {
362 if (argc <= 1) {
363 zit = new ZoneIterator();
364 } else {
365 zit = new ZoneIterator((const char**)&argv[1], argc - 1);
366 }
367 }
368
369 UnicodeString id;
370 if (dir != NULL) {
371 // file output
372 ostringstream path;
373 ios::openmode mode = ios::out;
374 if (linesep != NULL) {
375 mode |= ios::binary;
376 }
377 for (;;) {
378 TimeZone* tz = zit->next();
379 if (tz == NULL) {
380 break;
381 }
382 dumper.setTimeZone(tz);
383 tz->getID(id);
384
385 // target file path
386 path.str("");
387 path << dir << U_FILE_SEP_CHAR;
388 id = id.findAndReplace("/", "-");
389 path << id;
390
391 ofstream* fout = new ofstream(path.str().c_str(), mode);
392 if (fout->fail()) {
393 cerr << "Cannot open file " << path.str() << endl;
394 delete fout;
395 delete tz;
396 break;
397 }
398
399 dumper.dump(*fout);
400 fout->close();
401 delete fout;
402 delete tz;
403 }
404
405 } else {
406 // stdout
407 UBool bFirst = true;
408 for (;;) {
409 TimeZone* tz = zit->next();
410 if (tz == NULL) {
411 break;
412 }
413 dumper.setTimeZone(tz);
414 tz->getID(id);
415 if (bFirst) {
416 bFirst = false;
417 } else {
418 cout << endl;
419 }
420 cout << "ZONE: " << id << endl;
421 dumper.dump(cout);
422 delete tz;
423 }
424 }
425 delete zit;
426 }
427