Search in sources :

Example 1 with NameType

use of android.icu.text.TimeZoneNames.NameType in project j2objc by google.

the class TimeZoneFormat method parseExemplarLocation.

/**
 * Parse an exemplar location string.
 * @param text the text contains an exemplar location string at the position.
 * @param pos the position.
 * @return The zone ID for the parsed exemplar location.
 */
private String parseExemplarLocation(String text, ParsePosition pos) {
    int startIdx = pos.getIndex();
    int parsedPos = -1;
    String tzID = null;
    EnumSet<NameType> nameTypes = EnumSet.of(NameType.EXEMPLAR_LOCATION);
    Collection<MatchInfo> exemplarMatches = _tznames.find(text, startIdx, nameTypes);
    if (exemplarMatches != null) {
        MatchInfo exemplarMatch = null;
        for (MatchInfo match : exemplarMatches) {
            if (startIdx + match.matchLength() > parsedPos) {
                exemplarMatch = match;
                parsedPos = startIdx + match.matchLength();
            }
        }
        if (exemplarMatch != null) {
            tzID = getTimeZoneID(exemplarMatch.tzID(), exemplarMatch.mzID());
            pos.setIndex(parsedPos);
        }
    }
    if (tzID == null) {
        pos.setErrorIndex(startIdx);
    }
    return tzID;
}
Also used : GenericMatchInfo(android.icu.impl.TimeZoneGenericNames.GenericMatchInfo) MatchInfo(android.icu.text.TimeZoneNames.MatchInfo) GenericNameType(android.icu.impl.TimeZoneGenericNames.GenericNameType) NameType(android.icu.text.TimeZoneNames.NameType) AttributedString(java.text.AttributedString)

Example 2 with NameType

use of android.icu.text.TimeZoneNames.NameType in project j2objc by google.

the class TimeZoneGenericNames method loadStrings.

private synchronized void loadStrings(String tzCanonicalID) {
    if (tzCanonicalID == null || tzCanonicalID.length() == 0) {
        return;
    }
    // getGenericLocationName() formats a name and put it into the trie
    getGenericLocationName(tzCanonicalID);
    // Generic partial location format
    Set<String> mzIDs = _tznames.getAvailableMetaZoneIDs(tzCanonicalID);
    for (String mzID : mzIDs) {
        // if this time zone is not the golden zone of the meta zone,
        // partial location name (such as "PT (Los Angeles)") might be
        // available.
        String goldenID = _tznames.getReferenceZoneID(mzID, getTargetRegion());
        if (!tzCanonicalID.equals(goldenID)) {
            for (NameType genNonLocType : GENERIC_NON_LOCATION_TYPES) {
                String mzGenName = _tznames.getMetaZoneDisplayName(mzID, genNonLocType);
                if (mzGenName != null) {
                    // getPartialLocationName() formats a name and put it into the trie
                    getPartialLocationName(tzCanonicalID, mzID, (genNonLocType == NameType.LONG_GENERIC), mzGenName);
                }
            }
        }
    }
}
Also used : NameType(android.icu.text.TimeZoneNames.NameType)

Example 3 with NameType

use of android.icu.text.TimeZoneNames.NameType in project j2objc by google.

the class TimeZoneGenericNames method formatGenericNonLocationName.

/**
 * Private method to get a generic string, with fallback logics involved,
 * that is,
 *
 * 1. If a generic non-location string is available for the zone, return it.
 * 2. If a generic non-location string is associated with a meta zone and
 *    the zone never use daylight time around the given date, use the standard
 *    string (if available).
 * 3. If a generic non-location string is associated with a meta zone and
 *    the offset at the given time is different from the preferred zone for the
 *    current locale, then return the generic partial location string (if available)
 * 4. If a generic non-location string is not available, use generic location
 *    string.
 *
 * @param tz the requested time zone
 * @param date the date
 * @param type the generic name type, either LONG or SHORT
 * @return the name used for a generic name type, which could be the
 * generic name, or the standard name (if the zone does not observes DST
 * around the date), or the partial location name.
 */
private String formatGenericNonLocationName(TimeZone tz, GenericNameType type, long date) {
    assert (type == GenericNameType.LONG || type == GenericNameType.SHORT);
    String tzID = ZoneMeta.getCanonicalCLDRID(tz);
    if (tzID == null) {
        return null;
    }
    // Try to get a name from time zone first
    NameType nameType = (type == GenericNameType.LONG) ? NameType.LONG_GENERIC : NameType.SHORT_GENERIC;
    String name = _tznames.getTimeZoneDisplayName(tzID, nameType);
    if (name != null) {
        return name;
    }
    // Try meta zone
    String mzID = _tznames.getMetaZoneID(tzID, date);
    if (mzID != null) {
        boolean useStandard = false;
        int[] offsets = { 0, 0 };
        tz.getOffset(date, false, offsets);
        if (offsets[1] == 0) {
            useStandard = true;
            // Check if the zone actually uses daylight saving time around the time
            if (tz instanceof BasicTimeZone) {
                BasicTimeZone btz = (BasicTimeZone) tz;
                TimeZoneTransition before = btz.getPreviousTransition(date, true);
                if (before != null && (date - before.getTime() < DST_CHECK_RANGE) && before.getFrom().getDSTSavings() != 0) {
                    useStandard = false;
                } else {
                    TimeZoneTransition after = btz.getNextTransition(date, false);
                    if (after != null && (after.getTime() - date < DST_CHECK_RANGE) && after.getTo().getDSTSavings() != 0) {
                        useStandard = false;
                    }
                }
            } else {
                // If not BasicTimeZone... only if the instance is not an ICU's implementation.
                // We may get a wrong answer in edge case, but it should practically work OK.
                int[] tmpOffsets = new int[2];
                tz.getOffset(date - DST_CHECK_RANGE, false, tmpOffsets);
                if (tmpOffsets[1] != 0) {
                    useStandard = false;
                } else {
                    tz.getOffset(date + DST_CHECK_RANGE, false, tmpOffsets);
                    if (tmpOffsets[1] != 0) {
                        useStandard = false;
                    }
                }
            }
        }
        if (useStandard) {
            NameType stdNameType = (nameType == NameType.LONG_GENERIC) ? NameType.LONG_STANDARD : NameType.SHORT_STANDARD;
            String stdName = _tznames.getDisplayName(tzID, stdNameType, date);
            if (stdName != null) {
                name = stdName;
                // TODO: revisit this issue later
                // In CLDR, a same display name is used for both generic and standard
                // for some meta zones in some locales.  This looks like a data bugs.
                // For now, we check if the standard name is different from its generic
                // name below.
                String mzGenericName = _tznames.getMetaZoneDisplayName(mzID, nameType);
                if (stdName.equalsIgnoreCase(mzGenericName)) {
                    name = null;
                }
            }
        }
        if (name == null) {
            // Get a name from meta zone
            String mzName = _tznames.getMetaZoneDisplayName(mzID, nameType);
            if (mzName != null) {
                // Check if we need to use a partial location format.
                // This check is done by comparing offset with the meta zone's
                // golden zone at the given date.
                String goldenID = _tznames.getReferenceZoneID(mzID, getTargetRegion());
                if (goldenID != null && !goldenID.equals(tzID)) {
                    TimeZone goldenZone = TimeZone.getFrozenTimeZone(goldenID);
                    int[] offsets1 = { 0, 0 };
                    // Check offset in the golden zone with wall time.
                    // With getOffset(date, false, offsets1),
                    // you may get incorrect results because of time overlap at DST->STD
                    // transition.
                    goldenZone.getOffset(date + offsets[0] + offsets[1], true, offsets1);
                    if (offsets[0] != offsets1[0] || offsets[1] != offsets1[1]) {
                        // Now we need to use a partial location format.
                        name = getPartialLocationName(tzID, mzID, (nameType == NameType.LONG_GENERIC), mzName);
                    } else {
                        name = mzName;
                    }
                } else {
                    name = mzName;
                }
            }
        }
    }
    return name;
}
Also used : BasicTimeZone(android.icu.util.BasicTimeZone) TimeZoneTransition(android.icu.util.TimeZoneTransition) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) NameType(android.icu.text.TimeZoneNames.NameType)

Example 4 with NameType

use of android.icu.text.TimeZoneNames.NameType in project j2objc by google.

the class TimeZone method _getDisplayName.

/**
 * internal version (which is called by public APIs) accepts
 * SHORT, LONG, SHORT_GENERIC, LONG_GENERIC, SHORT_GMT, LONG_GMT,
 * SHORT_COMMONLY_USED and GENERIC_LOCATION.
 */
private String _getDisplayName(int style, boolean daylight, ULocale locale) {
    if (locale == null) {
        throw new NullPointerException("locale is null");
    }
    String result = null;
    if (style == GENERIC_LOCATION || style == LONG_GENERIC || style == SHORT_GENERIC) {
        // Generic format
        TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
        long date = System.currentTimeMillis();
        Output<TimeType> timeType = new Output<TimeType>(TimeType.UNKNOWN);
        switch(style) {
            case GENERIC_LOCATION:
                result = tzfmt.format(Style.GENERIC_LOCATION, this, date, timeType);
                break;
            case LONG_GENERIC:
                result = tzfmt.format(Style.GENERIC_LONG, this, date, timeType);
                break;
            case SHORT_GENERIC:
                result = tzfmt.format(Style.GENERIC_SHORT, this, date, timeType);
                break;
        }
        // appropriate for the requested daylight value.
        if (daylight && timeType.value == TimeType.STANDARD || !daylight && timeType.value == TimeType.DAYLIGHT) {
            int offset = daylight ? getRawOffset() + getDSTSavings() : getRawOffset();
            result = (style == SHORT_GENERIC) ? tzfmt.formatOffsetShortLocalizedGMT(offset) : tzfmt.formatOffsetLocalizedGMT(offset);
        }
    } else if (style == LONG_GMT || style == SHORT_GMT) {
        // Offset format
        TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
        int offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
        switch(style) {
            case LONG_GMT:
                result = tzfmt.formatOffsetLocalizedGMT(offset);
                break;
            case SHORT_GMT:
                result = tzfmt.formatOffsetISO8601Basic(offset, false, false, false);
                break;
        }
    } else {
        // Specific format
        assert (style == LONG || style == SHORT || style == SHORT_COMMONLY_USED);
        // Gets the name directly from TimeZoneNames
        long date = System.currentTimeMillis();
        TimeZoneNames tznames = TimeZoneNames.getInstance(locale);
        NameType nameType = null;
        switch(style) {
            case LONG:
                nameType = daylight ? NameType.LONG_DAYLIGHT : NameType.LONG_STANDARD;
                break;
            case SHORT:
            case SHORT_COMMONLY_USED:
                nameType = daylight ? NameType.SHORT_DAYLIGHT : NameType.SHORT_STANDARD;
                break;
        }
        result = tznames.getDisplayName(ZoneMeta.getCanonicalCLDRID(this), nameType, date);
        if (result == null) {
            // Fallback to localized GMT
            TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
            int offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
            result = (style == LONG) ? tzfmt.formatOffsetLocalizedGMT(offset) : tzfmt.formatOffsetShortLocalizedGMT(offset);
        }
    }
    assert (result != null);
    return result;
}
Also used : TimeZoneNames(android.icu.text.TimeZoneNames) NameType(android.icu.text.TimeZoneNames.NameType) TimeZoneFormat(android.icu.text.TimeZoneFormat) TimeType(android.icu.text.TimeZoneFormat.TimeType)

Example 5 with NameType

use of android.icu.text.TimeZoneNames.NameType in project j2objc by google.

the class TimeZoneFormatTest method TestGetDisplayNames.

@Test
public void TestGetDisplayNames() {
    long date = System.currentTimeMillis();
    NameType[] types = new NameType[] { NameType.LONG_STANDARD, NameType.LONG_DAYLIGHT, NameType.SHORT_STANDARD, NameType.SHORT_DAYLIGHT };
    Set<String> zones = ZoneMeta.getAvailableIDs(SystemTimeZoneType.ANY, null, null);
    int casesTested = 0;
    Random rnd = new Random(2016);
    for (ULocale uloc : ULocale.getAvailableLocales()) {
        if (rnd.nextDouble() > 0.01) {
            continue;
        }
        for (String zone : zones) {
            if (rnd.nextDouble() > 0.01) {
                continue;
            }
            casesTested++;
            // Test default TimeZoneNames (uses an overridden getDisplayNames)
            {
                TimeZoneNames tznames = TimeZoneNames.getInstance(uloc);
                tznames.loadAllDisplayNames();
                String[] result = new String[types.length];
                tznames.getDisplayNames(zone, types, date, result, 0);
                for (int i = 0; i < types.length; i++) {
                    NameType type = types[i];
                    String expected = result[i];
                    String actual = tznames.getDisplayName(zone, type, date);
                    assertEquals("TimeZoneNames: getDisplayNames() returns different result than getDisplayName()" + " for " + zone + " in locale " + uloc, expected, actual);
                }
                // Coverage for empty call to getDisplayNames
                tznames.getDisplayNames(null, null, 0, null, 0);
            }
            // Test TZDBTimeZoneNames (uses getDisplayNames from abstract class)
            {
                TimeZoneNames tznames = new TZDBTimeZoneNames(uloc);
                tznames.loadAllDisplayNames();
                String[] result = new String[types.length];
                tznames.getDisplayNames(zone, types, date, result, 0);
                for (int i = 0; i < types.length; i++) {
                    NameType type = types[i];
                    String expected = result[i];
                    String actual = tznames.getDisplayName(zone, type, date);
                    assertEquals("TZDBTimeZoneNames: getDisplayNames() returns different result than getDisplayName()" + " for " + zone + " in locale " + uloc, expected, actual);
                }
                // Coverage for empty call to getDisplayNames
                tznames.getDisplayNames(null, null, 0, null, 0);
            }
        }
    }
    assertTrue("No cases were tested", casesTested > 0);
}
Also used : ULocale(android.icu.util.ULocale) Random(java.util.Random) TZDBTimeZoneNames(android.icu.impl.TZDBTimeZoneNames) TimeZoneNames(android.icu.text.TimeZoneNames) NameType(android.icu.text.TimeZoneNames.NameType) TZDBTimeZoneNames(android.icu.impl.TZDBTimeZoneNames) Test(org.junit.Test)

Aggregations

NameType (android.icu.text.TimeZoneNames.NameType)5 TimeZoneNames (android.icu.text.TimeZoneNames)2 TZDBTimeZoneNames (android.icu.impl.TZDBTimeZoneNames)1 GenericMatchInfo (android.icu.impl.TimeZoneGenericNames.GenericMatchInfo)1 GenericNameType (android.icu.impl.TimeZoneGenericNames.GenericNameType)1 TimeZoneFormat (android.icu.text.TimeZoneFormat)1 TimeType (android.icu.text.TimeZoneFormat.TimeType)1 MatchInfo (android.icu.text.TimeZoneNames.MatchInfo)1 BasicTimeZone (android.icu.util.BasicTimeZone)1 TimeZone (android.icu.util.TimeZone)1 TimeZoneTransition (android.icu.util.TimeZoneTransition)1 ULocale (android.icu.util.ULocale)1 AttributedString (java.text.AttributedString)1 Random (java.util.Random)1 Test (org.junit.Test)1