2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Core / System / TimeZoneInfo.TransitionTime.cs
1 /*
2  * System.TimeZoneInfo.TransitionTime
3  *
4  * Author(s)
5  *      Stephane Delcroix <stephane@delcroix.org>
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
27 #if (INSIDE_CORLIB && (NET_4_0 || BOOTSTRAP_NET_4_0)) || (NET_2_1 && !INSIDE_CORLIB) || (NET_3_5 && !NET_4_0 && !BOOTSTRAP_NET_4_0)
28
29 using System.Runtime.Serialization;
30
31 namespace System
32 {
33         public sealed partial class TimeZoneInfo 
34         {
35                 [SerializableAttribute]
36                 public struct TransitionTime : IEquatable<TimeZoneInfo.TransitionTime>, ISerializable, IDeserializationCallback
37                 {
38                         DateTime timeOfDay;
39                         public DateTime TimeOfDay {
40                                 get { return timeOfDay; }
41                         }
42
43                         int month;
44                         public int Month {
45                                 get { return month; }
46                         }
47
48                         int day;
49                         public int Day {
50                                 get { 
51 #if STRICT
52                                         if (!isFixedDateRule)
53                                                 throw new Exception ("Day property is not valid for floating date rules");
54 #endif
55                                         return day; 
56                                 }
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                 
67                                         return week; 
68                                 }
69                         }
70
71                         DayOfWeek dayOfWeek;
72                         public DayOfWeek DayOfWeek {
73                                 get { 
74 #if STRICT
75                                         if (isFixedDateRule)
76                                                 throw new Exception ("DayOfWeek property is not valid for fixed date rules");
77 #endif
78         
79                                         return dayOfWeek; 
80                                 }
81                         }
82
83                         bool isFixedDateRule;
84                         public bool IsFixedDateRule {
85                                 get { return isFixedDateRule; }
86                         }
87
88                         public static TransitionTime CreateFixedDateRule (
89                                 DateTime timeOfDay, 
90                                 int month, 
91                                 int day)
92                         {
93                                 return new TransitionTime (timeOfDay, month, day);
94                         }
95
96                         public static TransitionTime CreateFloatingDateRule (
97                                 DateTime timeOfDay,
98                                 int month,
99                                 int week,
100                                 DayOfWeek dayOfWeek)
101                         {
102                                 return new TransitionTime (timeOfDay, month, week, dayOfWeek);
103                         }
104
105                         private TransitionTime (
106                                 DateTime timeOfDay,
107                                 int month,
108                                 int day) : this (timeOfDay, month)
109                         {
110                                 if (day < 1 || day > 31)
111                                         throw new ArgumentOutOfRangeException ("day parameter is less than 1 or greater than 31");
112
113                                 this.day = day; 
114                                 this.isFixedDateRule = true;
115                         }
116
117                         private TransitionTime (
118                                 DateTime timeOfDay,
119                                 int month,
120                                 int week,
121                                 DayOfWeek dayOfWeek)  : this (timeOfDay, month)
122                         {
123                                 if (week < 1 || week > 5)
124                                         throw new ArgumentOutOfRangeException ("week parameter is less than 1 or greater than 5");
125
126                                 if (dayOfWeek != DayOfWeek.Sunday &&
127                                                 dayOfWeek != DayOfWeek.Monday &&
128                                                 dayOfWeek != DayOfWeek.Tuesday &&
129                                                 dayOfWeek != DayOfWeek.Wednesday &&
130                                                 dayOfWeek != DayOfWeek.Thursday &&
131                                                 dayOfWeek != DayOfWeek.Friday &&
132                                                 dayOfWeek != DayOfWeek.Saturday)
133                                         throw new ArgumentOutOfRangeException ("dayOfWeek parameter is not a member od DayOfWeek enumeration");
134
135                                 this.week = week;
136                                 this.dayOfWeek = dayOfWeek;
137                                 this.isFixedDateRule = false;
138                         }
139
140                         private TransitionTime (
141                                 DateTime timeOfDay,
142                                 int month)
143                         {
144                                 if (timeOfDay.Year != 1 || timeOfDay.Month != 1 || timeOfDay.Day != 1)
145                                         throw new ArgumentException ("timeOfDay parameter has a non-default date component");
146
147                                 if (timeOfDay.Kind != DateTimeKind.Unspecified)
148                                         throw new ArgumentException ("timeOfDay parameter Kind's property is not DateTimeKind.Unspecified");
149
150                                 if (timeOfDay.Ticks % TimeSpan.TicksPerMillisecond != 0)
151                                         throw new ArgumentException ("timeOfDay parameter does not represent a whole number of milliseconds");
152
153                                 if (month < 1 || month > 12)
154                                         throw new ArgumentOutOfRangeException ("month parameter is less than 1 or greater than 12");
155                                 
156                                 this.timeOfDay = timeOfDay;
157                                 this.month = month;
158
159                                 this.week = -1;
160                                 this.dayOfWeek = (System.DayOfWeek)(-1);
161                                 this.day = -1;
162                                 this.isFixedDateRule = false;
163                         }
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
175                         public static bool operator != (TransitionTime t1, TransitionTime t2)
176                         {
177                                 return !(t1 == t2);
178                         }
179
180
181 #if NET_4_0
182                         void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
183 #else
184                         public void GetObjectData (SerializationInfo info, StreamingContext context)
185 #endif
186                         {
187                                 throw new NotImplementedException ();
188                         }
189         
190                         public override bool Equals (object other)
191                         {
192                                 if (other is TransitionTime)
193                                         return this == (TransitionTime) other;
194                                 return false;
195                         }
196
197                         public bool Equals (TimeZoneInfo.TransitionTime other)
198                         {
199                                 return this == other;
200                         }
201
202                         public override int GetHashCode ()
203                         {
204                                 return (day ^ (int)dayOfWeek ^ month ^ (int)timeOfDay.Ticks ^ week);
205                         }
206
207 #if NET_4_0
208                         void IDeserializationCallback.OnDeserialization (object sender)
209 #else
210                         public void OnDeserialization (object sender)
211 #endif
212                         {
213                                 throw new NotImplementedException ();
214                         }
215                 }
216         }
217 }
218
219 #endif