private void ParseDayLightTime(DaylightTime dayLightTime) { var startTime = dayLightTime.Start; var endTime = dayLightTime.End; var delta = dayLightTime.Delta; if (startTime.TimeOfDay > endTime.TimeOfDay) { // Clock move backward AmbiguiousTimeStart = startTime.Subtract(delta); AmbiguiousTimeEnd = startTime; InvalidTimeStart = endTime; InvalidTimeEnd = endTime.Add(delta); } else { // Clock move forward AmbiguiousTimeStart = endTime.Subtract(delta); AmbiguiousTimeEnd = endTime; InvalidTimeStart = startTime; InvalidTimeEnd = startTime.Add(delta); } if (startTime > endTime) { DayLightSavingTimeStart = InvalidTimeEnd; var monthOfYear = MonthOfYear.Of(12, Year.GetYear()); DayLightSavingTimeEnd = new DateTime(monthOfYear.Year.GetYear(), monthOfYear.Month.GetMonth(), monthOfYear.TotalDays, 12, 59, 59, 999); } else { DayLightSavingTimeStart = new DateTime(Year.GetYear(), 1, 1, 12, 0, 0, 0); DayLightSavingTimeEnd = AmbiguiousTimeStart; } }
internal static DaylightTime GetDaylightTime(TimeZoneInfo timeZone, int year) { var adjustRule = timeZone .GetAdjustmentRules() .SingleOrDefault(rule => rule.DateStart.Year <= year && rule.DateEnd.Year >= year); // There is no Day Light Time Saving of this year in this time zone if (adjustRule == null) { return(null); } var start = adjustRule.DaylightTransitionStart; var end = adjustRule.DaylightTransitionEnd; var startDate = start.IsFixedDateRule ? Dates.Of(year, Month.Of(start.Month), start.Day).Add(TimeSpan.FromTicks(start.TimeOfDay.Ticks)) : MonthOfYear.Of(start.Month, year) .GetDayOfWeekDateForWeek(start.Week, start.DayOfWeek) .Add(TimeSpan.FromTicks(start.TimeOfDay.Ticks)); DateTime endDate; if (end.IsFixedDateRule) { if (end.Day == 1 && end.Month == 1 && end.TimeOfDay == DateTime.MinValue) { endDate = DateTime.MaxValue; // never end } else { endDate = Dates.Of(year, Month.Of(end.Month), end.Day).Add(TimeSpan.FromTicks(end.TimeOfDay.Ticks)); } } else { endDate = MonthOfYear.Of(end.Month, year) .GetDayOfWeekDateForWeek(end.Week, end.DayOfWeek) .Add(TimeSpan.FromTicks(end.TimeOfDay.Ticks)); } return(new DaylightTime(startDate, endDate, adjustRule.DaylightDelta)); }