sun.util.calendar.ZoneInfoFile Java Examples

The following examples show how to use sun.util.calendar.ZoneInfoFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TimeZoneNameProviderImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private String[] getDisplayNameArray(String id, Locale locale) {
    Objects.requireNonNull(id);
    Objects.requireNonNull(locale);

    String[] ret =
        LocaleProviderAdapter.forType(type).getLocaleResources(locale).getTimeZoneNames(id);

    if (Objects.nonNull(ret) && type == LocaleProviderAdapter.Type.CLDR) {
        // check for CLDR's "no inheritance marker"
        for (int index = 0; index < ret.length; index++) {
            TimeZone tz = null;
            if (CLDR_NO_INHERITANCE_MARKER.equals(ret[index])) {
                if (Objects.isNull(tz)) {
                    tz = TimeZone.getTimeZone(id);
                }
                int offset = tz.getRawOffset();
                if (index == 3 || index == 4) { // daylight
                    offset += tz.getDSTSavings();
                }
                ret[index] = ZoneInfoFile.toCustomID(offset);
            }
        }
    }

    return ret;
}
 
Example #2
Source File: TimeZone.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private ZoneId toZoneId0() {
    String id = getID();
    TimeZone defaultZone = defaultTimeZone;
    // are we not defaultTimeZone but our id is equal to default's?
    if (defaultZone != this &&
        defaultZone != null && id.equals(defaultZone.getID())) {
        // delegate to default TZ which is effectively immutable
        return defaultZone.toZoneId();
    }
    // derive it ourselves
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #3
Source File: CLDRTimeZoneNameProviderImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private String toGMTFormat(String id, boolean daylight, boolean isShort, Locale l) {
    TimeZone tz = ZoneInfoFile.getZoneInfo(id);
    int offset = (tz.getRawOffset() + (daylight ? tz.getDSTSavings() : 0)) / 60000;
    LocaleResources lr = LocaleProviderAdapter.forType(Type.CLDR).getLocaleResources(l);
    ResourceBundle fd = lr.getJavaTimeFormatData();

    if (offset == 0) {
        return fd.getString("timezone.gmtZeroFormat");
    } else {
        String gmtFormat = fd.getString("timezone.gmtFormat");
        String hourFormat = fd.getString("timezone.hourFormat");

        if (offset > 0) {
            hourFormat = hourFormat.substring(0, hourFormat.indexOf(";"));
        } else {
            hourFormat = hourFormat.substring(hourFormat.indexOf(";") + 1);
            offset = -offset;
        }
        hourFormat = hourFormat
            .replaceFirst("H+", (isShort ? "\\%1\\$d" : "\\%1\\$02d"))
            .replaceFirst("m+", "\\%2\\$02d");
        return MessageFormat.format(gmtFormat,
                String.format(l, hourFormat, offset / 60, offset % 60));
    }
}
 
Example #4
Source File: TimeZone.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private ZoneId toZoneId0() {
    String id = getID();
    TimeZone defaultZone = defaultTimeZone;
    // are we not defaultTimeZone but our id is equal to default's?
    if (defaultZone != this &&
        defaultZone != null && id.equals(defaultZone.getID())) {
        // delegate to default TZ which is effectively immutable
        return defaultZone.toZoneId();
    }
    // derive it ourselves
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #5
Source File: TimeZone.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #6
Source File: TimeZone.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #7
Source File: TimeZone.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #8
Source File: TimeZone.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #9
Source File: TimeZone.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #10
Source File: TimeZone.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #11
Source File: TimeZone.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #12
Source File: TimeZone.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #13
Source File: TimeZone.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #14
Source File: TimeZone.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #15
Source File: TimeZone.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #16
Source File: TimeZone.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #17
Source File: TimeZone.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #18
Source File: TimeZone.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #19
Source File: TimeZone.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts this {@code TimeZone} object to a {@code ZoneId}.
 *
 * @return a {@code ZoneId} representing the same time zone as this
 *         {@code TimeZone}
 * @since 1.8
 */
public ZoneId toZoneId() {
    String id = getID();
    if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
        if ("EST".equals(id))
            return ZoneId.of("America/New_York");
        if ("MST".equals(id))
            return ZoneId.of("America/Denver");
        if ("HST".equals(id))
            return ZoneId.of("America/Honolulu");
    }
    return ZoneId.of(id, ZoneId.SHORT_IDS);
}
 
Example #20
Source File: TimeZone.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a custom time zone identifier and returns a corresponding zone.
 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
 *
 * @param id a string of the <a href="#CustomID">custom ID form</a>.
 * @return a newly created TimeZone with the given offset and
 * no daylight saving time, or null if the id cannot be parsed.
 */
private static final TimeZone parseCustomTimeZone(String id) {
    int length;

    // Error if the length of id isn't long enough or id doesn't
    // start with "GMT".
    if ((length = id.length()) < (GMT_ID_LENGTH + 2) ||
        id.indexOf(GMT_ID) != 0) {
        return null;
    }

    ZoneInfo zi;

    // First, we try to find it in the cache with the given
    // id. Even the id is not normalized, the returned ZoneInfo
    // should have its normalized id.
    zi = ZoneInfoFile.getZoneInfo(id);
    if (zi != null) {
        return zi;
    }

    int index = GMT_ID_LENGTH;
    boolean negative = false;
    char c = id.charAt(index++);
    if (c == '-') {
        negative = true;
    } else if (c != '+') {
        return null;
    }

    int hours = 0;
    int num = 0;
    int countDelim = 0;
    int len = 0;
    while (index < length) {
        c = id.charAt(index++);
        if (c == ':') {
            if (countDelim > 0) {
                return null;
            }
            if (len > 2) {
                return null;
            }
            hours = num;
            countDelim++;
            num = 0;
            len = 0;
            continue;
        }
        if (c < '0' || c > '9') {
            return null;
        }
        num = num * 10 + (c - '0');
        len++;
    }
    if (index != length) {
        return null;
    }
    if (countDelim == 0) {
        if (len <= 2) {
            hours = num;
            num = 0;
        } else {
            hours = num / 100;
            num %= 100;
        }
    } else {
        if (len != 2) {
            return null;
        }
    }
    if (hours > 23 || num > 59) {
        return null;
    }
    int gmtOffset =  (hours * 60 + num) * 60 * 1000;

    if (gmtOffset == 0) {
        zi = ZoneInfoFile.getZoneInfo(GMT_ID);
        if (negative) {
            zi.setID("GMT-00:00");
        } else {
            zi.setID("GMT+00:00");
        }
    } else {
        zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
    }
    return zi;
}
 
Example #21
Source File: TimeZone.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Parses a custom time zone identifier and returns a corresponding zone.
 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
 *
 * @param id a string of the <a href="#CustomID">custom ID form</a>.
 * @return a newly created TimeZone with the given offset and
 * no daylight saving time, or null if the id cannot be parsed.
 */
private static final TimeZone parseCustomTimeZone(String id) {
    int length;

    // Error if the length of id isn't long enough or id doesn't
    // start with "GMT".
    if ((length = id.length()) < (GMT_ID_LENGTH + 2) ||
        id.indexOf(GMT_ID) != 0) {
        return null;
    }

    ZoneInfo zi;

    // First, we try to find it in the cache with the given
    // id. Even the id is not normalized, the returned ZoneInfo
    // should have its normalized id.
    zi = ZoneInfoFile.getZoneInfo(id);
    if (zi != null) {
        return zi;
    }

    int index = GMT_ID_LENGTH;
    boolean negative = false;
    char c = id.charAt(index++);
    if (c == '-') {
        negative = true;
    } else if (c != '+') {
        return null;
    }

    int hours = 0;
    int num = 0;
    int countDelim = 0;
    int len = 0;
    while (index < length) {
        c = id.charAt(index++);
        if (c == ':') {
            if (countDelim > 0) {
                return null;
            }
            if (len > 2) {
                return null;
            }
            hours = num;
            countDelim++;
            num = 0;
            len = 0;
            continue;
        }
        if (c < '0' || c > '9') {
            return null;
        }
        num = num * 10 + (c - '0');
        len++;
    }
    if (index != length) {
        return null;
    }
    if (countDelim == 0) {
        if (len <= 2) {
            hours = num;
            num = 0;
        } else {
            hours = num / 100;
            num %= 100;
        }
    } else {
        if (len != 2) {
            return null;
        }
    }
    if (hours > 23 || num > 59) {
        return null;
    }
    int gmtOffset =  (hours * 60 + num) * 60 * 1000;

    if (gmtOffset == 0) {
        zi = ZoneInfoFile.getZoneInfo(GMT_ID);
        if (negative) {
            zi.setID("GMT-00:00");
        } else {
            zi.setID("GMT+00:00");
        }
    } else {
        zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
    }
    return zi;
}
 
Example #22
Source File: TimeZone.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a name in the specified {@code style} of this {@code TimeZone}
 * suitable for presentation to the user in the specified {@code
 * locale}. If the specified {@code daylight} is {@code true}, a Daylight
 * Saving Time name is returned (even if this {@code TimeZone} doesn't
 * observe Daylight Saving Time). Otherwise, a Standard Time name is
 * returned.
 *
 * <p>When looking up a time zone name, the {@linkplain
 * ResourceBundle.Control#getCandidateLocales(String,Locale) default
 * <code>Locale</code> search path of <code>ResourceBundle</code>} derived
 * from the specified {@code locale} is used. (No {@linkplain
 * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
 * <code>Locale</code>} search is performed.) If a time zone name in any
 * {@code Locale} of the search path, including {@link Locale#ROOT}, is
 * found, the name is returned. Otherwise, a string in the
 * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
 *
 * @param daylight {@code true} specifying a Daylight Saving Time name, or
 *                 {@code false} specifying a Standard Time name
 * @param style either {@link #LONG} or {@link #SHORT}
 * @param locale   the locale in which to supply the display name.
 * @return the human-readable name of this time zone in the given locale.
 * @exception IllegalArgumentException if {@code style} is invalid.
 * @exception NullPointerException if {@code locale} is {@code null}.
 * @since 1.2
 * @see java.text.DateFormatSymbols#getZoneStrings()
 */
public String getDisplayName(boolean daylight, int style, Locale locale) {
    if (style != SHORT && style != LONG) {
        throw new IllegalArgumentException("Illegal style: " + style);
    }
    String id = getID();
    String name = TimeZoneNameUtility.retrieveDisplayName(id, daylight, style, locale);
    if (name != null) {
        return name;
    }

    if (id.startsWith("GMT") && id.length() > 3) {
        char sign = id.charAt(3);
        if (sign == '+' || sign == '-') {
            return id;
        }
    }
    int offset = getRawOffset();
    if (daylight) {
        offset += getDSTSavings();
    }
    return ZoneInfoFile.toCustomID(offset);
}
 
Example #23
Source File: TimeZone.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a name in the specified {@code style} of this {@code TimeZone}
 * suitable for presentation to the user in the specified {@code
 * locale}. If the specified {@code daylight} is {@code true}, a Daylight
 * Saving Time name is returned (even if this {@code TimeZone} doesn't
 * observe Daylight Saving Time). Otherwise, a Standard Time name is
 * returned.
 *
 * <p>When looking up a time zone name, the {@linkplain
 * ResourceBundle.Control#getCandidateLocales(String,Locale) default
 * <code>Locale</code> search path of <code>ResourceBundle</code>} derived
 * from the specified {@code locale} is used. (No {@linkplain
 * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
 * <code>Locale</code>} search is performed.) If a time zone name in any
 * {@code Locale} of the search path, including {@link Locale#ROOT}, is
 * found, the name is returned. Otherwise, a string in the
 * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
 *
 * @param daylight {@code true} specifying a Daylight Saving Time name, or
 *                 {@code false} specifying a Standard Time name
 * @param style either {@link #LONG} or {@link #SHORT}
 * @param locale   the locale in which to supply the display name.
 * @return the human-readable name of this time zone in the given locale.
 * @exception IllegalArgumentException if {@code style} is invalid.
 * @exception NullPointerException if {@code locale} is {@code null}.
 * @since 1.2
 * @see java.text.DateFormatSymbols#getZoneStrings()
 */
public String getDisplayName(boolean daylight, int style, Locale locale) {
    if (style != SHORT && style != LONG) {
        throw new IllegalArgumentException("Illegal style: " + style);
    }
    String id = getID();
    String name = TimeZoneNameUtility.retrieveDisplayName(id, daylight, style, locale);
    if (name != null) {
        return name;
    }

    if (id.startsWith("GMT") && id.length() > 3) {
        char sign = id.charAt(3);
        if (sign == '+' || sign == '-') {
            return id;
        }
    }
    int offset = getRawOffset();
    if (daylight) {
        offset += getDSTSavings();
    }
    return ZoneInfoFile.toCustomID(offset);
}
 
Example #24
Source File: TimeZone.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a custom time zone identifier and returns a corresponding zone.
 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
 *
 * @param id a string of the <a href="#CustomID">custom ID form</a>.
 * @return a newly created TimeZone with the given offset and
 * no daylight saving time, or null if the id cannot be parsed.
 */
private static final TimeZone parseCustomTimeZone(String id) {
    int length;

    // Error if the length of id isn't long enough or id doesn't
    // start with "GMT".
    if ((length = id.length()) < (GMT_ID_LENGTH + 2) ||
        id.indexOf(GMT_ID) != 0) {
        return null;
    }

    ZoneInfo zi;

    // First, we try to find it in the cache with the given
    // id. Even the id is not normalized, the returned ZoneInfo
    // should have its normalized id.
    zi = ZoneInfoFile.getZoneInfo(id);
    if (zi != null) {
        return zi;
    }

    int index = GMT_ID_LENGTH;
    boolean negative = false;
    char c = id.charAt(index++);
    if (c == '-') {
        negative = true;
    } else if (c != '+') {
        return null;
    }

    int hours = 0;
    int num = 0;
    int countDelim = 0;
    int len = 0;
    while (index < length) {
        c = id.charAt(index++);
        if (c == ':') {
            if (countDelim > 0) {
                return null;
            }
            if (len > 2) {
                return null;
            }
            hours = num;
            countDelim++;
            num = 0;
            len = 0;
            continue;
        }
        if (c < '0' || c > '9') {
            return null;
        }
        num = num * 10 + (c - '0');
        len++;
    }
    if (index != length) {
        return null;
    }
    if (countDelim == 0) {
        if (len <= 2) {
            hours = num;
            num = 0;
        } else {
            hours = num / 100;
            num %= 100;
        }
    } else {
        if (len != 2) {
            return null;
        }
    }
    if (hours > 23 || num > 59) {
        return null;
    }
    int gmtOffset =  (hours * 60 + num) * 60 * 1000;

    if (gmtOffset == 0) {
        zi = ZoneInfoFile.getZoneInfo(GMT_ID);
        if (negative) {
            zi.setID("GMT-00:00");
        } else {
            zi.setID("GMT+00:00");
        }
    } else {
        zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
    }
    return zi;
}
 
Example #25
Source File: TimeZone.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a name in the specified {@code style} of this {@code TimeZone}
 * suitable for presentation to the user in the specified {@code
 * locale}. If the specified {@code daylight} is {@code true}, a Daylight
 * Saving Time name is returned (even if this {@code TimeZone} doesn't
 * observe Daylight Saving Time). Otherwise, a Standard Time name is
 * returned.
 *
 * <p>When looking up a time zone name, the {@linkplain
 * ResourceBundle.Control#getCandidateLocales(String,Locale) default
 * <code>Locale</code> search path of <code>ResourceBundle</code>} derived
 * from the specified {@code locale} is used. (No {@linkplain
 * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
 * <code>Locale</code>} search is performed.) If a time zone name in any
 * {@code Locale} of the search path, including {@link Locale#ROOT}, is
 * found, the name is returned. Otherwise, a string in the
 * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
 *
 * @param daylight {@code true} specifying a Daylight Saving Time name, or
 *                 {@code false} specifying a Standard Time name
 * @param style either {@link #LONG} or {@link #SHORT}
 * @param locale   the locale in which to supply the display name.
 * @return the human-readable name of this time zone in the given locale.
 * @exception IllegalArgumentException if {@code style} is invalid.
 * @exception NullPointerException if {@code locale} is {@code null}.
 * @since 1.2
 * @see java.text.DateFormatSymbols#getZoneStrings()
 */
public String getDisplayName(boolean daylight, int style, Locale locale) {
    if (style != SHORT && style != LONG) {
        throw new IllegalArgumentException("Illegal style: " + style);
    }
    String id = getID();
    String name = TimeZoneNameUtility.retrieveDisplayName(id, daylight, style, locale);
    if (name != null) {
        return name;
    }

    if (id.startsWith("GMT") && id.length() > 3) {
        char sign = id.charAt(3);
        if (sign == '+' || sign == '-') {
            return id;
        }
    }
    int offset = getRawOffset();
    if (daylight) {
        offset += getDSTSavings();
    }
    return ZoneInfoFile.toCustomID(offset);
}
 
Example #26
Source File: TimeZone.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a name in the specified {@code style} of this {@code TimeZone}
 * suitable for presentation to the user in the specified {@code
 * locale}. If the specified {@code daylight} is {@code true}, a Daylight
 * Saving Time name is returned (even if this {@code TimeZone} doesn't
 * observe Daylight Saving Time). Otherwise, a Standard Time name is
 * returned.
 *
 * <p>When looking up a time zone name, the {@linkplain
 * ResourceBundle.Control#getCandidateLocales(String,Locale) default
 * <code>Locale</code> search path of <code>ResourceBundle</code>} derived
 * from the specified {@code locale} is used. (No {@linkplain
 * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
 * <code>Locale</code>} search is performed.) If a time zone name in any
 * {@code Locale} of the search path, including {@link Locale#ROOT}, is
 * found, the name is returned. Otherwise, a string in the
 * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
 *
 * @param daylight {@code true} specifying a Daylight Saving Time name, or
 *                 {@code false} specifying a Standard Time name
 * @param style either {@link #LONG} or {@link #SHORT}
 * @param locale   the locale in which to supply the display name.
 * @return the human-readable name of this time zone in the given locale.
 * @exception IllegalArgumentException if {@code style} is invalid.
 * @exception NullPointerException if {@code locale} is {@code null}.
 * @since 1.2
 * @see java.text.DateFormatSymbols#getZoneStrings()
 */
public String getDisplayName(boolean daylight, int style, Locale locale) {
    if (style != SHORT && style != LONG) {
        throw new IllegalArgumentException("Illegal style: " + style);
    }
    String id = getID();
    String name = TimeZoneNameUtility.retrieveDisplayName(id, daylight, style, locale);
    if (name != null) {
        return name;
    }

    if (id.startsWith("GMT") && id.length() > 3) {
        char sign = id.charAt(3);
        if (sign == '+' || sign == '-') {
            return id;
        }
    }
    int offset = getRawOffset();
    if (daylight) {
        offset += getDSTSavings();
    }
    return ZoneInfoFile.toCustomID(offset);
}
 
Example #27
Source File: TimeZone.java    From jdk-1.7-annotated with Apache License 2.0 4 votes vote down vote up
/**
 * Parses a custom time zone identifier and returns a corresponding zone.
 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
 *
 * @param id a string of the <a href="#CustomID">custom ID form</a>.
 * @return a newly created TimeZone with the given offset and
 * no daylight saving time, or null if the id cannot be parsed.
 */
private static final TimeZone parseCustomTimeZone(String id) {
    int length;

    // Error if the length of id isn't long enough or id doesn't
    // start with "GMT".
    if ((length = id.length()) < (GMT_ID_LENGTH + 2) ||
        id.indexOf(GMT_ID) != 0) {
        return null;
    }

    ZoneInfo zi;

    // First, we try to find it in the cache with the given
    // id. Even the id is not normalized, the returned ZoneInfo
    // should have its normalized id.
    zi = ZoneInfoFile.getZoneInfo(id);
    if (zi != null) {
        return zi;
    }

    int index = GMT_ID_LENGTH;
    boolean negative = false;
    char c = id.charAt(index++);
    if (c == '-') {
        negative = true;
    } else if (c != '+') {
        return null;
    }

    int hours = 0;
    int num = 0;
    int countDelim = 0;
    int len = 0;
    while (index < length) {
        c = id.charAt(index++);
        if (c == ':') {
            if (countDelim > 0) {
                return null;
            }
            if (len > 2) {
                return null;
            }
            hours = num;
            countDelim++;
            num = 0;
            len = 0;
            continue;
        }
        if (c < '0' || c > '9') {
            return null;
        }
        num = num * 10 + (c - '0');
        len++;
    }
    if (index != length) {
        return null;
    }
    if (countDelim == 0) {
        if (len <= 2) {
            hours = num;
            num = 0;
        } else {
            hours = num / 100;
            num %= 100;
        }
    } else {
        if (len != 2) {
            return null;
        }
    }
    if (hours > 23 || num > 59) {
        return null;
    }
    int gmtOffset =  (hours * 60 + num) * 60 * 1000;

    if (gmtOffset == 0) {
        zi = ZoneInfoFile.getZoneInfo(GMT_ID);
        if (negative) {
            zi.setID("GMT-00:00");
        } else {
            zi.setID("GMT+00:00");
        }
    } else {
        zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
    }
    return zi;
}
 
Example #28
Source File: TimeZone.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a custom time zone identifier and returns a corresponding zone.
 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
 *
 * @param id a string of the <a href="#CustomID">custom ID form</a>.
 * @return a newly created TimeZone with the given offset and
 * no daylight saving time, or null if the id cannot be parsed.
 */
private static final TimeZone parseCustomTimeZone(String id) {
    int length;

    // Error if the length of id isn't long enough or id doesn't
    // start with "GMT".
    if ((length = id.length()) < (GMT_ID_LENGTH + 2) ||
        id.indexOf(GMT_ID) != 0) {
        return null;
    }

    ZoneInfo zi;

    // First, we try to find it in the cache with the given
    // id. Even the id is not normalized, the returned ZoneInfo
    // should have its normalized id.
    zi = ZoneInfoFile.getZoneInfo(id);
    if (zi != null) {
        return zi;
    }

    int index = GMT_ID_LENGTH;
    boolean negative = false;
    char c = id.charAt(index++);
    if (c == '-') {
        negative = true;
    } else if (c != '+') {
        return null;
    }

    int hours = 0;
    int num = 0;
    int countDelim = 0;
    int len = 0;
    while (index < length) {
        c = id.charAt(index++);
        if (c == ':') {
            if (countDelim > 0) {
                return null;
            }
            if (len > 2) {
                return null;
            }
            hours = num;
            countDelim++;
            num = 0;
            len = 0;
            continue;
        }
        if (c < '0' || c > '9') {
            return null;
        }
        num = num * 10 + (c - '0');
        len++;
    }
    if (index != length) {
        return null;
    }
    if (countDelim == 0) {
        if (len <= 2) {
            hours = num;
            num = 0;
        } else {
            hours = num / 100;
            num %= 100;
        }
    } else {
        if (len != 2) {
            return null;
        }
    }
    if (hours > 23 || num > 59) {
        return null;
    }
    int gmtOffset =  (hours * 60 + num) * 60 * 1000;

    if (gmtOffset == 0) {
        zi = ZoneInfoFile.getZoneInfo(GMT_ID);
        if (negative) {
            zi.setID("GMT-00:00");
        } else {
            zi.setID("GMT+00:00");
        }
    } else {
        zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
    }
    return zi;
}
 
Example #29
Source File: TimeZone.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a custom time zone identifier and returns a corresponding zone.
 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
 *
 * @param id a string of the <a href="#CustomID">custom ID form</a>.
 * @return a newly created TimeZone with the given offset and
 * no daylight saving time, or null if the id cannot be parsed.
 */
private static final TimeZone parseCustomTimeZone(String id) {
    int length;

    // Error if the length of id isn't long enough or id doesn't
    // start with "GMT".
    if ((length = id.length()) < (GMT_ID_LENGTH + 2) ||
        id.indexOf(GMT_ID) != 0) {
        return null;
    }

    ZoneInfo zi;

    // First, we try to find it in the cache with the given
    // id. Even the id is not normalized, the returned ZoneInfo
    // should have its normalized id.
    zi = ZoneInfoFile.getZoneInfo(id);
    if (zi != null) {
        return zi;
    }

    int index = GMT_ID_LENGTH;
    boolean negative = false;
    char c = id.charAt(index++);
    if (c == '-') {
        negative = true;
    } else if (c != '+') {
        return null;
    }

    int hours = 0;
    int num = 0;
    int countDelim = 0;
    int len = 0;
    while (index < length) {
        c = id.charAt(index++);
        if (c == ':') {
            if (countDelim > 0) {
                return null;
            }
            if (len > 2) {
                return null;
            }
            hours = num;
            countDelim++;
            num = 0;
            len = 0;
            continue;
        }
        if (c < '0' || c > '9') {
            return null;
        }
        num = num * 10 + (c - '0');
        len++;
    }
    if (index != length) {
        return null;
    }
    if (countDelim == 0) {
        if (len <= 2) {
            hours = num;
            num = 0;
        } else {
            hours = num / 100;
            num %= 100;
        }
    } else {
        if (len != 2) {
            return null;
        }
    }
    if (hours > 23 || num > 59) {
        return null;
    }
    int gmtOffset =  (hours * 60 + num) * 60 * 1000;

    if (gmtOffset == 0) {
        zi = ZoneInfoFile.getZoneInfo(GMT_ID);
        if (negative) {
            zi.setID("GMT-00:00");
        } else {
            zi.setID("GMT+00:00");
        }
    } else {
        zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
    }
    return zi;
}
 
Example #30
Source File: TimeZone.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a name in the specified {@code style} of this {@code TimeZone}
 * suitable for presentation to the user in the specified {@code
 * locale}. If the specified {@code daylight} is {@code true}, a Daylight
 * Saving Time name is returned (even if this {@code TimeZone} doesn't
 * observe Daylight Saving Time). Otherwise, a Standard Time name is
 * returned.
 *
 * <p>When looking up a time zone name, the {@linkplain
 * ResourceBundle.Control#getCandidateLocales(String,Locale) default
 * <code>Locale</code> search path of <code>ResourceBundle</code>} derived
 * from the specified {@code locale} is used. (No {@linkplain
 * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
 * <code>Locale</code>} search is performed.) If a time zone name in any
 * {@code Locale} of the search path, including {@link Locale#ROOT}, is
 * found, the name is returned. Otherwise, a string in the
 * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
 *
 * @param daylight {@code true} specifying a Daylight Saving Time name, or
 *                 {@code false} specifying a Standard Time name
 * @param style either {@link #LONG} or {@link #SHORT}
 * @param locale   the locale in which to supply the display name.
 * @return the human-readable name of this time zone in the given locale.
 * @exception IllegalArgumentException if {@code style} is invalid.
 * @exception NullPointerException if {@code locale} is {@code null}.
 * @since 1.2
 * @see java.text.DateFormatSymbols#getZoneStrings()
 */
public String getDisplayName(boolean daylight, int style, Locale locale) {
    if (style != SHORT && style != LONG) {
        throw new IllegalArgumentException("Illegal style: " + style);
    }
    String id = getID();
    String name = TimeZoneNameUtility.retrieveDisplayName(id, daylight, style, locale);
    if (name != null) {
        return name;
    }

    if (id.startsWith("GMT") && id.length() > 3) {
        char sign = id.charAt(3);
        if (sign == '+' || sign == '-') {
            return id;
        }
    }
    int offset = getRawOffset();
    if (daylight) {
        offset += getDSTSavings();
    }
    return ZoneInfoFile.toCustomID(offset);
}