Skip to content

Commit

Permalink
Ensure AdjustmentRule.DaylightDelta is within [-12,12] (dotnet/corecl…
Browse files Browse the repository at this point in the history
…r#18477)

* Modulo AdjustmentRule.DaylightDelta

* fix typo


Commit migrated from dotnet/coreclr@66ff5d7
  • Loading branch information
krwq authored Jun 15, 2018
1 parent 4cf5722 commit 42296b9
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public sealed partial class TimeZoneInfo
[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;
Expand Down Expand Up @@ -100,6 +102,7 @@ internal static AdjustmentRule CreateAdjustmentRule(
TimeSpan baseUtcOffsetDelta,
bool noDaylightTransitions)
{
AdjustDaylightDeltaToExpectedRange(ref daylightDelta, ref baseUtcOffsetDelta);
return new AdjustmentRule(
dateStart,
dateEnd,
Expand Down Expand Up @@ -186,6 +189,26 @@ private static void ValidateAdjustmentRule(
}
}

/// <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.
Expand Down

0 comments on commit 42296b9

Please sign in to comment.