Merge pull request #703 from ysw/master
[mono.git] / mcs / class / System.Core / Test / System / TimeZoneInfoTest.cs
1 /*
2  * TimeZoneInfo.Tests
3  *
4  * Author(s)
5  *      Stephane Delcroix <stephane@delcroix.org>
6  *
7  * Copyright 2011 Xamarin Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 using System;
30 using System.IO;
31 using System.Runtime.Serialization.Formatters.Binary;
32 using System.Collections;
33
34 using NUnit.Framework;
35 #if NET_2_0
36 namespace MonoTests.System
37 {
38         public class TimeZoneInfoTest
39         {
40                 [TestFixture]
41                 public class PropertiesTests
42                 {
43                         [Test]
44                         public void GetLocal ()
45                         {
46                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
47                                         return;
48                                 TimeZoneInfo local = TimeZoneInfo.Local;
49                                 Assert.IsNotNull (local);
50                                 Assert.IsTrue (true);
51                         }
52                 }
53
54                 [TestFixture]
55                 public class CreateCustomTimezoneTests
56                 {
57                         [Test]
58                         [ExpectedException (typeof (ArgumentNullException))]
59                         public void IdIsNullException ()
60                         {
61                                 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null); 
62                         }
63                 
64                         [Test]
65                         [ExpectedException (typeof (ArgumentException))]
66                         public void IdIsEmptyString ()
67                         {
68                                 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);   
69                         }
70                 
71                         [Test]
72                         [ExpectedException (typeof (ArgumentException))]
73                         public void OffsetIsNotMinutes ()
74                         {
75                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);  
76                         }
77                 
78                         [Test]
79                         [ExpectedException (typeof (ArgumentOutOfRangeException))]
80                         public void OffsetTooBig ()
81                         {
82                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
83                         }
84                 
85                         [Test]
86                         [ExpectedException (typeof (ArgumentOutOfRangeException))]
87                         public void OffsetTooSmall ()
88                         {
89                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
90                         }
91                 
92                 #if STRICT
93                         [Test]
94                         [ExpectedException (typeof (ArgumentException))]
95                         public void IdLongerThan32 ()
96                         {
97                                 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);        
98                         }       
99                 #endif
100                 
101                         [Test]
102                         [ExpectedException (typeof (InvalidTimeZoneException))]
103                         public void AdjustmentRulesOverlap ()
104                         {
105                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
106                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
107                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
108                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
109                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
110                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
111                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
112                         }
113                 
114                         [Test]
115                         [ExpectedException (typeof (InvalidTimeZoneException))]
116                         public void RulesNotOrdered ()
117                         {
118                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
119                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
120                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
121                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
122                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
123                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
124                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
125                         }
126                 
127                         [Test]
128                         [ExpectedException (typeof (InvalidTimeZoneException))]
129                         public void OffsetOutOfRange ()
130                         {
131                                 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
132                                 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
133                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
134                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
135                         }
136                 
137                         [Test]
138                         [ExpectedException (typeof (InvalidTimeZoneException))]
139                         public void NullRule ()
140                         {
141                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
142                         }
143                 
144                         [Test]
145                         [ExpectedException (typeof (InvalidTimeZoneException))]
146                         public void MultiplesRulesForDate ()
147                         {
148                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
149                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
150                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
151                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
152                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
153                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
154                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
155                         }
156
157                         [Test]
158                         public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
159                         {
160                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
161                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
162                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
163                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
164                                 Assert.IsTrue (tz.SupportsDaylightSavingTime);
165                         }
166
167                         [Test]
168                         public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
169                         {
170                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
171                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
172                         }
173
174                         [Test]
175                         public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
176                         {
177                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
178                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
179                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
180                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
181                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
182                         }
183
184                         [Test]
185                         public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
186                         {
187                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
188                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
189                         }
190                 }
191                 
192                 [TestFixture]
193                 public class IsDaylightSavingTimeTests
194                 {
195                         TimeZoneInfo london;
196                 
197                         [SetUp]
198                         public void CreateTimeZones ()
199                         {
200                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
201                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
202                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
203                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
204                         }
205                 
206                         [Test]
207                         public void NoDSTInUTC ()
208                         {
209                                 DateTime june01 = new DateTime (2007, 06, 01);
210                                 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
211                         }
212                 
213                         [Test]
214                         public void DSTInLondon ()
215                         {
216                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
217                                         return;
218                                 DateTime june01 = new DateTime (2007, 06, 01);
219                                 DateTime xmas = new DateTime (2007, 12, 25);
220                                 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
221                                 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
222                         }
223                 
224                         [Test]
225                         public void DSTTransisions ()
226                         {
227                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
228                                         return;
229                                 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
230                                 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
231                                 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
232                                 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
233                                 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
234                                 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
235                                 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
236                                 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
237                         }
238                 
239                         [Test]
240                         public void DSTTransisionsUTC ()
241                         {
242                                 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
243                                 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
244                                 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
245                                 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
246                                 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
247                                 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
248                                 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
249                                 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
250                         }
251                 
252                 #if SLOW_TESTS
253                         [Test]
254                         public void MatchTimeZoneBehavior ()
255                         {
256                                 TimeZone tzone = TimeZone.CurrentTimeZone;
257                                 TimeZoneInfo local = TimeZoneInfo.Local;
258                                 for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0, DateTimeKind.Local); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
259                                         date = DateTime.SpecifyKind (date, DateTimeKind.Local);
260                                         if (local.IsInvalidTime (date))
261                                                 continue;
262                                         Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
263                                 }
264                         }
265                 #endif
266                 }
267                 
268                 [TestFixture]
269                 public class ConvertTimeTests
270                 {
271                         TimeZoneInfo london;
272                 
273                         [SetUp]
274                         public void CreateTimeZones ()
275                         {
276                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
277                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
278                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
279                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
280                         }
281                 
282                         [Test]
283                         [ExpectedException (typeof (ArgumentException))]
284                         public void ConvertFromUtc_KindIsLocalException ()
285                         {
286                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
287                                         throw new ArgumentException ();
288                                 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);  
289                         }
290                 
291                         [Test]
292                         [ExpectedException (typeof (ArgumentNullException))]
293                         public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
294                         {
295                                 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);            
296                         }
297                 
298                         [Test]
299                         public void ConvertFromUtc_DestinationIsUTC ()
300                         {
301                                 DateTime now = DateTime.UtcNow;
302                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
303                                 Assert.AreEqual (now, converted);
304                         }
305                         
306                         [Test]
307                         public void ConvertFromUTC_ConvertInWinter ()
308                         {
309                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
310                                         return;
311                                 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
312                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
313                                 Assert.AreEqual (utc, converted);
314                         }
315                 
316                         [Test]
317                         public void ConvertFromUtc_ConvertInSummer ()
318                         {
319                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
320                                         return;
321                                 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
322                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
323                                 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
324                         }
325                 
326                         [Test]
327                         public void ConvertToUTC_KindIsUtc ()
328                         {
329                                 DateTime now = DateTime.UtcNow;
330                                 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
331                                 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
332                                 Assert.AreEqual (now, converted);
333                         }
334                 
335                         [Test]
336                         [ExpectedException (typeof (ArgumentException))]
337                         public void ConvertToUTC_KindIsUTCButSourceIsNot ()
338                         {
339                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
340                         }
341                 
342                         [Test]
343                         [ExpectedException (typeof (ArgumentException))]
344                         public void ConvertToUTC_KindIsLocalButSourceIsNot ()
345                         {
346                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
347                                         throw new ArgumentException ();
348                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);        
349                         }
350                 
351                         [Test]
352                         [ExpectedException (typeof (ArgumentException))]
353                         public void ConvertToUTC_InvalidDate ()
354                         {
355                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
356                         }
357                 
358                         [Test]
359                         [ExpectedException (typeof (ArgumentNullException))]
360                         public void ConvertToUTC_SourceIsNull ()
361                         {
362                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
363                         }
364                 
365                 #if SLOW_TESTS
366                         [Test]
367                         public void ConvertToUtc_MatchDateTimeBehavior ()
368                         {
369                                 for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
370                                         Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
371                                 }
372                         }
373                 #endif
374                 
375                         [Test]
376                         public void ConvertFromToUtc ()
377                         {
378                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
379                                         return;
380                                 DateTime utc = DateTime.UtcNow;
381                                 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
382                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
383                                 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
384                                 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
385                                 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
386                                 Assert.AreEqual (utc, back);
387                 
388                         }
389
390                         [Test]
391                         public void ConvertToTimeZone ()
392                         {
393                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
394                                         return;
395
396                                 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
397                         }
398                 }
399                 
400                 [TestFixture]
401                 public class IsInvalidTimeTests
402                 {
403                         TimeZoneInfo london;
404                 
405                         [SetUp]
406                         public void CreateTimeZones ()
407                         {
408                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
409                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
410                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
411                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
412                         }
413                 
414                 #if SLOW_TESTS
415                         [Test]
416                         public void UTCDate ()
417                         {
418                                 for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
419                                         date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
420                                         Assert.IsFalse (london.IsInvalidTime (date));
421                                 }
422                         }
423                 #endif
424                         [Test]
425                         public void InvalidDates ()
426                         {
427                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
428                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
429                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
430                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
431                         }
432                 }
433                 
434                 [TestFixture]
435                 public class IsAmbiguousTimeTests
436                 {
437                         TimeZoneInfo london;
438                 
439                         [SetUp]
440                         public void CreateTimeZones ()
441                         {
442                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
443                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
444                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
445                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
446                         }
447                 
448                         [Test]
449                         public void AmbiguousDates ()
450                         {
451                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
452                                         return;
453                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
454                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
455                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
456                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
457                         }
458                 
459                         [Test]
460                         public void AmbiguousUTCDates ()
461                         {
462                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
463                                         return;
464                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
465                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
466                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
467                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
468                         }
469                 
470                 #if SLOW_TESTS
471                         [Test]
472                         public void AmbiguousInUTC ()
473                         {
474                                 for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
475                                         Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
476                                 }
477                         }
478                 #endif
479                 }
480                 
481                 [TestFixture]
482                 public class GetSystemTimeZonesTests
483                 {
484                         [Test]
485                         public void NotEmpty ()
486                         {
487                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
488                                         return;
489                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
490                                 Assert.IsNotNull(systemTZ, "SystemTZ is null");
491                                 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
492                         }
493                 
494                         [Test]
495                         public void ContainsBrussels ()
496                         {
497                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
498                                         return;
499                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
500                                 foreach (TimeZoneInfo tz in systemTZ) {
501                                         if (tz.Id == "Europe/Brussels")
502                                                 return;
503                                 }
504                                 Assert.Fail ("Europe/Brussels not found in SystemTZ");
505                         }
506                 }
507                 
508                 [TestFixture]
509                 public class FindSystemTimeZoneByIdTests
510                 {
511                         [Test]
512                         [ExpectedException (typeof (ArgumentNullException))]
513                         public void NullId ()
514                         {
515                                 TimeZoneInfo.FindSystemTimeZoneById (null);
516                         }
517                 
518                         [Test]
519                         [ExpectedException (typeof (TimeZoneNotFoundException))]
520                         public void NonSystemTimezone ()
521                         {
522                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
523                                         throw new TimeZoneNotFoundException ();
524                                 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
525                         }
526                 
527                         [Test]
528                         public void FindBrusselsTZ ()
529                         {
530                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
531                                         return;
532                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
533                                 Assert.IsNotNull (brussels);
534                         }
535                 
536                         [Test]
537                         public void OffsetIsCorrectInKinshasa ()
538                         {
539                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
540                                         return;
541                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
542                                 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
543                         }
544                 
545                         [Test]
546                         public void OffsetIsCorrectInBrussels ()
547                         {
548                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
549                                         return;
550                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
551                                 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
552                         }
553                 
554                         [Test]
555                         public void NoDSTInKinshasa ()
556                         {
557                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
558                                         return;
559                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
560                                 Assert.IsFalse (kin.SupportsDaylightSavingTime);
561                         }
562                 
563                         [Test]
564                         public void BrusselsSupportsDST ()
565                         {
566                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
567                                         return;
568                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
569                                 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
570                         }
571                 
572                         [Test]
573                         public void MelbourneSupportsDST ()
574                         {
575                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
576                                         return;
577                                 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
578                                 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
579                         }
580                 
581                         [Test]
582                         public void RomeAndVaticanSharesTime ()
583                         {
584                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
585                                         return;
586                                 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
587                                 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
588                                 Assert.IsTrue (rome.HasSameRules (vatican));
589                         }
590
591                         [Test]
592                         public void FindSystemTimeZoneById_Local_Roundtrip ()
593                         {
594                                 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
595                         }
596
597                         [Test]
598                         public void Test326 ()
599                         {
600                                 DateTime utc = DateTime.UtcNow;
601                                 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
602                                 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
603                         }
604                 
605                 #if SLOW_TESTS
606                         [Test]
607                         public void BrusselsAdjustments ()
608                         {
609                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
610                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
611                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
612                                 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
613                 
614                                 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
615                 
616                                 for (DateTime date = new DateTime (2006, 01, 01, 0, 0, 0, DateTimeKind.Local); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,30,0)) {
617                                         Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
618                                         Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
619                                 }               
620                         }
621                 #endif
622                 }
623                 
624                 [TestFixture]
625                 public class GetAmbiguousTimeOffsetsTests
626                 {
627                         [Test]
628                         [ExpectedException (typeof(ArgumentException))]
629                         public void DateIsNotAmbiguous ()
630                         {
631                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
632                                         throw new ArgumentException ();
633                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
634                                 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
635                                 brussels.GetAmbiguousTimeOffsets (date);
636                         }
637                 
638                         [Test]
639                         public void AmbiguousOffsets ()
640                         {
641                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
642                                         return;
643                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
644                                 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
645                                 Assert.IsTrue (brussels.IsAmbiguousTime (date));
646                                 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
647                                 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
648                         }
649                 }
650
651                 [TestFixture]
652                 public class HasSameRulesTests
653                 {
654                         [Test]
655                         public void NullAdjustments () //bnc #391011
656                         {
657                                 TimeZoneInfo utc = TimeZoneInfo.Utc;
658                                 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
659                                 Assert.IsTrue (utc.HasSameRules (custom));
660                         }
661                 }
662
663                 [TestFixture]
664                 public class SerializationTests
665                 {
666                         [Test]
667                         public void Serialization_Deserialization ()
668                         {
669                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
670                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
671                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
672                                 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
673                                 MemoryStream stream = new MemoryStream ();
674                                 BinaryFormatter formatter = new BinaryFormatter ();
675                                 formatter.Serialize (stream, london);
676                                 stream.Position = 0;
677                                 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
678                                 stream.Close ();
679                                 stream.Dispose ();
680                                 Assert.AreEqual (london, deserialized);
681                         }
682                 }
683         }
684 }
685 #endif