2013-01-22 20:59:21 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of JSR-310 nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
package build.tools.tzdb;
|
|
|
|
|
|
|
|
import static build.tools.tzdb.Utils.*;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.DataOutputStream;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.text.ParsePosition;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
import java.util.Scanner;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.util.SortedMap;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
import java.util.regex.Matcher;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.util.regex.MatchResult;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
/**
|
2013-04-12 07:57:35 -07:00
|
|
|
* A compiler that reads a set of TZDB time-zone files and builds a single
|
|
|
|
* combined TZDB data file.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @since 1.8
|
|
|
|
*/
|
|
|
|
public final class TzdbZoneRulesCompiler {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2013-04-12 07:57:35 -07:00
|
|
|
new TzdbZoneRulesCompiler().compile(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void compile(String[] args) {
|
2013-01-22 20:59:21 -08:00
|
|
|
if (args.length < 2) {
|
|
|
|
outputHelp();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
Path srcDir = null;
|
|
|
|
Path dstFile = null;
|
2013-01-22 20:59:21 -08:00
|
|
|
String version = null;
|
2013-04-12 07:57:35 -07:00
|
|
|
// parse args/options
|
2013-01-22 20:59:21 -08:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < args.length; i++) {
|
|
|
|
String arg = args[i];
|
2013-04-12 07:57:35 -07:00
|
|
|
if (!arg.startsWith("-")) {
|
2013-01-22 20:59:21 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ("-srcdir".equals(arg)) {
|
2013-04-12 07:57:35 -07:00
|
|
|
if (srcDir == null && ++i < args.length) {
|
|
|
|
srcDir = Paths.get(args[i]);
|
2013-01-22 20:59:21 -08:00
|
|
|
continue;
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
} else if ("-dstfile".equals(arg)) {
|
|
|
|
if (dstFile == null && ++i < args.length) {
|
|
|
|
dstFile = Paths.get(args[i]);
|
2013-01-22 20:59:21 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if ("-verbose".equals(arg)) {
|
2013-04-12 07:57:35 -07:00
|
|
|
if (!verbose) {
|
2013-01-22 20:59:21 -08:00
|
|
|
verbose = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
} else if (!"-help".equals(arg)) {
|
2013-01-22 20:59:21 -08:00
|
|
|
System.out.println("Unrecognised option: " + arg);
|
|
|
|
}
|
|
|
|
outputHelp();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// check source directory
|
2013-04-12 07:57:35 -07:00
|
|
|
if (srcDir == null) {
|
|
|
|
System.err.println("Source directory must be specified using -srcdir");
|
|
|
|
System.exit(1);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
if (!Files.isDirectory(srcDir)) {
|
|
|
|
System.err.println("Source does not exist or is not a directory: " + srcDir);
|
|
|
|
System.exit(1);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
// parse source file names
|
2013-04-12 07:57:35 -07:00
|
|
|
if (i == args.length) {
|
|
|
|
i = 0;
|
|
|
|
args = new String[] {"africa", "antarctica", "asia", "australasia", "europe",
|
|
|
|
"northamerica","southamerica", "backward", "etcetera" };
|
|
|
|
System.out.println("Source filenames not specified, using default set ( ");
|
|
|
|
for (String name : args) {
|
|
|
|
System.out.printf(name + " ");
|
|
|
|
}
|
|
|
|
System.out.println(")");
|
|
|
|
}
|
|
|
|
// source files in this directory
|
|
|
|
List<Path> srcFiles = new ArrayList<>();
|
|
|
|
for (; i < args.length; i++) {
|
|
|
|
Path file = srcDir.resolve(args[i]);
|
|
|
|
if (Files.exists(file)) {
|
|
|
|
srcFiles.add(file);
|
|
|
|
} else {
|
|
|
|
System.err.println("Source directory does not contain source file: " + args[i]);
|
|
|
|
System.exit(1);
|
|
|
|
}
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
// check destination file
|
|
|
|
if (dstFile == null) {
|
|
|
|
dstFile = srcDir.resolve("tzdb.dat");
|
2013-01-22 20:59:21 -08:00
|
|
|
} else {
|
2013-04-12 07:57:35 -07:00
|
|
|
Path parent = dstFile.getParent();
|
|
|
|
if (parent != null && !Files.exists(parent)) {
|
|
|
|
System.err.println("Destination directory does not exist: " + parent);
|
|
|
|
System.exit(1);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
try {
|
|
|
|
// get tzdb source version
|
|
|
|
Matcher m = Pattern.compile("tzdata(?<ver>[0-9]{4}[A-z])")
|
|
|
|
.matcher(new String(Files.readAllBytes(srcDir.resolve("VERSION")),
|
|
|
|
"ISO-8859-1"));
|
|
|
|
if (m.find()) {
|
|
|
|
version = m.group("ver");
|
|
|
|
} else {
|
|
|
|
System.exit(1);
|
|
|
|
System.err.println("Source directory does not contain file: VERSION");
|
|
|
|
}
|
|
|
|
printVerbose("Compiling TZDB version " + version);
|
|
|
|
// parse source files
|
|
|
|
for (Path file : srcFiles) {
|
|
|
|
printVerbose("Parsing file: " + file);
|
|
|
|
parseFile(file);
|
|
|
|
}
|
|
|
|
// build zone rules
|
|
|
|
printVerbose("Building rules");
|
|
|
|
buildZoneRules();
|
|
|
|
// output to file
|
|
|
|
printVerbose("Outputting tzdb file: " + dstFile);
|
|
|
|
outputFile(dstFile, version, builtZones, links);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
System.out.println("Failed: " + ex.toString());
|
|
|
|
ex.printStackTrace();
|
|
|
|
System.exit(1);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output usage text for the command line.
|
|
|
|
*/
|
|
|
|
private static void outputHelp() {
|
|
|
|
System.out.println("Usage: TzdbZoneRulesCompiler <options> <tzdb source filenames>");
|
|
|
|
System.out.println("where options include:");
|
2013-04-12 07:57:35 -07:00
|
|
|
System.out.println(" -srcdir <directory> Where to find tzdb source directory (required)");
|
|
|
|
System.out.println(" -dstfile <file> Where to output generated file (default srcdir/tzdb.dat)");
|
2013-01-22 20:59:21 -08:00
|
|
|
System.out.println(" -help Print this usage message");
|
|
|
|
System.out.println(" -verbose Output verbose information during compilation");
|
2013-04-12 07:57:35 -07:00
|
|
|
System.out.println(" The source directory must contain the unpacked tzdb files, such as asia or europe");
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the file.
|
|
|
|
*/
|
2013-04-12 07:57:35 -07:00
|
|
|
private void outputFile(Path dstFile, String version,
|
|
|
|
SortedMap<String, ZoneRules> builtZones,
|
|
|
|
Map<String, String> links) {
|
|
|
|
try (DataOutputStream out = new DataOutputStream(Files.newOutputStream(dstFile))) {
|
2013-01-22 20:59:21 -08:00
|
|
|
// file version
|
|
|
|
out.writeByte(1);
|
|
|
|
// group
|
|
|
|
out.writeUTF("TZDB");
|
|
|
|
// versions
|
2013-04-12 07:57:35 -07:00
|
|
|
out.writeShort(1);
|
|
|
|
out.writeUTF(version);
|
2013-01-22 20:59:21 -08:00
|
|
|
// regions
|
2013-04-12 07:57:35 -07:00
|
|
|
String[] regionArray = builtZones.keySet().toArray(new String[builtZones.size()]);
|
2013-01-22 20:59:21 -08:00
|
|
|
out.writeShort(regionArray.length);
|
|
|
|
for (String regionId : regionArray) {
|
|
|
|
out.writeUTF(regionId);
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
// rules -- hashset -> remove the dup
|
|
|
|
List<ZoneRules> rulesList = new ArrayList<>(new HashSet<>(builtZones.values()));
|
2013-01-22 20:59:21 -08:00
|
|
|
out.writeShort(rulesList.size());
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
|
|
|
|
for (ZoneRules rules : rulesList) {
|
|
|
|
baos.reset();
|
|
|
|
DataOutputStream dataos = new DataOutputStream(baos);
|
|
|
|
rules.writeExternal(dataos);
|
|
|
|
dataos.close();
|
|
|
|
byte[] bytes = baos.toByteArray();
|
|
|
|
out.writeShort(bytes.length);
|
|
|
|
out.write(bytes);
|
|
|
|
}
|
|
|
|
// link version-region-rules
|
2013-04-12 07:57:35 -07:00
|
|
|
out.writeShort(builtZones.size());
|
|
|
|
for (Map.Entry<String, ZoneRules> entry : builtZones.entrySet()) {
|
|
|
|
int regionIndex = Arrays.binarySearch(regionArray, entry.getKey());
|
|
|
|
int rulesIndex = rulesList.indexOf(entry.getValue());
|
|
|
|
out.writeShort(regionIndex);
|
|
|
|
out.writeShort(rulesIndex);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
2013-02-12 09:25:43 -08:00
|
|
|
// alias-region
|
2013-04-12 07:57:35 -07:00
|
|
|
out.writeShort(links.size());
|
|
|
|
for (Map.Entry<String, String> entry : links.entrySet()) {
|
|
|
|
int aliasIndex = Arrays.binarySearch(regionArray, entry.getKey());
|
|
|
|
int regionIndex = Arrays.binarySearch(regionArray, entry.getValue());
|
|
|
|
out.writeShort(aliasIndex);
|
|
|
|
out.writeShort(regionIndex);
|
2013-02-12 09:25:43 -08:00
|
|
|
}
|
2013-01-22 20:59:21 -08:00
|
|
|
out.flush();
|
|
|
|
} catch (Exception ex) {
|
|
|
|
System.out.println("Failed: " + ex.toString());
|
|
|
|
ex.printStackTrace();
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
private static final Pattern YEAR = Pattern.compile("(?i)(?<min>min)|(?<max>max)|(?<only>only)|(?<year>[0-9]+)");
|
|
|
|
private static final Pattern MONTH = Pattern.compile("(?i)(jan)|(feb)|(mar)|(apr)|(may)|(jun)|(jul)|(aug)|(sep)|(oct)|(nov)|(dec)");
|
|
|
|
private static final Matcher DOW = Pattern.compile("(?i)(mon)|(tue)|(wed)|(thu)|(fri)|(sat)|(sun)").matcher("");
|
|
|
|
private static final Matcher TIME = Pattern.compile("(?<neg>-)?+(?<hour>[0-9]{1,2})(:(?<minute>[0-5][0-9]))?+(:(?<second>[0-5][0-9]))?+").matcher("");
|
|
|
|
|
2013-01-22 20:59:21 -08:00
|
|
|
/** The TZDB rules. */
|
|
|
|
private final Map<String, List<TZDBRule>> rules = new HashMap<>();
|
|
|
|
|
|
|
|
/** The TZDB zones. */
|
|
|
|
private final Map<String, List<TZDBZone>> zones = new HashMap<>();
|
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
/** The TZDB links. */
|
2013-01-22 20:59:21 -08:00
|
|
|
private final Map<String, String> links = new HashMap<>();
|
|
|
|
|
|
|
|
/** The built zones. */
|
|
|
|
private final SortedMap<String, ZoneRules> builtZones = new TreeMap<>();
|
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
/** Whether to output verbose messages. */
|
|
|
|
private boolean verbose;
|
2013-01-22 20:59:21 -08:00
|
|
|
|
|
|
|
/**
|
2013-04-12 07:57:35 -07:00
|
|
|
* private contructor
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-04-12 07:57:35 -07:00
|
|
|
private TzdbZoneRulesCompiler() {
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a source file.
|
|
|
|
*
|
|
|
|
* @param file the file being read, not null
|
|
|
|
* @throws Exception if an error occurs
|
|
|
|
*/
|
2013-04-12 07:57:35 -07:00
|
|
|
private void parseFile(Path file) throws Exception {
|
2013-01-22 20:59:21 -08:00
|
|
|
int lineNumber = 1;
|
|
|
|
String line = null;
|
|
|
|
try {
|
2013-04-12 07:57:35 -07:00
|
|
|
List<String> lines = Files.readAllLines(file, StandardCharsets.ISO_8859_1);
|
2013-01-22 20:59:21 -08:00
|
|
|
List<TZDBZone> openZone = null;
|
2013-04-12 07:57:35 -07:00
|
|
|
for (; lineNumber < lines.size(); lineNumber++) {
|
|
|
|
line = lines.get(lineNumber);
|
2013-01-22 20:59:21 -08:00
|
|
|
int index = line.indexOf('#'); // remove comments (doesn't handle # in quotes)
|
|
|
|
if (index >= 0) {
|
|
|
|
line = line.substring(0, index);
|
|
|
|
}
|
|
|
|
if (line.trim().length() == 0) { // ignore blank lines
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
Scanner s = new Scanner(line);
|
|
|
|
if (openZone != null && Character.isWhitespace(line.charAt(0)) && s.hasNext()) {
|
|
|
|
if (parseZoneLine(s, openZone)) {
|
2013-01-22 20:59:21 -08:00
|
|
|
openZone = null;
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-12 07:57:35 -07:00
|
|
|
if (s.hasNext()) {
|
|
|
|
String first = s.next();
|
2013-01-22 20:59:21 -08:00
|
|
|
if (first.equals("Zone")) {
|
2013-04-12 07:57:35 -07:00
|
|
|
openZone = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
zones.put(s.next(), openZone);
|
|
|
|
if (parseZoneLine(s, openZone)) {
|
|
|
|
openZone = null;
|
|
|
|
}
|
|
|
|
} catch (NoSuchElementException x) {
|
2013-01-22 20:59:21 -08:00
|
|
|
printVerbose("Invalid Zone line in file: " + file + ", line: " + line);
|
|
|
|
throw new IllegalArgumentException("Invalid Zone line");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
openZone = null;
|
|
|
|
if (first.equals("Rule")) {
|
2013-04-12 07:57:35 -07:00
|
|
|
try {
|
|
|
|
parseRuleLine(s);
|
|
|
|
} catch (NoSuchElementException x) {
|
2013-01-22 20:59:21 -08:00
|
|
|
printVerbose("Invalid Rule line in file: " + file + ", line: " + line);
|
|
|
|
throw new IllegalArgumentException("Invalid Rule line");
|
|
|
|
}
|
|
|
|
} else if (first.equals("Link")) {
|
2013-04-12 07:57:35 -07:00
|
|
|
try {
|
|
|
|
String realId = s.next();
|
|
|
|
String aliasId = s.next();
|
|
|
|
links.put(aliasId, realId);
|
|
|
|
} catch (NoSuchElementException x) {
|
2013-01-22 20:59:21 -08:00
|
|
|
printVerbose("Invalid Link line in file: " + file + ", line: " + line);
|
|
|
|
throw new IllegalArgumentException("Invalid Link line");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Unknown line");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
2013-04-12 07:57:35 -07:00
|
|
|
throw new Exception("Failed while parsing file '" + file + "' on line " + lineNumber + " '" + line + "'", ex);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a Rule line.
|
|
|
|
*
|
2013-04-12 07:57:35 -07:00
|
|
|
* @param s the line scanner, not null
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-04-12 07:57:35 -07:00
|
|
|
private void parseRuleLine(Scanner s) {
|
2013-01-22 20:59:21 -08:00
|
|
|
TZDBRule rule = new TZDBRule();
|
2013-04-12 07:57:35 -07:00
|
|
|
String name = s.next();
|
2013-01-22 20:59:21 -08:00
|
|
|
if (rules.containsKey(name) == false) {
|
|
|
|
rules.put(name, new ArrayList<TZDBRule>());
|
|
|
|
}
|
|
|
|
rules.get(name).add(rule);
|
2013-04-12 07:57:35 -07:00
|
|
|
rule.startYear = parseYear(s, 0);
|
|
|
|
rule.endYear = parseYear(s, rule.startYear);
|
2013-01-22 20:59:21 -08:00
|
|
|
if (rule.startYear > rule.endYear) {
|
|
|
|
throw new IllegalArgumentException("Year order invalid: " + rule.startYear + " > " + rule.endYear);
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
parseOptional(s.next()); // type is unused
|
|
|
|
parseMonthDayTime(s, rule);
|
|
|
|
rule.savingsAmount = parsePeriod(s.next());
|
|
|
|
rule.text = parseOptional(s.next());
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a Zone line.
|
|
|
|
*
|
2013-04-12 07:57:35 -07:00
|
|
|
* @param s the line scanner, not null
|
2013-01-22 20:59:21 -08:00
|
|
|
* @return true if the zone is complete
|
|
|
|
*/
|
2013-04-12 07:57:35 -07:00
|
|
|
private boolean parseZoneLine(Scanner s, List<TZDBZone> zoneList) {
|
2013-01-22 20:59:21 -08:00
|
|
|
TZDBZone zone = new TZDBZone();
|
|
|
|
zoneList.add(zone);
|
2013-04-12 07:57:35 -07:00
|
|
|
zone.standardOffset = parseOffset(s.next());
|
|
|
|
String savingsRule = parseOptional(s.next());
|
2013-01-22 20:59:21 -08:00
|
|
|
if (savingsRule == null) {
|
|
|
|
zone.fixedSavingsSecs = 0;
|
|
|
|
zone.savingsRule = null;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
zone.fixedSavingsSecs = parsePeriod(savingsRule);
|
|
|
|
zone.savingsRule = null;
|
|
|
|
} catch (Exception ex) {
|
|
|
|
zone.fixedSavingsSecs = null;
|
|
|
|
zone.savingsRule = savingsRule;
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
zone.text = s.next();
|
|
|
|
if (s.hasNext()) {
|
|
|
|
zone.year = Integer.parseInt(s.next());
|
|
|
|
if (s.hasNext()) {
|
|
|
|
parseMonthDayTime(s, zone);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a Rule line.
|
|
|
|
*
|
2013-04-12 07:57:35 -07:00
|
|
|
* @param s the line scanner, not null
|
2013-01-22 20:59:21 -08:00
|
|
|
* @param mdt the object to parse into, not null
|
|
|
|
*/
|
2013-04-12 07:57:35 -07:00
|
|
|
private void parseMonthDayTime(Scanner s, TZDBMonthDayTime mdt) {
|
|
|
|
mdt.month = parseMonth(s);
|
|
|
|
if (s.hasNext()) {
|
|
|
|
String dayRule = s.next();
|
2013-01-22 20:59:21 -08:00
|
|
|
if (dayRule.startsWith("last")) {
|
|
|
|
mdt.dayOfMonth = -1;
|
|
|
|
mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(4));
|
|
|
|
mdt.adjustForwards = false;
|
|
|
|
} else {
|
|
|
|
int index = dayRule.indexOf(">=");
|
|
|
|
if (index > 0) {
|
|
|
|
mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(0, index));
|
|
|
|
dayRule = dayRule.substring(index + 2);
|
|
|
|
} else {
|
|
|
|
index = dayRule.indexOf("<=");
|
|
|
|
if (index > 0) {
|
|
|
|
mdt.dayOfWeek = parseDayOfWeek(dayRule.substring(0, index));
|
|
|
|
mdt.adjustForwards = false;
|
|
|
|
dayRule = dayRule.substring(index + 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mdt.dayOfMonth = Integer.parseInt(dayRule);
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
if (s.hasNext()) {
|
|
|
|
String timeStr = s.next();
|
2013-01-22 20:59:21 -08:00
|
|
|
int secsOfDay = parseSecs(timeStr);
|
|
|
|
if (secsOfDay == 86400) {
|
|
|
|
mdt.endOfDay = true;
|
|
|
|
secsOfDay = 0;
|
|
|
|
}
|
|
|
|
LocalTime time = LocalTime.ofSecondOfDay(secsOfDay);
|
|
|
|
mdt.time = time;
|
|
|
|
mdt.timeDefinition = parseTimeDefinition(timeStr.charAt(timeStr.length() - 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
private int parseYear(Scanner s, int defaultYear) {
|
|
|
|
if (s.hasNext(YEAR)) {
|
|
|
|
s.next(YEAR);
|
|
|
|
MatchResult mr = s.match();
|
|
|
|
if (mr.group(1) != null) {
|
|
|
|
return 1900; // systemv has min
|
|
|
|
} else if (mr.group(2) != null) {
|
|
|
|
return YEAR_MAX_VALUE;
|
|
|
|
} else if (mr.group(3) != null) {
|
|
|
|
return defaultYear;
|
|
|
|
}
|
|
|
|
return Integer.parseInt(mr.group(4));
|
|
|
|
/*
|
|
|
|
if (mr.group("min") != null) {
|
2013-02-12 09:25:43 -08:00
|
|
|
//return YEAR_MIN_VALUE;
|
|
|
|
return 1900; // systemv has min
|
2013-04-12 07:57:35 -07:00
|
|
|
} else if (mr.group("max") != null) {
|
2013-01-22 20:59:21 -08:00
|
|
|
return YEAR_MAX_VALUE;
|
2013-04-12 07:57:35 -07:00
|
|
|
} else if (mr.group("only") != null) {
|
2013-01-22 20:59:21 -08:00
|
|
|
return defaultYear;
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
return Integer.parseInt(mr.group("year"));
|
|
|
|
*/
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
throw new IllegalArgumentException("Unknown year: " + s.next());
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
private int parseMonth(Scanner s) {
|
|
|
|
if (s.hasNext(MONTH)) {
|
|
|
|
s.next(MONTH);
|
2013-01-22 20:59:21 -08:00
|
|
|
for (int moy = 1; moy < 13; moy++) {
|
2013-04-12 07:57:35 -07:00
|
|
|
if (s.match().group(moy) != null) {
|
2013-01-22 20:59:21 -08:00
|
|
|
return moy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
throw new IllegalArgumentException("Unknown month: " + s.next());
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private int parseDayOfWeek(String str) {
|
|
|
|
if (DOW.reset(str).matches()) {
|
|
|
|
for (int dow = 1; dow < 8; dow++) {
|
|
|
|
if (DOW.group(dow) != null) {
|
|
|
|
return dow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Unknown day-of-week: " + str);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String parseOptional(String str) {
|
|
|
|
return str.equals("-") ? null : str;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int parseSecs(String str) {
|
|
|
|
if (str.equals("-")) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (TIME.reset(str).find()) {
|
|
|
|
int secs = Integer.parseInt(TIME.group("hour")) * 60 * 60;
|
|
|
|
if (TIME.group("minute") != null) {
|
|
|
|
secs += Integer.parseInt(TIME.group("minute")) * 60;
|
|
|
|
}
|
|
|
|
if (TIME.group("second") != null) {
|
|
|
|
secs += Integer.parseInt(TIME.group("second"));
|
|
|
|
}
|
|
|
|
if (TIME.group("neg") != null) {
|
|
|
|
secs = -secs;
|
|
|
|
}
|
|
|
|
return secs;
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException x) {}
|
|
|
|
throw new IllegalArgumentException(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ZoneOffset parseOffset(String str) {
|
|
|
|
int secs = parseSecs(str);
|
|
|
|
return ZoneOffset.ofTotalSeconds(secs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int parsePeriod(String str) {
|
|
|
|
return parseSecs(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
private TimeDefinition parseTimeDefinition(char c) {
|
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
// standard time
|
|
|
|
return TimeDefinition.STANDARD;
|
|
|
|
case 'u':
|
|
|
|
case 'U':
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
case 'z':
|
|
|
|
case 'Z':
|
|
|
|
// UTC
|
|
|
|
return TimeDefinition.UTC;
|
|
|
|
case 'w':
|
|
|
|
case 'W':
|
|
|
|
default:
|
|
|
|
// wall time
|
|
|
|
return TimeDefinition.WALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the rules, zones and links into real zones.
|
|
|
|
*
|
|
|
|
* @throws Exception if an error occurs
|
|
|
|
*/
|
|
|
|
private void buildZoneRules() throws Exception {
|
|
|
|
// build zones
|
|
|
|
for (String zoneId : zones.keySet()) {
|
|
|
|
printVerbose("Building zone " + zoneId);
|
|
|
|
List<TZDBZone> tzdbZones = zones.get(zoneId);
|
|
|
|
ZoneRulesBuilder bld = new ZoneRulesBuilder();
|
|
|
|
for (TZDBZone tzdbZone : tzdbZones) {
|
|
|
|
bld = tzdbZone.addToBuilder(bld, rules);
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
builtZones.put(zoneId, bld.toRules(zoneId));
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// build aliases
|
|
|
|
for (String aliasId : links.keySet()) {
|
|
|
|
String realId = links.get(aliasId);
|
|
|
|
printVerbose("Linking alias " + aliasId + " to " + realId);
|
|
|
|
ZoneRules realRules = builtZones.get(realId);
|
|
|
|
if (realRules == null) {
|
|
|
|
realId = links.get(realId); // try again (handle alias liked to alias)
|
|
|
|
printVerbose("Relinking alias " + aliasId + " to " + realId);
|
|
|
|
realRules = builtZones.get(realId);
|
|
|
|
if (realRules == null) {
|
2013-04-12 07:57:35 -07:00
|
|
|
throw new IllegalArgumentException("Alias '" + aliasId + "' links to invalid zone '" + realId);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
2013-02-12 09:25:43 -08:00
|
|
|
links.put(aliasId, realId);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
builtZones.put(aliasId, realRules);
|
|
|
|
}
|
|
|
|
// remove UTC and GMT
|
2013-04-12 07:57:35 -07:00
|
|
|
// builtZones.remove("UTC");
|
|
|
|
// builtZones.remove("GMT");
|
|
|
|
// builtZones.remove("GMT0");
|
2013-01-22 20:59:21 -08:00
|
|
|
builtZones.remove("GMT+0");
|
|
|
|
builtZones.remove("GMT-0");
|
2013-02-12 09:25:43 -08:00
|
|
|
links.remove("GMT+0");
|
|
|
|
links.remove("GMT-0");
|
2013-04-12 07:57:35 -07:00
|
|
|
// remove ROC, which is not supported in j.u.tz
|
|
|
|
builtZones.remove("ROC");
|
|
|
|
links.remove("ROC");
|
2013-10-21 11:16:02 -07:00
|
|
|
// remove EST, HST and MST. They are supported via
|
|
|
|
// the short-id mapping
|
|
|
|
builtZones.remove("EST");
|
|
|
|
builtZones.remove("HST");
|
|
|
|
builtZones.remove("MST");
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints a verbose message.
|
|
|
|
*
|
|
|
|
* @param message the message, not null
|
|
|
|
*/
|
|
|
|
private void printVerbose(String message) {
|
|
|
|
if (verbose) {
|
|
|
|
System.out.println(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class representing a month-day-time in the TZDB file.
|
|
|
|
*/
|
|
|
|
abstract class TZDBMonthDayTime {
|
|
|
|
/** The month of the cutover. */
|
|
|
|
int month = 1;
|
|
|
|
/** The day-of-month of the cutover. */
|
|
|
|
int dayOfMonth = 1;
|
|
|
|
/** Whether to adjust forwards. */
|
|
|
|
boolean adjustForwards = true;
|
|
|
|
/** The day-of-week of the cutover. */
|
|
|
|
int dayOfWeek = -1;
|
|
|
|
/** The time of the cutover. */
|
|
|
|
LocalTime time = LocalTime.MIDNIGHT;
|
|
|
|
/** Whether this is midnight end of day. */
|
|
|
|
boolean endOfDay;
|
|
|
|
/** The time of the cutover. */
|
|
|
|
TimeDefinition timeDefinition = TimeDefinition.WALL;
|
|
|
|
void adjustToFowards(int year) {
|
|
|
|
if (adjustForwards == false && dayOfMonth > 0) {
|
|
|
|
LocalDate adjustedDate = LocalDate.of(year, month, dayOfMonth).minusDays(6);
|
|
|
|
dayOfMonth = adjustedDate.getDayOfMonth();
|
|
|
|
month = adjustedDate.getMonth();
|
|
|
|
adjustForwards = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class representing a rule line in the TZDB file.
|
|
|
|
*/
|
|
|
|
final class TZDBRule extends TZDBMonthDayTime {
|
|
|
|
/** The start year. */
|
|
|
|
int startYear;
|
|
|
|
/** The end year. */
|
|
|
|
int endYear;
|
|
|
|
/** The amount of savings. */
|
|
|
|
int savingsAmount;
|
|
|
|
/** The text name of the zone. */
|
|
|
|
String text;
|
|
|
|
|
|
|
|
void addToBuilder(ZoneRulesBuilder bld) {
|
|
|
|
adjustToFowards(2004); // irrelevant, treat as leap year
|
|
|
|
bld.addRuleToWindow(startYear, endYear, month, dayOfMonth, dayOfWeek, time, endOfDay, timeDefinition, savingsAmount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class representing a linked set of zone lines in the TZDB file.
|
|
|
|
*/
|
|
|
|
final class TZDBZone extends TZDBMonthDayTime {
|
|
|
|
/** The standard offset. */
|
|
|
|
ZoneOffset standardOffset;
|
|
|
|
/** The fixed savings amount. */
|
|
|
|
Integer fixedSavingsSecs;
|
|
|
|
/** The savings rule. */
|
|
|
|
String savingsRule;
|
|
|
|
/** The text name of the zone. */
|
|
|
|
String text;
|
|
|
|
/** The year of the cutover. */
|
|
|
|
int year = YEAR_MAX_VALUE;
|
|
|
|
|
|
|
|
ZoneRulesBuilder addToBuilder(ZoneRulesBuilder bld, Map<String, List<TZDBRule>> rules) {
|
|
|
|
if (year != YEAR_MAX_VALUE) {
|
|
|
|
bld.addWindow(standardOffset, toDateTime(year), timeDefinition);
|
|
|
|
} else {
|
|
|
|
bld.addWindowForever(standardOffset);
|
|
|
|
}
|
|
|
|
if (fixedSavingsSecs != null) {
|
|
|
|
bld.setFixedSavingsToWindow(fixedSavingsSecs);
|
|
|
|
} else {
|
|
|
|
List<TZDBRule> tzdbRules = rules.get(savingsRule);
|
|
|
|
if (tzdbRules == null) {
|
|
|
|
throw new IllegalArgumentException("Rule not found: " + savingsRule);
|
|
|
|
}
|
|
|
|
for (TZDBRule tzdbRule : tzdbRules) {
|
|
|
|
tzdbRule.addToBuilder(bld);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bld;
|
|
|
|
}
|
|
|
|
|
|
|
|
private LocalDateTime toDateTime(int year) {
|
|
|
|
adjustToFowards(year);
|
|
|
|
LocalDate date;
|
|
|
|
if (dayOfMonth == -1) {
|
|
|
|
dayOfMonth = lengthOfMonth(month, isLeapYear(year));
|
|
|
|
date = LocalDate.of(year, month, dayOfMonth);
|
|
|
|
if (dayOfWeek != -1) {
|
|
|
|
date = previousOrSame(date, dayOfWeek);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
date = LocalDate.of(year, month, dayOfMonth);
|
|
|
|
if (dayOfWeek != -1) {
|
|
|
|
date = nextOrSame(date, dayOfWeek);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LocalDateTime ldt = LocalDateTime.of(date, time);
|
|
|
|
if (endOfDay) {
|
|
|
|
ldt = ldt.plusDays(1);
|
|
|
|
}
|
|
|
|
return ldt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|