private static DaylightTime CreateDaylightChanges(int year)
        {
            DaylightTime currentDaylightChanges = null;

            if (TimeZoneInfo.Local.SupportsDaylightSavingTime)
            {
                DateTime start;
                DateTime end;
                TimeSpan delta;

                foreach (var rule in TimeZoneInfo.Local.GetAdjustmentRules())
                {
                    if (rule.DateStart.Year <= year && rule.DateEnd.Year >= year && rule.DaylightDelta != TimeSpan.Zero)
                    {
                        start = TimeZoneInfo.TransitionTimeToDateTime(year, rule.DaylightTransitionStart);
                        end   = TimeZoneInfo.TransitionTimeToDateTime(year, rule.DaylightTransitionEnd);
                        delta = rule.DaylightDelta;

                        currentDaylightChanges = new DaylightTime(start, end, delta);
                        break;
                    }
                }
            }

            if (currentDaylightChanges == null)
            {
                currentDaylightChanges = new DaylightTime(DateTime.MinValue, DateTime.MinValue, TimeSpan.Zero);
            }

            return(currentDaylightChanges);
        }
示例#2
0
        public override DaylightTime GetDaylightChanges(int year)
        {
            if (year < 1 || year > 9999)
            {
                throw new ArgumentOutOfRangeException(nameof(year), Environment.GetResourceString("ArgumentOutOfRange_Range", 1, 9999));
            }
            Contract.EndContractBlock();

            Object objYear = (Object)year;

            if (!m_CachedDaylightChanges.Contains(objYear))
            {
                DaylightTime currentDaylightChanges = null;

                if (TimeZoneInfo.Local.SupportsDaylightSavingTime)
                {
                    DateTime start;
                    DateTime end;
                    TimeSpan delta;

                    foreach (var rule in TimeZoneInfo.Local.GetAdjustmentRules())
                    {
                        if (rule.DateStart.Year <= year && rule.DateEnd.Year >= year && rule.DaylightDelta != TimeSpan.Zero)
                        {
                            start = TimeZoneInfo.TransitionTimeToDateTime(year, rule.DaylightTransitionStart);
                            end   = TimeZoneInfo.TransitionTimeToDateTime(year, rule.DaylightTransitionEnd);
                            delta = rule.DaylightDelta;

                            currentDaylightChanges = new DaylightTime(start, end, delta);
                            break;
                        }
                    }
                }

                if (currentDaylightChanges == null)
                {
                    currentDaylightChanges = new DaylightTime(DateTime.MinValue, DateTime.MinValue, TimeSpan.Zero);
                }

                lock (m_CachedDaylightChanges)
                {
                    if (!m_CachedDaylightChanges.Contains(objYear))
                    {
                        m_CachedDaylightChanges.Add(objYear, currentDaylightChanges);
                    }
                }
            }

            DaylightTime result = (DaylightTime)m_CachedDaylightChanges[objYear];

            return(result);
        }
        private static DaylightTime CreateDaylightChanges(int year)
        {
            DateTime start = DateTime.MinValue;
            DateTime end   = DateTime.MinValue;
            TimeSpan delta = TimeSpan.Zero;

            if (TimeZoneInfo.Local.SupportsDaylightSavingTime)
            {
                foreach (TimeZoneInfo.AdjustmentRule rule in TimeZoneInfo.Local.GetAdjustmentRules())
                {
                    if (rule.DateStart.Year <= year && rule.DateEnd.Year >= year && rule.DaylightDelta != TimeSpan.Zero)
                    {
                        start = TimeZoneInfo.TransitionTimeToDateTime(year, rule.DaylightTransitionStart);
                        end   = TimeZoneInfo.TransitionTimeToDateTime(year, rule.DaylightTransitionEnd);
                        delta = rule.DaylightDelta;
                        break;
                    }
                }
            }

            return(new DaylightTime(start, end, delta));
        }