Example #1
0
        public static TimeZoneInfo FindSystemTimeZoneById(string id)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (id.Length == 0 || id.Length > 255 || id.Contains("\0"))
            {
                throw new ArgumentException(String.Format(SR.Argument_TimeZoneNotFound, id));
            }

            //
            // Check first the Utc Ids and return the cached one because in GetCorrespondingKind
            // we use reference equality
            //

            if (id.Equals(TimeZoneInfo.Utc.Id, StringComparison.OrdinalIgnoreCase))
            {
                return(TimeZoneInfo.Utc);
            }

            TimeZoneInfo value;
            CachedData   cache = s_cachedData;

            // Use the current cache if it exists
            if (cache._systemTimeZones != null)
            {
                if (cache._systemTimeZones.TryGetValue(id, out value))
                {
                    return(value);
                }
            }
            // See if the cache was fully filled, if not, fill it then check again.
            if (!cache.AreSystemTimesEnumerated)
            {
                cache.EnumerateSystemTimes();
                if (cache._systemTimeZones.TryGetValue(id, out value))
                {
                    return(value);
                }
            }
            throw new ArgumentException(String.Format(SR.Argument_TimeZoneNotFound, id));
        }