TimeZoneInfo.TransitionTime.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * System.TimeZoneInfo.TransitionTime
  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. using System.Runtime.Serialization;
  27. namespace System
  28. {
  29. public sealed partial class TimeZoneInfo
  30. {
  31. [SerializableAttribute]
  32. public struct TransitionTime : IEquatable<TimeZoneInfo.TransitionTime>, ISerializable, IDeserializationCallback
  33. {
  34. DateTime timeOfDay;
  35. public DateTime TimeOfDay {
  36. get { return timeOfDay; }
  37. }
  38. int month;
  39. public int Month {
  40. get { return month; }
  41. }
  42. int day;
  43. public int Day {
  44. get {
  45. #if STRICT
  46. if (!isFixedDateRule)
  47. throw new Exception ("Day property is not valid for floating date rules");
  48. #endif
  49. return day;
  50. }
  51. }
  52. int week;
  53. public int Week {
  54. get {
  55. #if STRICT
  56. if (isFixedDateRule)
  57. throw new Exception ("Week property is not valid for fixed date rules");
  58. #endif
  59. return week;
  60. }
  61. }
  62. DayOfWeek dayOfWeek;
  63. public DayOfWeek DayOfWeek {
  64. get {
  65. #if STRICT
  66. if (isFixedDateRule)
  67. throw new Exception ("DayOfWeek property is not valid for fixed date rules");
  68. #endif
  69. return dayOfWeek;
  70. }
  71. }
  72. bool isFixedDateRule;
  73. public bool IsFixedDateRule {
  74. get { return isFixedDateRule; }
  75. }
  76. public static TransitionTime CreateFixedDateRule (
  77. DateTime timeOfDay,
  78. int month,
  79. int day)
  80. {
  81. return new TransitionTime (timeOfDay, month, day);
  82. }
  83. public static TransitionTime CreateFloatingDateRule (
  84. DateTime timeOfDay,
  85. int month,
  86. int week,
  87. DayOfWeek dayOfWeek)
  88. {
  89. return new TransitionTime (timeOfDay, month, week, dayOfWeek);
  90. }
  91. private TransitionTime (
  92. DateTime timeOfDay,
  93. int month,
  94. int day) : this (timeOfDay, month)
  95. {
  96. if (day < 1 || day > 31)
  97. throw new ArgumentOutOfRangeException ("day parameter is less than 1 or greater than 31");
  98. this.day = day;
  99. this.isFixedDateRule = true;
  100. }
  101. private TransitionTime (
  102. DateTime timeOfDay,
  103. int month,
  104. int week,
  105. DayOfWeek dayOfWeek) : this (timeOfDay, month)
  106. {
  107. if (week < 1 || week > 5)
  108. throw new ArgumentOutOfRangeException ("week parameter is less than 1 or greater than 5");
  109. if (dayOfWeek != DayOfWeek.Sunday &&
  110. dayOfWeek != DayOfWeek.Monday &&
  111. dayOfWeek != DayOfWeek.Tuesday &&
  112. dayOfWeek != DayOfWeek.Wednesday &&
  113. dayOfWeek != DayOfWeek.Thursday &&
  114. dayOfWeek != DayOfWeek.Friday &&
  115. dayOfWeek != DayOfWeek.Saturday)
  116. throw new ArgumentOutOfRangeException ("dayOfWeek parameter is not a member od DayOfWeek enumeration");
  117. this.week = week;
  118. this.dayOfWeek = dayOfWeek;
  119. this.isFixedDateRule = false;
  120. }
  121. private TransitionTime (
  122. DateTime timeOfDay,
  123. int month)
  124. {
  125. if (timeOfDay.Year != 1 || timeOfDay.Month != 1 || timeOfDay.Day != 1)
  126. throw new ArgumentException ("timeOfDay parameter has a non-default date component");
  127. if (timeOfDay.Kind != DateTimeKind.Unspecified)
  128. throw new ArgumentException ("timeOfDay parameter Kind's property is not DateTimeKind.Unspecified");
  129. if (timeOfDay.Ticks % TimeSpan.TicksPerMillisecond != 0)
  130. throw new ArgumentException ("timeOfDay parameter does not represent a whole number of milliseconds");
  131. if (month < 1 || month > 12)
  132. throw new ArgumentOutOfRangeException ("month parameter is less than 1 or greater than 12");
  133. this.timeOfDay = timeOfDay;
  134. this.month = month;
  135. this.week = -1;
  136. this.dayOfWeek = (System.DayOfWeek)(-1);
  137. this.day = -1;
  138. this.isFixedDateRule = false;
  139. }
  140. public static bool operator == (TransitionTime t1, TransitionTime t2)
  141. {
  142. return ( t1.day == t2.day &&
  143. t1.dayOfWeek == t2.dayOfWeek &&
  144. t1.isFixedDateRule == t2.isFixedDateRule &&
  145. t1.month == t2.month &&
  146. t1.timeOfDay == t2.timeOfDay &&
  147. t1.week == t2.week);
  148. }
  149. public static bool operator != (TransitionTime t1, TransitionTime t2)
  150. {
  151. return !(t1 == t2);
  152. }
  153. public void GetObjectData (SerializationInfo info, StreamingContext context)
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. public override bool Equals (object other)
  158. {
  159. if (other is TransitionTime)
  160. return this == (TransitionTime) other;
  161. return false;
  162. }
  163. public bool Equals (TimeZoneInfo.TransitionTime other)
  164. {
  165. return this == other;
  166. }
  167. public override int GetHashCode ()
  168. {
  169. return (day ^ (int)dayOfWeek ^ month ^ (int)timeOfDay.Ticks ^ week);
  170. }
  171. public void OnDeserialization (object sender)
  172. {
  173. throw new NotImplementedException ();
  174. }
  175. }
  176. }
  177. }