-
Notifications
You must be signed in to change notification settings - Fork 423
CLDR-19106 check for empty datetimeformats #5219
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ | |
| import org.unicode.cldr.util.XPathParts; | ||
|
|
||
| public class CheckDates extends FactoryCheckCLDR { | ||
| private static final boolean DISABLE = true; | ||
| private static final boolean DEBUG = false; | ||
| private static final boolean DISABLE_DATE_ORDER = true; | ||
|
|
||
|
|
@@ -272,6 +273,8 @@ public CheckCLDR handleCheck( | |
| return this; | ||
| } | ||
|
|
||
| XPathParts parts = XPathParts.getFrozenInstance(fullPath); | ||
|
|
||
| if (!path.contains("/dates") || path.endsWith("/default") || path.endsWith("/alias")) { | ||
| return this; | ||
| } | ||
|
|
@@ -466,6 +469,10 @@ && isTooMuchWiderThan(value, abbrValue) | |
| final String collisionPrefix = "//ldml/dates/calendars/calendar"; | ||
| main: | ||
| if (path.startsWith(collisionPrefix)) { | ||
| if (parts.containsElement("dateTimeFormats") | ||
| && !parts.containsElement("appendItems")) { | ||
| checkDateTimeFormats(path, parts, value, result); | ||
| } | ||
| int pos = path.indexOf("\"]"); // end of first type | ||
| if (pos < 0 || skipPath(path)) { // skip narrow, no-calendar | ||
| break main; | ||
|
|
@@ -495,7 +502,6 @@ && isTooMuchWiderThan(value, abbrValue) | |
| DayPeriod dayPeriod = null; | ||
| final boolean isDayPeriod = path.contains("dayPeriod"); | ||
| if (isDayPeriod) { | ||
| XPathParts parts = XPathParts.getFrozenInstance(fullPath); | ||
| type = | ||
| Type.fromString( | ||
| parts.getAttributeValue(5, "type")); // format, stand-alone | ||
|
|
@@ -680,6 +686,93 @@ && isTooMuchWiderThan(value, abbrValue) | |
| return this; | ||
| } | ||
|
|
||
| private static final Pattern datePatternDoesntEndsWithDigits = | ||
| Pattern.compile(".*(MMM|LLL|[^yd])"); | ||
| private static final XPathParts timeParts = | ||
| XPathParts.getFrozenInstance( | ||
| "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/timeFormats/timeFormatLength[@type=\"short\"]/timeFormat[@type=\"standard\"]/pattern[@type=\"standard\"]"); | ||
| private static final XPathParts dateParts = | ||
| XPathParts.getFrozenInstance( | ||
| "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/dateFormats/dateFormatLength[@type=\"short\"]/dateFormat[@type=\"standard\"]/pattern[@type=\"standard\"]"); | ||
| private static final XPathParts availableParts = | ||
| XPathParts.getFrozenInstance( | ||
| "//ldml/dates/calendars/calendar[@type=\"generic\"]/dateTimeFormats/availableFormats/dateFormatItem[@id=\"yyyyQQQ\"]"); | ||
|
|
||
| private static final int calendarIndex = timeParts.findElement("calendar"); | ||
| private static final int dateFormatLengthIndex = timeParts.findElement("dateFormatLength"); | ||
| private static final int dateFormatIndex = timeParts.findElement("timeFormat"); | ||
|
|
||
| // NOTE: the timeFormatLength is always at the same position as the dateFormatLength, but just | ||
| // for consistency... | ||
|
|
||
| private void checkDateTimeFormats( | ||
| String path, XPathParts parts, String value, List<CheckStatus> result) { | ||
|
|
||
| // Make sure that if the format has adjacent placeholders, we don't run numbers together | ||
| if (!value.contains("}{")) { // only worry about adjacents | ||
| return; | ||
| } | ||
| // we assume that relative dates don't end with digits | ||
| if ("relative".equals(parts.getAttributeValue(dateFormatIndex, "type"))) { | ||
| return; | ||
| } | ||
|
|
||
| String length = parts.getAttributeValue(dateFormatLengthIndex, "type"); | ||
| String calendar = parts.getAttributeValue(calendarIndex, "type"); | ||
|
|
||
| // By the spec, the dateTimeFormatLength will be the same as the dateFormatLength | ||
| // So see if the corresponding date ends with digits | ||
| // However, this only accounts for the stock formats; not combinations of available. | ||
|
|
||
| CLDRFile resolvedCldrFile = getResolvedCldrFileToCheck(); | ||
|
|
||
| XPathParts currentDateParts = | ||
| dateParts | ||
| .cloneAsThawed() | ||
| .setAttribute(calendarIndex, "type", calendar) | ||
| .setAttribute(dateFormatLengthIndex, "type", length); | ||
| String dateValue = resolvedCldrFile.getStringValue(currentDateParts.toString()); | ||
| if (dateValue == null) { | ||
| throw new IllegalArgumentException( | ||
| "Missing value: " + currentDateParts); // should never occur | ||
| } | ||
|
|
||
| // TODO: This is a first pass, to correct the bad cases. | ||
| // We should check later to see if we can allow more cases. | ||
| // IN THAT CASE, the following code could be the start of that, so it is left here, but | ||
| // inoperative | ||
|
|
||
| if (!DISABLE) { | ||
| // all current formats are date + time, not reverse | ||
| if (value.contains("{0}{1}")) { | ||
| throw new IllegalArgumentException("Enhance checks to handle {0}{1}"); | ||
| } | ||
|
|
||
| // TODO: also check the available formats that are for a date | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an important TODO. We use these glue patterns to assemble date+time patterns from availableFormats.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. At first I wasn't checking for this, and just the short datetime pattern needed to be fixed. But then I realized that it needed to be extended. |
||
|
|
||
| if (datePatternDoesntEndsWithDigits.matcher(dateValue).matches()) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // My first version of this checked the corresponding time formats to see which could start | ||
| // with an integer. | ||
| // However, when we count available formats, essentially all locales have a time format | ||
| // starting with HH | ||
|
|
||
| String mergedPattern = value.replace("{1}", dateValue).replace("{0}", "HH…"); | ||
| CheckStatus item = | ||
| new CheckStatus() | ||
| .setCause(this) | ||
| .setMainType(CheckStatus.errorType) | ||
| .setSubtype(Subtype.illegalDatePattern) | ||
| .setMessage( | ||
| "The date and time must not have adjacent integers. " | ||
| + "This pattern could produce {0}", | ||
| mergedPattern); | ||
| result.add(item); | ||
| } | ||
|
|
||
| private void checkTimeFormatMatchesRegion(String value, List<CheckStatus> result) { | ||
| String localeID = getResolvedCldrFileToCheck().getLocaleID(); | ||
| if (LocaleNames.ROOT.equals(localeID)) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: this means we're checking all of the datetime glue patterns (short/medium/long/full and standard/atTime), correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm,, I had approved this when it was still draft, but somehow that got erased. Re-approving now.