1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.calendar;
  27 
  28 import java.io.ByteArrayInputStream;
  29 import java.io.DataInput;
  30 import java.io.DataInputStream;
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.io.StreamCorruptedException;
  34 import java.security.AccessController;
  35 import java.security.PrivilegedAction;
  36 import java.time.DayOfWeek;
  37 import java.time.LocalDateTime;
  38 import java.time.LocalTime;
  39 import java.time.Month;
  40 import java.time.OffsetDateTime;
  41 import java.time.ZoneOffset;
  42 import java.time.zone.ZoneRules;
  43 import java.time.zone.ZoneOffsetTransition;
  44 import java.time.zone.ZoneOffsetTransitionRule;
  45 import java.time.zone.ZoneOffsetTransitionRule.TimeDefinition;
  46 
  47 import java.util.ArrayList;
  48 import java.util.Arrays;
  49 import java.util.Calendar;
  50 import java.util.Collections;
  51 import java.util.HashMap;
  52 import java.util.List;
  53 import java.util.Locale;
  54 import java.util.Map;
  55 import java.util.Map.Entry;
  56 import java.util.Objects;
  57 import java.util.Set;
  58 import java.util.SimpleTimeZone;
  59 import java.util.concurrent.ConcurrentHashMap;
  60 import java.util.zip.CRC32;
  61 import java.util.zip.ZipFile;
  62 
  63 /**
  64  * Loads TZDB time-zone rules for j.u.TimeZone
  65  * <p>
  66  * @since 1.8
  67  */
  68 public final class ZoneInfoFile {
  69 
  70     /**
  71      * Gets all available IDs supported in the Java run-time.
  72      *
  73      * @return a set of time zone IDs.
  74      */
  75     public static Set<String> getZoneIds() {
  76         return zones.keySet();
  77     }
  78 
  79     /**
  80      * Gets all available IDs that have the same value as the
  81      * specified raw GMT offset.
  82      *
  83      * @param rawOffset  the GMT offset in milliseconds. This
  84      *                   value should not include any daylight saving time.
  85      * @return an array of time zone IDs.
  86      */
  87     public static String[] getZoneIds(int rawOffset) {
  88         List<String> ids = new ArrayList<>();
  89         for (String id : zones.keySet()) {
  90             ZoneInfo zi = getZoneInfo0(id);
  91             if (zi.getRawOffset() == rawOffset) {
  92                 ids.add(id);
  93             }
  94         }
  95         return ids.toArray(new String[ids.size()]);
  96     }
  97 
  98     public static ZoneInfo getZoneInfo(String zoneId) {
  99         if (!zones.containsKey(zoneId)) {
 100             return null;
 101         }
 102         // ZoneInfo is mutable, return the copy
 103 
 104         ZoneInfo zi = getZoneInfo0(zoneId);
 105         zi = (ZoneInfo)zi.clone();
 106         zi.setID(zoneId);
 107         return zi;
 108     }
 109 
 110     /**
 111      * Returns a Map from alias time zone IDs to their standard
 112      * time zone IDs.
 113      *
 114      * @return an unmodified alias mapping
 115      */
 116     public static Map<String, String> getAliasMap() {
 117         return aliases;
 118     }
 119 
 120     /**
 121      * Gets the version of this tz data.
 122      *
 123      * @return the tzdb version
 124      */
 125     public static String getVersion() {
 126         return versionId;
 127     }
 128 
 129     /**
 130      * Gets a ZoneInfo with the given GMT offset. The object
 131      * has its ID in the format of GMT{+|-}hh:mm.
 132      *
 133      * @param originalId  the given custom id (before normalized such as "GMT+9")
 134      * @param gmtOffset   GMT offset <em>in milliseconds</em>
 135      * @return a ZoneInfo constructed with the given GMT offset
 136      */
 137     public static ZoneInfo getCustomTimeZone(String originalId, int gmtOffset) {
 138         String id = toCustomID(gmtOffset);
 139         return new ZoneInfo(id, gmtOffset);
 140 /*
 141         ZoneInfo zi = getFromCache(id);
 142         if (zi == null) {
 143             zi = new ZoneInfo(id, gmtOffset);
 144             zi = addToCache(id, zi);
 145             if (!id.equals(originalId)) {
 146                 zi = addToCache(originalId, zi);
 147             }
 148         }
 149         return (ZoneInfo) zi.clone();
 150 */
 151     }
 152 
 153     public static String toCustomID(int gmtOffset) {
 154         char sign;
 155         int offset = gmtOffset / 60000;
 156 
 157         if (offset >= 0) {
 158             sign = '+';
 159         } else {
 160             sign = '-';
 161             offset = -offset;
 162         }
 163         int hh = offset / 60;
 164         int mm = offset % 60;
 165 
 166         char[] buf = new char[] { 'G', 'M', 'T', sign, '0', '0', ':', '0', '0' };
 167         if (hh >= 10) {
 168             buf[4] += hh / 10;
 169         }
 170         buf[5] += hh % 10;
 171         if (mm != 0) {
 172             buf[7] += mm / 10;
 173             buf[8] += mm % 10;
 174         }
 175         return new String(buf);
 176     }
 177 
 178 
 179     ///////////////////////////////////////////////////////////
 180 
 181     private static ZoneInfo getZoneInfo0(String zoneId) {
 182         try {
 183 
 184             Object obj = zones.get(zoneId);
 185             if (obj instanceof byte[]) {
 186                 byte[] bytes = (byte[]) obj;
 187                 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
 188                 obj = getZoneInfo(dis, zoneId);
 189                 zones.put(zoneId, obj);
 190             }
 191             return (ZoneInfo)obj;
 192         } catch (Exception ex) {
 193             throw new RuntimeException("Invalid binary time-zone data: TZDB:" +
 194                 zoneId + ", version: " + versionId, ex);
 195         }
 196     }
 197 
 198     private ZoneInfoFile() {
 199     }
 200 
 201     private static String versionId;
 202     private final static Map<String, Object> zones = new ConcurrentHashMap<>();
 203     private static Map<String, String> aliases = new HashMap<>();
 204 
 205     // Flag for supporting JDK backward compatible IDs, such as "EST".
 206     private static final boolean USE_OLDMAPPING;
 207 
 208     static {
 209         String oldmapping = AccessController.doPrivileged(
 210             new sun.security.action.GetPropertyAction("sun.timezone.ids.oldmapping", "false")).toLowerCase(Locale.ROOT);
 211         USE_OLDMAPPING = (oldmapping.equals("yes") || oldmapping.equals("true"));
 212         AccessController.doPrivileged(new PrivilegedAction<Object>() {
 213             public Object run() {
 214                 try {
 215 
 216                     String libDir = System.getProperty("java.home") + File.separator + "lib";
 217                     File tzdbJar = new File(libDir, "tzdb.jar");
 218                     try (ZipFile zf = new ZipFile(tzdbJar);
 219                         DataInputStream dis = new DataInputStream(
 220                             zf.getInputStream(zf.getEntry("TZDB.dat")))) {
 221                         load(dis);
 222                     }
 223                 } catch (Exception x) {
 224                     throw new Error(x);
 225                 }
 226                 return null;
 227             }
 228         });
 229     }
 230 
 231     // Must be invoked after loading in all data
 232     private static void addOldMapping() {
 233         String[][] oldMappings = new String[][] {
 234             { "ACT", "Australia/Darwin" },
 235             { "AET", "Australia/Sydney" },
 236             { "AGT", "America/Argentina/Buenos_Aires" },
 237             { "ART", "Africa/Cairo" },
 238             { "AST", "America/Anchorage" },
 239             { "BET", "America/Sao_Paulo" },
 240             { "BST", "Asia/Dhaka" },
 241             { "CAT", "Africa/Harare" },
 242             { "CNT", "America/St_Johns" },
 243             { "CST", "America/Chicago" },
 244             { "CTT", "Asia/Shanghai" },
 245             { "EAT", "Africa/Addis_Ababa" },
 246             { "ECT", "Europe/Paris" },
 247             { "IET", "America/Indiana/Indianapolis" },
 248             { "IST", "Asia/Kolkata" },
 249             { "JST", "Asia/Tokyo" },
 250             { "MIT", "Pacific/Apia" },
 251             { "NET", "Asia/Yerevan" },
 252             { "NST", "Pacific/Auckland" },
 253             { "PLT", "Asia/Karachi" },
 254             { "PNT", "America/Phoenix" },
 255             { "PRT", "America/Puerto_Rico" },
 256             { "PST", "America/Los_Angeles" },
 257             { "SST", "Pacific/Guadalcanal" },
 258             { "VST", "Asia/Ho_Chi_Minh" },
 259         };
 260         for (String[] alias : oldMappings) {
 261             String k = alias[0];
 262             String v = alias[1];
 263             if (zones.containsKey(v)) {  // make sure we do have the data
 264                 aliases.put(k, v);
 265                 zones.put(k, zones.get(v));
 266             }
 267         }
 268         if (USE_OLDMAPPING) {
 269             if (zones.containsKey("America/New_York")) {
 270                 aliases.put("EST", "America/New_York");
 271                 zones.put("EST", zones.get("America/New_York"));
 272             }
 273             if (zones.containsKey("America/Denver")) {
 274                 aliases.put("MST", "America/Denver");
 275                 zones.put("MST", zones.get("America/Denver"));
 276             }
 277             if (zones.containsKey("Pacific/Honolulu")) {
 278                 aliases.put("HST", "Pacific/Honolulu");
 279                 zones.put("HST", zones.get("Pacific/Honolulu"));
 280             }
 281         }
 282     }
 283 
 284     /**
 285      * Loads the rules from a DateInputStream
 286      *
 287      * @param dis  the DateInputStream to load, not null
 288      * @throws Exception if an error occurs
 289      */
 290     private static void load(DataInputStream dis) throws ClassNotFoundException, IOException {
 291         if (dis.readByte() != 1) {
 292             throw new StreamCorruptedException("File format not recognised");
 293         }
 294         // group
 295         String groupId = dis.readUTF();
 296         if ("TZDB".equals(groupId) == false) {
 297             throw new StreamCorruptedException("File format not recognised");
 298         }
 299         // versions, only keep the last one
 300         int versionCount = dis.readShort();
 301         for (int i = 0; i < versionCount; i++) {
 302             versionId = dis.readUTF();
 303 
 304         }
 305         // regions
 306         int regionCount = dis.readShort();
 307         String[] regionArray = new String[regionCount];
 308         for (int i = 0; i < regionCount; i++) {
 309             regionArray[i] = dis.readUTF();
 310         }
 311         // rules
 312         int ruleCount = dis.readShort();
 313         Object[] ruleArray = new Object[ruleCount];
 314         for (int i = 0; i < ruleCount; i++) {
 315             byte[] bytes = new byte[dis.readShort()];
 316             dis.readFully(bytes);
 317             ruleArray[i] = bytes;
 318         }
 319         // link version-region-rules, only keep the last version, if more than one
 320         for (int i = 0; i < versionCount; i++) {
 321             regionCount = dis.readShort();
 322             zones.clear();
 323             for (int j = 0; j < regionCount; j++) {
 324                 String region = regionArray[dis.readShort()];
 325                 Object rule = ruleArray[dis.readShort() & 0xffff];
 326                 zones.put(region, rule);
 327             }
 328         }
 329         // remove the following ids from the map, they
 330         // are exclued from the "old" ZoneInfo
 331         zones.remove("ROC");
 332         for (int i = 0; i < versionCount; i++) {
 333             int aliasCount = dis.readShort();
 334             aliases.clear();
 335             for (int j = 0; j < aliasCount; j++) {
 336                 String alias = regionArray[dis.readShort()];
 337                 String region = regionArray[dis.readShort()];
 338                 aliases.put(alias, region);
 339             }
 340         }
 341         // old us time-zone names
 342         addOldMapping();
 343         aliases = Collections.unmodifiableMap(aliases);
 344     }
 345 
 346     /////////////////////////Ser/////////////////////////////////
 347     public static ZoneInfo getZoneInfo(DataInput in, String zoneId) throws Exception {
 348         byte type = in.readByte();
 349         // TBD: assert ZRULES:
 350         int stdSize = in.readInt();
 351         long[] stdTrans = new long[stdSize];
 352         for (int i = 0; i < stdSize; i++) {
 353             stdTrans[i] = readEpochSec(in);
 354         }
 355         int [] stdOffsets = new int[stdSize + 1];
 356         for (int i = 0; i < stdOffsets.length; i++) {
 357             stdOffsets[i] = readOffset(in);
 358         }
 359         int savSize = in.readInt();
 360         long[] savTrans = new long[savSize];
 361         for (int i = 0; i < savSize; i++) {
 362             savTrans[i] = readEpochSec(in);
 363         }
 364         int[] savOffsets = new int[savSize + 1];
 365         for (int i = 0; i < savOffsets.length; i++) {
 366             savOffsets[i] = readOffset(in);
 367         }
 368         int ruleSize = in.readByte();
 369         ZoneOffsetTransitionRule[] rules = new ZoneOffsetTransitionRule[ruleSize];
 370         for (int i = 0; i < ruleSize; i++) {
 371             rules[i] = readZOTRule(in);
 372         }
 373         return getZoneInfo(zoneId, stdTrans, stdOffsets, savTrans, savOffsets, rules);
 374     }
 375 
 376     public static int readOffset(DataInput in) throws IOException {
 377         int offsetByte = in.readByte();
 378         return offsetByte == 127 ? in.readInt() : offsetByte * 900;
 379     }
 380 
 381     static long readEpochSec(DataInput in) throws IOException {
 382         int hiByte = in.readByte() & 255;
 383         if (hiByte == 255) {
 384             return in.readLong();
 385         } else {
 386             int midByte = in.readByte() & 255;
 387             int loByte = in.readByte() & 255;
 388             long tot = ((hiByte << 16) + (midByte << 8) + loByte);
 389             return (tot * 900) - 4575744000L;
 390         }
 391     }
 392 
 393     static ZoneOffsetTransitionRule readZOTRule(DataInput in) throws IOException {
 394         int data = in.readInt();
 395         Month month = Month.of(data >>> 28);
 396         int dom = ((data & (63 << 22)) >>> 22) - 32;
 397         int dowByte = (data & (7 << 19)) >>> 19;
 398         DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte);
 399         int timeByte = (data & (31 << 14)) >>> 14;
 400         TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12];
 401         int stdByte = (data & (255 << 4)) >>> 4;
 402         int beforeByte = (data & (3 << 2)) >>> 2;
 403         int afterByte = (data & 3);
 404         LocalTime time = (timeByte == 31 ? LocalTime.ofSecondOfDay(in.readInt()) : LocalTime.of(timeByte % 24, 0));
 405         ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900));
 406         ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800));
 407         ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800));
 408         return ZoneOffsetTransitionRule.of(month, dom, dow, time, timeByte == 24, defn, std, before, after);
 409     }
 410 
 411     /////////////////////////ZoneRules --> ZoneInfo/////////////////////////////////
 412 
 413     // ZoneInfo starts with UTC1900
 414     private static final long UTC1900 = -2208988800L;
 415     // ZoneInfo ends with   UTC2037
 416     private static final long UTC2037 =
 417         LocalDateTime.of(2038, 1, 1, 0, 0, 0).toEpochSecond(ZoneOffset.UTC) - 1;
 418 
 419     /* Get a ZoneInfo instance.
 420      *
 421      * @param standardTransitions  the standard transitions, not null
 422      * @param standardOffsets  the standard offsets, not null
 423      * @param savingsInstantTransitions  the standard transitions, not null
 424      * @param wallOffsets  the wall offsets, not null
 425      * @param lastRules  the recurring last rules, size 15 or less, not null
 426      */
 427     private static ZoneInfo getZoneInfo(String zoneId,
 428                                         long[] standardTransitions,
 429                                         int[] standardOffsets,
 430                                         long[] savingsInstantTransitions,
 431                                         int[] wallOffsets,
 432                                         ZoneOffsetTransitionRule[] lastRules) {
 433         int rawOffset = 0;
 434         int dstSavings = 0;
 435         int checksum = 0;
 436         int[] params = null;
 437         boolean willGMTOffsetChange = false;
 438 
 439         // rawOffset, pick the last one
 440         if (standardTransitions.length > 0)
 441             rawOffset = standardOffsets[standardOffsets.length - 1] * 1000;
 442         else
 443             rawOffset = standardOffsets[0] * 1000;
 444 
 445         // transitions, offsets;
 446         long[] transitions = null;
 447         int[]  offsets = null;
 448         int    nOffsets = 0;
 449         int    nTrans = 0;
 450 
 451         if (savingsInstantTransitions.length != 0) {
 452             transitions = new long[250];
 453             offsets = new int[100];    // TBD: ZoneInfo actually can't handle
 454                                        // offsets.length > 16 (4-bit index limit)
 455             // last year in trans table
 456             // It should not matter to use before or after offset for year
 457             int lastyear = LocalDateTime.ofEpochSecond(
 458                 savingsInstantTransitions[savingsInstantTransitions.length - 1], 0,
 459                 ZoneOffset.ofTotalSeconds(wallOffsets[savingsInstantTransitions.length - 1])).getYear();
 460             // int lastyear = savingsLocalTransitions[savingsLocalTransitions.length - 1].getYear();
 461 
 462             int i = 0, k = 1;
 463             while (i < savingsInstantTransitions.length &&
 464                    savingsInstantTransitions[i] < UTC1900) {
 465                  i++;     // skip any date before UTC1900
 466             }
 467             if (i < savingsInstantTransitions.length) {
 468                 // javazic writes the last GMT offset into index 0!
 469                 if (i < savingsInstantTransitions.length) {
 470                     offsets[0] = standardOffsets[standardOffsets.length - 1] * 1000;
 471                     nOffsets = 1;
 472                 }
 473                 // ZoneInfo has a beginning entry for 1900.
 474                 // Only add it if this is not the only one in table
 475                 nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets,
 476                                     UTC1900,
 477                                     wallOffsets[i],
 478                                     getStandardOffset(standardTransitions, standardOffsets, UTC1900));
 479             }
 480             for (; i < savingsInstantTransitions.length; i++) {
 481                 //if (savingsLocalTransitions[i * 2].getYear() > LASTYEAR) {
 482                 if (savingsInstantTransitions[i] > UTC2037) {
 483                     // no trans beyond LASTYEAR
 484                     lastyear = LASTYEAR;
 485                     break;
 486                 }
 487                 long trans = savingsInstantTransitions[i];
 488                 while (k < standardTransitions.length) {
 489                     // some standard offset transitions don't exist in
 490                     // savingInstantTrans, if the offset "change" doesn't
 491                     // really change the "effectiveWallOffset". For example
 492                     // the 1999/2000 pair in Zone Arg/Buenos_Aires, in which
 493                     // the daylightsaving "happened" but it actually does
 494                     //  not result in the timezone switch. ZoneInfo however
 495                     // needs them in its transitions table
 496                     long trans_s = standardTransitions[k];
 497                     if (trans_s >= UTC1900) {
 498                         if (trans_s > trans)
 499                             break;
 500                         if (trans_s < trans) {
 501                             if (nOffsets + 2 >= offsets.length) {
 502                                 offsets = Arrays.copyOf(offsets, offsets.length + 100);
 503                             }
 504                             if (nTrans + 1 >= transitions.length) {
 505                                 transitions = Arrays.copyOf(transitions, transitions.length + 100);
 506                             }
 507                             nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets,
 508                                                 trans_s,
 509                                                 wallOffsets[i],
 510                                                 standardOffsets[k+1]);
 511                         }
 512                     }
 513                     k++;
 514                 }
 515                 if (nOffsets + 2 >= offsets.length) {
 516                     offsets = Arrays.copyOf(offsets, offsets.length + 100);
 517                 }
 518                 if (nTrans + 1 >= transitions.length) {
 519                     transitions = Arrays.copyOf(transitions, transitions.length + 100);
 520                 }
 521                 nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets,
 522                                     trans,
 523                                     wallOffsets[i + 1],
 524                                     getStandardOffset(standardTransitions, standardOffsets, trans));
 525             }
 526             // append any leftover standard trans
 527             while (k < standardTransitions.length) {
 528                 long trans = standardTransitions[k];
 529                 if (trans >= UTC1900) {
 530                     int offset = wallOffsets[i];
 531                     int offsetIndex = indexOf(offsets, 0, nOffsets, offset);
 532                     if (offsetIndex == nOffsets)
 533                         nOffsets++;
 534                     transitions[nTrans++] = ((trans * 1000) << TRANSITION_NSHIFT) |
 535                                             (offsetIndex & OFFSET_MASK);
 536                 }
 537                 k++;
 538             }
 539             if (lastRules.length > 1) {
 540                 // fill the gap between the last trans until LASTYEAR
 541                 while (lastyear++ < LASTYEAR) {
 542                     for (ZoneOffsetTransitionRule zotr : lastRules) {
 543                         ZoneOffsetTransition zot = zotr.createTransition(lastyear);
 544                         //long trans = zot.getDateTimeBefore().toEpochSecond();
 545                         long trans = zot.toEpochSecond();
 546                         if (nOffsets + 2 >= offsets.length) {
 547                             offsets = Arrays.copyOf(offsets, offsets.length + 100);
 548                         }
 549                         if (nTrans + 1 >= transitions.length) {
 550                             transitions = Arrays.copyOf(transitions, transitions.length + 100);
 551                         }
 552                         nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets,
 553                                             trans,
 554                                             zot.getOffsetAfter().getTotalSeconds(),
 555                                             getStandardOffset(standardTransitions, standardOffsets, trans));
 556                     }
 557                 }
 558                 ZoneOffsetTransitionRule startRule =  lastRules[lastRules.length - 2];
 559                 ZoneOffsetTransitionRule endRule =  lastRules[lastRules.length - 1];
 560                 params = new int[10];
 561                 if (startRule.getOffsetBefore().compareTo(startRule.getOffsetAfter()) < 0 &&
 562                     endRule.getOffsetBefore().compareTo(endRule.getOffsetAfter()) > 0) {
 563                     ZoneOffsetTransitionRule tmp;
 564                     tmp = startRule;
 565                     startRule = endRule;
 566                     endRule = tmp;
 567                 }
 568                 params[0] = startRule.getMonth().getValue() - 1;
 569                 // params[1] = startRule.getDayOfMonthIndicator();
 570                 // params[2] = toCalendarDOW[startRule.getDayOfWeek().getValue()];
 571                 int       dom = startRule.getDayOfMonthIndicator();
 572                 DayOfWeek dow = startRule.getDayOfWeek();
 573                 if (dow == null) {
 574                     params[1] = startRule.getDayOfMonthIndicator();
 575                     params[2] = 0;
 576                 } else {
 577                     // ZoneRulesBuilder adjusts < 0 case (-1, for last, don't have
 578                     // "<=" case yet) to positive value if not February (it appears
 579                     // we don't have February cutoff in tzdata table yet)
 580                     // Ideally, if JSR310 can just pass in the nagative and
 581                     // we can then pass in the dom = -1, dow > 0 into ZoneInfo
 582                     //
 583                     // hacking, assume the >=24 is the result of ZRB optimization for
 584                     // "last", it works for now.
 585                     if (dom < 0 || dom >= 24) {
 586                         params[1] = -1;
 587                         params[2] = toCalendarDOW[dow.getValue()];
 588                     } else {
 589                         params[1] = dom;
 590                         // To specify a day of week on or after an exact day of month,
 591                         // set the month to an exact month value, day-of-month to the
 592                         // day on or after which the rule is applied, and day-of-week
 593                         // to a negative Calendar.DAY_OF_WEEK DAY_OF_WEEK field value.
 594                         params[2] = -toCalendarDOW[dow.getValue()];
 595                     }
 596                 }
 597                 params[3] = startRule.getLocalTime().toSecondOfDay() * 1000;
 598                 params[4] = toSTZTime[startRule.getTimeDefinition().ordinal()];
 599 
 600                 params[5] = endRule.getMonth().getValue() - 1;
 601                 // params[6] = endRule.getDayOfMonthIndicator();
 602                 // params[7] = toCalendarDOW[endRule.getDayOfWeek().getValue()];
 603                 dom = endRule.getDayOfMonthIndicator();
 604                 dow = endRule.getDayOfWeek();
 605                 if (dow == null) {
 606                     params[6] = dom;
 607                     params[7] = 0;
 608                 } else {
 609                     // hacking: see comment above
 610                     if (dom < 0 || dom >= 24) {
 611                         params[6] = -1;
 612                         params[7] = toCalendarDOW[dow.getValue()];
 613                     } else {
 614                         params[6] = dom;
 615                         params[7] = -toCalendarDOW[dow.getValue()];
 616                     }
 617                 }
 618                 params[8] = endRule.getLocalTime().toSecondOfDay() * 1000;
 619                 params[9] = toSTZTime[endRule.getTimeDefinition().ordinal()];
 620                 dstSavings = (startRule.getOffsetAfter().getTotalSeconds()
 621                              - startRule.getOffsetBefore().getTotalSeconds()) * 1000;
 622                 // Note: known mismatching -> Asia/Amman
 623                 // ZoneInfo :      startDayOfWeek=5     <= Thursday
 624                 //                 startTime=86400000   <= 24 hours
 625                 // This:           startDayOfWeek=6
 626                 //                 startTime=0
 627                 // Below is the workaround, it probably slows down everyone a little
 628                 if (params[2] == 6 && params[3] == 0 && zoneId.equals("Asia/Amman")) {
 629                     params[2] = 5;
 630                     params[3] = 86400000;
 631                 }
 632             } else if (nTrans > 0) {  // only do this if there is something in table already
 633                 if (lastyear < LASTYEAR) {
 634                     // ZoneInfo has an ending entry for 2037
 635                     long trans = OffsetDateTime.of(LASTYEAR, Month.JANUARY.getValue(), 1, 0, 0, 0, 0,
 636                                                    ZoneOffset.ofTotalSeconds(rawOffset/1000))
 637                                                .toEpochSecond();
 638                     int offsetIndex = indexOf(offsets, 0, nOffsets, rawOffset/1000);
 639                     if (offsetIndex == nOffsets)
 640                         nOffsets++;
 641                     transitions[nTrans++] = (trans * 1000) << TRANSITION_NSHIFT |
 642                                        (offsetIndex & OFFSET_MASK);
 643                 } else if (savingsInstantTransitions.length > 2) {
 644                     // Workaround: create the params based on the last pair for
 645                     // zones like Israel and Iran which have trans defined
 646                     // up until 2037, but no "transition rule" defined
 647                     //
 648                     // Note: Known mismatching for Israel, Asia/Jerusalem/Tel Aviv
 649                     // ZoneInfo:        startMode=3
 650                     //                  startMonth=2
 651                     //                  startDay=26
 652                     //                  startDayOfWeek=6
 653                     //
 654                     // This:            startMode=1
 655                     //                  startMonth=2
 656                     //                  startDay=27
 657                     //                  startDayOfWeek=0
 658                     // these two are actually the same for 2037, the SimpleTimeZone
 659                     // for the last "known" year
 660                     int m = savingsInstantTransitions.length;
 661                     long startTrans = savingsInstantTransitions[m - 2];
 662                     int startOffset = wallOffsets[m - 2 + 1];
 663                     int startStd = getStandardOffset(standardTransitions, standardOffsets, startTrans);
 664                     long endTrans =  savingsInstantTransitions[m - 1];
 665                     int endOffset = wallOffsets[m - 1 + 1];
 666                     int endStd = getStandardOffset(standardTransitions, standardOffsets, endTrans);
 667 
 668                     if (startOffset > startStd && endOffset == endStd) {
 669                         /*
 670                         m = savingsLocalTransitions.length;
 671                         LocalDateTime startLDT = savingsLocalTransitions[m -4];  //gap
 672                         LocalDateTime endLDT = savingsLocalTransitions[m - 1];   //over
 673                          */
 674                         // last - 1 trans
 675                         m = savingsInstantTransitions.length - 2;
 676                         ZoneOffset before = ZoneOffset.ofTotalSeconds(wallOffsets[m]);
 677                         ZoneOffset after = ZoneOffset.ofTotalSeconds(wallOffsets[m + 1]);
 678                         ZoneOffsetTransition trans = ZoneOffsetTransition.of(
 679                             LocalDateTime.ofEpochSecond(savingsInstantTransitions[m], 0, before),
 680                             before,
 681                             after);
 682                         LocalDateTime startLDT;
 683                         if (trans.isGap()) {
 684                             startLDT = trans.getDateTimeBefore();
 685                         } else {
 686                             startLDT = trans.getDateTimeAfter();
 687                         }
 688                         // last trans
 689                         m = savingsInstantTransitions.length - 1;
 690                         before = ZoneOffset.ofTotalSeconds(wallOffsets[m]);
 691                         after = ZoneOffset.ofTotalSeconds(wallOffsets[m + 1]);
 692                         trans = ZoneOffsetTransition.of(
 693                             LocalDateTime.ofEpochSecond(savingsInstantTransitions[m], 0, before),
 694                             before,
 695                             after);
 696                         LocalDateTime endLDT;
 697                         if (trans.isGap()) {
 698                             endLDT = trans.getDateTimeAfter();
 699                         } else {
 700                             endLDT = trans.getDateTimeBefore();
 701                         }
 702                         params = new int[10];
 703                         params[0] = startLDT.getMonthValue() - 1;
 704                         params[1] = startLDT.getDayOfMonth();
 705                         params[2] = 0;
 706                         params[3] = startLDT.toLocalTime().toSecondOfDay() * 1000;
 707                         params[4] = SimpleTimeZone.WALL_TIME;
 708                         params[5] = endLDT.getMonthValue() - 1;
 709                         params[6] = endLDT.getDayOfMonth();
 710                         params[7] = 0;
 711                         params[8] = endLDT.toLocalTime().toSecondOfDay() * 1000;
 712                         params[9] = SimpleTimeZone.WALL_TIME;
 713                         dstSavings = (startOffset - startStd) * 1000;
 714                     }
 715                 }
 716             }
 717             if (transitions != null && transitions.length != nTrans) {
 718                 if (nTrans == 0) {
 719                    transitions = null;
 720                 } else {
 721                     transitions = Arrays.copyOf(transitions, nTrans);
 722                 }
 723             }
 724             if (offsets != null && offsets.length != nOffsets) {
 725                 if (nOffsets == 0) {
 726                    offsets = null;
 727                 } else {
 728                     offsets = Arrays.copyOf(offsets, nOffsets);
 729                 }
 730             }
 731             if (transitions != null) {
 732                 Checksum sum = new Checksum();
 733                 for (i = 0; i < transitions.length; i++) {
 734                     long val = transitions[i];
 735                     int dst = (int)((val >>> DST_NSHIFT) & 0xfL);
 736                     int saving = (dst == 0) ? 0 : offsets[dst];
 737                     int index = (int)(val & OFFSET_MASK);
 738                     int offset = offsets[index];
 739                     long second = (val >> TRANSITION_NSHIFT);
 740                     // javazic uses "index of the offset in offsets",
 741                     // instead of the real offset value itself to
 742                     // calculate the checksum. Have to keep doing
 743                     // the same thing, checksum is part of the
 744                     // ZoneInfo serialization form.
 745                     sum.update(second + index);
 746                     sum.update(index);
 747                     sum.update(dst == 0 ? -1 : dst);
 748                 }
 749                 checksum = (int)sum.getValue();
 750             }
 751         }
 752         return new ZoneInfo(zoneId, rawOffset, dstSavings, checksum, transitions,
 753                             offsets, params, willGMTOffsetChange);
 754     }
 755 
 756     private static int getStandardOffset(long[] standardTransitions,
 757                                          int[] standardOffsets,
 758                                          long epochSec) {
 759         int index  = Arrays.binarySearch(standardTransitions, epochSec);
 760         if (index < 0) {
 761             // switch negative insert position to start of matched range
 762             index = -index - 2;
 763         }
 764         return standardOffsets[index + 1];
 765     }
 766 
 767     private static int toCalendarDOW[] = new int[] {
 768         -1,
 769         Calendar.MONDAY,
 770         Calendar.TUESDAY,
 771         Calendar.WEDNESDAY,
 772         Calendar.THURSDAY,
 773         Calendar.FRIDAY,
 774         Calendar.SATURDAY,
 775         Calendar.SUNDAY
 776     };
 777 
 778     private static int toSTZTime[] = new int[] {
 779         SimpleTimeZone.UTC_TIME,
 780         SimpleTimeZone.WALL_TIME,
 781         SimpleTimeZone.STANDARD_TIME,
 782     };
 783 
 784     private static final long OFFSET_MASK = 0x0fL;
 785     private static final long DST_MASK = 0xf0L;
 786     private static final int  DST_NSHIFT = 4;
 787     private static final int  TRANSITION_NSHIFT = 12;
 788     private static final int  LASTYEAR = 2037;
 789 
 790     // from: 0 for offset lookup, 1 for dstsvings lookup
 791     private static int indexOf(int[] offsets, int from, int nOffsets, int offset) {
 792         offset *= 1000;
 793         for (; from < nOffsets; from++) {
 794             if (offsets[from] == offset)
 795                 return from;
 796         }
 797         offsets[from] = offset;
 798         return from;
 799     }
 800 
 801     // return updated nOffsets
 802     private static int addTrans(long transitions[], int nTrans,
 803                                 int offsets[], int nOffsets,
 804                                 long trans, int offset, int stdOffset) {
 805         int offsetIndex = indexOf(offsets, 0, nOffsets, offset);
 806         if (offsetIndex == nOffsets)
 807             nOffsets++;
 808         int dstIndex = 0;
 809         if (offset != stdOffset) {
 810             dstIndex = indexOf(offsets, 1, nOffsets, offset - stdOffset);
 811             if (dstIndex == nOffsets)
 812                 nOffsets++;
 813         }
 814         transitions[nTrans] = ((trans * 1000) << TRANSITION_NSHIFT) |
 815                               ((dstIndex << DST_NSHIFT) & DST_MASK) |
 816                               (offsetIndex & OFFSET_MASK);
 817         return nOffsets;
 818     }
 819 
 820     /////////////////////////////////////////////////////////////
 821     // ZoneInfo checksum, copy/pasted from javazic
 822     private static class Checksum extends CRC32 {
 823         public void update(int val) {
 824             byte[] b = new byte[4];
 825             b[0] = (byte)((val >>> 24) & 0xff);
 826             b[1] = (byte)((val >>> 16) & 0xff);
 827             b[2] = (byte)((val >>> 8) & 0xff);
 828             b[3] = (byte)(val & 0xff);
 829             update(b);
 830         }
 831         void update(long val) {
 832             byte[] b = new byte[8];
 833             b[0] = (byte)((val >>> 56) & 0xff);
 834             b[1] = (byte)((val >>> 48) & 0xff);
 835             b[2] = (byte)((val >>> 40) & 0xff);
 836             b[3] = (byte)((val >>> 32) & 0xff);
 837             b[4] = (byte)((val >>> 24) & 0xff);
 838             b[5] = (byte)((val >>> 16) & 0xff);
 839             b[6] = (byte)((val >>> 8) & 0xff);
 840             b[7] = (byte)(val & 0xff);
 841             update(b);
 842         }
 843     }
 844 }