Пример #1
0
            /// <summary>
            /// Returns the particular date in the specified year
            /// on which the daylight saving time period applies.
            /// </summary>
            private DateTime GetParticularDay(int year, DaylightLimit rule)
            {
                if (!rule.Enabled)
                {
                    return(DateTime.MinValue);
                }

                // Get the first occurence of the requested weekday
                DateTime result = new DateTime(year, rule.Month, 1,
                                               rule.Hour, rule.Minute, rule.Second,
                                               rule.Milliseconds, DateTimeKind.Local);
                int diff = rule.DayOfWeek - (int)result.DayOfWeek;

                if (diff < 0)
                {
                    diff += 7;
                }
                result = result.AddDays(diff);

                // Get the day when the daylight time change applies
                result = result.AddDays((rule.Day - 1) * 7);
                if (result.Month == rule.Month)
                {
                    return(result);
                }
                return(result.AddDays(-7.0));
            }
Пример #2
0
            /// <summary>
            /// Returns the particular date in the specified year
            /// on which the daylight saving time period applies.
            /// </summary>
            private DateTime GetParticularDay(int year, DaylightLimit rule)
            {
                DateTime result = DateTime.MinValue;

                if (rule.IsEmpty)
                {
                    return(result);
                }

                // Get the first and last day of the month
                DateTime first = new DateTime(year, rule.Month, 1,
                                              rule.Hour, rule.Minute, rule.Second,
                                              rule.Milliseconds, DateTimeKind.Local);
                DateTime last = first;

                try
                {
                    last = last.AddMonths(1).AddDays(-1);
                }
                catch (ArgumentOutOfRangeException)
                {
                    last = DateTime.MaxValue;
                }

                // Get the number of Sundays in the month
                int sunday = (first.DayOfWeek == DayOfWeek.Sunday) ?
                             1 : 7 - (int)first.DayOfWeek + 1;
                int numberOfSundays = (int)Math.Floor(
                    (last.Day - sunday) / 7.0) + 1;

                // Get the day when the daylight time change applies
                if (numberOfSundays <= 4)
                {
                    int dif = rule.DayOfWeek - ((int)first.DayOfWeek);
                    if (dif < 0)
                    {
                        dif += 7;
                    }
                    dif += 7 * (numberOfSundays - 1);
                    if (dif > 0)
                    {
                        return(first.AddDays(dif));
                    }
                    return(first);
                }
                else
                {
                    int dif = ((int)last.DayOfWeek) - rule.DayOfWeek;
                    if (dif < 0)
                    {
                        dif += 7;
                    }
                    if (dif > 0)
                    {
                        return(last.AddDays(-dif));
                    }
                    return(last);
                }
            }