Пример #1
0
        /// <summary>
        /// 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.
        ///
        /// <para>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 <seealso cref="Locale#ROOT"/>, is
        /// found, the name is returned. Otherwise, a string in the
        /// <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
        ///
        /// </para>
        /// </summary>
        /// <param name="daylight"> {@code true} specifying a Daylight Saving Time name, or
        ///                 {@code false} specifying a Standard Time name </param>
        /// <param name="style"> either <seealso cref="#LONG"/> or <seealso cref="#SHORT"/> </param>
        /// <param name="locale">   the locale in which to supply the display name. </param>
        /// <returns> the human-readable name of this time zone in the given locale. </returns>
        /// <exception cref="IllegalArgumentException"> if {@code style} is invalid. </exception>
        /// <exception cref="NullPointerException"> if {@code locale} is {@code null}.
        /// @since 1.2 </exception>
        /// <seealso cref= java.text.DateFormatSymbols#getZoneStrings() </seealso>
        public virtual String GetDisplayName(bool daylight, int style, Locale locale)
        {
            if (style != SHORT && style != LONG)
            {
                throw new IllegalArgumentException("Illegal style: " + style);
            }
            String id   = ID;
            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 = RawOffset;

            if (daylight)
            {
                offset += DSTSavings;
            }
            return(ZoneInfoFile.toCustomID(offset));
        }
Пример #2
0
        /// <summary>
        /// Converts this {@code TimeZone} object to a {@code ZoneId}.
        /// </summary>
        /// <returns> a {@code ZoneId} representing the same time zone as this
        ///         {@code TimeZone}
        /// @since 1.8 </returns>
        public virtual ZoneId ToZoneId()
        {
            String id = ID;

            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));
        }
Пример #3
0
        /// <summary>
        /// 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)
        /// </summary>
        /// <param name="id"> a string of the <a href="#CustomID">custom ID form</a>. </param>
        /// <returns> a newly created TimeZone with the given offset and
        /// no daylight saving time, or null if the id cannot be parsed. </returns>
        private static 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;
            bool 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.ID = "GMT-00:00";
                }
                else
                {
                    zi.ID = "GMT+00:00";
                }
            }
            else
            {
                zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
            }
            return(zi);
        }