Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/tzdb/TzdbZoneRulesCompiler.java
32287 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos27*28* All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions are met:32*33* * Redistributions of source code must retain the above copyright notice,34* this list of conditions and the following disclaimer.35*36* * Redistributions in binary form must reproduce the above copyright notice,37* this list of conditions and the following disclaimer in the documentation38* and/or other materials provided with the distribution.39*40* * Neither the name of JSR-310 nor the names of its contributors41* may be used to endorse or promote products derived from this software42* without specific prior written permission.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR48* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,49* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,50* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR51* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF52* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING53* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS54* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/56package build.tools.tzdb;5758import static build.tools.tzdb.Utils.*;5960import java.io.ByteArrayOutputStream;61import java.io.DataOutputStream;62import java.nio.charset.StandardCharsets;63import java.nio.file.Files;64import java.nio.file.Path;65import java.nio.file.Paths;66import java.text.ParsePosition;67import java.util.ArrayList;68import java.util.Arrays;69import java.util.HashMap;70import java.util.HashSet;71import java.util.List;72import java.util.Map;73import java.util.NoSuchElementException;74import java.util.Scanner;75import java.util.SortedMap;76import java.util.TreeMap;77import java.util.regex.Matcher;78import java.util.regex.MatchResult;79import java.util.regex.Pattern;8081/**82* A compiler that reads a set of TZDB time-zone files and builds a single83* combined TZDB data file.84*85* @since 1.886*/87public final class TzdbZoneRulesCompiler {8889public static void main(String[] args) {90new TzdbZoneRulesCompiler().compile(args);91}9293private void compile(String[] args) {94if (args.length < 2) {95outputHelp();96return;97}98Path srcDir = null;99Path dstFile = null;100String version = null;101// parse args/options102int i;103for (i = 0; i < args.length; i++) {104String arg = args[i];105if (!arg.startsWith("-")) {106break;107}108if ("-srcdir".equals(arg)) {109if (srcDir == null && ++i < args.length) {110srcDir = Paths.get(args[i]);111continue;112}113} else if ("-dstfile".equals(arg)) {114if (dstFile == null && ++i < args.length) {115dstFile = Paths.get(args[i]);116continue;117}118} else if ("-verbose".equals(arg)) {119if (!verbose) {120verbose = true;121continue;122}123} else if (!"-help".equals(arg)) {124System.out.println("Unrecognised option: " + arg);125}126outputHelp();127return;128}129// check source directory130if (srcDir == null) {131System.err.println("Source directory must be specified using -srcdir");132System.exit(1);133}134if (!Files.isDirectory(srcDir)) {135System.err.println("Source does not exist or is not a directory: " + srcDir);136System.exit(1);137}138// parse source file names139if (i == args.length) {140i = 0;141args = new String[] {"africa", "antarctica", "asia", "australasia", "europe",142"northamerica","southamerica", "backward", "etcetera" };143System.out.println("Source filenames not specified, using default set ( ");144for (String name : args) {145System.out.printf(name + " ");146}147System.out.println(")");148}149// source files in this directory150List<Path> srcFiles = new ArrayList<>();151for (; i < args.length; i++) {152Path file = srcDir.resolve(args[i]);153if (Files.exists(file)) {154srcFiles.add(file);155} else {156System.err.println("Source directory does not contain source file: " + args[i]);157System.exit(1);158}159}160// check destination file161if (dstFile == null) {162dstFile = srcDir.resolve("tzdb.dat");163} else {164Path parent = dstFile.getParent();165if (parent != null && !Files.exists(parent)) {166System.err.println("Destination directory does not exist: " + parent);167System.exit(1);168}169}170try {171// get tzdb source version172Matcher m = Pattern.compile("tzdata(?<ver>[0-9]{4}[A-z])")173.matcher(new String(Files.readAllBytes(srcDir.resolve("VERSION")),174"ISO-8859-1"));175if (m.find()) {176version = m.group("ver");177} else {178System.exit(1);179System.err.println("Source directory does not contain file: VERSION");180}181printVerbose("Compiling TZDB version " + version);182// parse source files183for (Path file : srcFiles) {184printVerbose("Parsing file: " + file);185parseFile(file);186}187// build zone rules188printVerbose("Building rules");189buildZoneRules();190// output to file191printVerbose("Outputting tzdb file: " + dstFile);192outputFile(dstFile, version, builtZones, links);193} catch (Exception ex) {194System.out.println("Failed: " + ex.toString());195ex.printStackTrace();196System.exit(1);197}198System.exit(0);199}200201/**202* Output usage text for the command line.203*/204private static void outputHelp() {205System.out.println("Usage: TzdbZoneRulesCompiler <options> <tzdb source filenames>");206System.out.println("where options include:");207System.out.println(" -srcdir <directory> Where to find tzdb source directory (required)");208System.out.println(" -dstfile <file> Where to output generated file (default srcdir/tzdb.dat)");209System.out.println(" -help Print this usage message");210System.out.println(" -verbose Output verbose information during compilation");211System.out.println(" The source directory must contain the unpacked tzdb files, such as asia or europe");212}213214/**215* Outputs the file.216*/217private void outputFile(Path dstFile, String version,218SortedMap<String, ZoneRules> builtZones,219Map<String, String> links) {220try (DataOutputStream out = new DataOutputStream(Files.newOutputStream(dstFile))) {221// file version222out.writeByte(1);223// group224out.writeUTF("TZDB");225// versions226out.writeShort(1);227out.writeUTF(version);228// regions229String[] regionArray = builtZones.keySet().toArray(new String[builtZones.size()]);230out.writeShort(regionArray.length);231for (String regionId : regionArray) {232out.writeUTF(regionId);233}234// rules -- hashset -> remove the dup235List<ZoneRules> rulesList = new ArrayList<>(new HashSet<>(builtZones.values()));236out.writeShort(rulesList.size());237ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);238for (ZoneRules rules : rulesList) {239baos.reset();240DataOutputStream dataos = new DataOutputStream(baos);241rules.writeExternal(dataos);242dataos.close();243byte[] bytes = baos.toByteArray();244out.writeShort(bytes.length);245out.write(bytes);246}247// link version-region-rules248out.writeShort(builtZones.size());249for (Map.Entry<String, ZoneRules> entry : builtZones.entrySet()) {250int regionIndex = Arrays.binarySearch(regionArray, entry.getKey());251int rulesIndex = rulesList.indexOf(entry.getValue());252out.writeShort(regionIndex);253out.writeShort(rulesIndex);254}255// alias-region256out.writeShort(links.size());257for (Map.Entry<String, String> entry : links.entrySet()) {258int aliasIndex = Arrays.binarySearch(regionArray, entry.getKey());259int regionIndex = Arrays.binarySearch(regionArray, entry.getValue());260out.writeShort(aliasIndex);261out.writeShort(regionIndex);262}263out.flush();264} catch (Exception ex) {265System.out.println("Failed: " + ex.toString());266ex.printStackTrace();267System.exit(1);268}269}270271private static final Pattern YEAR = Pattern.compile("(?i)(?<min>min)|(?<max>max)|(?<only>only)|(?<year>[0-9]+)");272private static final Pattern MONTH = Pattern.compile("(?i)(jan)|(feb)|(mar)|(apr)|(may)|(jun)|(jul)|(aug)|(sep)|(oct)|(nov)|(dec)");273private static final Matcher DOW = Pattern.compile("(?i)(mon)|(tue)|(wed)|(thu)|(fri)|(sat)|(sun)").matcher("");274private static final Matcher TIME = Pattern.compile("(?<neg>-)?+(?<hour>[0-9]{1,2})(:(?<minute>[0-5][0-9]))?+(:(?<second>[0-5][0-9]))?+").matcher("");275276/** The TZDB rules. */277private final Map<String, List<TZDBRule>> rules = new HashMap<>();278279/** The TZDB zones. */280private final Map<String, List<TZDBZone>> zones = new HashMap<>();281282/** The TZDB links. */283private final Map<String, String> links = new HashMap<>();284285/** The built zones. */286private final SortedMap<String, ZoneRules> builtZones = new TreeMap<>();287288/** Whether to output verbose messages. */289private boolean verbose;290291/**292* private contructor293*/294private TzdbZoneRulesCompiler() {295}296297/**298* Parses a source file.299*300* @param file the file being read, not null301* @throws Exception if an error occurs302*/303private void parseFile(Path file) throws Exception {304int lineNumber = 1;305String line = null;306try {307List<String> lines = Files.readAllLines(file, StandardCharsets.ISO_8859_1);308List<TZDBZone> openZone = null;309for (; lineNumber < lines.size(); lineNumber++) {310line = lines.get(lineNumber);311int index = line.indexOf('#'); // remove comments (doesn't handle # in quotes)312if (index >= 0) {313line = line.substring(0, index);314}315if (line.trim().length() == 0) { // ignore blank lines316continue;317}318Scanner s = new Scanner(line);319if (openZone != null && Character.isWhitespace(line.charAt(0)) && s.hasNext()) {320if (parseZoneLine(s, openZone)) {321openZone = null;322}323} else {324if (s.hasNext()) {325String first = s.next();326if (first.equals("Zone")) {327openZone = new ArrayList<>();328try {329zones.put(s.next(), openZone);330if (parseZoneLine(s, openZone)) {331openZone = null;332}333} catch (NoSuchElementException x) {334printVerbose("Invalid Zone line in file: " + file + ", line: " + line);335throw new IllegalArgumentException("Invalid Zone line");336}337} else {338openZone = null;339if (first.equals("Rule")) {340try {341parseRuleLine(s);342} catch (NoSuchElementException x) {343printVerbose("Invalid Rule line in file: " + file + ", line: " + line);344throw new IllegalArgumentException("Invalid Rule line");345}346} else if (first.equals("Link")) {347try {348String realId = s.next();349String aliasId = s.next();350links.put(aliasId, realId);351} catch (NoSuchElementException x) {352printVerbose("Invalid Link line in file: " + file + ", line: " + line);353throw new IllegalArgumentException("Invalid Link line");354}355356} else {357throw new IllegalArgumentException("Unknown line");358}359}360}361}362}363} catch (Exception ex) {364throw new Exception("Failed while parsing file '" + file + "' on line " + lineNumber + " '" + line + "'", ex);365}366}367368/**369* Parses a Rule line.370*371* @param s the line scanner, not null372*/373private void parseRuleLine(Scanner s) {374TZDBRule rule = new TZDBRule();375String name = s.next();376if (rules.containsKey(name) == false) {377rules.put(name, new ArrayList<TZDBRule>());378}379rules.get(name).add(rule);380rule.startYear = parseYear(s, 0);381rule.endYear = parseYear(s, rule.startYear);382if (rule.startYear > rule.endYear) {383throw new IllegalArgumentException("Year order invalid: " + rule.startYear + " > " + rule.endYear);384}385parseOptional(s.next()); // type is unused386parseMonthDayTime(s, rule);387rule.savingsAmount = parsePeriod(s.next());388rule.text = parseOptional(s.next());389}390391/**392* Parses a Zone line.393*394* @param s the line scanner, not null395* @return true if the zone is complete396*/397private boolean parseZoneLine(Scanner s, List<TZDBZone> zoneList) {398TZDBZone zone = new TZDBZone();399zoneList.add(zone);400zone.standardOffset = parseOffset(s.next());401String savingsRule = parseOptional(s.next());402if (savingsRule == null) {403zone.fixedSavingsSecs = 0;404zone.savingsRule = null;405} else {406try {407zone.fixedSavingsSecs = parsePeriod(savingsRule);408zone.savingsRule = null;409} catch (Exception ex) {410zone.fixedSavingsSecs = null;411zone.savingsRule = savingsRule;412}413}414zone.text = s.next();415if (s.hasNext()) {416zone.year = Integer.parseInt(s.next());417if (s.hasNext()) {418parseMonthDayTime(s, zone);419}420return false;421} else {422return true;423}424}425426/**427* Parses a Rule line.428*429* @param s the line scanner, not null430* @param mdt the object to parse into, not null431*/432private void parseMonthDayTime(Scanner s, TZDBMonthDayTime mdt) {433mdt.month = parseMonth(s);434if (s.hasNext()) {435String dayRule = s.next();436if (dayRule.startsWith("last")) {437mdt.dayOfMonth = -1;438mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(4));439mdt.adjustForwards = false;440} else {441int index = dayRule.indexOf(">=");442if (index > 0) {443mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(0, index));444dayRule = dayRule.substring(index + 2);445} else {446index = dayRule.indexOf("<=");447if (index > 0) {448mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(0, index));449mdt.adjustForwards = false;450dayRule = dayRule.substring(index + 2);451}452}453mdt.dayOfMonth = Integer.parseInt(dayRule);454}455if (s.hasNext()) {456String timeStr = s.next();457int secsOfDay = parseSecs(timeStr);458if (secsOfDay == 86400) {459mdt.endOfDay = true;460secsOfDay = 0;461}462LocalTime time = LocalTime.ofSecondOfDay(secsOfDay);463mdt.time = time;464mdt.timeDefinition = parseTimeDefinition(timeStr.charAt(timeStr.length() - 1));465}466}467}468469private int parseYear(Scanner s, int defaultYear) {470if (s.hasNext(YEAR)) {471s.next(YEAR);472MatchResult mr = s.match();473if (mr.group(1) != null) {474return 1900; // systemv has min475} else if (mr.group(2) != null) {476return YEAR_MAX_VALUE;477} else if (mr.group(3) != null) {478return defaultYear;479}480return Integer.parseInt(mr.group(4));481/*482if (mr.group("min") != null) {483//return YEAR_MIN_VALUE;484return 1900; // systemv has min485} else if (mr.group("max") != null) {486return YEAR_MAX_VALUE;487} else if (mr.group("only") != null) {488return defaultYear;489}490return Integer.parseInt(mr.group("year"));491*/492}493throw new IllegalArgumentException("Unknown year: " + s.next());494}495496private int parseMonth(Scanner s) {497if (s.hasNext(MONTH)) {498s.next(MONTH);499for (int moy = 1; moy < 13; moy++) {500if (s.match().group(moy) != null) {501return moy;502}503}504}505throw new IllegalArgumentException("Unknown month: " + s.next());506}507508private int parseDayOfWeek(String str) {509if (DOW.reset(str).matches()) {510for (int dow = 1; dow < 8; dow++) {511if (DOW.group(dow) != null) {512return dow;513}514}515}516throw new IllegalArgumentException("Unknown day-of-week: " + str);517}518519private String parseOptional(String str) {520return str.equals("-") ? null : str;521}522523private int parseSecs(String str) {524if (str.equals("-")) {525return 0;526}527try {528if (TIME.reset(str).find()) {529int secs = Integer.parseInt(TIME.group("hour")) * 60 * 60;530if (TIME.group("minute") != null) {531secs += Integer.parseInt(TIME.group("minute")) * 60;532}533if (TIME.group("second") != null) {534secs += Integer.parseInt(TIME.group("second"));535}536if (TIME.group("neg") != null) {537secs = -secs;538}539return secs;540}541} catch (NumberFormatException x) {}542throw new IllegalArgumentException(str);543}544545private ZoneOffset parseOffset(String str) {546int secs = parseSecs(str);547return ZoneOffset.ofTotalSeconds(secs);548}549550private int parsePeriod(String str) {551return parseSecs(str);552}553554private TimeDefinition parseTimeDefinition(char c) {555switch (c) {556case 's':557case 'S':558// standard time559return TimeDefinition.STANDARD;560case 'u':561case 'U':562case 'g':563case 'G':564case 'z':565case 'Z':566// UTC567return TimeDefinition.UTC;568case 'w':569case 'W':570default:571// wall time572return TimeDefinition.WALL;573}574}575576/**577* Build the rules, zones and links into real zones.578*579* @throws Exception if an error occurs580*/581private void buildZoneRules() throws Exception {582// build zones583for (String zoneId : zones.keySet()) {584printVerbose("Building zone " + zoneId);585List<TZDBZone> tzdbZones = zones.get(zoneId);586ZoneRulesBuilder bld = new ZoneRulesBuilder();587for (TZDBZone tzdbZone : tzdbZones) {588bld = tzdbZone.addToBuilder(bld, rules);589}590builtZones.put(zoneId, bld.toRules(zoneId));591}592593// build aliases594for (String aliasId : links.keySet()) {595String realId = links.get(aliasId);596printVerbose("Linking alias " + aliasId + " to " + realId);597ZoneRules realRules = builtZones.get(realId);598if (realRules == null) {599realId = links.get(realId); // try again (handle alias liked to alias)600printVerbose("Relinking alias " + aliasId + " to " + realId);601realRules = builtZones.get(realId);602if (realRules == null) {603throw new IllegalArgumentException("Alias '" + aliasId + "' links to invalid zone '" + realId);604}605links.put(aliasId, realId);606}607builtZones.put(aliasId, realRules);608}609// remove UTC and GMT610// builtZones.remove("UTC");611// builtZones.remove("GMT");612// builtZones.remove("GMT0");613builtZones.remove("GMT+0");614builtZones.remove("GMT-0");615links.remove("GMT+0");616links.remove("GMT-0");617// remove ROC, which is not supported in j.u.tz618builtZones.remove("ROC");619links.remove("ROC");620// remove EST, HST and MST. They are supported via621// the short-id mapping622builtZones.remove("EST");623builtZones.remove("HST");624builtZones.remove("MST");625}626627/**628* Prints a verbose message.629*630* @param message the message, not null631*/632private void printVerbose(String message) {633if (verbose) {634System.out.println(message);635}636}637638/**639* Class representing a month-day-time in the TZDB file.640*/641abstract class TZDBMonthDayTime {642/** The month of the cutover. */643int month = 1;644/** The day-of-month of the cutover. */645int dayOfMonth = 1;646/** Whether to adjust forwards. */647boolean adjustForwards = true;648/** The day-of-week of the cutover. */649int dayOfWeek = -1;650/** The time of the cutover. */651LocalTime time = LocalTime.MIDNIGHT;652/** Whether this is midnight end of day. */653boolean endOfDay;654/** The time of the cutover. */655TimeDefinition timeDefinition = TimeDefinition.WALL;656void adjustToFowards(int year) {657if (adjustForwards == false && dayOfMonth > 0) {658LocalDate adjustedDate = LocalDate.of(year, month, dayOfMonth).minusDays(6);659dayOfMonth = adjustedDate.getDayOfMonth();660month = adjustedDate.getMonth();661adjustForwards = true;662}663}664}665666/**667* Class representing a rule line in the TZDB file.668*/669final class TZDBRule extends TZDBMonthDayTime {670/** The start year. */671int startYear;672/** The end year. */673int endYear;674/** The amount of savings. */675int savingsAmount;676/** The text name of the zone. */677String text;678679void addToBuilder(ZoneRulesBuilder bld) {680adjustToFowards(2004); // irrelevant, treat as leap year681bld.addRuleToWindow(startYear, endYear, month, dayOfMonth, dayOfWeek, time, endOfDay, timeDefinition, savingsAmount);682}683}684685/**686* Class representing a linked set of zone lines in the TZDB file.687*/688final class TZDBZone extends TZDBMonthDayTime {689/** The standard offset. */690ZoneOffset standardOffset;691/** The fixed savings amount. */692Integer fixedSavingsSecs;693/** The savings rule. */694String savingsRule;695/** The text name of the zone. */696String text;697/** The year of the cutover. */698int year = YEAR_MAX_VALUE;699700ZoneRulesBuilder addToBuilder(ZoneRulesBuilder bld, Map<String, List<TZDBRule>> rules) {701if (year != YEAR_MAX_VALUE) {702bld.addWindow(standardOffset, toDateTime(year), timeDefinition);703} else {704bld.addWindowForever(standardOffset);705}706if (fixedSavingsSecs != null) {707bld.setFixedSavingsToWindow(fixedSavingsSecs);708} else {709List<TZDBRule> tzdbRules = rules.get(savingsRule);710if (tzdbRules == null) {711throw new IllegalArgumentException("Rule not found: " + savingsRule);712}713for (TZDBRule tzdbRule : tzdbRules) {714tzdbRule.addToBuilder(bld);715}716}717return bld;718}719720private LocalDateTime toDateTime(int year) {721adjustToFowards(year);722LocalDate date;723if (dayOfMonth == -1) {724dayOfMonth = lengthOfMonth(month, isLeapYear(year));725date = LocalDate.of(year, month, dayOfMonth);726if (dayOfWeek != -1) {727date = previousOrSame(date, dayOfWeek);728}729} else {730date = LocalDate.of(year, month, dayOfMonth);731if (dayOfWeek != -1) {732date = nextOrSame(date, dayOfWeek);733}734}735LocalDateTime ldt = LocalDateTime.of(date, time);736if (endOfDay) {737ldt = ldt.plusDays(1);738}739return ldt;740}741}742}743744745