Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
naotoj committed Oct 11, 2023
1 parent 508fa71 commit 606ca09
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public class CLDRConverter {
static Map<String, String> pluralRules;
static Map<String, String> dayPeriodRules;

// TZDB Short Names Map
private static Map<String, String> tzdbShortNamesMap;

static enum DraftType {
UNCONFIRMED,
PROVISIONAL,
Expand Down Expand Up @@ -284,6 +287,9 @@ public static void main(String[] args) throws Exception {
pluralRules = generateRules(handlerPlurals);
dayPeriodRules = generateRules(handlerDayPeriodRule);

// TZDB short names map
tzdbShortNamesMap = generateTZDBShortNamesMap();

List<Bundle> bundles = readBundleList();
convertBundles(bundles);

Expand Down Expand Up @@ -771,7 +777,9 @@ private static Map<String, Object> extractZoneNames(Map<String, Object> map, Str
if (meta != null) {
String metaKey = METAZONE_ID_PREFIX + meta;
data = map.get(metaKey);
if (data instanceof String[]) {
if (data instanceof String[] tznames) {
// TZDB short names
fillTZDBShortNames(tzid, tznames);
// Keep the metazone prefix here.
names.put(metaKey, data);
names.put(tzid, meta);
Expand Down Expand Up @@ -1246,6 +1254,74 @@ private static Map<Locale, String> coverageLevelsMap() throws Exception {
return covMap;
}

private static Map<String, String> generateTZDBShortNamesMap() throws IOException {
final Map<String, String> shortNameMap = HashMap.newHashMap(1024);
Files.walk(Path.of(tzDataDir), 1, FileVisitOption.FOLLOW_LINKS)
.map(Path::toFile)
.filter(File::isFile)
.forEach(f -> {
try {
String zone = null;
String format = null;
for (var line : Files.readAllLines(f.toPath())) {
if (line.contains("#STDOFF")) continue;
line = line.replaceAll("[ \t]*#.*", "");
if (line.startsWith("Zone")) {
var s = line.split("[ \t]+", -1);
zone = s[1];
format = s[4];
} else {
if (zone != null) {
if (line.isBlank()) {
shortNameMap.put(zone, format);
zone = null;
format = null;
} else {
format = line.split("[ \t]+", -1)[3];
}
}
}

}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
});
return shortNameMap;
}
//
private static void fillTZDBShortNames(String tzid, String[] names) {
var format = tzdbShortNamesMap.get(tzid);
if (format != null) {
IntStream.of(1, 3, 5).forEach(i -> {
if (names[i] == null) {
names[i] = getTZDBShortName(format, i);
}
});
}
}

// fmt could be either
// X%sT, +XX/+YY, or +XX
private static final String getTZDBShortName(String f, int i) {
if (f.contains("%s")) {
return switch (i) {
case 1 -> f.formatted("S");
case 3 -> f.formatted("D");
case 5 -> f.formatted("");
default -> throw new InternalError();
};
} else if (f.contains("/")) {
return switch (i) {
case 1, 5 -> f.substring(0, f.indexOf("/"));
case 3 -> f.substring(f.indexOf("/") + 1);
default -> throw new InternalError();
};
} else {
return f;
}
}

// for debug
static void dumpMap(Map<String, Object> map) {
map.entrySet().stream()
Expand Down
6 changes: 4 additions & 2 deletions make/modules/jdk.localedata/Gensrc.gmk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2014, 2023, 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
Expand Down Expand Up @@ -36,6 +36,7 @@ TARGETS += $(GENSRC_LOCALEDATA)
CLDR_DATA_DIR := $(TOPDIR)/make/data/cldr/common
GENSRC_DIR := $(SUPPORT_OUTPUTDIR)/gensrc/jdk.localedata
CLDR_GEN_DONE := $(GENSRC_DIR)/_cldr-gensrc.marker
TZ_DATA_DIR := $(MODULE_SRC)/../java.base/share/data/tzdata

$(CLDR_GEN_DONE): $(wildcard $(CLDR_DATA_DIR)/dtd/*.dtd) \
$(wildcard $(CLDR_DATA_DIR)/main/*.xml) \
Expand All @@ -47,7 +48,8 @@ $(CLDR_GEN_DONE): $(wildcard $(CLDR_DATA_DIR)/dtd/*.dtd) \
$(TOOL_CLDRCONVERTER) -base $(CLDR_DATA_DIR) \
-baselocales "en-US" \
-year $(COPYRIGHT_YEAR) \
-o $(GENSRC_DIR))
-o $(GENSRC_DIR) \
-tzdatadir $(TZ_DATA_DIR))
$(TOUCH) $@

TARGETS += $(CLDR_GEN_DONE)
Expand Down

0 comments on commit 606ca09

Please sign in to comment.