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