TimeZoneInfo.AdjustmentRule.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * System.TimeZoneInfo.AdjustmentRule
  3. *
  4. * Author(s)
  5. * Stephane Delcroix <[email protected]>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #if (INSIDE_CORLIB && NET_4_0) || (!INSIDE_CORLIB && (NET_3_5 && !NET_4_0 && !MOBILE))
  27. using System.Runtime.CompilerServices;
  28. using System.Runtime.Serialization;
  29. namespace System
  30. {
  31. public sealed partial class TimeZoneInfo {
  32. [SerializableAttribute]
  33. #if MOBILE
  34. [TypeForwardedFrom (Consts.AssemblySystem_Core)]
  35. #elif NET_4_0
  36. [TypeForwardedFrom (Consts.AssemblySystemCore_3_5)]
  37. #endif
  38. public sealed class AdjustmentRule : IEquatable<TimeZoneInfo.AdjustmentRule>, ISerializable, IDeserializationCallback
  39. {
  40. DateTime dateEnd;
  41. public DateTime DateEnd {
  42. get { return dateEnd; }
  43. }
  44. DateTime dateStart;
  45. public DateTime DateStart {
  46. get { return dateStart; }
  47. }
  48. TimeSpan daylightDelta;
  49. public TimeSpan DaylightDelta {
  50. get { return daylightDelta; }
  51. }
  52. TransitionTime daylightTransitionEnd;
  53. public TransitionTime DaylightTransitionEnd {
  54. get { return daylightTransitionEnd; }
  55. }
  56. TransitionTime daylightTransitionStart;
  57. public TransitionTime DaylightTransitionStart {
  58. get { return daylightTransitionStart; }
  59. }
  60. public static AdjustmentRule CreateAdjustmentRule (
  61. DateTime dateStart,
  62. DateTime dateEnd,
  63. TimeSpan daylightDelta,
  64. TransitionTime daylightTransitionStart,
  65. TransitionTime daylightTransitionEnd)
  66. {
  67. return new AdjustmentRule (dateStart, dateEnd, daylightDelta, daylightTransitionStart, daylightTransitionEnd);
  68. }
  69. private AdjustmentRule (SerializationInfo info, StreamingContext context)
  70. {
  71. if (info == null)
  72. throw new ArgumentNullException ("info");
  73. dateStart = (DateTime) info.GetValue ("DateStart", typeof (DateTime));
  74. dateEnd = (DateTime) info.GetValue ("DateEnd", typeof (DateTime));
  75. daylightDelta = (TimeSpan) info.GetValue ("DaylightDelta", typeof (TimeSpan));
  76. daylightTransitionStart = (TimeZoneInfo.TransitionTime) info.GetValue ("DaylightTransitionStart", typeof (TimeZoneInfo.TransitionTime));
  77. daylightTransitionEnd = (TimeZoneInfo.TransitionTime) info.GetValue ("DaylightTransitionEnd", typeof (TimeZoneInfo.TransitionTime));
  78. }
  79. private AdjustmentRule (
  80. DateTime dateStart,
  81. DateTime dateEnd,
  82. TimeSpan daylightDelta,
  83. TransitionTime daylightTransitionStart,
  84. TransitionTime daylightTransitionEnd)
  85. {
  86. if (dateStart.Kind != DateTimeKind.Unspecified || dateEnd.Kind != DateTimeKind.Unspecified)
  87. throw new ArgumentException ("the Kind property of dateStart or dateEnd parameter does not equal DateTimeKind.Unspecified");
  88. if (daylightTransitionStart == daylightTransitionEnd)
  89. throw new ArgumentException ("daylightTransitionStart parameter cannot equal daylightTransitionEnd parameter");
  90. if (dateStart.Ticks % TimeSpan.TicksPerDay != 0 || dateEnd.Ticks % TimeSpan.TicksPerDay != 0)
  91. throw new ArgumentException ("dateStart or dateEnd parameter includes a time of day value");
  92. if (dateEnd < dateStart)
  93. throw new ArgumentOutOfRangeException ("dateEnd is earlier than dateStart");
  94. if (daylightDelta > new TimeSpan (14, 0, 0) || daylightDelta < new TimeSpan (-14, 0, 0))
  95. throw new ArgumentOutOfRangeException ("daylightDelta is less than -14 or greater than 14 hours");
  96. if (daylightDelta.Ticks % TimeSpan.TicksPerSecond != 0)
  97. throw new ArgumentOutOfRangeException ("daylightDelta parameter does not represent a whole number of seconds");
  98. this.dateStart = dateStart;
  99. this.dateEnd = dateEnd;
  100. this.daylightDelta = daylightDelta;
  101. this.daylightTransitionStart = daylightTransitionStart;
  102. this.daylightTransitionEnd = daylightTransitionEnd;
  103. }
  104. public bool Equals (TimeZoneInfo.AdjustmentRule other)
  105. {
  106. return dateStart == other.dateStart &&
  107. dateEnd == other.dateEnd &&
  108. daylightDelta == other.daylightDelta &&
  109. daylightTransitionStart == other.daylightTransitionStart &&
  110. daylightTransitionEnd == other.daylightTransitionEnd;
  111. }
  112. public override int GetHashCode ()
  113. {
  114. return dateStart.GetHashCode () ^
  115. dateEnd.GetHashCode () ^
  116. daylightDelta.GetHashCode () ^
  117. daylightTransitionStart.GetHashCode () ^
  118. daylightTransitionEnd.GetHashCode ();
  119. }
  120. #if NET_4_0
  121. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  122. #else
  123. public void GetObjectData (SerializationInfo info, StreamingContext context)
  124. #endif
  125. {
  126. if (info == null)
  127. throw new ArgumentNullException ("info");
  128. info.AddValue ("DateStart", DateStart);
  129. info.AddValue ("DateEnd", DateEnd);
  130. info.AddValue ("DaylightDelta", DaylightDelta);
  131. info.AddValue ("DaylightTransitionStart", DaylightTransitionStart);
  132. info.AddValue ("DaylightTransitionEnd", DaylightTransitionEnd);
  133. }
  134. #if NET_4_0
  135. void IDeserializationCallback.OnDeserialization (object sender)
  136. #else
  137. public void OnDeserialization (object sender)
  138. #endif
  139. {
  140. try {
  141. TimeZoneInfo.AdjustmentRule.Validate (dateStart, dateEnd, daylightDelta,
  142. daylightTransitionStart, daylightTransitionEnd);
  143. } catch (ArgumentException ex) {
  144. throw new SerializationException ("invalid serialization data", ex);
  145. }
  146. }
  147. private static void Validate (
  148. DateTime dateStart,
  149. DateTime dateEnd,
  150. TimeSpan daylightDelta,
  151. TransitionTime daylightTransitionStart,
  152. TransitionTime daylightTransitionEnd)
  153. {
  154. if (dateStart.Kind != DateTimeKind.Unspecified || dateEnd.Kind != DateTimeKind.Unspecified)
  155. throw new ArgumentException ("the Kind property of dateStart or dateEnd parameter does not equal DateTimeKind.Unspecified");
  156. if (daylightTransitionStart == daylightTransitionEnd)
  157. throw new ArgumentException ("daylightTransitionStart parameter cannot equal daylightTransitionEnd parameter");
  158. if (dateStart.Ticks % TimeSpan.TicksPerDay != 0 || dateEnd.Ticks % TimeSpan.TicksPerDay != 0)
  159. throw new ArgumentException ("dateStart or dateEnd parameter includes a time of day value");
  160. if (dateEnd < dateStart)
  161. throw new ArgumentOutOfRangeException ("dateEnd is earlier than dateStart");
  162. if (daylightDelta > new TimeSpan (14, 0, 0) || daylightDelta < new TimeSpan (-14, 0, 0))
  163. throw new ArgumentOutOfRangeException ("daylightDelta is less than -14 or greater than 14 hours");
  164. if (daylightDelta.Ticks % TimeSpan.TicksPerSecond != 0)
  165. throw new ArgumentOutOfRangeException ("daylightDelta parameter does not represent a whole number of seconds");
  166. }
  167. }
  168. }
  169. }
  170. #endif