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