Esempio n. 1
0
        /// <summary>
        /// Gets the tz database id from <see cref="TimeZoneInfo"/>.
        /// </summary>
        /// <param name="timeZoneInfo"><see cref="TimeZoneInfo"/>.</param>
        /// <param name="preferredTzId">Preferred TzId type.</param>
        /// <returns>tz database id corresponds to the Windows time zone id.</returns>
        public static string GetTzIdFromTimeZoneInfo(TimeZoneInfo timeZoneInfo, PreferredTzId preferredTzId = PreferredTzId.Default)
        {
            string id;

            if (!TryGetTzIdFromTimeZoneInfo(timeZoneInfo, out id, preferredTzId))
            {
                throw new TimeZoneInfoNotFoundException(String.Format(ResourceMessage.ErrorMessages.TimeZondIdNotFound, timeZoneInfo.Id));
            }

            return(id);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets <see cref="TimeZoneInfo"/> from Windows time zone id.
        /// </summary>
        /// <param name="windowsId">tz database id.</param>
        /// <returns><see cref="TimeZoneInfo"/>.</returns>
        /// <exception cref="TimeZoneInfoNotFoundException">Thrown if the time zone id is not found.</exception>
        public static TimeZoneInfo GetTimeZoneInfoFromWindowsId(string windowsId, PreferredTzId preferredTzId = PreferredTzId.Default)
        {
            TimeZoneInfo result;
            Exception    exception;

            if (PRESUME_WINDOWS_TIME_ZONE_ENVIRONMENT)
            {
                if (!tryGetSystemTimeZoneInfo(windowsId, out result, out exception))
                {
                    Exception ex;

                    tryGetTzTimeZoneInfoFromWindowsId(windowsId, out result, out ex, preferredTzId);
                }
            }
            else
            {
                if (!tryGetTzTimeZoneInfoFromWindowsId(windowsId, out result, out exception, preferredTzId))
                {
                    if (exception == null)
                    {
                        tryGetSystemTimeZoneInfo(windowsId, out result, out exception);
                    }
                    else
                    {
                        tryGetSystemTimeZoneInfo(windowsId, out result);
                    }
                }
            }

            if (result == null)
            {
                string message = String.Format(ResourceMessage.ErrorMessages.TimeZoneInfoNotFoundForId, windowsId);

                if (exception != null)
                {
                    throw new TimeZoneInfoNotFoundException(message, exception);
                }
                else
                {
                    throw new TimeZoneInfoNotFoundException(message);
                }
            }

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Gets System TimeZoneInfo which has Tz database id.
        /// </summary>
        /// <param name="windowsId"></param>
        /// <param name="timeZoneInfo"><see cref="TimeZoneInfo"/>.</param>
        /// <param name="exception"><see cref="Exception"/> which was thrown while finding TimeZone.</param>
        /// <param name="preferredTzId">Preferred TzId type.</param>
        /// <returns>true if the <see cref="TimeZoneInfo"/> is found, otherwise false.</returns>
        private static bool tryGetTzTimeZoneInfoFromWindowsId(string windowsId, out TimeZoneInfo timeZoneInfo, out Exception exception, PreferredTzId preferredTzId = PreferredTzId.Default)
        {
            TimeZoneInfo tzi = null;
            Exception    ex  = null;

            string id;

            if (TryGetTzIdFromWindowsId(windowsId, out id, preferredTzId))
            {
                if (!tryGetSystemTimeZoneInfo(id, out tzi, out ex))
                {
                    string id2;

                    if (preferredTzId != PreferredTzId.Default && TryGetTzIdFromWindowsId(windowsId, out id2, PreferredTzId.Default) && id != id2)
                    {
                        tryGetSystemTimeZoneInfo(id2, out timeZoneInfo);
                    }
                }
            }

            timeZoneInfo = tzi;
            exception    = ex;

            if (timeZoneInfo == null)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the tz database id from <see cref="TimeZoneInfo"/>.
        /// </summary>
        /// <param name="timeZoneInfo"><see cref="TimeZoneInfo"/>.</param>
        /// <param name="tzId">tz database id corresponds to the Windows time zone id.</param>
        /// <param name="preferredTzId">Preferred TzId type.</param>
        /// <returns>true if the tz database id is found, otherwise false.</returns>
        public static bool TryGetTzIdFromTimeZoneInfo(TimeZoneInfo timeZoneInfo, out string tzId, PreferredTzId preferredTzId = PreferredTzId.Default)
        {
            string id = timeZoneInfo.Id;

            if (TZ_ID_TO_WINDOWS_ID_MAP.ContainsKey(id))
            {
                tzId = id;
                return(true);
            }

            return(TryGetTzIdFromWindowsId(id, out tzId, preferredTzId));
        }
Esempio n. 5
0
        /// <summary>
        /// Gets the tz database id corresponds to a Windows time zone id.
        /// </summary>
        /// <param name="windowsId">Windows time zone id.</param>
        /// <param name="tzId">tz database id corresponds to the Windows time zone id.</param>
        /// <param name="preferredTzId">Preferred TzId type.</param>
        /// <returns>true if the tz database id is found, otherwise false.</returns>
        public static bool TryGetTzIdFromWindowsId(string windowsId, out string tzId, PreferredTzId preferredTzId = PreferredTzId.Default)
        {
            // To increase possiblity to clear TIME_ZONE_ID_DATABASE, always try to load lazy map for this small map(TerritoryIndipendetnMap).
            var territoryIndipendentMap = WINDOWS_ID_TO_TZ_ID_TERRITORY_INDIPENDENT_MAP;

            if (territoryIndipendentMap != null && preferredTzId == PreferredTzId.TerritoryIndipendent)
            {
                if (territoryIndipendentMap.TryGetValue(windowsId, out tzId))
                {
                    return(true);
                }
            }

            return(WINDOWS_ID_TO_TZ_ID_MAP.TryGetValue(windowsId, out tzId));
        }