Merge pull request #1870 from saper/langinfo_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                                 Assert.AreEqual(DateTimeKind.Local, converted.Kind);
455                                 DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
456                                 Assert.AreEqual(back.Kind, DateTimeKind.Utc);
457                                 Assert.AreEqual(utc, back);
458                         }
459
460                         [Test]
461                         public void ConvertToTimeZone ()
462                         {
463                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
464                                         Assert.Ignore ("Not running on Unix.");
465
466                                 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
467                         }
468
469                         [Test]
470                         [ExpectedException (typeof (ArgumentNullException))]
471                         public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
472                         {
473                                 TimeZoneInfo.ConvertTime (DateTime.Now, null);
474                         }
475
476                         [Test]
477                         public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
478                         {
479                                 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
480                                 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
481                                 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
482                                 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
483                                 
484                                 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
485                                 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
486                                 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
487                                 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
488
489                                 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
490                                 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
491                                 Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
492                                 Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
493                         }
494
495                         [Test]
496                         [ExpectedException (typeof (ArgumentNullException))]
497                         public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
498                         {
499                                 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
500                         }
501
502                         [Test]
503                         [ExpectedException (typeof (ArgumentNullException))]
504                         public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
505                         {
506                                 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
507                         }
508
509                         [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
510                         public void ConvertTime_AdjustmentRuleAfterNewYears ()
511                         {
512                                 TimeZoneInfo tz;
513                                 if (Environment.OSVersion.Platform == PlatformID.Unix)
514                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
515                                 else
516                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
517
518                                 // DST start: 9/29/2013 2:00:00 AM
519                                 // DST end: 4/6/2014 3:00:00 AM
520                                 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
521                                 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
522                                 Assert.AreEqual (10, ddt.Day, "#1.1");
523                                 Assert.AreEqual (1, ddt.Month, "#1.2");
524                                 Assert.AreEqual (2014, ddt.Year, "#1.3");
525                                 Assert.AreEqual (12, ddt.Hour, "#1.4");
526                                 Assert.AreEqual (0, ddt.Minute, "#1.5");
527                                 Assert.AreEqual (0, ddt.Second, "#1.6");
528                                 
529                                 // DST start: 9/29/2014 2:00:00 AM
530                                 // DST end: 4/6/2015 3:00:00 AM
531                                 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
532                                 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
533                                 Assert.AreEqual (10, ddt.Day, "#2.1");
534                                 Assert.AreEqual (6, ddt.Month, "#2.2");
535                                 Assert.AreEqual (2014, ddt.Year, "#2.3");
536                                 Assert.AreEqual (11, ddt.Hour, "#2.4");
537                                 Assert.AreEqual (0, ddt.Minute, "#2.5");
538                                 Assert.AreEqual (0, ddt.Second, "#2.6");
539                                 
540                                 // DST start: 9/29/2014 2:00:00 AM
541                                 // DST end: 4/6/2015 3:00:00 AM
542                                 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
543                                 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
544                                 Assert.AreEqual (10, ddt.Day, "#3.1");
545                                 Assert.AreEqual (10, ddt.Month, "#3.2");
546                                 Assert.AreEqual (2014, ddt.Year, "#3.3");
547                                 Assert.AreEqual (12, ddt.Hour, "#3.4");
548                                 Assert.AreEqual (0, ddt.Minute, "#3.5");
549                                 Assert.AreEqual (0, ddt.Second, "#3.6");
550                         }
551
552                         [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
553                         public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
554                                 
555                                 TimeZoneInfo easternTimeZone;
556                                 TimeZoneInfo pacificTimeZone;
557
558                                 if (Environment.OSVersion.Platform == PlatformID.Unix) {
559                                         // *nix
560                                         easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
561                                         pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");   
562                                 }
563                                 else {
564                                         // Windows
565                                         easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
566                                         pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
567                                 }
568
569                                 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
570                                 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
571                                 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
572                         
573                                 // Last midnight in PST as EST should be 3AM
574                                 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
575
576                                 Assert.AreEqual (expectedDate, lastMidnightAsEST);
577                                 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
578                         }
579                 }
580                 
581                 [TestFixture]
582                 public class IsInvalidTimeTests
583                 {
584                         TimeZoneInfo london;
585                 
586                         [SetUp]
587                         public void CreateTimeZones ()
588                         {
589                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
590                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
591                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
592                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
593                         }
594                 
595                 #if SLOW_TESTS
596                         [Test]
597                         public void UTCDate ()
598                         {
599                                 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)) {
600                                         date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
601                                         Assert.IsFalse (london.IsInvalidTime (date));
602                                 }
603                         }
604                 #endif
605                         [Test]
606                         public void InvalidDates ()
607                         {
608                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
609                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
610                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
611                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
612                         }
613                 }
614                 
615                 [TestFixture]
616                 public class IsAmbiguousTimeTests
617                 {
618                         TimeZoneInfo london;
619                 
620                         [SetUp]
621                         public void CreateTimeZones ()
622                         {
623                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
624                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
625                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
626                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
627                         }
628                 
629                         [Test]
630                         public void AmbiguousDates ()
631                         {
632                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
633                                         Assert.Ignore ("Not running on Unix.");
634                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
635                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
636                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
637                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
638                         }
639                 
640                         [Test]
641                         public void AmbiguousUTCDates ()
642                         {
643                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
644                                         Assert.Ignore ("Not running on Unix.");
645                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
646                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
647                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
648                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
649                         }
650                 
651                 #if SLOW_TESTS
652                         [Test]
653                         public void AmbiguousInUTC ()
654                         {
655                                 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)) {
656                                         Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
657                                 }
658                         }
659                 #endif
660                 }
661                 
662                 [TestFixture]
663                 public class GetSystemTimeZonesTests
664                 {
665                         [Test]
666                         public void Identity ()
667                         {
668                                 Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
669                         }
670
671                         [Test]
672                         public void NotEmpty ()
673                         {
674                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
675                                         Assert.Ignore ("Not running on Unix.");
676                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
677                                 Assert.IsNotNull(systemTZ, "SystemTZ is null");
678                                 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
679                         }
680                 
681                         [Test]
682                         public void ContainsBrussels ()
683                         {
684                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
685                                         Assert.Ignore ("Not running on Unix.");
686                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
687                                 foreach (TimeZoneInfo tz in systemTZ) {
688                                         if (tz.Id == "Europe/Brussels")
689                                                 return;
690                                 }
691                                 Assert.Fail ("Europe/Brussels not found in SystemTZ");
692                         }
693                 }
694                 
695                 [TestFixture]
696                 public class FindSystemTimeZoneByIdTests
697                 {
698                         [Test]
699                         [ExpectedException (typeof (ArgumentNullException))]
700                         public void NullId ()
701                         {
702                                 TimeZoneInfo.FindSystemTimeZoneById (null);
703                         }
704                 
705                         [Test]
706                         [ExpectedException (typeof (TimeZoneNotFoundException))]
707                         public void NonSystemTimezone ()
708                         {
709                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
710                                         throw new TimeZoneNotFoundException ();
711                                 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
712                         }
713                 
714                         [Test]
715                         public void FindBrusselsTZ ()
716                         {
717                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
718                                         Assert.Ignore ("Not running on Unix.");
719                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
720                                 Assert.IsNotNull (brussels);
721                         }
722                 
723                         [Test]
724                         public void OffsetIsCorrectInKinshasa ()
725                         {
726                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
727                                         Assert.Ignore ("Not running on Unix.");
728                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
729                                 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
730                         }
731                 
732                         [Test]
733                         public void OffsetIsCorrectInBrussels ()
734                         {
735                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
736                                         Assert.Ignore ("Not running on Unix.");
737                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
738                                 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
739                         }
740                 
741                         [Test]
742                         public void NoDSTInKinshasa ()
743                         {
744                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
745                                         Assert.Ignore ("Not running on Unix.");
746                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
747                                 Assert.IsFalse (kin.SupportsDaylightSavingTime);
748                         }
749                 
750                         [Test]
751                         public void BrusselsSupportsDST ()
752                         {
753                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
754                                         Assert.Ignore ("Not running on Unix.");
755                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
756                                 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
757                         }
758                 
759                         [Test]
760                         public void MelbourneSupportsDST ()
761                         {
762                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
763                                         Assert.Ignore ("Not running on Unix.");
764                                 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
765                                 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
766                         }
767                 
768                         [Test]
769                         public void RomeAndVaticanSharesTime ()
770                         {
771                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
772                                         Assert.Ignore ("Not running on Unix.");
773                                 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
774                                 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
775                                 Assert.IsTrue (rome.HasSameRules (vatican));
776                         }
777
778                         [Test]
779                         public void FindSystemTimeZoneById_Local_Roundtrip ()
780                         {
781                                 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
782                         }
783
784                         [Test]
785                         public void Test326 ()
786                         {
787                                 DateTime utc = DateTime.UtcNow;
788                                 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
789                                 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
790                         }
791                 
792                 #if SLOW_TESTS
793                         [Test]
794                         public void BrusselsAdjustments ()
795                         {
796                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
797                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
798                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
799                                 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
800                 
801                                 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
802                 
803                                 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)) {
804                                         Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
805                                         Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
806                                 }               
807                         }
808                 #endif
809
810                         [Test]
811                         public void SubminuteDSTOffsets ()
812                         {
813                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
814                                         Assert.Ignore ();
815
816                                 var subMinuteDSTs = new string [] {
817                                         "Europe/Dublin", // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
818                                         "Europe/Amsterdam",
819                                         "America/St_Johns",
820                                         "Canada/Newfoundland",
821                                         "Europe/Moscow",
822                                         "Europe/Riga",
823                                         "N/A", // testing that the test doesn't fail with inexistent TZs
824                                 };
825                                 foreach (var tz in subMinuteDSTs) {
826                                         try {
827                                                 TimeZoneInfo.FindSystemTimeZoneById (tz);
828                                         } catch (TimeZoneNotFoundException) {
829                                                 // ok;
830                                         } catch (Exception ex) {
831                                                 Assert.Fail (string.Format ("Failed to load TZ {0}: {1}", tz, ex.ToString ()));
832                                         }
833                                 }
834                         }
835                 }
836                 
837                 [TestFixture]
838                 public class GetAmbiguousTimeOffsetsTests
839                 {
840                         [Test]
841                         [ExpectedException (typeof(ArgumentException))]
842                         public void DateIsNotAmbiguous ()
843                         {
844                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
845                                         throw new ArgumentException ();
846                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
847                                 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
848                                 brussels.GetAmbiguousTimeOffsets (date);
849                         }
850                 
851                         [Test]
852                         public void AmbiguousOffsets ()
853                         {
854                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
855                                         Assert.Ignore ("Not running on Unix.");
856                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
857                                 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
858                                 Assert.IsTrue (brussels.IsAmbiguousTime (date));
859                                 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
860                                 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
861                         }
862                 }
863
864                 [TestFixture]
865                 public class HasSameRulesTests
866                 {
867                         [Test]
868                         public void NullAdjustments () //bnc #391011
869                         {
870                                 TimeZoneInfo utc = TimeZoneInfo.Utc;
871                                 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
872                                 Assert.IsTrue (utc.HasSameRules (custom));
873                         }
874                 }
875
876                 [TestFixture]
877                 public class SerializationTests
878                 {
879                         [Test]
880                         public void Serialization_Deserialization ()
881                         {
882                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
883                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
884                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
885                                 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
886                                 MemoryStream stream = new MemoryStream ();
887                                 BinaryFormatter formatter = new BinaryFormatter ();
888                                 formatter.Serialize (stream, london);
889                                 stream.Position = 0;
890                                 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
891                                 stream.Close ();
892                                 stream.Dispose ();
893                                 Assert.IsTrue (london.Equals (deserialized));
894                         }
895                 }
896
897                 [TestFixture]
898                 public class MultipleDaylightSavingTimeTests {
899                         private TimeZoneInfo cairo;
900                         private DateTime dst1Start;
901                         private DateTime dst1End;
902                         private DateTime dst2Start;
903                         private DateTime dst2End;
904
905                         private TimeSpan baseUtcOffset;
906                         private TimeSpan dstUtcOffset;
907                         private TimeSpan dstOffset;
908
909                         [SetUp]
910                         public void CreateTimeZones ()
911                         {
912                                 /*
913                                 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
914                                         Delta: 01:00:00
915                                         Begins at 12:00 AM on 16 May
916                                         Ends at 1:00 AM on 29 June
917                                 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
918                                         Delta: 01:00:00
919                                         Begins at 12:00 AM on 29 July
920                                         Ends at 12:00 AM on 26 September
921                                 */
922                                 dst1Start = new DateTime (2014, 5, 16);
923                                 dst1End = new DateTime (2014, 6, 29);
924                                 dst2Start = new DateTime (2014, 7, 29);
925                                 dst2End = new DateTime (2014, 9, 26);
926
927                                 baseUtcOffset = new TimeSpan (2, 0, 0);
928                                 dstUtcOffset = new TimeSpan (3, 0, 0);
929                                 dstOffset = dstUtcOffset - baseUtcOffset;
930
931                                 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
932                                         new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
933                                         CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
934
935                                 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
936                                         new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
937                                         CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
938
939                                 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
940                                         new [] {rule1, rule2});
941                         }
942
943                         private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
944                         {
945                                 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
946                                 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
947                         }
948
949                         [Test]
950                         public void GetUtcOffset_FromUTC ()
951                         {
952                                 var d = dst1Start.Add (-baseUtcOffset);
953                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
954                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
955                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
956                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
957
958                                 d = dst1End.Add (-baseUtcOffset-dstOffset);
959                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
960                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
961                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
962                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
963
964                                 d = dst2Start.Add (-baseUtcOffset);
965                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
966                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
967                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
968                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
969
970                                 d = dst2End.Add (-baseUtcOffset-dstOffset);
971                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
972                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
973                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
974                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
975                         }
976
977                         [Test]
978                         public void GetUtcOffset_FromLocal ()
979                         {
980                                 var d = dst1Start.Add (-baseUtcOffset);
981                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
982                                 d = d.ToLocalTime ();
983                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
984                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
985                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
986
987                                 d = dst1End.Add (-baseUtcOffset-dstOffset);
988                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
989                                 d = d.ToLocalTime ();
990                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
991                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
992                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
993
994                                 d = dst2Start.Add (-baseUtcOffset);
995                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
996                                 d = d.ToLocalTime ();
997                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
998                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
999                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1000
1001                                 d = dst2End.Add (-baseUtcOffset-dstOffset);
1002                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1003                                 d = d.ToLocalTime ();
1004                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1005                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1006                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1007                         }
1008
1009                         [Test]
1010                         public void GetUtcOffset_FromUnspecified ()
1011                         {
1012                                 var d = dst1Start.Add (dstOffset);
1013                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1014                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1015                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1016
1017                                 d = dst1End.Add (-dstOffset);
1018                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1019                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1020                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1021
1022                                 d = dst2Start.Add (dstOffset);
1023                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1024                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1025                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1026
1027                                 d = dst2End.Add (-dstOffset);
1028                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1029                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1030                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1031                         }
1032                 }
1033
1034                 [TestFixture]
1035                 public class GetDaylightChanges
1036                 {
1037                         MethodInfo getChanges;
1038
1039                         [SetUp]
1040                         public void Setup ()
1041                         {
1042                                 var flags = BindingFlags.Instance | BindingFlags.NonPublic;
1043                                 getChanges = typeof (TimeZoneInfo).GetMethod ("GetDaylightChanges", flags);
1044                         }
1045
1046                         [Test]
1047                         public void TestSydneyDaylightChanges ()
1048                         {
1049                                 TimeZoneInfo tz;
1050                                 if (Environment.OSVersion.Platform == PlatformID.Unix)
1051                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Sydney");
1052                                 else
1053                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("W. Australia Standard Time");
1054
1055                                 var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
1056
1057                                 Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
1058                                 Assert.AreEqual (new DateTime (2014, 10, 5, 2, 0, 0), changes.Start);
1059                                 Assert.AreEqual (new DateTime (2014, 4, 6, 3, 0, 0), changes.End);
1060                         }
1061                 }
1062         }
1063 }