2 * System.TimeZoneInfo.TransitionTime
5 * Stephane Delcroix <stephane@delcroix.org>
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:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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.
27 #if INSIDE_CORLIB || (NET_3_5 && !NET_4_0 && !MOBILE)
29 using System.Runtime.CompilerServices;
30 using System.Runtime.Serialization;
34 #if NET_4_0 || !INSIDE_CORLIB
37 sealed partial class TimeZoneInfo
39 [SerializableAttribute]
41 [TypeForwardedFrom (Consts.AssemblySystem_Core)]
43 [TypeForwardedFrom (Consts.AssemblySystemCore_3_5)]
45 public struct TransitionTime : IEquatable<TimeZoneInfo.TransitionTime>, ISerializable, IDeserializationCallback
48 public DateTime TimeOfDay {
49 get { return timeOfDay; }
62 throw new Exception ("Day property is not valid for floating date rules");
73 throw new Exception ("Week property is not valid for fixed date rules");
81 public DayOfWeek DayOfWeek {
85 throw new Exception ("DayOfWeek property is not valid for fixed date rules");
93 public bool IsFixedDateRule {
94 get { return isFixedDateRule; }
97 public static TransitionTime CreateFixedDateRule (
102 return new TransitionTime (timeOfDay, month, day);
105 public static TransitionTime CreateFloatingDateRule (
111 return new TransitionTime (timeOfDay, month, week, dayOfWeek);
114 private TransitionTime (SerializationInfo info, StreamingContext context)
117 throw new ArgumentNullException ("info");
118 timeOfDay = (DateTime) info.GetValue ("TimeOfDay", typeof (DateTime));
119 month = (byte) info.GetValue ("Month", typeof (byte));
120 week = (byte) info.GetValue ("Week", typeof (byte));
121 day = (byte) info.GetValue ("Day", typeof (byte));
122 dayOfWeek = (DayOfWeek) info.GetValue ("DayOfWeek", typeof (DayOfWeek));
123 isFixedDateRule = (bool) info.GetValue ("IsFixedDateRule", typeof (bool));
128 dayOfWeek = (DayOfWeek) (-1);
130 if (!isFixedDateRule)
134 private TransitionTime (
137 int day) : this (timeOfDay, month)
139 if (day < 1 || day > 31)
140 throw new ArgumentOutOfRangeException ("day parameter is less than 1 or greater than 31");
143 this.isFixedDateRule = true;
146 private TransitionTime (
150 DayOfWeek dayOfWeek) : this (timeOfDay, month)
152 if (week < 1 || week > 5)
153 throw new ArgumentOutOfRangeException ("week parameter is less than 1 or greater than 5");
155 if (dayOfWeek != DayOfWeek.Sunday &&
156 dayOfWeek != DayOfWeek.Monday &&
157 dayOfWeek != DayOfWeek.Tuesday &&
158 dayOfWeek != DayOfWeek.Wednesday &&
159 dayOfWeek != DayOfWeek.Thursday &&
160 dayOfWeek != DayOfWeek.Friday &&
161 dayOfWeek != DayOfWeek.Saturday)
162 throw new ArgumentOutOfRangeException ("dayOfWeek parameter is not a member od DayOfWeek enumeration");
165 this.dayOfWeek = dayOfWeek;
166 this.isFixedDateRule = false;
169 private TransitionTime (
173 if (timeOfDay.Year != 1 || timeOfDay.Month != 1 || timeOfDay.Day != 1)
174 throw new ArgumentException ("timeOfDay parameter has a non-default date component");
176 if (timeOfDay.Kind != DateTimeKind.Unspecified)
177 throw new ArgumentException ("timeOfDay parameter Kind's property is not DateTimeKind.Unspecified");
179 if (timeOfDay.Ticks % TimeSpan.TicksPerMillisecond != 0)
180 throw new ArgumentException ("timeOfDay parameter does not represent a whole number of milliseconds");
182 if (month < 1 || month > 12)
183 throw new ArgumentOutOfRangeException ("month parameter is less than 1 or greater than 12");
185 this.timeOfDay = timeOfDay;
189 this.dayOfWeek = (System.DayOfWeek)(-1);
191 this.isFixedDateRule = false;
194 public static bool operator == (TransitionTime t1, TransitionTime t2)
196 return ( t1.day == t2.day &&
197 t1.dayOfWeek == t2.dayOfWeek &&
198 t1.isFixedDateRule == t2.isFixedDateRule &&
199 t1.month == t2.month &&
200 t1.timeOfDay == t2.timeOfDay &&
204 public static bool operator != (TransitionTime t1, TransitionTime t2)
211 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
213 public void GetObjectData (SerializationInfo info, StreamingContext context)
217 throw new ArgumentNullException ("info");
218 info.AddValue ("TimeOfDay", TimeOfDay);
219 info.AddValue ("Month", System.Convert.ToByte(Month));
221 info.AddValue ("Week", System.Convert.ToByte(week));
223 info.AddValue ("Week", (byte) 1);
225 info.AddValue ("Day", System.Convert.ToByte(day));
227 info.AddValue ("Day", (byte) 1);
228 if (dayOfWeek != ((System.DayOfWeek) (-1)))
229 info.AddValue ("DayOfWeek", dayOfWeek);
231 info.AddValue ("DayOfWeek", DayOfWeek.Sunday);
232 info.AddValue ("IsFixedDateRule", IsFixedDateRule);
235 public override bool Equals (object obj)
237 if (obj is TransitionTime)
238 return this == (TransitionTime) obj;
242 public bool Equals (TimeZoneInfo.TransitionTime other)
244 return this == other;
247 public override int GetHashCode ()
249 return (day ^ (int)dayOfWeek ^ month ^ (int)timeOfDay.Ticks ^ week);
253 void IDeserializationCallback.OnDeserialization (object sender)
255 public void OnDeserialization (object sender)
259 TimeZoneInfo.TransitionTime.Validate (timeOfDay, month, week, day, dayOfWeek, isFixedDateRule);
260 } catch (ArgumentException ex) {
261 throw new SerializationException ("invalid serialization data", ex);
265 private static void Validate (DateTime timeOfDay, int month,int week, int day, DayOfWeek dayOfWeek, bool isFixedDateRule)
267 if (timeOfDay.Year != 1 || timeOfDay.Month != 1 || timeOfDay.Day != 1)
268 throw new ArgumentException ("timeOfDay parameter has a non-default date component");
270 if (timeOfDay.Kind != DateTimeKind.Unspecified)
271 throw new ArgumentException ("timeOfDay parameter Kind's property is not DateTimeKind.Unspecified");
273 if (timeOfDay.Ticks % TimeSpan.TicksPerMillisecond != 0)
274 throw new ArgumentException ("timeOfDay parameter does not represent a whole number of milliseconds");
276 if (day < 1 || day > 31) {
277 if (!(!isFixedDateRule && day == -1))
278 throw new ArgumentOutOfRangeException ("day parameter is less than 1 or greater than 31");
281 if (week < 1 || week > 5) {
282 if (!(isFixedDateRule && week == -1))
283 throw new ArgumentOutOfRangeException ("week parameter is less than 1 or greater than 5");
286 if (month < 1 || month > 12)
287 throw new ArgumentOutOfRangeException ("month parameter is less than 1 or greater than 12");
289 if (dayOfWeek != DayOfWeek.Sunday &&
290 dayOfWeek != DayOfWeek.Monday &&
291 dayOfWeek != DayOfWeek.Tuesday &&
292 dayOfWeek != DayOfWeek.Wednesday &&
293 dayOfWeek != DayOfWeek.Thursday &&
294 dayOfWeek != DayOfWeek.Friday &&
295 dayOfWeek != DayOfWeek.Saturday) {
296 if (!(isFixedDateRule && dayOfWeek == (DayOfWeek) (-1)))
297 throw new ArgumentOutOfRangeException ("dayOfWeek parameter is not a member od DayOfWeek enumeration");