aboutsummaryrefslogtreecommitdiff
path: root/test/sun/util/calendar/zi/Zoneinfo.java
blob: e58fd188a309a266ce155bdcb47aa027b069d884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * Zoneinfo provides javazic compiler front-end functionality.
 * @since 1.4
 */
class Zoneinfo {

    private static final int minYear = 1900;
    private static final int maxYear = 2037;
    private static final long minTime = Time.getLocalTime(minYear, Month.JANUARY, 1, 0);
    private static int startYear = minYear;
    private static int endYear = maxYear;

    /**
     * True if javazic should generate a list of SimpleTimeZone
     * instances for the SimpleTimeZone-based time zone support.
     */
    static boolean isYearForTimeZoneDataSpecified = false;

    /**
     * Zone name to Zone mappings
     */
    private Map<String,Zone> zones;

    /**
     * Rule name to Rule mappings
     */
    private Map<String,Rule> rules;

    /**
     * Alias name to real name mappings
     */
    private Map<String,String> aliases;

    /**
     * Constracts a Zoneinfo.
     */
    Zoneinfo() {
        zones = new HashMap<String,Zone>();
        rules = new HashMap<String,Rule>();
        aliases = new HashMap<String,String>();
    }

    /**
     * Adds the given zone to the list of Zones.
     * @param zone Zone to be added to the list.
     */
    void add(Zone zone) {
        String name = zone.getName();
        zones.put(name, zone);
    }

    /**
     * Adds the given rule to the list of Rules.
     * @param rule Rule to be added to the list.
     */
    void add(Rule rule) {
        String name = rule.getName();
        rules.put(name, rule);
    }

    /**
     * Puts the specifid name pair to the alias table.
     * @param name1 an alias time zone name
     * @param name2 the real time zone of the alias name
     */
    void putAlias(String name1, String name2) {
        aliases.put(name1, name2);
    }

    /**
     * Sets the given year for SimpleTimeZone list output.
     * This method is called when the -S option is specified.
     * @param year the year for which SimpleTimeZone list should be generated
     */
    static void setYear(int year) {
        setStartYear(year);
        setEndYear(year);
        isYearForTimeZoneDataSpecified = true;
    }

    /**
     * Sets the start year.
     * @param year the start year value
     * @throws IllegalArgumentException if the specified year value is
     * smaller than the minimum year or greater than the end year.
     */
    static void setStartYear(int year) {
        if (year < minYear || year > endYear) {
            throw new IllegalArgumentException("invalid start year specified: " + year);
        }
        startYear = year;
    }

    /**
     * @return the start year value
     */
    static int getStartYear() {
        return startYear;
    }

    /**
     * Sets the end year.
     * @param year the end year value
     * @throws IllegalArgumentException if the specified year value is
     * smaller than the start year or greater than the maximum year.
     */
    static void setEndYear(int year) {
        if (year < startYear || year > maxYear) {
            throw new IllegalArgumentException();
        }
        endYear = year;
    }

    /**
     * @return the end year value
     */
    static int getEndYear() {
        return endYear;
    }

    /**
     * @return the minimum year value
     */
    static int getMinYear() {
        return minYear;
    }

    /**
     * @return the maximum year value
     */
    static int getMaxYear() {
        return maxYear;
    }

    /**
     * @return the alias table
     */
    Map<String,String> getAliases() {
        return aliases;
    }

    /**
     * @return the Zone list
     */
    Map<String,Zone> getZones() {
        return zones;
    }

    /**
     * @return a Zone specified by name.
     * @param name a zone name
     */
    Zone getZone(String name) {
        return zones.get(name);
    }

    /**
     * @return a Rule specified by name.
     * @param name a rule name
     */
    Rule getRule(String name) {
        return rules.get(name);
    }

    private static String line;

    private static int lineNum;

    /**
     * Parses the specified time zone data file and creates a Zoneinfo
     * that has all Rules, Zones and Links (aliases) information.
     * @param fname the time zone data file name
     * @return a Zoneinfo object
     */
    static Zoneinfo parse(String fname) {
        BufferedReader in = null;
        try {
            FileReader fr = new FileReader(fname);
            in = new BufferedReader(fr);
        } catch (FileNotFoundException e) {
            panic("can't open file: "+fname);
        }
        Zoneinfo zi = new Zoneinfo();
        boolean continued = false;
        Zone zone = null;
        String l;
        lineNum = 0;

        try {
            while ((line = in.readLine()) != null) {
                lineNum++;
                // skip blank and comment lines
                if (line.length() == 0 || line.charAt(0) == '#') {
                    continue;
                }

                // trim trailing comments
                int rindex = line.lastIndexOf('#');
                if (rindex != -1) {
                    // take the data part of the line
                    l = line.substring(0, rindex);
                } else {
                    l = line;
                }

                StringTokenizer tokens = new StringTokenizer(l);
                if (!tokens.hasMoreTokens()) {
                    continue;
                }
                String token = tokens.nextToken();

                if (continued || "Zone".equals(token)) {
                    if (zone == null) {
                        if (!tokens.hasMoreTokens()) {
                            panic("syntax error: zone no more token");
                        }
                        token = tokens.nextToken();
                        // if the zone name is in "GMT+hh" or "GMT-hh"
                        // format, ignore it due to spec conflict.
                        if (token.startsWith("GMT+") || token.startsWith("GMT-")) {
                            continue;
                        }
                        zone = new Zone(token);
                    } else {
                        // no way to push the current token back...
                        tokens = new StringTokenizer(l);
                    }

                    ZoneRec zrec = ZoneRec.parse(tokens);
                    zrec.setLine(line);
                    zone.add(zrec);
                    if ((continued = zrec.hasUntil()) == false) {
                        if (Zone.isTargetZone(zone.getName())) {
                            // zone.resolve(zi);
                            zi.add(zone);
                        }
                        zone = null;
                    }
                } else if ("Rule".equals(token)) {
                    if (!tokens.hasMoreTokens()) {
                        panic("syntax error: rule no more token");
                    }
                    token = tokens.nextToken();
                    Rule rule = zi.getRule(token);
                    if (rule == null) {
                        rule = new Rule(token);
                        zi.add(rule);
                    }
                    RuleRec rrec = RuleRec.parse(tokens);
                    rrec.setLine(line);
                    rule.add(rrec);
                } else if ("Link".equals(token)) {
                    // Link <newname> <oldname>
                    try {
                        String name1 = tokens.nextToken();
                        String name2 = tokens.nextToken();

                        // if the zone name is in "GMT+hh" or "GMT-hh"
                        // format, ignore it due to spec conflict with
                        // custom time zones. Also, ignore "ROC" for
                        // PC-ness.
                        if (name2.startsWith("GMT+") || name2.startsWith("GMT-")
                            || "ROC".equals(name2)) {
                            continue;
                        }
                        zi.putAlias(name2, name1);
                    } catch (Exception e) {
                        panic("syntax error: no more token for Link");
                    }
                }
            }
            in.close();
        } catch (IOException ex) {
            panic("IO error: " + ex.getMessage());
        }

        return zi;
    }

    /**
     * Interprets a zone and constructs a Timezone object that
     * contains enough information on GMT offsets and DST schedules to
     * generate a zone info database.
     *
     * @param zoneName the zone name for which a Timezone object is
     * constructed.
     *
     * @return a Timezone object that contains all GMT offsets and DST
     * rules information.
     */
    Timezone phase2(String zoneName) {
        Timezone tz = new Timezone(zoneName);
        Zone zone = getZone(zoneName);
        zone.resolve(this);

        // TODO: merge phase2's for the regular and SimpleTimeZone ones.
        if (isYearForTimeZoneDataSpecified) {
            ZoneRec zrec = zone.get(zone.size()-1);
            tz.setLastZoneRec(zrec);
            tz.setRawOffset(zrec.getGmtOffset());
            if (zrec.hasRuleReference()) {
                /*
                 * This part assumes that the specified year is covered by
                 * the rules referred to by the last zone record.
                 */
                List<RuleRec> rrecs = zrec.getRuleRef().getRules(startYear);

                if (rrecs.size() == 2) {
                    // make sure that one is a start rule and the other is
                    // an end rule.
                    RuleRec r0 = rrecs.get(0);
                    RuleRec r1 = rrecs.get(1);
                    if (r0.getSave() == 0 && r1.getSave() > 0) {
                        rrecs.set(0, r1);
                        rrecs.set(1, r0);
                    } else if (!(r0.getSave() > 0 && r1.getSave() == 0)) {
                        rrecs = null;
                        Main.error(zoneName + ": rules for " +  startYear + " not found.");
                    }
                } else {
                    rrecs = null;
                }
                if (rrecs != null) {
                    tz.setLastRules(rrecs);
                }
            }
            return tz;
        }

        int gmtOffset;
        int year = minYear;
        int fromYear = year;
        long fromTime = Time.getLocalTime(startYear,
                                          Month.JANUARY,
                                          1, 0);

        // take the index 0 for the GMT offset of the last zone record
        ZoneRec zrec = zone.get(zone.size()-1);
        tz.getOffsetIndex(zrec.getGmtOffset());

        int lastGmtOffsetValue = -1;
        ZoneRec prevzrec = null;
        int currentSave = 0;
        boolean usedZone;
        for (int zindex = 0; zindex < zone.size(); zindex++) {
            zrec = zone.get(zindex);
            usedZone = false;
            gmtOffset = zrec.getGmtOffset();
            int stdOffset = zrec.getDirectSave();

            if (gmtOffset != lastGmtOffsetValue) {
                tz.setRawOffset(gmtOffset, fromTime);
                lastGmtOffsetValue = gmtOffset;
            }
            // If this is the last zone record, take the last rule info.
            if (!zrec.hasUntil()) {
                if (zrec.hasRuleReference()) {
                    tz.setLastRules(zrec.getRuleRef().getLastRules());
                } else if (stdOffset != 0) {
                    // in case the last rule is all year round DST-only
                    // (Asia/Amman once announced this rule.)
                    tz.setLastDSTSaving(stdOffset);
                }
            }
            if (!zrec.hasRuleReference()) {
                if (!zrec.hasUntil() || zrec.getUntilTime(stdOffset) >= fromTime) {
                    tz.addTransition(fromTime,
                                     tz.getOffsetIndex(gmtOffset+stdOffset),
                                     tz.getDstOffsetIndex(stdOffset));
                    usedZone = true;
                }
                currentSave = stdOffset;
                // optimization in case the last rule is fixed.
                if (!zrec.hasUntil()) {
                    if (tz.getNTransitions() > 0) {
                        if (stdOffset == 0) {
                            tz.setDSTType(Timezone.X_DST);
                        } else {
                            tz.setDSTType(Timezone.LAST_DST);
                        }
                        long time = Time.getLocalTime(maxYear,
                                                      Month.JANUARY, 1, 0);
                        time -= zrec.getGmtOffset();
                        tz.addTransition(time,
                                         tz.getOffsetIndex(gmtOffset+stdOffset),
                                         tz.getDstOffsetIndex(stdOffset));
                        tz.addUsedRec(zrec);
                    } else {
                        tz.setDSTType(Timezone.NO_DST);
                    }
                    break;
                }
            } else {
                Rule rule = zrec.getRuleRef();
                boolean fromTimeUsed = false;
                currentSave = 0;
            year_loop:
                for (year = getMinYear(); year <= endYear; year++) {
                    if (zrec.hasUntil() && year > zrec.getUntilYear()) {
                        break;
                    }
                    List<RuleRec> rules = rule.getRules(year);
                    if (rules.size() > 0) {
                        for (int i = 0; i < rules.size(); i++) {
                            RuleRec rrec = rules.get(i);
                            long transition = rrec.getTransitionTime(year,
                                                                     gmtOffset,
                                                                     currentSave);
                            if (zrec.hasUntil()) {
                                if (transition >= zrec.getUntilTime(currentSave)) {
                                    // If the GMT offset changed from the previous one,
                                    // record fromTime as a transition.
                                    if (!fromTimeUsed && prevzrec != null
                                        && gmtOffset != prevzrec.getGmtOffset()) {
                                        tz.addTransition(fromTime,
                                                         tz.getOffsetIndex(gmtOffset+currentSave),
                                                         tz.getDstOffsetIndex(currentSave));
                                        fromTimeUsed = true; // for consistency
                                    }
                                    break year_loop;
                                }
                            }

                            if (fromTimeUsed == false) {
                                if (fromTime <= transition) {
                                    fromTimeUsed = true;

                                    if (fromTime != minTime) {
                                        int prevsave;

                                        // See if until time in the previous
                                        // ZoneRec is the same thing as the
                                        // local time in the next rule.
                                        // (examples are Asia/Ashkhabad in 1991,
                                        // Europe/Riga in 1989)

                                        if (i > 0) {
                                            prevsave = rules.get(i-1).getSave();
                                        } else {
                                            List<RuleRec> prevrules = rule.getRules(year-1);

                                            if (prevrules.size() > 0) {
                                                prevsave = prevrules.get(prevrules.size()-1).getSave();
                                            } else {
                                                prevsave = 0;
                                            }
                                        }

                                        if (rrec.isSameTransition(prevzrec, prevsave, gmtOffset)) {
                                            currentSave = rrec.getSave();
                                            tz.addTransition(fromTime,
                                                         tz.getOffsetIndex(gmtOffset+currentSave),
                                                         tz.getDstOffsetIndex(currentSave));
                                            tz.addUsedRec(rrec);
                                            usedZone = true;
                                            continue;
                                        }
                                        if (!prevzrec.hasRuleReference()
                                            || rule != prevzrec.getRuleRef()
                                            || (rule == prevzrec.getRuleRef()
                                                && gmtOffset != prevzrec.getGmtOffset())) {
                                            int save = (fromTime == transition) ? rrec.getSave() : currentSave;
                                            tz.addTransition(fromTime,
                                                         tz.getOffsetIndex(gmtOffset+save),
                                                         tz.getDstOffsetIndex(save));
                                            tz.addUsedRec(rrec);
                                            usedZone = true;
                                        }
                                    } else {  // fromTime == minTime
                                        int save = rrec.getSave();
                                        tz.addTransition(minTime,
                                                         tz.getOffsetIndex(gmtOffset),
                                                         tz.getDstOffsetIndex(0));

                                        tz.addTransition(transition,
                                                         tz.getOffsetIndex(gmtOffset+save),
                                                         tz.getDstOffsetIndex(save));

                                        tz.addUsedRec(rrec);
                                        usedZone = true;
                                    }
                                } else if (year == fromYear && i == rules.size()-1) {
                                    int save = rrec.getSave();
                                    tz.addTransition(fromTime,
                                                     tz.getOffsetIndex(gmtOffset+save),
                                                     tz.getDstOffsetIndex(save));
                                }
                            }

                            currentSave = rrec.getSave();
                            if (fromTime < transition) {
                                tz.addTransition(transition,
                                                 tz.getOffsetIndex(gmtOffset+currentSave),
                                                 tz.getDstOffsetIndex(currentSave));
                                tz.addUsedRec(rrec);
                                usedZone = true;
                            }
                        }
                    } else {
                        if (year == fromYear) {
                            tz.addTransition(fromTime,
                                             tz.getOffsetIndex(gmtOffset+currentSave),
                                             tz.getDstOffsetIndex(currentSave));
                            fromTimeUsed = true;
                        }
                        if (year == endYear && !zrec.hasUntil()) {
                            if (tz.getNTransitions() > 0) {
                                // Assume that this Zone stopped DST
                                tz.setDSTType(Timezone.X_DST);
                                long time = Time.getLocalTime(maxYear, Month.JANUARY,
                                                              1, 0);
                                time -= zrec.getGmtOffset();
                                tz.addTransition(time,
                                                 tz.getOffsetIndex(gmtOffset),
                                                 tz.getDstOffsetIndex(0));
                                usedZone = true;
                            } else {
                                tz.setDSTType(Timezone.NO_DST);
                            }
                        }
                    }
                }
            }
            if (usedZone) {
                tz.addUsedRec(zrec);
            }
            if (zrec.hasUntil() && zrec.getUntilTime(currentSave) > fromTime) {
                fromTime = zrec.getUntilTime(currentSave);
                fromYear = zrec.getUntilYear();
                year = zrec.getUntilYear();
            }
            prevzrec = zrec;
        }

        if (tz.getDSTType() == Timezone.UNDEF_DST) {
            tz.setDSTType(Timezone.DST);
        }
        tz.optimize();
        tz.checksum();
        return tz;
    }

    private static void panic(String msg) {
        Main.panic(msg);
    }
}