Skip to content

🚧 Cldr 17851/sdi era metadata bis #4648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,13 @@ public class LDMLConstants {
public static final String SEGMENTATIONS = "segmentations";
public static final String TIMEZONE_DATA = "timezoneData";
public static final String CALENDAR_DATA = "calendarData";
public static final String CALENDAR_PREFERENCE_DATA = "calendarPreferenceData";
public static final String CALENDAR_PREFERENCE = "calendarPreference";
public static final String CALENDAR_PREFERENCE_DATA = "calendarPreferenceData";
public static final String CALENDAR_SYSTEM = "calendarSystem";
public static final String INHERIT_ERAS = "inheritEras";
public static final String CODE = "code";
public static final String END = "end";
public static final String START = "start";
public static final String ORDERING = "ordering";
public static final String WEEK_DATA = "weekData";
public static final String MEASUREMENT_DATA = "measurementData";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.unicode.cldr.util.Rational.FormatStyle;
import org.unicode.cldr.util.ScriptToExemplars;
import org.unicode.cldr.util.SimpleUnicodeSetFormatter;
import org.unicode.cldr.util.SupplementalCalendarData;
import org.unicode.cldr.util.SupplementalDataInfo;
import org.unicode.cldr.util.SupplementalDataInfo.CurrencyNumberInfo;
import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo;
Expand Down Expand Up @@ -293,58 +294,6 @@ public void setCachingEnabled(boolean enabled) {

HelpMessages helpMessages;

/* 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
* 2 days before era end date to avoid time zone issues.
* 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<>() {
{ // 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(),
new GregorianCalendar(622, 6, 12).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()));
}
};

// map relativeTimePattern counts to possible numeric examples.
// For few , many, and other there is not a single number that is in that category for
// all locales, so we provide a list of values that might be good examples and use the
Expand Down Expand Up @@ -3151,30 +3100,88 @@ private void handleDateRangePattern(String value, List<String> examples) {
setBackground(dateFormat.format(DATE_SAMPLE2))));
}

/**
* 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. Generates a sample date based on this info and formats it */
private void handleEras(XPathParts parts, String value, List<String> examples) {
String calendarId = parts.getAttributeValue(3, "type");
String type = parts.getAttributeValue(-1, "type");
String id =
(calendarId.startsWith("islamic"))
? "islamic"
: calendarId; // islamic variations map to same sample
if (!CALENDAR_ERAS.containsKey(id)) {
return;
}
int typeIndex = Integer.parseInt(type);
if (calendarId.equals("japanese")) {
if (typeIndex < 235) { // examples only for 2 most recent eras
return;
} else {
typeIndex %= 235; // map to length 2 list
String id = calendarId;
if (id.equals("generic") || id.equals("iso8601")) {
id = "gregorian"; // use Gregorian eras, 'generic' is not in the data
}
final SupplementalCalendarData.CalendarData calendarData =
supplementalDataInfo.getCalendarData().get(id);

if (calendarData == null) {
throw new IllegalArgumentException("Could not load supplementalCalendarData for " + id);
}
final int typeIndex = Integer.parseInt(type);

final SupplementalCalendarData.EraData eraData = calendarData.get(typeIndex);
if (eraData == null) {
return; // no era data
}
GregorianCalendar startCal = eraData.getStartCalendar();
GregorianCalendar endCal = eraData.getEndCalendar();

final SupplementalCalendarData.EraData eminusone = calendarData.get(typeIndex - 1);
final SupplementalCalendarData.EraData eplusone = calendarData.get(typeIndex + 1);

SupplementalCalendarData.EraData prevEra = null;
SupplementalCalendarData.EraData nextEra = null;

// see if we can find the 'prev' and 'next' era by date
if (eminusone != null && eminusone.compareTo(eraData) < 0) {
prevEra = eminusone;
} else if (eplusone != null && eplusone.compareTo(eraData) < 0) {
prevEra = eplusone;
}
if (eminusone != null && eminusone.compareTo(eraData) > 0) {
nextEra = eminusone;
} else if (eplusone != null && eplusone.compareTo(eraData) > 0) {
nextEra = eplusone;
}

if (startCal == null && prevEra != null && prevEra.getEnd() != null) {
startCal = prevEra.getEndCalendar();
// shift forward so we are in the next era
startCal.setTimeInMillis(
startCal.getTimeInMillis() + DateConstants.MILLIS_PER_TWO_DAYS);
}
if (endCal == null && nextEra != null && nextEra.getStart() != null) {
endCal = nextEra.getStartCalendar();
// shift backward so we are in the prev era
endCal.setTimeInMillis(endCal.getTimeInMillis() - DateConstants.MILLIS_PER_TWO_DAYS);
}

GregorianCalendar sampleDate = null;

if (startCal != null && endCal != null) {
// roll back a day to not hit the edge
sampleDate = endCal;
sampleDate.setTimeInMillis(
sampleDate.getTimeInMillis() - DateConstants.MILLIS_PER_TWO_DAYS);
} else if (startCal == null && endCal != null) {
// roll back a day to not hit the edge
sampleDate = endCal;
sampleDate.setTimeInMillis(
sampleDate.getTimeInMillis() - DateConstants.MILLIS_PER_TWO_DAYS);
} else if (startCal != null && endCal == null) {
sampleDate = new GregorianCalendar(2002, 6, 15); // CLDR repo root commit
if (sampleDate.before(startCal)) {
sampleDate = startCal;
sampleDate.setTimeInMillis(
sampleDate.getTimeInMillis() + DateConstants.MILLIS_PER_TWO_DAYS);
}
} else {
// System.err.println("No good date for " + eraData);
// TODO: should be an error in TestSupplementalDataInfo
sampleDate = null;
}
List<Date> eraDates = CALENDAR_ERAS.get(id);
Date sample = eraDates.get(typeIndex);

if (sampleDate == null) return; // could not find the time

final Date sample = sampleDate.getTime();

String skeleton = "Gy";
String checkPath =
"//ldml/dates/calendars/calendar[@type=\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class DateConstants {
public static final long MILLIS_PER_MINUTE = MILLIS_PER_SECOND * 60;
public static final long MILLIS_PER_HOUR = MILLIS_PER_MINUTE * 60;
public static final long MILLIS_PER_DAY = MILLIS_PER_HOUR * 24;
public static final long MILLIS_PER_TWO_DAYS = 2 * MILLIS_PER_DAY;
public static final long MILLIS_PER_MONTH = MILLIS_PER_DAY * 30;

private static final Date getRecentHistory(Date d) {
Expand Down
Loading
Loading