Skip to content

Commit

Permalink
CLDR-7401 Style, formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
haytenf committed Aug 13, 2024
1 parent c5f5aa1 commit 666d719
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,26 +276,51 @@ public void setCachingEnabled(boolean enabled) {

/* For each calendar type, maps the closest two eras to 2025
* defined in that calendar to their corresponding start/end date.
* Dates are adjusted to be 2 days after official era start date and
* Dates are adjusted to be 2 days after official era start date and
* 2 days before era end date to avoid time zone issues.
* TODO: include methods for calendarData in supplementalDataInfo API
* TODO: include methods for calendarData in supplementalDataInfo API
* to extract this data directly from supplementaldata.xml
*/
public static final Map<String, List<Date>> CALENDAR_ERAS = new HashMap<String, List<Date>>() {{ // month 0-indexed. start/end days adjusted by +/- 2, respectively
put("gregorian", List.of(new GregorianCalendar(0, 11, 29).getTime(), new GregorianCalendar(1, 0, 03).getTime()));
put("japanese", List.of(new GregorianCalendar(1989, 0, 10).getTime(), new GregorianCalendar(2019, 4, 3).getTime()));
put("islamic", List.of(new GregorianCalendar(622, 6, 17).getTime()));
put("chinese", List.of(new GregorianCalendar(-2636, 0, 03).getTime()));
put("hebrew", List.of(new GregorianCalendar(-3760, 9, 9).getTime()));
put("buddhist", List.of(new GregorianCalendar(-542, 0, 03).getTime()));
put("coptic", List.of(new GregorianCalendar(284, 07, 26).getTime(), new GregorianCalendar(284, 07, 31).getTime()));
put("persian", List.of(new GregorianCalendar(622, 0, 03).getTime()));
put("dangi", List.of(new GregorianCalendar(-2332, 0, 03).getTime()));
put("ethiopic", List.of(new GregorianCalendar(8, 07, 26).getTime(), new GregorianCalendar(8, 07, 31).getTime()));
put("ethiopic-amete-alem", List.of(new GregorianCalendar(-5492, 07, 27).getTime()));
put("indian", List.of(new GregorianCalendar(79, 0, 03).getTime()));
put("roc", List.of(new GregorianCalendar(1911, 11, 29).getTime(), new GregorianCalendar(1912, 0, 03).getTime()));
}};
*/
public static final Map<String, List<Date>> CALENDAR_ERAS =
new HashMap<String, List<Date>>() {
{ // month 0-indexed. start/end days adjusted by +/- 2, respectively
put(
"gregorian",
List.of(
new GregorianCalendar(0, 11, 29).getTime(),
new GregorianCalendar(1, 0, 03).getTime()));
put(
"japanese",
List.of(
new GregorianCalendar(1989, 0, 10).getTime(),
new GregorianCalendar(2019, 4, 3).getTime()));
put("islamic", List.of(new GregorianCalendar(622, 6, 17).getTime()));
put("chinese", List.of(new GregorianCalendar(-2636, 0, 03).getTime()));
put("hebrew", List.of(new GregorianCalendar(-3760, 9, 9).getTime()));
put("buddhist", List.of(new GregorianCalendar(-542, 0, 03).getTime()));
put(
"coptic",
List.of(
new GregorianCalendar(284, 07, 26).getTime(),
new GregorianCalendar(284, 07, 31).getTime()));
put("persian", List.of(new GregorianCalendar(622, 0, 03).getTime()));
put("dangi", List.of(new GregorianCalendar(-2332, 0, 03).getTime()));
put(
"ethiopic",
List.of(
new GregorianCalendar(8, 07, 26).getTime(),
new GregorianCalendar(8, 07, 31).getTime()));
put(
"ethiopic-amete-alem",
List.of(new GregorianCalendar(-5492, 07, 27).getTime()));
put("indian", List.of(new GregorianCalendar(79, 0, 03).getTime()));
put(
"roc",
List.of(
new GregorianCalendar(1911, 11, 29).getTime(),
new GregorianCalendar(1912, 0, 03).getTime()));
}
};

public CLDRFile getCldrFile() {
return cldrFile;
Expand Down Expand Up @@ -2889,21 +2914,23 @@ private String handleDateRangePattern(String value) {
}

/**
* Add examples for eras. First checks if there is info for this
* calendar type and this era type in the CALENDAR_ERAS map, then
* generates a sample date based on this info and formats it
* Add examples for eras. First checks if there is info for this calendar type and this era type
* in the CALENDAR_ERAS map, then generates a sample date based on this info and formats it
*/
private String handleEras(XPathParts parts, String value) {
String calendarId = parts.getAttributeValue(3, "type");
String type = parts.getAttributeValue(-1, "type");
String id = (calendarId.contains("islamic")) ? "islamic" : calendarId; // islamic variations map to same sample
String id =
(calendarId.startsWith("islamic"))
? "islamic"
: calendarId; // islamic variations map to same sample
if (!CALENDAR_ERAS.containsKey(id)) {
return null;
return null;
}
int typeIndex = Integer.parseInt(type);
if (calendarId.equals("japanese")) {
if (calendarId.equals("japanese")) {
if (typeIndex < 235) { // examples only for 2 most recent eras
return null;
return null;
} else {
typeIndex %= 235; // map to length 2 list
}
Expand All @@ -2918,8 +2945,7 @@ private String handleEras(XPathParts parts, String value) {
+ skeleton
+ "\"]";
String dateFormat = cldrFile.getWinningValue(checkPath);
SimpleDateFormat sdf =
icuServiceBuilder.getDateFormat(calendarId, dateFormat);
SimpleDateFormat sdf = icuServiceBuilder.getDateFormat(calendarId, dateFormat);
DateFormatSymbols dfs = sdf.getDateFormatSymbols();
String[] eraNames = dfs.getEras();
eraNames[typeIndex] = value; // overwrite era to current value
Expand All @@ -2929,22 +2955,21 @@ private String handleEras(XPathParts parts, String value) {
}

/**
* Add examples for quarters for the gregorian calendar,
* matching each quarter type (1, 2, 3, 4) to a corresponding
* sample month and formatting an example with that date
* Add examples for quarters for the gregorian calendar, matching each quarter type (1, 2, 3, 4)
* to a corresponding sample month and formatting an example with that date
*/
private String handleQuarters(XPathParts parts, String value) {
String calendarId = parts.getAttributeValue(3, "type");
if (!calendarId.equals("gregorian")) {
return null;
}
String width = parts.findAttributeValue("quarterWidth", "type");
String width = parts.findAttributeValue("quarterWidth", "type");
if (width.equals("narrow")) {
return null;
return null;
}
String context = parts.findAttributeValue("quarterContext", "type");
String type = parts.getAttributeValue(-1, "type"); // 1-indexed
int quarterIndex = Integer.parseInt(type) - 1;
String type = parts.getAttributeValue(-1, "type"); // 1-indexed
int quarterIndex = Integer.parseInt(type) - 1;
String skeleton = (width.equals("wide")) ? "yQQQQ" : "yQQQ";
String checkPath =
"//ldml/dates/calendars/calendar[@type=\""
Expand All @@ -2953,20 +2978,19 @@ private String handleQuarters(XPathParts parts, String value) {
+ skeleton
+ "\"]";
String dateFormat = cldrFile.getWinningValue(checkPath);
SimpleDateFormat sdf =
icuServiceBuilder.getDateFormat(calendarId, dateFormat);
SimpleDateFormat sdf = icuServiceBuilder.getDateFormat(calendarId, dateFormat);
DateFormatSymbols dfs = sdf.getDateFormatSymbols();
int widthVal = width.equals("abbreviated") ? 0 : 1;
String[] quarterNames = dfs.getQuarters(0, widthVal); // 0 for formatting
quarterNames[quarterIndex] = value;
dfs.setQuarters(quarterNames, 0, widthVal);
sdf.setDateFormatSymbols(dfs);
sdf.setTimeZone(ZONE_SAMPLE);
final int[] monthSamples = new int[]{1, 4, 7, 10}; // {feb, may, oct, nov}
final int[] monthSamples = new int[] {1, 4, 7, 10}; // {feb, may, oct, nov}
int month = monthSamples[quarterIndex];
calendar.set(1999, month, 5, 13, 25, 59);
calendar.set(1999, month, 5, 13, 25, 59);
Date sample = calendar.getTime();
return sdf.format(sample);
return sdf.format(sample);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableSet;
import com.ibm.icu.dev.test.TestFmwk;
import com.ibm.icu.impl.Relation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -35,7 +36,6 @@
import org.unicode.cldr.util.SupplementalDataInfo.PluralType;
import org.unicode.cldr.util.UnitPathType;
import org.unicode.cldr.util.With;
import com.ibm.icu.impl.Relation;

public class TestExampleGenerator extends TestFmwk {

Expand Down Expand Up @@ -1652,50 +1652,53 @@ public void TestEraMap() {
ExampleGenerator exampleGenerator = getExampleGenerator("en");
Relation<String, String> keyToSubtypes = SupplementalDataInfo.getInstance().getBcp47Keys();
Set<String> calendars = keyToSubtypes.get("ca"); // gets calendar codes
Map<String, String> codeToType = new HashMap<String, String>() {{ // calendars where code != type
put("gregory", "gregorian");
put("ethioaa", "ethiopic-amete-alem");
put("islamic-civil", "islamic");
put("islamic-rgsa", "islamic");
put("islamic-tbla", "islamic");
put("islamic-umalqura", "islamic");
put("islamicc", "islamic");
}};
Map<String, String> codeToType =
new HashMap<String, String>() {
{ // calendars where code != type
put("gregory", "gregorian");
put("iso8601", "gregorian");
put("ethioaa", "ethiopic-amete-alem");
put("islamic-civil", "islamic");
put("islamic-rgsa", "islamic");
put("islamic-tbla", "islamic");
put("islamic-umalqura", "islamic");
put("islamicc", "islamic");
}
};
for (String id : calendars) {
if (id.equals("iso8601")) { // generic
continue;
}
if (codeToType.containsKey(id)) {
id = codeToType.get(id);
}
Map<String, List<Date>> calendarMap = exampleGenerator.CALENDAR_ERAS;
assertTrue("CALENDAR_ERAS map contains calendar type \"" + id + "\"", calendarMap.containsKey(id));
}
assertTrue(
"CALENDAR_ERAS map contains calendar type \"" + id + "\"",
calendarMap.containsKey(id));
}
}

public void TestEraFormats() {
public void TestEraFormats() {
ExampleGenerator exampleGeneratorJa = getExampleGenerator("ja");
ExampleGenerator exampleGeneratorEs = getExampleGenerator("es");
ExampleGenerator exampleGeneratorZh = getExampleGenerator("zh");
checkValue(
"japanese type=235 abbreviated",
"〖平成1年〗",
exampleGeneratorJa,
"japanese type=235 abbreviated",
"〖平成1年〗",
exampleGeneratorJa,
"//ldml/dates/calendars/calendar[@type=\"japanese\"]/eras/eraAbbr/era[@type=\"235\"]");
checkValue(
"gregorian type=0 wide",
"〖1 antes de Cristo〗",
exampleGeneratorEs,
"gregorian type=0 wide",
"〖1 antes de Cristo〗",
exampleGeneratorEs,
"//ldml/dates/calendars/calendar[@type=\"gregorian\"]/eras/eraNames/era[@type=\"0\"]");
checkValue(
"gregorian type=0-variant wide",
"〖1 antes de la era común〗",
exampleGeneratorEs,
"gregorian type=0-variant wide",
"〖1 antes de la era común〗",
exampleGeneratorEs,
"//ldml/dates/calendars/calendar[@type=\"gregorian\"]/eras/eraNames/era[@type=\"0\"][@alt=\"variant\"]");
checkValue(
"roc type=1 abbreviated",
"〖民国1年〗",
exampleGeneratorZh,
"roc type=1 abbreviated",
"〖民国1年〗",
exampleGeneratorZh,
"//ldml/dates/calendars/calendar[@type=\"roc\"]/eras/eraAbbr/era[@type=\"1\"]");
}

Expand Down Expand Up @@ -1723,5 +1726,3 @@ public void TestQuarterFormats() {
"//ldml/dates/calendars/calendar[@type=\"gregorian\"]/quarters/quarterContext[@type=\"stand-alone\"]/quarterWidth[@type=\"abbreviated\"]/quarter[@type=\"4\"]");
}
}


0 comments on commit 666d719

Please sign in to comment.