TimeZoneInfo.TransitionTime.cs 6.0 KB

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