TimeZoneInfo.TransitionTime.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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) || (!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 MOBILE
  35. [TypeForwardedFrom (Consts.AssemblySystem_Core)]
  36. #elif NET_4_0
  37. [TypeForwardedFrom (Consts.AssemblySystemCore_3_5)]
  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 (SerializationInfo info, StreamingContext context)
  99. {
  100. if (info == null)
  101. throw new ArgumentNullException ("info");
  102. timeOfDay = (DateTime) info.GetValue ("TimeOfDay", typeof (DateTime));
  103. month = (byte) info.GetValue ("Month", typeof (byte));
  104. week = (byte) info.GetValue ("Week", typeof (byte));
  105. day = (byte) info.GetValue ("Day", typeof (byte));
  106. dayOfWeek = (DayOfWeek) info.GetValue ("DayOfWeek", typeof (DayOfWeek));
  107. isFixedDateRule = (bool) info.GetValue ("IsFixedDateRule", typeof (bool));
  108. if (isFixedDateRule)
  109. {
  110. week = -1;
  111. dayOfWeek = (DayOfWeek) (-1);
  112. }
  113. if (!isFixedDateRule)
  114. day = -1;
  115. }
  116. private TransitionTime (
  117. DateTime timeOfDay,
  118. int month,
  119. int day) : this (timeOfDay, month)
  120. {
  121. if (day < 1 || day > 31)
  122. throw new ArgumentOutOfRangeException ("day parameter is less than 1 or greater than 31");
  123. this.day = day;
  124. this.isFixedDateRule = true;
  125. }
  126. private TransitionTime (
  127. DateTime timeOfDay,
  128. int month,
  129. int week,
  130. DayOfWeek dayOfWeek) : this (timeOfDay, month)
  131. {
  132. if (week < 1 || week > 5)
  133. throw new ArgumentOutOfRangeException ("week parameter is less than 1 or greater than 5");
  134. if (dayOfWeek != DayOfWeek.Sunday &&
  135. dayOfWeek != DayOfWeek.Monday &&
  136. dayOfWeek != DayOfWeek.Tuesday &&
  137. dayOfWeek != DayOfWeek.Wednesday &&
  138. dayOfWeek != DayOfWeek.Thursday &&
  139. dayOfWeek != DayOfWeek.Friday &&
  140. dayOfWeek != DayOfWeek.Saturday)
  141. throw new ArgumentOutOfRangeException ("dayOfWeek parameter is not a member od DayOfWeek enumeration");
  142. this.week = week;
  143. this.dayOfWeek = dayOfWeek;
  144. this.isFixedDateRule = false;
  145. }
  146. private TransitionTime (
  147. DateTime timeOfDay,
  148. int month)
  149. {
  150. if (timeOfDay.Year != 1 || timeOfDay.Month != 1 || timeOfDay.Day != 1)
  151. throw new ArgumentException ("timeOfDay parameter has a non-default date component");
  152. if (timeOfDay.Kind != DateTimeKind.Unspecified)
  153. throw new ArgumentException ("timeOfDay parameter Kind's property is not DateTimeKind.Unspecified");
  154. if (timeOfDay.Ticks % TimeSpan.TicksPerMillisecond != 0)
  155. throw new ArgumentException ("timeOfDay parameter does not represent a whole number of milliseconds");
  156. if (month < 1 || month > 12)
  157. throw new ArgumentOutOfRangeException ("month parameter is less than 1 or greater than 12");
  158. this.timeOfDay = timeOfDay;
  159. this.month = month;
  160. this.week = -1;
  161. this.dayOfWeek = (System.DayOfWeek)(-1);
  162. this.day = -1;
  163. this.isFixedDateRule = false;
  164. }
  165. public static bool operator == (TransitionTime t1, TransitionTime t2)
  166. {
  167. return ( t1.day == t2.day &&
  168. t1.dayOfWeek == t2.dayOfWeek &&
  169. t1.isFixedDateRule == t2.isFixedDateRule &&
  170. t1.month == t2.month &&
  171. t1.timeOfDay == t2.timeOfDay &&
  172. t1.week == t2.week);
  173. }
  174. public static bool operator != (TransitionTime t1, TransitionTime t2)
  175. {
  176. return !(t1 == t2);
  177. }
  178. #if NET_4_0
  179. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  180. #else
  181. public void GetObjectData (SerializationInfo info, StreamingContext context)
  182. #endif
  183. {
  184. if (info == null)
  185. throw new ArgumentNullException ("info");
  186. info.AddValue ("TimeOfDay", TimeOfDay);
  187. info.AddValue ("Month", System.Convert.ToByte(Month));
  188. if (week > -1)
  189. info.AddValue ("Week", System.Convert.ToByte(week));
  190. else
  191. info.AddValue ("Week", (byte) 1);
  192. if (day > -1)
  193. info.AddValue ("Day", System.Convert.ToByte(day));
  194. else
  195. info.AddValue ("Day", (byte) 1);
  196. if (dayOfWeek != ((System.DayOfWeek) (-1)))
  197. info.AddValue ("DayOfWeek", dayOfWeek);
  198. else
  199. info.AddValue ("DayOfWeek", DayOfWeek.Sunday);
  200. info.AddValue ("IsFixedDateRule", IsFixedDateRule);
  201. }
  202. public override bool Equals (object obj)
  203. {
  204. if (obj is TransitionTime)
  205. return this == (TransitionTime) obj;
  206. return false;
  207. }
  208. public bool Equals (TimeZoneInfo.TransitionTime other)
  209. {
  210. return this == other;
  211. }
  212. public override int GetHashCode ()
  213. {
  214. return (day ^ (int)dayOfWeek ^ month ^ (int)timeOfDay.Ticks ^ week);
  215. }
  216. #if NET_4_0
  217. void IDeserializationCallback.OnDeserialization (object sender)
  218. #else
  219. public void OnDeserialization (object sender)
  220. #endif
  221. {
  222. try {
  223. TimeZoneInfo.TransitionTime.Validate (timeOfDay, month, week, day, dayOfWeek, isFixedDateRule);
  224. } catch (ArgumentException ex) {
  225. throw new SerializationException ("invalid serialization data", ex);
  226. }
  227. }
  228. private static void Validate (DateTime timeOfDay, int month,int week, int day, DayOfWeek dayOfWeek, bool isFixedDateRule)
  229. {
  230. if (timeOfDay.Year != 1 || timeOfDay.Month != 1 || timeOfDay.Day != 1)
  231. throw new ArgumentException ("timeOfDay parameter has a non-default date component");
  232. if (timeOfDay.Kind != DateTimeKind.Unspecified)
  233. throw new ArgumentException ("timeOfDay parameter Kind's property is not DateTimeKind.Unspecified");
  234. if (timeOfDay.Ticks % TimeSpan.TicksPerMillisecond != 0)
  235. throw new ArgumentException ("timeOfDay parameter does not represent a whole number of milliseconds");
  236. if (day < 1 || day > 31) {
  237. if (!(!isFixedDateRule && day == -1))
  238. throw new ArgumentOutOfRangeException ("day parameter is less than 1 or greater than 31");
  239. }
  240. if (week < 1 || week > 5) {
  241. if (!(isFixedDateRule && week == -1))
  242. throw new ArgumentOutOfRangeException ("week parameter is less than 1 or greater than 5");
  243. }
  244. if (month < 1 || month > 12)
  245. throw new ArgumentOutOfRangeException ("month parameter is less than 1 or greater than 12");
  246. if (dayOfWeek != DayOfWeek.Sunday &&
  247. dayOfWeek != DayOfWeek.Monday &&
  248. dayOfWeek != DayOfWeek.Tuesday &&
  249. dayOfWeek != DayOfWeek.Wednesday &&
  250. dayOfWeek != DayOfWeek.Thursday &&
  251. dayOfWeek != DayOfWeek.Friday &&
  252. dayOfWeek != DayOfWeek.Saturday) {
  253. if (!(isFixedDateRule && dayOfWeek == (DayOfWeek) (-1)))
  254. throw new ArgumentOutOfRangeException ("dayOfWeek parameter is not a member od DayOfWeek enumeration");
  255. }
  256. }
  257. }
  258. }
  259. }
  260. #endif