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