TimeZoneInfo.AdjustmentRule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Runtime.Serialization;
  5. namespace System
  6. {
  7. public sealed partial class TimeZoneInfo
  8. {
  9. [Serializable]
  10. public sealed class AdjustmentRule :
  11. #nullable disable // see comment on String
  12. IEquatable<AdjustmentRule>,
  13. #nullable restore
  14. ISerializable, IDeserializationCallback
  15. {
  16. private static readonly TimeSpan DaylightDeltaAdjustment = TimeSpan.FromHours(24.0);
  17. private static readonly TimeSpan MaxDaylightDelta = TimeSpan.FromHours(12.0);
  18. private readonly DateTime _dateStart;
  19. private readonly DateTime _dateEnd;
  20. private readonly TimeSpan _daylightDelta;
  21. private readonly TransitionTime _daylightTransitionStart;
  22. private readonly TransitionTime _daylightTransitionEnd;
  23. private readonly TimeSpan _baseUtcOffsetDelta; // delta from the default Utc offset (utcOffset = defaultUtcOffset + _baseUtcOffsetDelta)
  24. private readonly bool _noDaylightTransitions;
  25. public DateTime DateStart => _dateStart;
  26. public DateTime DateEnd => _dateEnd;
  27. public TimeSpan DaylightDelta => _daylightDelta;
  28. public TransitionTime DaylightTransitionStart => _daylightTransitionStart;
  29. public TransitionTime DaylightTransitionEnd => _daylightTransitionEnd;
  30. internal TimeSpan BaseUtcOffsetDelta => _baseUtcOffsetDelta;
  31. /// <summary>
  32. /// Gets a value indicating that this AdjustmentRule fixes the time zone offset
  33. /// from DateStart to DateEnd without any daylight transitions in between.
  34. /// </summary>
  35. internal bool NoDaylightTransitions => _noDaylightTransitions;
  36. internal bool HasDaylightSaving =>
  37. DaylightDelta != TimeSpan.Zero ||
  38. (DaylightTransitionStart != default && DaylightTransitionStart.TimeOfDay != DateTime.MinValue) ||
  39. (DaylightTransitionEnd != default && DaylightTransitionEnd.TimeOfDay != DateTime.MinValue.AddMilliseconds(1));
  40. public bool Equals(AdjustmentRule? other) =>
  41. other != null &&
  42. _dateStart == other._dateStart &&
  43. _dateEnd == other._dateEnd &&
  44. _daylightDelta == other._daylightDelta &&
  45. _baseUtcOffsetDelta == other._baseUtcOffsetDelta &&
  46. _daylightTransitionEnd.Equals(other._daylightTransitionEnd) &&
  47. _daylightTransitionStart.Equals(other._daylightTransitionStart);
  48. public override int GetHashCode() => _dateStart.GetHashCode();
  49. private AdjustmentRule(
  50. DateTime dateStart,
  51. DateTime dateEnd,
  52. TimeSpan daylightDelta,
  53. TransitionTime daylightTransitionStart,
  54. TransitionTime daylightTransitionEnd,
  55. TimeSpan baseUtcOffsetDelta,
  56. bool noDaylightTransitions)
  57. {
  58. ValidateAdjustmentRule(dateStart, dateEnd, daylightDelta,
  59. daylightTransitionStart, daylightTransitionEnd, noDaylightTransitions);
  60. _dateStart = dateStart;
  61. _dateEnd = dateEnd;
  62. _daylightDelta = daylightDelta;
  63. _daylightTransitionStart = daylightTransitionStart;
  64. _daylightTransitionEnd = daylightTransitionEnd;
  65. _baseUtcOffsetDelta = baseUtcOffsetDelta;
  66. _noDaylightTransitions = noDaylightTransitions;
  67. }
  68. public static AdjustmentRule CreateAdjustmentRule(
  69. DateTime dateStart,
  70. DateTime dateEnd,
  71. TimeSpan daylightDelta,
  72. TransitionTime daylightTransitionStart,
  73. TransitionTime daylightTransitionEnd)
  74. {
  75. return new AdjustmentRule(
  76. dateStart,
  77. dateEnd,
  78. daylightDelta,
  79. daylightTransitionStart,
  80. daylightTransitionEnd,
  81. baseUtcOffsetDelta: TimeSpan.Zero,
  82. noDaylightTransitions: false);
  83. }
  84. internal static AdjustmentRule CreateAdjustmentRule(
  85. DateTime dateStart,
  86. DateTime dateEnd,
  87. TimeSpan daylightDelta,
  88. TransitionTime daylightTransitionStart,
  89. TransitionTime daylightTransitionEnd,
  90. TimeSpan baseUtcOffsetDelta,
  91. bool noDaylightTransitions)
  92. {
  93. AdjustDaylightDeltaToExpectedRange(ref daylightDelta, ref baseUtcOffsetDelta);
  94. return new AdjustmentRule(
  95. dateStart,
  96. dateEnd,
  97. daylightDelta,
  98. daylightTransitionStart,
  99. daylightTransitionEnd,
  100. baseUtcOffsetDelta,
  101. noDaylightTransitions);
  102. }
  103. //
  104. // When Windows sets the daylight transition start Jan 1st at 12:00 AM, it means the year starts with the daylight saving on.
  105. // We have to special case this value and not adjust it when checking if any date is in the daylight saving period.
  106. //
  107. internal bool IsStartDateMarkerForBeginningOfYear() =>
  108. !NoDaylightTransitions &&
  109. DaylightTransitionStart.Month == 1 && DaylightTransitionStart.Day == 1 && DaylightTransitionStart.TimeOfDay.Hour == 0 &&
  110. DaylightTransitionStart.TimeOfDay.Minute == 0 && DaylightTransitionStart.TimeOfDay.Second == 0 &&
  111. _dateStart.Year == _dateEnd.Year;
  112. //
  113. // When Windows sets the daylight transition end Jan 1st at 12:00 AM, it means the year ends with the daylight saving on.
  114. // We have to special case this value and not adjust it when checking if any date is in the daylight saving period.
  115. //
  116. internal bool IsEndDateMarkerForEndOfYear() =>
  117. !NoDaylightTransitions &&
  118. DaylightTransitionEnd.Month == 1 && DaylightTransitionEnd.Day == 1 && DaylightTransitionEnd.TimeOfDay.Hour == 0 &&
  119. DaylightTransitionEnd.TimeOfDay.Minute == 0 && DaylightTransitionEnd.TimeOfDay.Second == 0 &&
  120. _dateStart.Year == _dateEnd.Year;
  121. /// <summary>
  122. /// Helper function that performs all of the validation checks for the factory methods and deserialization callback.
  123. /// </summary>
  124. private static void ValidateAdjustmentRule(
  125. DateTime dateStart,
  126. DateTime dateEnd,
  127. TimeSpan daylightDelta,
  128. TransitionTime daylightTransitionStart,
  129. TransitionTime daylightTransitionEnd,
  130. bool noDaylightTransitions)
  131. {
  132. if (dateStart.Kind != DateTimeKind.Unspecified && dateStart.Kind != DateTimeKind.Utc)
  133. {
  134. throw new ArgumentException(SR.Argument_DateTimeKindMustBeUnspecifiedOrUtc, nameof(dateStart));
  135. }
  136. if (dateEnd.Kind != DateTimeKind.Unspecified && dateEnd.Kind != DateTimeKind.Utc)
  137. {
  138. throw new ArgumentException(SR.Argument_DateTimeKindMustBeUnspecifiedOrUtc, nameof(dateEnd));
  139. }
  140. if (daylightTransitionStart.Equals(daylightTransitionEnd) && !noDaylightTransitions)
  141. {
  142. throw new ArgumentException(SR.Argument_TransitionTimesAreIdentical, nameof(daylightTransitionEnd));
  143. }
  144. if (dateStart > dateEnd)
  145. {
  146. throw new ArgumentException(SR.Argument_OutOfOrderDateTimes, nameof(dateStart));
  147. }
  148. // This cannot use UtcOffsetOutOfRange to account for the scenario where Samoa moved across the International Date Line,
  149. // which caused their current BaseUtcOffset to be +13. But on the other side of the line it was UTC-11 (+1 for daylight).
  150. // So when trying to describe DaylightDeltas for those times, the DaylightDelta needs
  151. // to be -23 (what it takes to go from UTC+13 to UTC-10)
  152. if (daylightDelta.TotalHours < -23.0 || daylightDelta.TotalHours > 14.0)
  153. {
  154. throw new ArgumentOutOfRangeException(nameof(daylightDelta), daylightDelta, SR.ArgumentOutOfRange_UtcOffset);
  155. }
  156. if (daylightDelta.Ticks % TimeSpan.TicksPerMinute != 0)
  157. {
  158. throw new ArgumentException(SR.Argument_TimeSpanHasSeconds, nameof(daylightDelta));
  159. }
  160. if (dateStart != DateTime.MinValue && dateStart.Kind == DateTimeKind.Unspecified && dateStart.TimeOfDay != TimeSpan.Zero)
  161. {
  162. throw new ArgumentException(SR.Argument_DateTimeHasTimeOfDay, nameof(dateStart));
  163. }
  164. if (dateEnd != DateTime.MaxValue && dateEnd.Kind == DateTimeKind.Unspecified && dateEnd.TimeOfDay != TimeSpan.Zero)
  165. {
  166. throw new ArgumentException(SR.Argument_DateTimeHasTimeOfDay, nameof(dateEnd));
  167. }
  168. }
  169. /// <summary>
  170. /// Ensures the daylight delta is within [-12, 12] hours
  171. /// </summary>>
  172. private static void AdjustDaylightDeltaToExpectedRange(ref TimeSpan daylightDelta, ref TimeSpan baseUtcOffsetDelta)
  173. {
  174. if (daylightDelta > MaxDaylightDelta)
  175. {
  176. daylightDelta -= DaylightDeltaAdjustment;
  177. baseUtcOffsetDelta += DaylightDeltaAdjustment;
  178. }
  179. else if (daylightDelta < -MaxDaylightDelta)
  180. {
  181. daylightDelta += DaylightDeltaAdjustment;
  182. baseUtcOffsetDelta -= DaylightDeltaAdjustment;
  183. }
  184. System.Diagnostics.Debug.Assert(daylightDelta <= MaxDaylightDelta && daylightDelta >= -MaxDaylightDelta,
  185. "DaylightDelta should not ever be more than 24h");
  186. }
  187. void IDeserializationCallback.OnDeserialization(object? sender)
  188. {
  189. // OnDeserialization is called after each instance of this class is deserialized.
  190. // This callback method performs AdjustmentRule validation after being deserialized.
  191. try
  192. {
  193. ValidateAdjustmentRule(_dateStart, _dateEnd, _daylightDelta,
  194. _daylightTransitionStart, _daylightTransitionEnd, _noDaylightTransitions);
  195. }
  196. catch (ArgumentException e)
  197. {
  198. throw new SerializationException(SR.Serialization_InvalidData, e);
  199. }
  200. }
  201. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  202. {
  203. if (info == null)
  204. {
  205. throw new ArgumentNullException(nameof(info));
  206. }
  207. info.AddValue("DateStart", _dateStart); // Do not rename (binary serialization)
  208. info.AddValue("DateEnd", _dateEnd); // Do not rename (binary serialization)
  209. info.AddValue("DaylightDelta", _daylightDelta); // Do not rename (binary serialization)
  210. info.AddValue("DaylightTransitionStart", _daylightTransitionStart); // Do not rename (binary serialization)
  211. info.AddValue("DaylightTransitionEnd", _daylightTransitionEnd); // Do not rename (binary serialization)
  212. info.AddValue("BaseUtcOffsetDelta", _baseUtcOffsetDelta); // Do not rename (binary serialization)
  213. info.AddValue("NoDaylightTransitions", _noDaylightTransitions); // Do not rename (binary serialization)
  214. }
  215. private AdjustmentRule(SerializationInfo info, StreamingContext context)
  216. {
  217. if (info == null)
  218. {
  219. throw new ArgumentNullException(nameof(info));
  220. }
  221. _dateStart = (DateTime)info.GetValue("DateStart", typeof(DateTime))!; // Do not rename (binary serialization)
  222. _dateEnd = (DateTime)info.GetValue("DateEnd", typeof(DateTime))!; // Do not rename (binary serialization)
  223. _daylightDelta = (TimeSpan)info.GetValue("DaylightDelta", typeof(TimeSpan))!; // Do not rename (binary serialization)
  224. _daylightTransitionStart = (TransitionTime)info.GetValue("DaylightTransitionStart", typeof(TransitionTime))!; // Do not rename (binary serialization)
  225. _daylightTransitionEnd = (TransitionTime)info.GetValue("DaylightTransitionEnd", typeof(TransitionTime))!; // Do not rename (binary serialization)
  226. object? o = info.GetValueNoThrow("BaseUtcOffsetDelta", typeof(TimeSpan)); // Do not rename (binary serialization)
  227. if (o != null)
  228. {
  229. _baseUtcOffsetDelta = (TimeSpan)o;
  230. }
  231. o = info.GetValueNoThrow("NoDaylightTransitions", typeof(bool)); // Do not rename (binary serialization)
  232. if (o != null)
  233. {
  234. _noDaylightTransitions = (bool)o;
  235. }
  236. }
  237. }
  238. }
  239. }