Skip to content

Commit

Permalink
8209167: Use CLDR's time zone mappings for Windows
Browse files Browse the repository at this point in the history
Reviewed-by: rrich
Backport-of: 009cd15
  • Loading branch information
GoeLin committed Apr 25, 2023
1 parent 3c81352 commit 6dc0712
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 288 deletions.
2 changes: 1 addition & 1 deletion make/copy/Copy-java.base.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $(eval $(call IncludeCustomExtension, copy/Copy-java.base.gmk))

################################################################################

ifeq ($(call isTargetOs, windows aix), true)
ifeq ($(call isTargetOs, aix), true)

TZMAPPINGS_SRC := $(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS)/conf

Expand Down
22 changes: 15 additions & 7 deletions make/gensrc/GensrcCLDR.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ CLDR_BASE_LOCALES := "en-US"
ZONENAME_TEMPLATE := $(TOPDIR)/src/java.base/share/classes/java/time/format/ZoneName.java.template
TZDATA_DIR := $(TOPDIR)/make/data/tzdata

$(CLDR_BASEMETAINFO_FILE): $(wildcard $(CLDRSRCDIR)/common/dtd/*.dtd) \
$(wildcard $(CLDRSRCDIR)/common/main/en*.xml) \
$(wildcard $(CLDRSRCDIR)/common/supplemental/*.xml) \
# tzmappings generation for Windows.
# Since the rule is shared with CLDR_BASEMETAINFO_FILE target, note that
# just removing the target tzmappings will not recreate the tzmappings file.
ifeq ($(OPENJDK_TARGET_OS), windows)
CLDR_WINTZMAPPINGS := $(GENSRC_BASEDIR)/windows/conf/tzmappings
$(CLDR_WINTZMAPPINGS): $(CLDR_BASEMETAINFO_FILE)
endif

$(CLDR_BASEMETAINFO_FILE): $(wildcard $(CLDRSRCDIR)/dtd/*.dtd) \
$(wildcard $(CLDRSRCDIR)/main/en*.xml) \
$(wildcard $(CLDRSRCDIR)/supplemental/*.xml) \
$(ZONENAME_TEMPLATE) \
$(BUILD_TOOLS_JDK)
$(MKDIR) -p $(GENSRC_BASEDIR)
Expand All @@ -50,14 +58,14 @@ $(CLDR_BASEMETAINFO_FILE): $(wildcard $(CLDRSRCDIR)/common/dtd/*.dtd) \
-zntempfile $(ZONENAME_TEMPLATE) \
-tzdatadir $(TZDATA_DIR)

$(CLDR_METAINFO_FILE): $(wildcard $(CLDRSRCDIR)/common/dtd/*.dtd) \
$(wildcard $(CLDRSRCDIR)/common/main/*.xml) \
$(wildcard $(CLDRSRCDIR)/common/supplemental/*.xml) \
$(CLDR_METAINFO_FILE): $(wildcard $(CLDRSRCDIR)/dtd/*.dtd) \
$(wildcard $(CLDRSRCDIR)/main/*.xml) \
$(wildcard $(CLDRSRCDIR)/supplemental/*.xml) \
$(BUILD_TOOLS_JDK)
$(MKDIR) -p $(GENSRC_DIR)
$(TOOL_CLDRCONVERTER) -base $(CLDRSRCDIR) \
-baselocales $(CLDR_BASE_LOCALES) \
-o $(GENSRC_DIR)

GENSRC_JAVA_BASE += $(CLDR_BASEMETAINFO_FILE)
GENSRC_JAVA_BASE += $(CLDR_BASEMETAINFO_FILE) $(CLDR_WINTZMAPPINGS)
GENSRC_JDK_LOCALEDATA += $(CLDR_METAINFO_FILE)
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class CLDRConverter {
private static String METAZONES_SOURCE_FILE;
private static String LIKELYSUBTAGS_SOURCE_FILE;
private static String TIMEZONE_SOURCE_FILE;
private static String WINZONES_SOURCE_FILE;
static String DESTINATION_DIR = "build/gensrc";

static final String LOCALE_NAME_PREFIX = "locale.displayname.";
Expand All @@ -92,6 +93,7 @@ public class CLDRConverter {

private static SupplementDataParseHandler handlerSuppl;
private static LikelySubtagsParseHandler handlerLikelySubtags;
private static WinZonesParseHandler handlerWinZones;
static SupplementalMetadataParseHandler handlerSupplMeta;
static NumberingSystemsParseHandler handlerNumbering;
static MetaZonesParseHandler handlerMetaZones;
Expand Down Expand Up @@ -242,6 +244,7 @@ public static void main(String[] args) throws Exception {
METAZONES_SOURCE_FILE = CLDR_BASE + "/supplemental/metaZones.xml";
TIMEZONE_SOURCE_FILE = CLDR_BASE + "/bcp47/timezone.xml";
SPPL_META_SOURCE_FILE = CLDR_BASE + "/supplemental/supplementalMetadata.xml";
WINZONES_SOURCE_FILE = CLDR_BASE + "/supplemental/windowsZones.xml";

if (BASE_LOCALES.isEmpty()) {
setupBaseLocales("en-US");
Expand All @@ -256,9 +259,12 @@ public static void main(String[] args) throws Exception {
List<Bundle> bundles = readBundleList();
convertBundles(bundles);

// Generate java.time.format.ZoneName.java
if (isBaseModule) {
// Generate java.time.format.ZoneName.java
generateZoneName();

// Generate Windows tzmappings
generateWindowsTZMappings();
}
}

Expand Down Expand Up @@ -442,6 +448,10 @@ private static void parseSupplemental() throws Exception {
// Currently interested in deprecated time zone ids and language aliases.
handlerSupplMeta = new SupplementalMetadataParseHandler();
parseLDMLFile(new File(SPPL_META_SOURCE_FILE), handlerSupplMeta);

// Parse windowsZones
handlerWinZones = new WinZonesParseHandler();
parseLDMLFile(new File(WINZONES_SOURCE_FILE), handlerWinZones);
}

// Parsers for data in "bcp47" directory
Expand Down Expand Up @@ -1062,4 +1072,42 @@ private static Stream<String> extractLinks(Path tzFile) {
throw new UncheckedIOException(e);
}
}

// Generate tzmappings for Windows. The format is:
//
// (Windows Zone Name):(REGION):(Java TZID)
//
// where:
// Windows Zone Name: arbitrary time zone name string used in Windows
// REGION: ISO3166 or UN M.49 code
// Java TZID: Java's time zone ID
//
// Note: the entries are alphabetically sorted, *except* the "world" region
// code, i.e., "001". It should be the last entry for the same windows time
// zone name entries. (cf. TimeZone_md.c)
private static void generateWindowsTZMappings() throws Exception {
Files.createDirectories(Paths.get(DESTINATION_DIR, "windows", "conf"));
Files.write(Paths.get(DESTINATION_DIR, "windows", "conf", "tzmappings"),
handlerWinZones.keySet().stream()
.map(k -> k + ":" + handlerWinZones.get(k) + ":")
.sorted(new Comparator<String>() {
public int compare(String t1, String t2) {
String[] s1 = t1.split(":");
String[] s2 = t2.split(":");
if (s1[0].equals(s2[0])) {
if (s1[1].equals("001")) {
return 1;
} else if (s2[1].equals("001")) {
return -1;
} else {
return s1[1].compareTo(s2[1]);
}
} else {
return s1[0].compareTo(s2[0]);
}
}
})
.collect(Collectors.toList()),
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2018, 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.
*/

package build.tools.cldrconverter;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
* Handles parsing of files in Locale Data Markup Language for
* windowsZones.xml
*/

class WinZonesParseHandler extends AbstractLDMLHandler<Object> {
@Override
public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException {
// avoid HTTP traffic to unicode.org
if (systemID.startsWith(CLDRConverter.SPPL_LDML_DTD_SYSTEM_ID)) {
return new InputSource((new File(CLDRConverter.LOCAL_SPPL_LDML_DTD)).toURI().toString());
}
return null;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case "mapZone":
String zoneName = attributes.getValue("other");
String territory = attributes.getValue("territory");
String javatz = attributes.getValue("type").replaceFirst("\\s.*", "");
put(zoneName + ":" + territory, javatz);
pushIgnoredContainer(qName);
break;
default:
// treat anything else as a container
pushContainer(qName, attributes);
break;
}
}
}
11 changes: 11 additions & 0 deletions make/lib/Lib-java.base.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,14 @@ ifeq ($(STATIC_BUILD), true)

TARGETS += $(JAVA_BASE_EXPORT_SYMBOL_FILE)
endif

################################################################################
# Copy tzmappings file for Windows

ifeq ($(OPENJDK_TARGET_OS), windows)
$(eval $(call SetupCopyFiles, COPY_TZMAPPINGS, \
FILES := $(SUPPORT_OUTPUTDIR)/gensrc/java.base/windows/conf/tzmappings, \
DEST := $(call FindLibDirForModule, $(MODULE)), \
))
TARGETS += $(COPY_TZMAPPINGS)
endif

1 comment on commit 6dc0712

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.