Ensure AdjustmentRule.DaylightDelta is within [-12,12] (dotnet/coreclr#18477)
authorKrzysztof Wicher <mordotymoja@gmail.com>
Fri, 15 Jun 2018 18:08:29 +0000 (11:08 -0700)
committerGitHub <noreply@github.com>
Fri, 15 Jun 2018 18:08:29 +0000 (11:08 -0700)
* Modulo AdjustmentRule.DaylightDelta

* fix typo

Commit migrated from https://github.com/dotnet/coreclr/commit/66ff5d7ed68c050ba53968c2ecf88a6c9a2a3224

src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.AdjustmentRule.cs

index 54b5d46..aceb7b9 100644 (file)
@@ -11,6 +11,8 @@ namespace System
         [Serializable]
         public sealed class AdjustmentRule : IEquatable<AdjustmentRule>, ISerializable, IDeserializationCallback
         {
+            private static readonly TimeSpan DaylightDeltaAdjustment = TimeSpan.FromHours(24.0);
+            private static readonly TimeSpan MaxDaylightDelta = TimeSpan.FromHours(12.0);
             private readonly DateTime _dateStart;
             private readonly DateTime _dateEnd;
             private readonly TimeSpan _daylightDelta;
@@ -100,6 +102,7 @@ namespace System
                 TimeSpan baseUtcOffsetDelta,
                 bool noDaylightTransitions)
             {
+                AdjustDaylightDeltaToExpectedRange(ref daylightDelta, ref baseUtcOffsetDelta);
                 return new AdjustmentRule(
                     dateStart,
                     dateEnd,
@@ -186,6 +189,26 @@ namespace System
                 }
             }
 
+            /// <summary>
+            /// Ensures the daylight delta is within [-12, 12] hours
+            /// </summary>>
+            private static void AdjustDaylightDeltaToExpectedRange(ref TimeSpan daylightDelta, ref TimeSpan baseUtcOffsetDelta)
+            {
+                if (daylightDelta > MaxDaylightDelta)
+                {
+                    daylightDelta -= DaylightDeltaAdjustment;
+                    baseUtcOffsetDelta += DaylightDeltaAdjustment;
+                }
+                else if (daylightDelta < -MaxDaylightDelta)
+                {
+                    daylightDelta += DaylightDeltaAdjustment;
+                    baseUtcOffsetDelta -= DaylightDeltaAdjustment;
+                }
+
+                System.Diagnostics.Debug.Assert(daylightDelta <= MaxDaylightDelta && daylightDelta >= -MaxDaylightDelta,
+                                                "DaylightDelta should not ever be more than 24h");
+            }
+
             void IDeserializationCallback.OnDeserialization(object sender)
             {
                 // OnDeserialization is called after each instance of this class is deserialized.