From dc8e7678bb734a8144a6083b80abb45f3327bb84 Mon Sep 17 00:00:00 2001 From: Andrew Barker <59669636+st-abarker@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:22:26 -0700 Subject: [PATCH] Fix calculation of date a year previous when getting a VTimeZone from a DateTime and tzid. --- src/Ical.Net/CalendarComponents/VTimeZone.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Ical.Net/CalendarComponents/VTimeZone.cs b/src/Ical.Net/CalendarComponents/VTimeZone.cs index 08aff0d0..075efbdd 100644 --- a/src/Ical.Net/CalendarComponents/VTimeZone.cs +++ b/src/Ical.Net/CalendarComponents/VTimeZone.cs @@ -34,13 +34,24 @@ public static VTimeZone FromDateTimeZone(string tzId, DateTime earlistDateTimeTo var vTimeZone = new VTimeZone(tzId); var earliestYear = 1900; + var earliestMonth = earlistDateTimeToSupport.Month; + var earliestDay = earlistDateTimeToSupport.Day; // Support date/times for January 1st of the previous year by default. if (earlistDateTimeToSupport.Year > 1900) { earliestYear = earlistDateTimeToSupport.Year - 1; + // Since we went back a year, we can't still be in a leap-year + if (earliestMonth == 2 && earliestDay == 29) + earliestDay = 28; } - var earliest = Instant.FromUtc(earliestYear, earlistDateTimeToSupport.Month, - earlistDateTimeToSupport.Day, earlistDateTimeToSupport.Hour, earlistDateTimeToSupport.Minute); + else + { + // Going back to 1900, which wasn't a leap year, so we need to switch to Feb 20 + if (earliestMonth == 2 && earliestDay == 29) + earliestDay = 28; + } + var earliest = Instant.FromUtc(earliestYear, earliestMonth, earliestDay, + earlistDateTimeToSupport.Hour, earlistDateTimeToSupport.Minute); // Only include historical data if asked to do so. Otherwise, // use only the most recent adjustment rules available. @@ -368,4 +379,4 @@ public override int GetHashCode() } } } -} \ No newline at end of file +}