Replicate .NET's TimeZoneInfo.IsDaylightSavingTime() and GetUtcOffset()
[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                 static FieldInfo localField;
43                 static FieldInfo cachedDataField;
44                 static object localFieldObj;
45
46                 public static void SetLocal (TimeZoneInfo val)
47                 {
48                         if (localField == null) {
49                                 if (Type.GetType ("Mono.Runtime") != null) {
50                                         localField = typeof (TimeZoneInfo).GetField ("local",
51                                                         BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
52                                 } else {
53                                         cachedDataField = typeof (TimeZoneInfo).GetField ("s_cachedData",
54                                                         BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
55
56                                         localField = cachedDataField.FieldType.GetField ("m_localTimeZone",
57                                                 BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic);
58                                 }
59                         }
60
61                         if (cachedDataField != null)
62                                 localFieldObj = cachedDataField.GetValue (null);
63
64                         localField.SetValue (localFieldObj, val);
65                 }
66
67                 [TestFixture]
68                 public class PropertiesTests
69                 {
70                         [Test]
71                         public void GetLocal ()
72                         {
73                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
74                                         Assert.Ignore ("Not running on Unix.");
75                                 TimeZoneInfo local = TimeZoneInfo.Local;
76                                 Assert.IsNotNull (local);
77                                 Assert.IsTrue (true);
78                         }
79
80                         [DllImport ("libc")]
81                         private static extern int readlink (string path, byte[] buffer, int buflen);
82
83                         [Test] // Covers #24958
84                         public void LocalId ()
85                         {
86                                 byte[] buf = new byte [512];
87
88                                 var path = "/etc/localtime";
89                                 try {
90                                         var ret = readlink (path, buf, buf.Length);
91                                         if (ret == -1)
92                                                 return; // path is not a symbolic link, nothing to test
93                                 } catch (DllNotFoundException e) {
94                                         return;
95                                 }
96 #if !MONOTOUCH && !XAMMAC
97                                 // this assumption is incorrect for iOS, tvO, watchOS and OSX
98                                 Assert.IsTrue (TimeZoneInfo.Local.Id != "Local", "Local timezone id should not be \"Local\"");
99 #endif
100                         }
101                 }
102
103                 [TestFixture]
104                 public class CreateCustomTimezoneTests
105                 {
106                         [Test]
107                         [ExpectedException (typeof (ArgumentNullException))]
108                         public void IdIsNullException ()
109                         {
110                                 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null); 
111                         }
112                 
113                         [Test]
114                         [ExpectedException (typeof (ArgumentException))]
115                         public void IdIsEmptyString ()
116                         {
117                                 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);   
118                         }
119                 
120                         [Test]
121                         [ExpectedException (typeof (ArgumentException))]
122                         public void OffsetIsNotMinutes ()
123                         {
124                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);  
125                         }
126                 
127                         [Test]
128                         [ExpectedException (typeof (ArgumentOutOfRangeException))]
129                         public void OffsetTooBig ()
130                         {
131                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
132                         }
133                 
134                         [Test]
135                         [ExpectedException (typeof (ArgumentOutOfRangeException))]
136                         public void OffsetTooSmall ()
137                         {
138                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
139                         }
140                 
141                 #if STRICT
142                         [Test]
143                         [ExpectedException (typeof (ArgumentException))]
144                         public void IdLongerThan32 ()
145                         {
146                                 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);        
147                         }       
148                 #endif
149                 
150                         [Test]
151                         [ExpectedException (typeof (InvalidTimeZoneException))]
152                         public void AdjustmentRulesOverlap ()
153                         {
154                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
155                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
156                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
157                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
158                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
159                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
160                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
161                         }
162                 
163                         [Test]
164                         [ExpectedException (typeof (InvalidTimeZoneException))]
165                         public void RulesNotOrdered ()
166                         {
167                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
168                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
169                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
170                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
171                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
172                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
173                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
174                         }
175                 
176                         [Test]
177                         [ExpectedException (typeof (InvalidTimeZoneException))]
178                         public void OffsetOutOfRange ()
179                         {
180                                 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
181                                 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
182                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
183                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
184                         }
185                 
186                         [Test]
187                         [ExpectedException (typeof (InvalidTimeZoneException))]
188                         public void NullRule ()
189                         {
190                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
191                         }
192                 
193                         [Test]
194                         [ExpectedException (typeof (InvalidTimeZoneException))]
195                         public void MultiplesRulesForDate ()
196                         {
197                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
198                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
199                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
200                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
201                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
202                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
203                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
204                         }
205
206                         [Test]
207                         public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
208                         {
209                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
210                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
211                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
212                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
213                                 Assert.IsTrue (tz.SupportsDaylightSavingTime);
214                         }
215
216                         [Test]
217                         public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
218                         {
219                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
220                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
221                         }
222
223                         [Test]
224                         public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
225                         {
226                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
227                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
228                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
229                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
230                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
231                         }
232
233                         [Test]
234                         public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
235                         {
236                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
237                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
238                         }
239                 }
240                 
241                 [TestFixture]
242                 public class IsDaylightSavingTimeTests
243                 {
244                         TimeZoneInfo london;
245                 
246                         [SetUp]
247                         public void CreateTimeZones ()
248                         {
249                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
250                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
251                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
252                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
253                         }
254                 
255                         [Test]
256                         public void NoDSTInUTC ()
257                         {
258                                 DateTime june01 = new DateTime (2007, 06, 01);
259                                 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
260                         }
261                 
262                         [Test]
263                         public void DSTInLondon ()
264                         {
265                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
266                                         Assert.Ignore ("Not running on Unix.");
267                                 DateTime june01 = new DateTime (2007, 06, 01);
268                                 DateTime xmas = new DateTime (2007, 12, 25);
269                                 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
270                                 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
271                         }
272                 
273                         [Test]
274                         public void DSTTransisions ()
275                         {
276                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
277                                         Assert.Ignore ("Not running on Unix.");
278                                 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
279                                 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
280                                 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
281                                 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
282                                 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
283                                 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
284                                 Assert.IsFalse (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
285                                 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
286                         }
287                 
288                         [Test]
289                         public void DSTTransisionsUTC ()
290                         {
291                                 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
292                                 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
293                                 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
294                                 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
295                                 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
296                                 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
297                                 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
298                                 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
299                         }
300                 
301                 #if SLOW_TESTS
302                         [Test]
303                         public void MatchTimeZoneBehavior ()
304                         {
305                                 TimeZone tzone = TimeZone.CurrentTimeZone;
306                                 TimeZoneInfo local = TimeZoneInfo.Local;
307                                 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)) {
308                                         date = DateTime.SpecifyKind (date, DateTimeKind.Local);
309                                         if (local.IsInvalidTime (date))
310                                                 continue;
311                                         Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
312                                 }
313                         }
314                 #endif
315                         [Test (Description="Description xambug #17155")]
316                         public void AdjustmentRuleAfterNewYears ()
317                         {
318                                 TimeZoneInfo tz;
319                                 if (Environment.OSVersion.Platform == PlatformID.Unix)
320                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
321                                 else
322                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
323
324                                 // DST start: 9/29/2013 2:00:00 AM
325                                 // DST end: 4/6/2014 3:00:00 AM
326                                 DateTime dt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
327                                 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#1.1");
328
329                                 // DST start: 9/29/2014 2:00:00 AM
330                                 // DST end: 4/6/2015 3:00:00 AM
331                                 dt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
332                                 Assert.IsFalse (tz.IsDaylightSavingTime (dt), "#2.1");
333
334                                 // DST start: 9/29/2014 2:00:00 AM
335                                 // DST end: 4/6/2015 3:00:00 AM
336                                 dt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
337                                 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#3.1");
338                         }
339
340                         [Test] //Covers #26008
341                         public void DSTWithFloatingDateRule ()
342                         {
343                                 // Construct a custom time zone where daylight saving time starts on the
344                                 // 2nd Sunday in March.
345                                 var transitionToDaylight = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 2, DayOfWeek.Sunday);
346                                 var transitionToStandard = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 11, 1, DayOfWeek.Sunday);
347                                 var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1, 0, 0), transitionToDaylight, transitionToStandard);
348                                 var timeZone = TimeZoneInfo.CreateCustomTimeZone ("BugCheck", new TimeSpan (-8, 0, 0), "Testing", "Testing Standard", "Testing Daylight", new TimeZoneInfo.AdjustmentRule [] { adjustment });
349                                 // See if March 7, 2014 is listed as being during daylight saving time.
350                                 // If it is DST, then the runtime has the bug that we are looking for.
351                                 Assert.IsFalse (timeZone.IsDaylightSavingTime (new DateTime (2014, 3, 7, 12, 0, 0, DateTimeKind.Unspecified)));
352                         }
353
354                         [Test] //Covers #25050
355                         public void TestAthensDST ()
356                         {
357                                 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
358                                 var date = new DateTime (2014, 3, 30 , 2, 0, 0);
359                                 Assert.IsFalse (tzi.IsDaylightSavingTime (date));
360                                 Assert.AreEqual (new TimeSpan (2,0,0), tzi.GetUtcOffset (date));
361                         }
362
363                         [Test]
364                         public void TestAthensDST_InDSTDelta ()
365                         {
366                                 // In .NET GetUtcOffset() returns the BaseUtcOffset for times within the hour
367                                 // lost when DST starts but IsDaylightSavingTime() returns true.
368
369                                 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
370
371                                 var date = new DateTime (2014, 3, 30 , 3, 0, 0);
372                                 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
373                                 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
374                                 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
375
376                                 date = new DateTime (2014, 3, 30 , 3, 1, 0);
377                                 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
378                                 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
379                                 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
380
381                                 date = new DateTime (2014, 3, 30 , 3, 59, 0);
382                                 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
383                                 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
384                                 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
385
386                                 date = new DateTime (2014, 3, 30 , 4, 0, 0);
387                                 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
388                                 Assert.AreEqual (new TimeSpan (3, 0, 0), tzi.GetUtcOffset (date));
389                                 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
390                         }
391
392                         [Test]
393                         public void TestAthensDST_InDSTDelta_NoTransitions ()
394                         {
395                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
396                                         Assert.Ignore ("TimeZoneInfo on Mono on Windows and .NET has no transitions");
397
398                                 // Repeat the previous test but this time force using AdjustmentRules by nulling out TimeZoneInfo.transitions
399
400                                 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
401
402                                 var transitionsField = typeof (TimeZoneInfo).GetField ("transitions", BindingFlags.Instance | BindingFlags.NonPublic);
403                                 var transitions = transitionsField.GetValue (tzi);
404                                 Assert.IsNotNull (transitions, "Expected Athens TimeZoneInfo.transitions to be non-null");
405                                 transitionsField.SetValue (tzi, null);
406
407                                 try {
408
409                                         var date = new DateTime (2014, 3, 30 , 3, 0, 0);
410                                         Assert.IsTrue (tzi.IsDaylightSavingTime (date));
411                                         Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
412                                         Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
413
414                                         date = new DateTime (2014, 3, 30 , 3, 1, 0);
415                                         Assert.IsTrue (tzi.IsDaylightSavingTime (date));
416                                         Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
417                                         Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
418
419                                         date = new DateTime (2014, 3, 30 , 3, 59, 0);
420                                         Assert.IsTrue (tzi.IsDaylightSavingTime (date));
421                                         Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
422                                         Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
423
424                                         date = new DateTime (2014, 3, 30 , 4, 0, 0);
425                                         Assert.IsTrue (tzi.IsDaylightSavingTime (date));
426                                         Assert.AreEqual (new TimeSpan (3, 0, 0), tzi.GetUtcOffset (date));
427                                         Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
428
429                                 } finally {
430                                         transitionsField.SetValue (tzi, transitions);
431                                 }
432                         }
433
434                         [Test] //Covers #41349
435                         public void TestIsDST_DateTimeOffset ()
436                         {
437                                 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
438                                 var date = new DateTime (2014, 3, 30 , 2, 0, 0);
439                                 var offset = tzi.GetUtcOffset (date);
440                                 var dateOffset = new DateTimeOffset (date, offset);
441                                 Assert.IsFalse (tzi.IsDaylightSavingTime (dateOffset));
442
443                                 date = new DateTime (2014, 3, 30 , 3, 0, 0);
444                                 offset = tzi.GetUtcOffset (date);
445                                 dateOffset = new DateTimeOffset (date, offset);
446                                 Assert.IsTrue (tzi.IsDaylightSavingTime (dateOffset));
447                         }
448                 }
449                 
450                 [TestFixture]
451                 public class ConvertTimeTests_LocalUtc : ConvertTimeTests
452                 {
453                         static TimeZoneInfo oldLocal;
454
455                         [SetUp]
456                         public void SetLocal ()
457                         {
458                                 base.CreateTimeZones ();
459
460                                 oldLocal = TimeZoneInfo.Local;
461                                 TimeZoneInfoTest.SetLocal (TimeZoneInfo.Utc);
462                         }
463
464                         [TearDown]
465                         public void RestoreLocal ()
466                         {
467                                 TimeZoneInfoTest.SetLocal (oldLocal);
468                         }
469                 }
470
471                 [TestFixture]
472                 public class ConvertTimeTests
473                 {
474                         TimeZoneInfo london;
475                 
476                         [SetUp]
477                         public void CreateTimeZones ()
478                         {
479                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
480                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
481                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
482                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
483                         }
484                 
485                         [Test]
486                         [ExpectedException (typeof (ArgumentException))]
487                         public void ConvertFromUtc_KindIsLocalException ()
488                         {
489                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
490                                         throw new ArgumentException ();
491                                 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);  
492                         }
493                 
494                         [Test]
495                         [ExpectedException (typeof (ArgumentNullException))]
496                         public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
497                         {
498                                 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);            
499                         }
500                 
501                         [Test]
502                         public void ConvertFromUtc_DestinationIsUTC ()
503                         {
504                                 DateTime now = DateTime.UtcNow;
505                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
506                                 Assert.AreEqual (now, converted);
507                         }
508                         
509                         [Test]
510                         public void ConvertFromUTC_ConvertInWinter ()
511                         {
512                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
513                                         Assert.Ignore ("Not running on Unix.");
514                                 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
515                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
516                                 Assert.AreEqual (utc, converted);
517                         }
518                 
519                         [Test]
520                         public void ConvertFromUtc_ConvertInSummer ()
521                         {
522                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
523                                         Assert.Ignore ("Not running on Unix.");
524                                 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
525                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
526                                 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
527                         }
528                 
529                         [Test]
530                         public void ConvertToUTC_KindIsUtc ()
531                         {
532                                 DateTime now = DateTime.UtcNow;
533                                 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
534                                 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
535                                 Assert.AreEqual (now, converted);
536                         }
537                 
538                         [Test]
539                         [ExpectedException (typeof (ArgumentException))]
540                         public void ConvertToUTC_KindIsUTCButSourceIsNot ()
541                         {
542                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
543                         }
544                 
545                         [Test]
546                         [ExpectedException (typeof (ArgumentException))]
547                         public void ConvertToUTC_KindIsLocalButSourceIsNot ()
548                         {
549                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
550                                         throw new ArgumentException ();
551                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);        
552                         }
553                 
554                         [Test]
555                         [ExpectedException (typeof (ArgumentException))]
556                         public void ConvertToUTC_InvalidDate ()
557                         {
558                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
559                         }
560                 
561                         [Test]
562                         [ExpectedException (typeof (ArgumentNullException))]
563                         public void ConvertToUTC_SourceIsNull ()
564                         {
565                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
566                         }
567                 
568                 #if SLOW_TESTS
569                         [Test]
570                         public void ConvertToUtc_MatchDateTimeBehavior ()
571                         {
572                                 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)) {
573                                         Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
574                                 }
575                         }
576                 #endif
577                 
578                         [Test]
579                         public void ConvertFromToUtc ()
580                         {
581                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
582                                         Assert.Ignore ("Not running on Unix.");
583                                 DateTime utc = DateTime.UtcNow;
584                                 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
585                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
586                                 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
587                                 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
588                                 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
589                                 Assert.AreEqual (utc, back);
590                 
591                         }
592
593                         [Test]
594                         public void ConvertTimeToUtc_Overflow ()
595                         {
596                                 var res = TimeZoneInfo.ConvertTimeToUtc (new DateTime (0));
597                                 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#1");
598
599                                 res = TimeZoneInfo.ConvertTimeToUtc (DateTime.MaxValue);
600                                 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#2");
601                         }
602
603                         [Test]
604                         public void ConvertFromToUtc_Utc ()
605                         {
606                                 DateTime utc = DateTime.UtcNow;
607                                 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
608                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, TimeZoneInfo.Utc);
609                                 Assert.AreEqual (DateTimeKind.Utc, converted.Kind);
610                                 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, TimeZoneInfo.Utc);
611                                 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
612                                 Assert.AreEqual (utc, back);
613                         }
614
615                         [Test]
616                         public void ConvertFromToLocal ()
617                         {
618                                 DateTime utc = DateTime.UtcNow;
619                                 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
620                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, TimeZoneInfo.Local);
621                                 var expectedKind = (TimeZoneInfo.Local == TimeZoneInfo.Utc)? DateTimeKind.Utc : DateTimeKind.Local;
622                                 Assert.AreEqual (expectedKind, converted.Kind);
623                                 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, TimeZoneInfo.Local);
624                                 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
625                                 Assert.AreEqual (utc, back);
626                         }
627
628                         [Test]
629                         public void ConvertToTimeZone ()
630                         {
631                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
632                                         Assert.Ignore ("Not running on Unix.");
633
634                                 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
635                         }
636
637                         [Test]
638                         [ExpectedException (typeof (ArgumentNullException))]
639                         public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
640                         {
641                                 TimeZoneInfo.ConvertTime (DateTime.Now, null);
642                         }
643
644                         [Test]
645                         public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
646                         {
647                                 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
648                                 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
649                                 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
650                                 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
651                                 
652                                 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
653                                 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
654                                 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
655                                 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
656
657                                 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
658                                 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
659                                 var expectedKind = (TimeZoneInfo.Local == TimeZoneInfo.Utc)? DateTimeKind.Utc : sdt.Kind;
660                                 Assert.AreEqual (expectedKind,  ddt.Kind, "#3.1");
661                                 Assert.AreEqual (DateTimeKind.Unspecified, sdt.Kind, "#3.2");
662                         }
663
664                         [Test]
665                         [ExpectedException (typeof (ArgumentNullException))]
666                         public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
667                         {
668                                 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
669                         }
670
671                         [Test]
672                         [ExpectedException (typeof (ArgumentNullException))]
673                         public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
674                         {
675                                 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
676                         }
677
678                         [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
679                         public void ConvertTime_AdjustmentRuleAfterNewYears ()
680                         {
681                                 TimeZoneInfo tz;
682                                 if (Environment.OSVersion.Platform == PlatformID.Unix)
683                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
684                                 else
685                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
686
687                                 // DST start: 9/29/2013 2:00:00 AM
688                                 // DST end: 4/6/2014 3:00:00 AM
689                                 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
690                                 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
691                                 Assert.AreEqual (10, ddt.Day, "#1.1");
692                                 Assert.AreEqual (1, ddt.Month, "#1.2");
693                                 Assert.AreEqual (2014, ddt.Year, "#1.3");
694                                 Assert.AreEqual (12, ddt.Hour, "#1.4");
695                                 Assert.AreEqual (0, ddt.Minute, "#1.5");
696                                 Assert.AreEqual (0, ddt.Second, "#1.6");
697                                 
698                                 // DST start: 9/29/2014 2:00:00 AM
699                                 // DST end: 4/6/2015 3:00:00 AM
700                                 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
701                                 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
702                                 Assert.AreEqual (10, ddt.Day, "#2.1");
703                                 Assert.AreEqual (6, ddt.Month, "#2.2");
704                                 Assert.AreEqual (2014, ddt.Year, "#2.3");
705                                 Assert.AreEqual (11, ddt.Hour, "#2.4");
706                                 Assert.AreEqual (0, ddt.Minute, "#2.5");
707                                 Assert.AreEqual (0, ddt.Second, "#2.6");
708                                 
709                                 // DST start: 9/29/2014 2:00:00 AM
710                                 // DST end: 4/6/2015 3:00:00 AM
711                                 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
712                                 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
713                                 Assert.AreEqual (10, ddt.Day, "#3.1");
714                                 Assert.AreEqual (10, ddt.Month, "#3.2");
715                                 Assert.AreEqual (2014, ddt.Year, "#3.3");
716                                 Assert.AreEqual (12, ddt.Hour, "#3.4");
717                                 Assert.AreEqual (0, ddt.Minute, "#3.5");
718                                 Assert.AreEqual (0, ddt.Second, "#3.6");
719                         }
720
721                         [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
722                         public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
723                                 
724                                 TimeZoneInfo easternTimeZone;
725                                 TimeZoneInfo pacificTimeZone;
726
727                                 if (Environment.OSVersion.Platform == PlatformID.Unix) {
728                                         // *nix
729                                         easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
730                                         pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");   
731                                 }
732                                 else {
733                                         // Windows
734                                         easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
735                                         pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
736                                 }
737
738                                 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
739                                 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
740                                 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
741                         
742                                 // Last midnight in PST as EST should be 3AM
743                                 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
744
745                                 Assert.AreEqual (expectedDate, lastMidnightAsEST);
746                                 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
747                         }
748
749                         [Test]
750                         public void ConvertTimeBySystemTimeZoneId_UtcId ()
751                         {
752                                 DateTime localTime = TimeZoneInfo.ConvertTime (DateTime.UtcNow, TimeZoneInfo.Utc, TimeZoneInfo.Local);
753
754                                 TimeZoneInfo.ConvertTimeBySystemTimeZoneId (DateTime.UtcNow, TimeZoneInfo.Utc.Id, TimeZoneInfo.Local.Id);
755                         }
756                 }
757                 
758                 [TestFixture]
759                 public class IsInvalidTimeTests
760                 {
761                         TimeZoneInfo london;
762                 
763                         [SetUp]
764                         public void CreateTimeZones ()
765                         {
766                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
767                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
768                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
769                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
770                         }
771                 
772                 #if SLOW_TESTS
773                         [Test]
774                         public void UTCDate ()
775                         {
776                                 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)) {
777                                         date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
778                                         Assert.IsFalse (london.IsInvalidTime (date));
779                                 }
780                         }
781                 #endif
782                         [Test]
783                         public void InvalidDates ()
784                         {
785                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
786                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
787                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
788                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
789                         }
790                 }
791                 
792                 [TestFixture]
793                 public class IsAmbiguousTimeTests
794                 {
795                         TimeZoneInfo london;
796                 
797                         [SetUp]
798                         public void CreateTimeZones ()
799                         {
800                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
801                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
802                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
803                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
804                         }
805                 
806                         [Test]
807                         public void AmbiguousDates ()
808                         {
809                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
810                                         Assert.Ignore ("Not running on Unix.");
811                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
812                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
813                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
814                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
815                         }
816                 
817                         [Test]
818                         public void AmbiguousUTCDates ()
819                         {
820                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
821                                         Assert.Ignore ("Not running on Unix.");
822                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
823                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
824                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
825                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
826                         }
827                 
828                 #if SLOW_TESTS
829                         [Test]
830                         public void AmbiguousInUTC ()
831                         {
832                                 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)) {
833                                         Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
834                                 }
835                         }
836                 #endif
837                 }
838                 
839                 [TestFixture]
840                 public class GetSystemTimeZonesTests
841                 {
842                         [Test]
843                         public void Identity ()
844                         {
845                                 Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
846                         }
847
848                         [Test]
849                         public void NotEmpty ()
850                         {
851                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
852                                         Assert.Ignore ("Not running on Unix.");
853                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
854                                 Assert.IsNotNull(systemTZ, "SystemTZ is null");
855                                 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
856                         }
857                 
858                         [Test]
859                         public void ContainsBrussels ()
860                         {
861                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
862                                         Assert.Ignore ("Not running on Unix.");
863                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
864                                 foreach (TimeZoneInfo tz in systemTZ) {
865                                         if (tz.Id == "Europe/Brussels")
866                                                 return;
867                                 }
868                                 Assert.Fail ("Europe/Brussels not found in SystemTZ");
869                         }
870
871                         [Test]
872                         public void ReflectionReturnsTheCorrectMethod ()
873                         {
874                                 var method = (MethodInfo) typeof (TimeZoneInfo).GetMember ("GetSystemTimeZones", MemberTypes.Method, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)[0];
875
876                                 var timeZones = (global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo>) method.Invoke (null, null);
877                                 Assert.IsTrue (timeZones.Count > 0, "GetSystemTimeZones should not return an empty collection.");
878                         }
879
880 #if !MOBILE
881                         [Test]
882                         public void WindowsRegistryTimezoneWithParentheses ()
883                         {
884                                 var method = (MethodInfo) typeof (TimeZoneInfo).GetMember ("TrimSpecial", MemberTypes.Method, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)[0];
885
886                                 var name = method.Invoke (null, new object [] { " <--->  Central Standard Time (Mexico)   ||<<>>" });
887                                 Assert.AreEqual (name, "Central Standard Time (Mexico)", "#1");
888                         }
889 #endif
890                 }
891                 
892                 [TestFixture]
893                 public class FindSystemTimeZoneByIdTests
894                 {
895                         [Test]
896                         [ExpectedException (typeof (ArgumentNullException))]
897                         public void NullId ()
898                         {
899                                 TimeZoneInfo.FindSystemTimeZoneById (null);
900                         }
901                 
902                         [Test]
903                         [ExpectedException (typeof (TimeZoneNotFoundException))]
904                         public void NonSystemTimezone ()
905                         {
906                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
907                                         throw new TimeZoneNotFoundException ();
908                                 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
909                         }
910                 
911                         [Test]
912                         public void FindBrusselsTZ ()
913                         {
914                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
915                                         Assert.Ignore ("Not running on Unix.");
916                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
917                                 Assert.IsNotNull (brussels);
918                         }
919                 
920                         [Test]
921                         public void OffsetIsCorrectInKinshasa ()
922                         {
923                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
924                                         Assert.Ignore ("Not running on Unix.");
925                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
926                                 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
927                         }
928                 
929                         [Test]
930                         public void OffsetIsCorrectInBrussels ()
931                         {
932                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
933                                         Assert.Ignore ("Not running on Unix.");
934                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
935                                 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
936                         }
937                 
938                         [Test]
939                         public void NoDSTInKinshasa ()
940                         {
941                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
942                                         Assert.Ignore ("Not running on Unix.");
943                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
944                                 Assert.IsFalse (kin.SupportsDaylightSavingTime);
945                         }
946                 
947                         [Test]
948                         public void BrusselsSupportsDST ()
949                         {
950                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
951                                         Assert.Ignore ("Not running on Unix.");
952                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
953                                 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
954                         }
955                 
956                         [Test]
957                         public void MelbourneSupportsDST ()
958                         {
959                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
960                                         Assert.Ignore ("Not running on Unix.");
961                                 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
962                                 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
963                         }
964                 
965                         [Test]
966                         public void RomeAndVaticanSharesTime ()
967                         {
968                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
969                                         Assert.Ignore ("Not running on Unix.");
970                                 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
971                                 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
972                                 Assert.IsTrue (rome.HasSameRules (vatican));
973                         }
974
975                         [Test]
976                         public void FindSystemTimeZoneById_Local_Roundtrip ()
977                         {
978                                 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
979                         }
980
981                         [Test]
982                         public void Test326 ()
983                         {
984                                 DateTime utc = DateTime.UtcNow;
985                                 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
986                                 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
987                         }
988                 
989                 #if SLOW_TESTS
990                         [Test]
991                         public void BrusselsAdjustments ()
992                         {
993                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
994                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
995                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
996                                 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
997                 
998                                 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
999                 
1000                                 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)) {
1001                                         Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
1002                                         Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
1003                                 }               
1004                         }
1005                 #endif
1006
1007                         [Test]
1008                         public void SubminuteDSTOffsets ()
1009                         {
1010                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
1011                                         Assert.Ignore ();
1012
1013                                 var subMinuteDSTs = new string [] {
1014                                         "Europe/Dublin", // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
1015                                         "Europe/Amsterdam",
1016                                         "America/St_Johns",
1017                                         "Canada/Newfoundland",
1018                                         "Europe/Moscow",
1019                                         "Europe/Riga",
1020                                 };
1021                                 foreach (var tz in subMinuteDSTs) {
1022                                         TimeZoneInfo.FindSystemTimeZoneById (tz);
1023                                 }
1024                         }
1025
1026                         [Test]
1027                         [ExpectedException (typeof (TimeZoneNotFoundException))]
1028                         public void InvalidName ()
1029                         {
1030                                 TimeZoneInfo.FindSystemTimeZoneById ("N/A");
1031                         }
1032                 }
1033                 
1034                 [TestFixture]
1035                 public class GetAmbiguousTimeOffsetsTests
1036                 {
1037                         [Test]
1038                         [ExpectedException (typeof(ArgumentException))]
1039                         public void DateIsNotAmbiguous ()
1040                         {
1041                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
1042                                         throw new ArgumentException ();
1043                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
1044                                 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
1045                                 brussels.GetAmbiguousTimeOffsets (date);
1046                         }
1047                 
1048                         [Test]
1049                         public void AmbiguousOffsets ()
1050                         {
1051                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
1052                                         Assert.Ignore ("Not running on Unix.");
1053                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
1054                                 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
1055                                 Assert.IsTrue (brussels.IsAmbiguousTime (date));
1056                                 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
1057                                 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
1058                         }
1059                 }
1060
1061                 [TestFixture]
1062                 public class HasSameRulesTests
1063                 {
1064                         [Test]
1065                         public void NullAdjustments () //bnc #391011
1066                         {
1067                                 TimeZoneInfo utc = TimeZoneInfo.Utc;
1068                                 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
1069                                 Assert.IsTrue (utc.HasSameRules (custom));
1070                         }
1071                 }
1072
1073                 [TestFixture]
1074                 public class SerializationTests
1075                 {
1076                         [Test]
1077                         public void Serialization_Deserialization ()
1078                         {
1079                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
1080                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
1081                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
1082                                 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
1083                                 MemoryStream stream = new MemoryStream ();
1084                                 BinaryFormatter formatter = new BinaryFormatter ();
1085                                 formatter.Serialize (stream, london);
1086                                 stream.Position = 0;
1087                                 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
1088                                 stream.Close ();
1089                                 stream.Dispose ();
1090                                 Assert.IsTrue (london.Equals (deserialized));
1091                         }
1092                 }
1093
1094                 [TestFixture]
1095                 public class MultipleDaylightSavingTimeTests {
1096                         private TimeZoneInfo cairo;
1097                         private DateTime dst1Start;
1098                         private DateTime dst1End;
1099                         private DateTime dst2Start;
1100                         private DateTime dst2End;
1101
1102                         private TimeSpan baseUtcOffset;
1103                         private TimeSpan dstUtcOffset;
1104                         private TimeSpan dstOffset;
1105
1106                         [SetUp]
1107                         public void CreateTimeZones ()
1108                         {
1109                                 /*
1110                                 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
1111                                         Delta: 01:00:00
1112                                         Begins at 12:00 AM on 16 May
1113                                         Ends at 1:00 AM on 29 June
1114                                 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
1115                                         Delta: 01:00:00
1116                                         Begins at 12:00 AM on 29 July
1117                                         Ends at 12:00 AM on 26 September
1118                                 */
1119                                 dst1Start = new DateTime (2014, 5, 16);
1120                                 dst1End = new DateTime (2014, 6, 29);
1121                                 dst2Start = new DateTime (2014, 7, 29);
1122                                 dst2End = new DateTime (2014, 9, 26);
1123
1124                                 baseUtcOffset = new TimeSpan (2, 0, 0);
1125                                 dstUtcOffset = new TimeSpan (3, 0, 0);
1126                                 dstOffset = dstUtcOffset - baseUtcOffset;
1127
1128                                 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
1129                                         new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
1130                                         CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
1131
1132                                 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
1133                                         new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
1134                                         CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
1135
1136                                 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
1137                                         new [] {rule1, rule2});
1138                         }
1139
1140                         private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
1141                         {
1142                                 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
1143                                 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
1144                         }
1145
1146                         [Test]
1147                         public void GetUtcOffset_FromUTC ()
1148                         {
1149                                 var d = dst1Start.Add (-baseUtcOffset);
1150                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1151                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1152                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1153                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1154
1155                                 d = dst1End.Add (-baseUtcOffset-dstOffset);
1156                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1157                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1158                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1159                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1160
1161                                 d = dst2Start.Add (-baseUtcOffset);
1162                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1163                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1164                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1165                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1166
1167                                 d = dst2End.Add (-baseUtcOffset-dstOffset);
1168                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1169                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1170                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1171                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1172                         }
1173
1174                         [Test]
1175                         public void GetUtcOffset_FromLocal ()
1176                         {
1177                                 var d = dst1Start.Add (-baseUtcOffset);
1178                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1179                                 d = d.ToLocalTime ();
1180                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1181                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1182                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1183
1184                                 d = dst1End.Add (-baseUtcOffset-dstOffset);
1185                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1186                                 d = d.ToLocalTime ();
1187                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1188                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1189                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1190
1191                                 d = dst2Start.Add (-baseUtcOffset);
1192                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1193                                 d = d.ToLocalTime ();
1194                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1195                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1196                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1197
1198                                 d = dst2End.Add (-baseUtcOffset-dstOffset);
1199                                 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1200                                 d = d.ToLocalTime ();
1201                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1202                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1203                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1204                         }
1205
1206                         [Test]
1207                         public void GetUtcOffset_FromUnspecified ()
1208                         {
1209                                 var d = dst1Start.Add (dstOffset);
1210                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1211                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1212                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1213
1214                                 d = dst1End.Add (-dstOffset);
1215                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1216                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1217                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1218
1219                                 d = dst2Start.Add (dstOffset);
1220                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1221                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1222                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1223
1224                                 d = dst2End.Add (-dstOffset);
1225                                 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1226                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1227                                 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1228                         }
1229
1230                   [Test]
1231                   public void  GetUtcOffset_FromDateTimeOffset ()
1232                   {
1233                           DateTimeOffset offset;
1234
1235                           offset = new DateTimeOffset(dst1Start, baseUtcOffset);
1236                           Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, -1))), "dst1Start_with_baseUtcOffset#before");
1237                           Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset), "dst1Start_with_baseUtcOffset#exact");
1238                           Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, 1))), "dst1Start_with_baseUtcOffset#after");
1239
1240                           offset = new DateTimeOffset(dst1End, dstOffset + baseUtcOffset);
1241                           Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, -1))), "dst1End_with_dstOffset+baseUtcOffset#before");
1242                           Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset), "dst1End_with_dstOffset+baseUtcOffset#exact");
1243                           Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, 1))), "dst1End_with_dstOffset+baseUtcOffset#after");
1244
1245                           offset = new DateTimeOffset(dst2Start, baseUtcOffset);
1246                           Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, -1))), "dst2Start_with_baseUtcOffset#before");
1247                           Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset), "dst2Start_with_baseUtcOffset#exact");
1248                           Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, 1))), "dst2Start_with_baseUtcOffset#after");
1249
1250                           offset = new DateTimeOffset(dst2End, baseUtcOffset + dstOffset);
1251                           Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, -1))), "dst2End_with_dstOffset+baseUtcOffset#before");
1252                           Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset), "dst2End_with_dstOffset+baseUtcOffset#exact");
1253                           Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, 1))), "dst2End_with_dstOffset+baseUtcOffset#after");
1254                   }
1255
1256                         [Test]
1257                         public void DTS_WithMinimalDate ()
1258                         {
1259                                 TimeZoneInfo.TransitionTime startTransition, endTransition;
1260                                 startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 4, 0, 0),
1261                                                                                                                                                                   10, 2, DayOfWeek.Sunday);
1262                                 endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 3, 0, 0),
1263                                                                                                                                                                 3, 2, DayOfWeek.Sunday);
1264
1265                                 var ctz = TimeZoneInfo.CreateCustomTimeZone ("test", TimeSpan.FromHours (-5), "display", "sdisplay", "dst", new [] {
1266                                         TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue, DateTime.MaxValue.Date, TimeSpan.FromHours (-1), startTransition, endTransition) });
1267
1268                                 var offset = ctz.GetUtcOffset (DateTime.MinValue);
1269                                 Assert.AreEqual (TimeSpan.FromHours (-5), offset); // TODO: Wrong it should be -6
1270                         }
1271     }
1272
1273                 [TestFixture]
1274                 public class GetDaylightChanges
1275                 {
1276                         MethodInfo getChanges;
1277
1278                         [SetUp]
1279                         public void Setup ()
1280                         {
1281                                 var flags = BindingFlags.Instance | BindingFlags.NonPublic;
1282                                 getChanges = typeof (TimeZoneInfo).GetMethod ("GetDaylightChanges", flags);
1283                         }
1284
1285                         [Test]
1286                         public void TestSydneyDaylightChanges ()
1287                         {
1288                                 TimeZoneInfo tz;
1289                                 if (Environment.OSVersion.Platform == PlatformID.Unix)
1290                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Sydney");
1291                                 else
1292                                         tz = TimeZoneInfo.FindSystemTimeZoneById ("W. Australia Standard Time");
1293
1294                                 var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
1295
1296                                 Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
1297                                 Assert.AreEqual (new DateTime (2014, 10, 5, 2, 0, 0), changes.Start);
1298                                 Assert.AreEqual (new DateTime (2014, 4, 6, 3, 0, 0), changes.End);
1299                         }
1300
1301                         [Test]
1302                         public void AllTimeZonesDaylightChanges ()
1303                         {
1304                                 foreach (var tz in TimeZoneInfo.GetSystemTimeZones ()) {
1305                                         try {
1306                                                 for (var year = 1950; year <= 2051; year++)
1307                                                         getChanges.Invoke (tz, new object [] {year} );
1308                                         } catch (Exception e) {
1309                                                 Assert.Fail ("TimeZone " + tz.Id + " exception: " + e.ToString ()); 
1310                                         }
1311                                 }
1312                         }
1313                 }
1314
1315                 [TestFixture]
1316                 public class ParseTZBuffer
1317                 {
1318                         MethodInfo parseTZBuffer;
1319
1320                         [SetUp]
1321                         public void Setup()
1322                         {
1323                                 var flags = BindingFlags.Static | BindingFlags.NonPublic;
1324                                 parseTZBuffer = typeof (TimeZoneInfo).GetMethod ("ParseTZBuffer", flags);
1325                         }
1326
1327                         [Test]
1328                         public void Bug31432 ()
1329                         {
1330                                 // Europe/Moscow from failing device
1331                                 var base64Data = "VFppZjIAAAAAAAAAAAAAAAAAAAAAAAAPAAAADwAAAAAAAABNAAAADwAAACKbXx7HnT7yeZ4q7vme9zlpn4RX+aDYbOmhABYJoTymQKQQbcCkPTKwpRVosKU9A8CnHkVQtaQZYBUnp9AWGNxAFwjbUBf6D8AY6g7QGdtDQBrMk9AbvKDwHKyR8B2cgvAejHPwH3xk8CBsVfAhXEbwIkw38CM8KPAkLBnwJRwK8CYL+/AnBSdwJ/UYcCjlF4ApeL+AKdTQQCrEszArtNxwLKTNcC2UvnAuhK9wL3SgcDBkkXAxXbzwMnKX8DM9nvA0UnnwNR2A8DYyW/A2/WLwOBt4cDjdRPA5+1pwOr0m8DvbPHA8pkNwPbsecD6GJXA/mwBwQGYHcEGEHPBCRelwQ2P+8EQly3BFQ+DwRgWtcEcjwvBH7snwSQOk8EnOq/BK44bwS66N8EzMo3BNjm/wVEwdYAIBAgMBAwUEBQYFBwgHCQcJBwkHCQoLCgsKCwoLCgsKCwoMDQoJBwsKCwoLCgsKCwoLCgsKCwoLCgsKCwoLCgsKCwoLCgsKCwoLCg4KAAAjOQAAAAAxhwEEAAAjdwAAAAA/lwEIAAAqMAADAAA4QAENAABGUAEPAAAqMAARAAAcIAAVAAA4QAEZAAAqMAARAAA4QAEZAAAqMAEdAAAcIAAVAAA4QAARTU1UAE1TVABNRFNUAFMATQBNU0sARUVUAE1TRABFRVNUAAAAAAAAAAAAAAABAQEBAQAAAAAAAAAAAAAAAAAAAFRaaWYyAAAAAAAAAAAAAAAAAAAAAAAAEAAAABAAAAAAAAAATgAAABAAAAAm/////1a2wMf/////m18ex/////+dPvJ5/////54q7vn/////nvc5af////+fhFf5/////6DYbOn/////oQAWCf////+hPKZA/////6QQbcD/////pD0ysP////+lFWiw/////6U9A8D/////px5FUP////+1pBlgAAAAABUnp9AAAAAAFhjcQAAAAAAXCNtQAAAAABf6D8AAAAAAGOoO0AAAAAAZ20NAAAAAABrMk9AAAAAAG7yg8AAAAAAcrJHwAAAAAB2cgvAAAAAAHoxz8AAAAAAffGTwAAAAACBsVfAAAAAAIVxG8AAAAAAiTDfwAAAAACM8KPAAAAAAJCwZ8AAAAAAlHArwAAAAACYL+/AAAAAAJwUncAAAAAAn9RhwAAAAACjlF4AAAAAAKXi/gAAAAAAp1NBAAAAAACrEszAAAAAAK7TccAAAAAAspM1wAAAAAC2UvnAAAAAALoSvcAAAAAAvdKBwAAAAADBkkXAAAAAAMV288AAAAAAycpfwAAAAADM9nvAAAAAANFJ58AAAAAA1HYDwAAAAADYyW/AAAAAANv1i8AAAAAA4G3hwAAAAADjdRPAAAAAAOftacAAAAAA6vSbwAAAAADvbPHAAAAAAPKZDcAAAAAA9ux5wAAAAAD6GJXAAAAAAP5sAcAAAAABAZgdwAAAAAEGEHPAAAAAAQkXpcAAAAABDY/7wAAAAAEQly3AAAAAARUPg8AAAAABGBa1wAAAAAEcjwvAAAAAAR+7J8AAAAABJA6TwAAAAAEnOq/AAAAAASuOG8AAAAABLro3wAAAAAEzMo3AAAAAATY5v8AAAAABUTB1gAQMCAwQCBAYFBgcGCAkICggKCAoICgsMCwwLDAsMCwwLDAsNDgsKCAwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCw8LAAAjOQAAAAAjOQAEAAAxhwEIAAAjdwAEAAA/lwEMAAAqMAADAAA4QAERAABGUAETAAAqMAAVAAAcIAAZAAA4QAEdAAAqMAAVAAA4QAEdAAAqMAEhAAAcIAAZAAA4QAAVTE1UAE1NVABNU1QATURTVABTAE0ATVNLAEVFVABNU0QARUVTVAAAAAAAAAAAAAAAAAEBAQEBAAAAAAAAAAAAAAAAAAAAAApNU0stMwo=";
1332
1333                                 var data = Convert.FromBase64String (base64Data);
1334
1335                                 var tz = parseTZBuffer.Invoke (null, new object[] { "Test", data, data.Length});
1336                                 Assert.IsTrue (tz != null);
1337                         }
1338                 }
1339         }
1340 }