[eglib] Prefer <langinfo.h> to <localcharset.h>
[mono.git] / mcs / class / corlib / 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 using System.Reflection;
34 using System.Globalization;
35
36 using NUnit.Framework;
37 namespace MonoTests.System
38 {
39         public class TimeZoneInfoTest
40         {
41                 [TestFixture]
42                 public class PropertiesTests
43                 {
44                         [Test]
45                         public void GetLocal ()
46                         {
47                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
48                                         Assert.Ignore ("Not running on Unix.");
49                                 TimeZoneInfo local = TimeZoneInfo.Local;
50                                 Assert.IsNotNull (local);
51                                 Assert.IsTrue (true);
52                         }
53                 }
54
55                 [TestFixture]
56                 public class CreateCustomTimezoneTests
57                 {
58                         [Test]
59                         [ExpectedException (typeof (ArgumentNullException))]
60                         public void IdIsNullException ()
61                         {
62                                 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null); 
63                         }
64                 
65                         [Test]
66                         [ExpectedException (typeof (ArgumentException))]
67                         public void IdIsEmptyString ()
68                         {
69                                 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);   
70                         }
71                 
72                         [Test]
73                         [ExpectedException (typeof (ArgumentException))]
74                         public void OffsetIsNotMinutes ()
75                         {
76                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);  
77                         }
78                 
79                         [Test]
80                         [ExpectedException (typeof (ArgumentOutOfRangeException))]
81                         public void OffsetTooBig ()
82                         {
83                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
84                         }
85                 
86                         [Test]
87                         [ExpectedException (typeof (ArgumentOutOfRangeException))]
88                         public void OffsetTooSmall ()
89                         {
90                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
91                         }
92                 
93                 #if STRICT
94                         [Test]
95                         [ExpectedException (typeof (ArgumentException))]
96                         public void IdLongerThan32 ()
97                         {
98                                 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);        
99                         }       
100                 #endif
101                 
102                         [Test]
103                         [ExpectedException (typeof (InvalidTimeZoneException))]
104                         public void AdjustmentRulesOverlap ()
105                         {
106                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
107                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
108                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
109                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
110                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
111                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
112                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
113                         }
114                 
115                         [Test]
116                         [ExpectedException (typeof (InvalidTimeZoneException))]
117                         public void RulesNotOrdered ()
118                         {
119                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
120                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
121                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
122                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
123                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
124                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
125                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
126                         }
127                 
128                         [Test]
129                         [ExpectedException (typeof (InvalidTimeZoneException))]
130                         public void OffsetOutOfRange ()
131                         {
132                                 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
133                                 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
134                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
135                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
136                         }
137                 
138                         [Test]
139                         [ExpectedException (typeof (InvalidTimeZoneException))]
140                         public void NullRule ()
141                         {
142                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
143                         }
144                 
145                         [Test]
146                         [ExpectedException (typeof (InvalidTimeZoneException))]
147                         public void MultiplesRulesForDate ()
148                         {
149                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
150                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
151                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
152                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
153                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
154                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
155                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
156                         }
157
158                         [Test]
159                         public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
160                         {
161                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
162                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
163                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
164                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
165                                 Assert.IsTrue (tz.SupportsDaylightSavingTime);
166                         }
167
168                         [Test]
169                         public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
170                         {
171                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
172                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
173                         }
174
175                         [Test]
176                         public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
177                         {
178                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
179                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
180                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
181                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
182                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
183                         }
184
185                         [Test]
186                         public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
187                         {
188                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
189                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
190                         }
191                 }
192                 
193                 [TestFixture]
194                 public class IsDaylightSavingTimeTests
195                 {
196                         TimeZoneInfo london;
197                 
198                         [SetUp]
199                         public void CreateTimeZones ()
200                         {
201                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
202                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
203                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
204                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
205                         }
206                 
207                         [Test]
208                         public void NoDSTInUTC ()
209                         {
210                                 DateTime june01 = new DateTime (2007, 06, 01);
211                                 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
212                         }
213                 
214                         [Test]
215                         public void DSTInLondon ()
216                         {
217                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
218                                         Assert.Ignore ("Not running on Unix.");
219                                 DateTime june01 = new DateTime (2007, 06, 01);
220                                 DateTime xmas = new DateTime (2007, 12, 25);
221                                 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
222                                 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
223                         }
224                 
225                         [Test]
226                         public void DSTTransisions ()
227                         {
228                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
229                                         Assert.Ignore ("Not running on Unix.");
230                                 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
231                                 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
232                                 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
233                                 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
234                                 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
235                                 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
236                                 Assert.IsFalse (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
237                                 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
238                         }
239                 
240                         [Test]
241                         public void DSTTransisionsUTC ()
242                         {
243                                 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
244                                 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
245                                 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
246                                 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
247                                 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
248                                 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
249                                 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
250                                 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
251                         }
252                 
253                 #if SLOW_TESTS
254                         [Test]
255                         public void MatchTimeZoneBehavior ()
256                         {
257                                 TimeZone tzone = TimeZone.CurrentTimeZone;
258                                 TimeZoneInfo local = TimeZoneInfo.Local;
259                                 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)) {
260                                         date = DateTime.SpecifyKind (date, DateTimeKind.Local);
261                                         if (local.IsInvalidTime (date))
262                                                 continue;
263                                         Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
264                                 }
265                         }
266                 #endif
267                         [Test (Description="Description xambug #17155")]
268                         public void AdjustmentRuleAfterNewYears ()
269                         {
270                                 TimeZoneInfo tz;
271                                 if (Environment.OSVersion.Platform == PlatformID.Unix)
272                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
273                                 else
274                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
275
276                                 // DST start: 9/29/2013 2:00:00 AM
277                                 // DST end: 4/6/2014 3:00:00 AM
278                                 DateTime dt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
279                                 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#1.1");
280
281                                 // DST start: 9/29/2014 2:00:00 AM
282                                 // DST end: 4/6/2015 3:00:00 AM
283                                 dt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
284                                 Assert.IsFalse (tz.IsDaylightSavingTime (dt), "#2.1");
285
286                                 // DST start: 9/29/2014 2:00:00 AM
287                                 // DST end: 4/6/2015 3:00:00 AM
288                                 dt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
289                                 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#3.1");
290                         }
291
292                         [Test] //Covers #26008
293                         public void DSTWithFloatingDateRule ()
294                         {
295                                 // Construct a custom time zone where daylight saving time starts on the
296                                 // 2nd Sunday in March.
297                                 var transitionToDaylight = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 2, DayOfWeek.Sunday);
298                                 var transitionToStandard = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 11, 1, DayOfWeek.Sunday);
299                                 var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1, 0, 0), transitionToDaylight, transitionToStandard);
300                                 var timeZone = TimeZoneInfo.CreateCustomTimeZone ("BugCheck", new TimeSpan (-8, 0, 0), "Testing", "Testing Standard", "Testing Daylight", new TimeZoneInfo.AdjustmentRule [] { adjustment });
301                                 // See if March 7, 2014 is listed as being during daylight saving time.
302                                 // If it is DST, then the runtime has the bug that we are looking for.
303                                 Assert.IsFalse (timeZone.IsDaylightSavingTime (new DateTime (2014, 3, 7, 12, 0, 0, DateTimeKind.Unspecified)));
304                         }
305
306                         [Test] //Covers #25050
307                         public void TestAthensDST ()
308                         {
309                                 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
310                                 var date = new DateTime (2014, 3, 30 , 2, 0, 0);
311                                 Assert.IsFalse (tzi.IsDaylightSavingTime (date));
312                                 Assert.AreEqual (new TimeSpan (2,0,0), tzi.GetUtcOffset (date));
313                         }
314                 }
315                 
316                 [TestFixture]
317                 public class ConvertTimeTests
318                 {
319                         TimeZoneInfo london;
320                 
321                         [SetUp]
322                         public void CreateTimeZones ()
323                         {
324                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
325                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
326                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
327                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
328                         }
329                 
330                         [Test]
331                         [ExpectedException (typeof (ArgumentException))]
332                         public void ConvertFromUtc_KindIsLocalException ()
333                         {
334                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
335                                         throw new ArgumentException ();
336                                 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);  
337                         }
338                 
339                         [Test]
340                         [ExpectedException (typeof (ArgumentNullException))]
341                         public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
342                         {
343                                 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);            
344                         }
345                 
346                         [Test]
347                         public void ConvertFromUtc_DestinationIsUTC ()
348                         {
349                                 DateTime now = DateTime.UtcNow;
350                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
351                                 Assert.AreEqual (now, converted);
352                         }
353                         
354                         [Test]
355                         public void ConvertFromUTC_ConvertInWinter ()
356                         {
357                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
358                                         Assert.Ignore ("Not running on Unix.");
359                                 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
360                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
361                                 Assert.AreEqual (utc, converted);
362                         }
363                 
364                         [Test]
365                         public void ConvertFromUtc_ConvertInSummer ()
366                         {
367                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
368                                         Assert.Ignore ("Not running on Unix.");
369                                 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
370                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
371                                 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
372                         }
373                 
374                         [Test]
375                         public void ConvertToUTC_KindIsUtc ()
376                         {
377                                 DateTime now = DateTime.UtcNow;
378                                 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
379                                 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
380                                 Assert.AreEqual (now, converted);
381                         }
382                 
383                         [Test]
384                         [ExpectedException (typeof (ArgumentException))]
385                         public void ConvertToUTC_KindIsUTCButSourceIsNot ()
386                         {
387                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
388                         }
389                 
390                         [Test]
391                         [ExpectedException (typeof (ArgumentException))]
392                         public void ConvertToUTC_KindIsLocalButSourceIsNot ()
393                         {
394                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
395                                         throw new ArgumentException ();
396                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);        
397                         }
398                 
399                         [Test]
400                         [ExpectedException (typeof (ArgumentException))]
401                         public void ConvertToUTC_InvalidDate ()
402                         {
403                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
404                         }
405                 
406                         [Test]
407                         [ExpectedException (typeof (ArgumentNullException))]
408                         public void ConvertToUTC_SourceIsNull ()
409                         {
410                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
411                         }
412                 
413                 #if SLOW_TESTS
414                         [Test]
415                         public void ConvertToUtc_MatchDateTimeBehavior ()
416                         {
417                                 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)) {
418                                         Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
419                                 }
420                         }
421                 #endif
422                 
423                         [Test]
424                         public void ConvertFromToUtc ()
425                         {
426                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
427                                         Assert.Ignore ("Not running on Unix.");
428                                 DateTime utc = DateTime.UtcNow;
429                                 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
430                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
431                                 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
432                                 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
433                                 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
434                                 Assert.AreEqual (utc, back);
435                 
436                         }
437
438                         [Test]
439                         public void ConvertTimeToUtc_Overflow ()
440                         {
441                                 var res = TimeZoneInfo.ConvertTimeToUtc (new DateTime (0));
442                                 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#1");
443
444                                 res = TimeZoneInfo.ConvertTimeToUtc (DateTime.MaxValue);
445                                 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#2");
446                         }
447
448                         [Test]
449                         public void ConvertFromToLocal ()
450                         {
451                                 DateTime utc = DateTime.UtcNow;
452                                 Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
453                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
454                         #if NET_4_0
455                                 Assert.AreEqual(DateTimeKind.Local, converted.Kind);
456                         #else
457                                 Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
458                         #endif
459                                 DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
460                                 Assert.AreEqual(back.Kind, DateTimeKind.Utc);
461                                 Assert.AreEqual(utc, back);
462                         }
463
464                         [Test]
465                         public void ConvertToTimeZone ()
466                         {
467                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
468                                         Assert.Ignore ("Not running on Unix.");
469
470                                 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
471                         }
472
473                         [Test]
474                         [ExpectedException (typeof (ArgumentNullException))]
475                         public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
476                         {
477                                 TimeZoneInfo.ConvertTime (DateTime.Now, null);
478                         }
479
480                         [Test]
481                         public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
482                         {
483                                 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
484                                 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
485                                 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
486                                 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
487                                 
488                                 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
489                                 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
490                                 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
491                                 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
492
493                                 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
494                                 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
495                                 Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
496                                 Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
497                         }
498
499                         [Test]
500                         [ExpectedException (typeof (ArgumentNullException))]
501                         public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
502                         {
503                                 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
504                         }
505
506                         [Test]
507                         [ExpectedException (typeof (ArgumentNullException))]
508                         public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
509                         {
510                                 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
511                         }
512
513                         [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
514                         public void ConvertTime_AdjustmentRuleAfterNewYears ()
515                         {
516                                 TimeZoneInfo tz;
517                                 if (Environment.OSVersion.Platform == PlatformID.Unix)
518                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
519                                 else
520                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
521
522                                 // DST start: 9/29/2013 2:00:00 AM
523                                 // DST end: 4/6/2014 3:00:00 AM
524                                 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
525                                 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
526                                 Assert.AreEqual (10, ddt.Day, "#1.1");
527                                 Assert.AreEqual (1, ddt.Month, "#1.2");
528                                 Assert.AreEqual (2014, ddt.Year, "#1.3");
529                                 Assert.AreEqual (12, ddt.Hour, "#1.4");
530                                 Assert.AreEqual (0, ddt.Minute, "#1.5");
531                                 Assert.AreEqual (0, ddt.Second, "#1.6");
532                                 
533                                 // DST start: 9/29/2014 2:00:00 AM
534                                 // DST end: 4/6/2015 3:00:00 AM
535                                 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
536                                 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
537                                 Assert.AreEqual (10, ddt.Day, "#2.1");
538                                 Assert.AreEqual (6, ddt.Month, "#2.2");
539                                 Assert.AreEqual (2014, ddt.Year, "#2.3");
540                                 Assert.AreEqual (11, ddt.Hour, "#2.4");
541                                 Assert.AreEqual (0, ddt.Minute, "#2.5");
542                                 Assert.AreEqual (0, ddt.Second, "#2.6");
543                                 
544                                 // DST start: 9/29/2014 2:00:00 AM
545                                 // DST end: 4/6/2015 3:00:00 AM
546                                 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
547                                 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
548                                 Assert.AreEqual (10, ddt.Day, "#3.1");
549                                 Assert.AreEqual (10, ddt.Month, "#3.2");
550                                 Assert.AreEqual (2014, ddt.Year, "#3.3");
551                                 Assert.AreEqual (12, ddt.Hour, "#3.4");
552                                 Assert.AreEqual (0, ddt.Minute, "#3.5");
553                                 Assert.AreEqual (0, ddt.Second, "#3.6");
554                         }
555
556                         [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
557                         public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
558                                 
559                                 TimeZoneInfo easternTimeZone;
560                                 TimeZoneInfo pacificTimeZone;
561
562                                 if (Environment.OSVersion.Platform == PlatformID.Unix) {
563                                         // *nix
564                                         easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
565                                         pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");   
566                                 }
567                                 else {
568                                         // Windows
569                                         easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
570                                         pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
571                                 }
572
573                                 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
574                                 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
575                                 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
576                         
577                                 // Last midnight in PST as EST should be 3AM
578                                 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
579
580                                 Assert.AreEqual (expectedDate, lastMidnightAsEST);
581                                 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
582                         }
583                 }
584                 
585                 [TestFixture]
586                 public class IsInvalidTimeTests
587                 {
588                         TimeZoneInfo london;
589                 
590                         [SetUp]
591                         public void CreateTimeZones ()
592                         {
593                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
594                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
595                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
596                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
597                         }
598                 
599                 #if SLOW_TESTS
600                         [Test]
601                         public void UTCDate ()
602                         {
603                                 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)) {
604                                         date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
605                                         Assert.IsFalse (london.IsInvalidTime (date));
606                                 }
607                         }
608                 #endif
609                         [Test]
610                         public void InvalidDates ()
611                         {
612                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
613                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
614                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
615                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
616                         }
617                 }
618                 
619                 [TestFixture]
620                 public class IsAmbiguousTimeTests
621                 {
622                         TimeZoneInfo london;
623                 
624                         [SetUp]
625                         public void CreateTimeZones ()
626                         {
627                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
628                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
629                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
630                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
631                         }
632                 
633                         [Test]
634                         public void AmbiguousDates ()
635                         {
636                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
637                                         Assert.Ignore ("Not running on Unix.");
638                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
639                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
640                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
641                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
642                         }
643                 
644                         [Test]
645                         public void AmbiguousUTCDates ()
646                         {
647                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
648                                         Assert.Ignore ("Not running on Unix.");
649                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
650                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
651                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
652                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
653                         }
654                 
655                 #if SLOW_TESTS
656                         [Test]
657                         public void AmbiguousInUTC ()
658                         {
659                                 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)) {
660                                         Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
661                                 }
662                         }
663                 #endif
664                 }
665                 
666                 [TestFixture]
667                 public class GetSystemTimeZonesTests
668                 {
669                         [Test]
670                         public void Identity ()
671                         {
672                                 Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
673                         }
674
675                         [Test]
676                         public void NotEmpty ()
677                         {
678                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
679                                         Assert.Ignore ("Not running on Unix.");
680                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
681                                 Assert.IsNotNull(systemTZ, "SystemTZ is null");
682                                 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
683                         }
684                 
685                         [Test]
686                         public void ContainsBrussels ()
687                         {
688                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
689                                         Assert.Ignore ("Not running on Unix.");
690                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
691                                 foreach (TimeZoneInfo tz in systemTZ) {
692                                         if (tz.Id == "Europe/Brussels")
693                                                 return;
694                                 }
695                                 Assert.Fail ("Europe/Brussels not found in SystemTZ");
696                         }
697                 }
698                 
699                 [TestFixture]
700                 public class FindSystemTimeZoneByIdTests
701                 {
702                         [Test]
703                         [ExpectedException (typeof (ArgumentNullException))]
704                         public void NullId ()
705                         {
706                                 TimeZoneInfo.FindSystemTimeZoneById (null);
707                         }
708                 
709                         [Test]
710                         [ExpectedException (typeof (TimeZoneNotFoundException))]
711                         public void NonSystemTimezone ()
712                         {
713                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
714                                         throw new TimeZoneNotFoundException ();
715                                 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
716                         }
717                 
718                         [Test]
719                         public void FindBrusselsTZ ()
720                         {
721                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
722                                         Assert.Ignore ("Not running on Unix.");
723                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
724                                 Assert.IsNotNull (brussels);
725                         }
726                 
727                         [Test]
728                         public void OffsetIsCorrectInKinshasa ()
729                         {
730                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
731                                         Assert.Ignore ("Not running on Unix.");
732                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
733                                 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
734                         }
735                 
736                         [Test]
737                         public void OffsetIsCorrectInBrussels ()
738                         {
739                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
740                                         Assert.Ignore ("Not running on Unix.");
741                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
742                                 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
743                         }
744                 
745                         [Test]
746                         public void NoDSTInKinshasa ()
747                         {
748                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
749                                         Assert.Ignore ("Not running on Unix.");
750                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
751                                 Assert.IsFalse (kin.SupportsDaylightSavingTime);
752                         }
753                 
754                         [Test]
755                         public void BrusselsSupportsDST ()
756                         {
757                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
758                                         Assert.Ignore ("Not running on Unix.");
759                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
760                                 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
761                         }
762                 
763                         [Test]
764                         public void MelbourneSupportsDST ()
765                         {
766                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
767                                         Assert.Ignore ("Not running on Unix.");
768                                 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
769                                 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
770                         }
771                 
772                         [Test]
773                         public void RomeAndVaticanSharesTime ()
774                         {
775                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
776                                         Assert.Ignore ("Not running on Unix.");
777                                 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
778                                 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
779                                 Assert.IsTrue (rome.HasSameRules (vatican));
780                         }
781
782                         [Test]
783                         public void FindSystemTimeZoneById_Local_Roundtrip ()
784                         {
785                                 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
786                         }
787
788                         [Test]
789                         public void Test326 ()
790                         {
791                                 DateTime utc = DateTime.UtcNow;
792                                 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
793                                 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
794                         }
795                 
796                 #if SLOW_TESTS
797                         [Test]
798                         public void BrusselsAdjustments ()
799                         {
800                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
801                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
802                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
803                                 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
804                 
805                                 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
806                 
807                                 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)) {
808                                         Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
809                                         Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
810                                 }               
811                         }
812                 #endif
813
814                         [Test]
815                         public void SubminuteDSTOffsets ()
816                         {
817                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
818                                         Assert.Ignore ();
819
820                                 var subMinuteDSTs = new string [] {
821                                         "Europe/Dublin", // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
822                                         "Europe/Amsterdam",
823                                         "America/St_Johns",
824                                         "Canada/Newfoundland",
825                                         "Europe/Moscow",
826                                         "Europe/Riga",
827                                         "N/A", // testing that the test doesn't fail with inexistent TZs
828                                 };
829                                 foreach (var tz in subMinuteDSTs) {
830                                         try {
831                                                 TimeZoneInfo.FindSystemTimeZoneById (tz);
832                                         } catch (TimeZoneNotFoundException) {
833                                                 // ok;
834                                         } catch (Exception ex) {
835                                                 Assert.Fail (string.Format ("Failed to load TZ {0}: {1}", tz, ex.ToString ()));
836                                         }
837                                 }
838                         }
839                 }
840                 
841                 [TestFixture]
842                 public class GetAmbiguousTimeOffsetsTests
843                 {
844                         [Test]
845                         [ExpectedException (typeof(ArgumentException))]
846                         public void DateIsNotAmbiguous ()
847                         {
848                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
849                                         throw new ArgumentException ();
850                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
851                                 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
852                                 brussels.GetAmbiguousTimeOffsets (date);
853                         }
854                 
855                         [Test]
856                         public void AmbiguousOffsets ()
857                         {
858                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
859                                         Assert.Ignore ("Not running on Unix.");
860                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
861                                 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
862                                 Assert.IsTrue (brussels.IsAmbiguousTime (date));
863                                 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
864                                 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
865                         }
866                 }
867
868                 [TestFixture]
869                 public class HasSameRulesTests
870                 {
871                         [Test]
872                         public void NullAdjustments () //bnc #391011
873                         {
874                                 TimeZoneInfo utc = TimeZoneInfo.Utc;
875                                 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
876                                 Assert.IsTrue (utc.HasSameRules (custom));
877                         }
878                 }
879
880                 [TestFixture]
881                 public class SerializationTests
882                 {
883                         [Test]
884                         public void Serialization_Deserialization ()
885                         {
886                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
887                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
888                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
889                                 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
890                                 MemoryStream stream = new MemoryStream ();
891                                 BinaryFormatter formatter = new BinaryFormatter ();
892                                 formatter.Serialize (stream, london);
893                                 stream.Position = 0;
894                                 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
895                                 stream.Close ();
896                                 stream.Dispose ();
897                                 Assert.IsTrue (london.Equals (deserialized));
898                         }
899                 }
900
901                 [TestFixture]
902                 public class MultipleDaylightSavingTimeTests {
903                         private TimeZoneInfo cairo;
904                         private DateTime dst1Start;
905                         private DateTime dst1End;
906                         private DateTime dst2Start;
907                         private DateTime dst2End;
908
909                         private TimeSpan baseUtcOffset;
910                         private TimeSpan dstUtcOffset;
911                         private TimeSpan dstOffset;
912
913                         [SetUp]
914                         public void CreateTimeZones ()
915                         {
916                                 /*
917                                 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
918                                         Delta: 01:00:00
919                                         Begins at 12:00 AM on 16 May
920                                         Ends at 1:00 AM on 29 June
921                                 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
922                                         Delta: 01:00:00
923                                         Begins at 12:00 AM on 29 July
924                                         Ends at 12:00 AM on 26 September
925                                 */
926                                 dst1Start = new DateTime (2014, 5, 16);
927                                 dst1End = new DateTime (2014, 6, 29);
928                                 dst2Start = new DateTime (2014, 7, 29);
929                                 dst2End = new DateTime (2014, 9, 26);
930
931                                 baseUtcOffset = new TimeSpan (2, 0, 0);
932                                 dstUtcOffset = new TimeSpan (3, 0, 0);
933                                 dstOffset = dstUtcOffset - baseUtcOffset;
934
935                                 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
936                                         new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
937                                         CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
938
939                                 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
940                                         new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
941                                         CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
942
943                                 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
944                                         new [] {rule1, rule2});
945                         }
946
947                         private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
948                         {
949                                 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
950                                 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
951                         }
952
953                         [Test]
954                         public void GetUtcOffset_FromUTC ()
955                         {
956                                 var d = dst1Start.Add (-baseUtcOffset);
957                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
958                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
959                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
960                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
961
962                                 d = dst1End.Add (-baseUtcOffset-dstOffset);
963                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
964                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
965                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
966                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
967
968                                 d = dst2Start.Add (-baseUtcOffset);
969                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
970                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
971                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
972                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
973
974                                 d = dst2End.Add (-baseUtcOffset-dstOffset);
975                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
976                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
977                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
978                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
979                         }
980
981                         [Test]
982                         public void GetUtcOffset_FromLocal ()
983                         {
984                                 var d = dst1Start.Add (-baseUtcOffset);
985                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
986                                 d = d.ToLocalTime ();
987                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
988                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
989                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
990
991                                 d = dst1End.Add (-baseUtcOffset-dstOffset);
992                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
993                                 d = d.ToLocalTime ();
994                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
995                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
996                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
997
998                                 d = dst2Start.Add (-baseUtcOffset);
999                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1000                                 d = d.ToLocalTime ();
1001                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1002                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1003                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1004
1005                                 d = dst2End.Add (-baseUtcOffset-dstOffset);
1006                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1007                                 d = d.ToLocalTime ();
1008                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1009                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1010                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1011                         }
1012
1013                         [Test]
1014                         public void GetUtcOffset_FromUnspecified ()
1015                         {
1016                                 var d = dst1Start.Add (dstOffset);
1017                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1018                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1019                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1020
1021                                 d = dst1End.Add (-dstOffset);
1022                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1023                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1024                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1025
1026                                 d = dst2Start.Add (dstOffset);
1027                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1028                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1029                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1030
1031                                 d = dst2End.Add (-dstOffset);
1032                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1033                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1034                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1035                         }
1036                 }
1037
1038                 [TestFixture]
1039                 public class GetDaylightChanges
1040                 {
1041                         MethodInfo getChanges;
1042
1043                         [SetUp]
1044                         public void Setup ()
1045                         {
1046                                 var flags = BindingFlags.Instance | BindingFlags.NonPublic;
1047                                 getChanges = typeof (TimeZoneInfo).GetMethod ("GetDaylightChanges", flags);
1048                         }
1049
1050                         [Test]
1051                         public void TestSydneyDaylightChanges ()
1052                         {
1053                                 TimeZoneInfo tz;
1054                                 if (Environment.OSVersion.Platform == PlatformID.Unix)
1055                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Sydney");
1056                                 else
1057                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("W. Australia Standard Time");
1058
1059                                 var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
1060
1061                                 Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
1062                                 Assert.AreEqual (new DateTime (2014, 10, 5, 2, 0, 0), changes.Start);
1063                                 Assert.AreEqual (new DateTime (2014, 4, 6, 3, 0, 0), changes.End);
1064                         }
1065                 }
1066         }
1067 }