Search in sources :

Example 1 with Skeleton

use of com.github.anba.es6draft.runtime.objects.intl.DateFieldSymbolTable.Skeleton in project es6draft by anba.

the class DateTimeFormatConstructor method BasicFormatMatcher.

/**
     * 12.1.3 BasicFormatMatcher (options, formats)
     * 
     * @param formatRecord
     *            the format matcher record
     * @param dataLocale
     *            the locale
     * @return the basic format matcher
     */
public static String BasicFormatMatcher(FormatMatcherRecord formatRecord, String dataLocale) {
    ULocale locale = ULocale.forLanguageTag(dataLocale);
    DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale);
    // ICU4J only provides access to date- or time-only skeletons, with the exception of the
    // weekday property, which may also appear in time-only skeletons or as a single skeleton
    // property. That means we need to handle four different cases:
    // 1) formatRecord contains only date properties
    // 2) formatRecord contains only time properties
    // 3) formatRecord contains date and time properties
    // 4) formatRecord contains only the weekday property
    boolean optDate = formatRecord.isDate();
    boolean optTime = formatRecord.isTime();
    boolean optDateTime = optDate && optTime;
    // get the preferred hour representation (12-hour-cycle or 24-hour-cycle)
    boolean optHour12 = formatRecord.isHour12(locale);
    // handle date and time patterns separately
    int bestDateScore = Integer.MIN_VALUE, bestTimeScore = Integer.MIN_VALUE;
    String bestDateFormat = null, bestTimeFormat = null;
    Map<String, String> skeletons = addCanonicalSkeletons(generator.getSkeletons(null));
    for (Map.Entry<String, String> entry : skeletons.entrySet()) {
        Skeleton skeleton = new Skeleton(entry.getKey());
        // getSkeletons() does not return any date+time skeletons
        assert !(skeleton.isDate() && skeleton.isTime());
        // skip skeleton if it contains unsupported fields
        if (skeleton.has(DateField.Quarter) || skeleton.has(DateField.Week)) {
            continue;
        }
        if (skeleton.has(DateField.Year) && skeleton.getSymbol(DateField.Year) != 'y') {
            continue;
        }
        if (skeleton.has(DateField.Day) && skeleton.getSymbol(DateField.Day) != 'd') {
            continue;
        }
        if (skeleton.has(DateField.Second) && skeleton.getSymbol(DateField.Second) != 's') {
            continue;
        }
        if (optDateTime) {
            // the date-skeleton part
            if (skeleton.isTime() && skeleton.has(DateField.Weekday)) {
                continue;
            }
            // skip time-skeleton if hour representation does not match requested value
            if (skeleton.isTime() && skeleton.isHour12() != optHour12) {
                continue;
            }
            if (skeleton.isDate()) {
                int score = computeScore(formatRecord, skeleton);
                if (score > bestDateScore) {
                    bestDateScore = score;
                    bestDateFormat = entry.getValue();
                }
            } else {
                int score = computeScore(formatRecord, skeleton);
                if (score > bestTimeScore) {
                    bestTimeScore = score;
                    bestTimeFormat = entry.getValue();
                }
            }
        } else if (optDate) {
            // skip time-skeletons if only date fields were requested
            if (skeleton.isTime()) {
                continue;
            }
            int score = computeScore(formatRecord, skeleton);
            if (score > bestDateScore) {
                bestDateScore = score;
                bestDateFormat = entry.getValue();
            }
        } else if (optTime) {
            // skip date-skeletons if only time fields were requested
            if (skeleton.isDate()) {
                continue;
            }
            // skip time-skeleton if hour representation does not match requested value
            if (skeleton.isHour12() != optHour12) {
                continue;
            }
            int score = computeScore(formatRecord, skeleton);
            if (score > bestTimeScore) {
                bestTimeScore = score;
                bestTimeFormat = entry.getValue();
            }
        } else {
            // weekday-only case
            int score = computeScore(formatRecord, skeleton);
            if (score > bestDateScore) {
                bestDateScore = score;
                bestDateFormat = entry.getValue();
            }
        }
    }
    assert !optDate || bestDateFormat != null;
    assert !optTime || bestTimeFormat != null;
    assert !(!optDate && !optTime) || bestDateFormat != null;
    if (optDateTime) {
        String dateTimeFormat = generator.getDateTimeFormat();
        return MessageFormat.format(dateTimeFormat, bestTimeFormat, bestDateFormat);
    }
    if (optTime) {
        return bestTimeFormat;
    }
    return bestDateFormat;
}
Also used : ULocale(com.ibm.icu.util.ULocale) DateTimePatternGenerator(com.ibm.icu.text.DateTimePatternGenerator) Skeleton(com.github.anba.es6draft.runtime.objects.intl.DateFieldSymbolTable.Skeleton) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Aggregations

Skeleton (com.github.anba.es6draft.runtime.objects.intl.DateFieldSymbolTable.Skeleton)1 DateTimePatternGenerator (com.ibm.icu.text.DateTimePatternGenerator)1 ULocale (com.ibm.icu.util.ULocale)1 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1