From 42296b99a85484e1c62deae60af6d58b8375851e Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Fri, 15 Jun 2018 11:08:29 -0700 Subject: [PATCH] Ensure AdjustmentRule.DaylightDelta is within [-12,12] (dotnet/coreclr#18477) * Modulo AdjustmentRule.DaylightDelta * fix typo Commit migrated from https://github.com/dotnet/coreclr/commit/66ff5d7ed68c050ba53968c2ecf88a6c9a2a3224 --- .../src/System/TimeZoneInfo.AdjustmentRule.cs | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.AdjustmentRule.cs b/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.AdjustmentRule.cs index 54b5d46..aceb7b9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.AdjustmentRule.cs +++ b/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.AdjustmentRule.cs @@ -11,6 +11,8 @@ namespace System [Serializable] public sealed class AdjustmentRule : IEquatable, 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 } } + /// + /// Ensures the daylight delta is within [-12, 12] hours + /// > + 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. -- 2.7.4