TimeZoneInfo.TransitionTime.cs 5.6 KB

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