Skip to content

Commit 93b75f9

Browse files
MaximLipninscott-ferguson-unity
authored andcommitted
Avoid creating un-representable DateTime for transition point (mono#16753)
Fixes mono#16742
1 parent 9d66cfb commit 93b75f9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mcs/class/corlib/System/TimeZoneInfo.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,8 +1287,11 @@ private bool TryGetTransitionOffset (DateTime dateTime, out TimeSpan offset, out
12871287

12881288
private static DateTime TransitionPoint (TransitionTime transition, int year)
12891289
{
1290-
if (transition.IsFixedDateRule)
1291-
return new DateTime (year, transition.Month, transition.Day) + transition.TimeOfDay.TimeOfDay;
1290+
if (transition.IsFixedDateRule) {
1291+
var daysInMonth = DateTime.DaysInMonth (year, transition.Month);
1292+
var transitionDay = transition.Day <= daysInMonth ? transition.Day : daysInMonth;
1293+
return new DateTime (year, transition.Month, transitionDay) + transition.TimeOfDay.TimeOfDay;
1294+
}
12921295

12931296
DayOfWeek first = (new DateTime (year, transition.Month, 1)).DayOfWeek;
12941297
int day = 1 + (transition.Week - 1) * 7 + (transition.DayOfWeek - first + 7) % 7;

mcs/class/corlib/Test/System/TimeZoneInfoTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,15 @@ public void TestIsDST_DateTimeOffset ()
854854
Assert.IsTrue (tzi.IsDaylightSavingTime (dateOffset));
855855
}
856856

857+
// https://github.com/mono/mono/issues/16742
858+
[Test]
859+
public void Bug_16472 ()
860+
{
861+
var parsedTime = DateTime.Parse ("1948-02-19T23:00:00Z", CultureInfo.InvariantCulture);
862+
var newTime = TimeZoneInfo.ConvertTime (parsedTime, TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Rome")));
863+
Assert.AreEqual (1948, newTime.Year);
864+
}
865+
857866
// https://github.com/mono/mono/issues/9664
858867
[Test]
859868
public void Bug_9664 ()

0 commit comments

Comments
 (0)