Skip to content

Commit a20ea88

Browse files
committed
CLDR-17851 fix for BH era
- BH, added in CLDR-18464, is out of order… attempt to find the prev and next era by date.
1 parent 398b9cb commit a20ea88

File tree

2 files changed

+70
-26
lines changed

2 files changed

+70
-26
lines changed

tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,19 +3120,34 @@ private void handleEras(XPathParts parts, String value, List<String> examples) {
31203120
if (eraData == null) {
31213121
return; // no era data
31223122
}
3123-
GregorianCalendar startCal = forDateString(eraData.getStart());
3124-
GregorianCalendar endCal = forDateString(eraData.getEnd());
3123+
GregorianCalendar startCal = eraData.getStartCalendar();
3124+
GregorianCalendar endCal = eraData.getEndCalendar();
31253125

3126-
final SupplementalCalendarData.EraData prevEra = calendarData.get(typeIndex - 1);
3127-
final SupplementalCalendarData.EraData nextEra = calendarData.get(typeIndex + 1);
3126+
final SupplementalCalendarData.EraData eminusone = calendarData.get(typeIndex - 1);
3127+
final SupplementalCalendarData.EraData eplusone = calendarData.get(typeIndex + 1);
3128+
3129+
SupplementalCalendarData.EraData prevEra = null;
3130+
SupplementalCalendarData.EraData nextEra = null;
3131+
3132+
// see if we can find the 'prev' and 'next' era by date
3133+
if (eminusone != null && eminusone.compareTo(eraData) < 0) {
3134+
prevEra = eminusone;
3135+
} else if (eplusone != null && eplusone.compareTo(eraData) < 0) {
3136+
prevEra = eplusone;
3137+
}
3138+
if (eminusone != null && eminusone.compareTo(eraData) > 0) {
3139+
nextEra = eminusone;
3140+
} else if (eplusone != null && eplusone.compareTo(eraData) > 0) {
3141+
nextEra = eplusone;
3142+
}
31283143

31293144
if (startCal == null && prevEra != null && prevEra.getEnd() != null) {
3130-
startCal = forDateString(prevEra.getEnd());
3145+
startCal = prevEra.getEndCalendar();
31313146
// shift forward so we are in the next era
31323147
startCal.setTimeInMillis(startCal.getTimeInMillis() + (DateConstants.MILLIS_PER_DAY));
31333148
}
31343149
if (endCal == null && nextEra != null && nextEra.getStart() != null) {
3135-
endCal = forDateString(nextEra.getStart());
3150+
endCal = nextEra.getStartCalendar();
31363151
// shift backward so we are in the prev era
31373152
endCal.setTimeInMillis(endCal.getTimeInMillis() - (DateConstants.MILLIS_PER_DAY));
31383153
}
@@ -3150,7 +3165,7 @@ private void handleEras(XPathParts parts, String value, List<String> examples) {
31503165
sampleDate.setTimeInMillis(
31513166
sampleDate.getTimeInMillis() - (DateConstants.MILLIS_PER_DAY));
31523167
} else if (startCal != null && endCal == null) {
3153-
sampleDate = forDateString("2002-07-15"); // CLDR repo root commit
3168+
sampleDate = new GregorianCalendar(2002, 6, 15); // CLDR repo root commit
31543169
if (sampleDate.before(startCal)) {
31553170
sampleDate = startCal;
31563171
sampleDate.setTimeInMillis(
@@ -3183,24 +3198,6 @@ private void handleEras(XPathParts parts, String value, List<String> examples) {
31833198
examples.add(sdf.format(sample));
31843199
}
31853200

3186-
private GregorianCalendar forDateString(String ymd) {
3187-
if (ymd == null) return null;
3188-
int multiplier = 1;
3189-
if (ymd.startsWith("-")) {
3190-
multiplier = -1;
3191-
ymd = ymd.substring(1);
3192-
}
3193-
final String[] parts = ymd.split("-");
3194-
try {
3195-
return new GregorianCalendar(
3196-
multiplier * Integer.parseInt(parts[0]),
3197-
Integer.parseInt(parts[1]) - 1,
3198-
Integer.parseInt(parts[2]));
3199-
} catch (NumberFormatException nfe) {
3200-
throw new IllegalArgumentException("While parsing date string " + ymd, nfe);
3201-
}
3202-
}
3203-
32043201
/**
32053202
* Add examples for quarters for the gregorian calendar, matching each quarter type (1, 2, 3, 4)
32063203
* to a corresponding sample month and formatting an example with that date

tools/cldr-code/src/main/java/org/unicode/cldr/util/SupplementalCalendarData.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.unicode.cldr.util;
22

33
import com.ibm.icu.impl.locale.XCldrStub.ImmutableMap;
4+
import com.ibm.icu.util.GregorianCalendar;
45
import java.util.HashMap;
56
import java.util.Iterator;
67
import java.util.Map;
@@ -10,7 +11,7 @@
1011

1112
public class SupplementalCalendarData implements Iterable<String> {
1213
/** an <era> element */
13-
public static class EraData {
14+
public static class EraData implements Comparable<EraData> {
1415
static final int INDEX = 4; // index of our element
1516

1617
/** the <era> xpath */
@@ -33,6 +34,14 @@ public String getEnd() {
3334
return xpath.getAttributeValue(INDEX, LDMLConstants.END);
3435
}
3536

37+
public GregorianCalendar getStartCalendar() {
38+
return forDateString(getStart());
39+
}
40+
41+
public GregorianCalendar getEndCalendar() {
42+
return forDateString(getEnd());
43+
}
44+
3645
public String getCode() {
3746
return xpath.getAttributeValue(INDEX, LDMLConstants.CODE);
3847
}
@@ -49,6 +58,44 @@ public String toString() {
4958
"ERA %d, [%s-%s] code=%s, aliases=%s",
5059
getType(), getStart(), getEnd(), getCode(), String.join(",", getAliases()));
5160
}
61+
62+
private static final GregorianCalendar forDateString(String ymd) {
63+
if (ymd == null) return null;
64+
int multiplier = 1;
65+
if (ymd.startsWith("-")) {
66+
multiplier = -1;
67+
ymd = ymd.substring(1);
68+
}
69+
final String[] parts = ymd.split("-");
70+
try {
71+
return new GregorianCalendar(
72+
multiplier * Integer.parseInt(parts[0]),
73+
Integer.parseInt(parts[1]) - 1,
74+
Integer.parseInt(parts[2]));
75+
} catch (NumberFormatException nfe) {
76+
throw new IllegalArgumentException("While parsing date string " + ymd, nfe);
77+
}
78+
}
79+
80+
private GregorianCalendar getLatest() {
81+
GregorianCalendar l = getEndCalendar();
82+
if (l == null) l = getStartCalendar();
83+
return l;
84+
}
85+
86+
/** only works within the same cal system */
87+
@Override
88+
public int compareTo(EraData o) {
89+
final GregorianCalendar l = getLatest();
90+
final GregorianCalendar ol = o.getLatest();
91+
if (l == null || ol == null) {
92+
// compare by id
93+
return Integer.compare(getType(), o.getType());
94+
} else {
95+
// compare by date
96+
return l.compareTo(ol);
97+
}
98+
}
5299
}
53100

54101
/** a <calendar type=> element */

0 commit comments

Comments
 (0)