Пример #1
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);
        }