Merge pull request #398 from xcrash/feature/wcf.mex.https
[mono.git] / mcs / class / System.Core / Test / System / TimeZoneInfoTest.cs
1 /*
2  * TimeZoneInfo.Tests
3  *
4  * Author(s)
5  *      Stephane Delcroix <stephane@delcroix.org>
6  *
7  * Copyright 2011 Xamarin Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 using System;
30 using System.Collections;
31
32 using NUnit.Framework;
33 #if NET_2_0
34 namespace MonoTests.System
35 {
36         public class TimeZoneInfoTest
37         {
38                 [TestFixture]
39                 public class PropertiesTests
40                 {
41                         [Test]
42                         public void GetLocal ()
43                         {
44                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
45                                         return;
46                                 TimeZoneInfo local = TimeZoneInfo.Local;
47                                 Assert.IsNotNull (local);
48                                 Assert.IsTrue (true);
49                         }
50                 }
51
52                 [TestFixture]
53                 public class CreateCustomTimezoneTests
54                 {
55                         [Test]
56                         [ExpectedException (typeof (ArgumentNullException))]
57                         public void IdIsNullException ()
58                         {
59                                 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null); 
60                         }
61                 
62                         [Test]
63                         [ExpectedException (typeof (ArgumentException))]
64                         public void IdIsEmptyString ()
65                         {
66                                 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);   
67                         }
68                 
69                         [Test]
70                         [ExpectedException (typeof (ArgumentException))]
71                         public void OffsetIsNotMinutes ()
72                         {
73                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);  
74                         }
75                 
76                         [Test]
77                         [ExpectedException (typeof (ArgumentOutOfRangeException))]
78                         public void OffsetTooBig ()
79                         {
80                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
81                         }
82                 
83                         [Test]
84                         [ExpectedException (typeof (ArgumentOutOfRangeException))]
85                         public void OffsetTooSmall ()
86                         {
87                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
88                         }
89                 
90                 #if STRICT
91                         [Test]
92                         [ExpectedException (typeof (ArgumentException))]
93                         public void IdLongerThan32 ()
94                         {
95                                 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);        
96                         }       
97                 #endif
98                 
99                         [Test]
100                         [ExpectedException (typeof (InvalidTimeZoneException))]
101                         public void AdjustmentRulesOverlap ()
102                         {
103                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
104                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
105                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
106                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
107                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
108                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
109                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
110                         }
111                 
112                         [Test]
113                         [ExpectedException (typeof (InvalidTimeZoneException))]
114                         public void RulesNotOrdered ()
115                         {
116                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
117                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
118                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
119                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
120                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
121                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
122                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
123                         }
124                 
125                         [Test]
126                         [ExpectedException (typeof (InvalidTimeZoneException))]
127                         public void OffsetOutOfRange ()
128                         {
129                                 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
130                                 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
131                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
132                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
133                         }
134                 
135                         [Test]
136                         [ExpectedException (typeof (InvalidTimeZoneException))]
137                         public void NullRule ()
138                         {
139                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
140                         }
141                 
142                         [Test]
143                         [ExpectedException (typeof (InvalidTimeZoneException))]
144                         public void MultiplesRulesForDate ()
145                         {
146                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
147                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
148                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
149                                 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
150                                 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
151                                 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
152                                 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
153                         }
154
155                         [Test]
156                         public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
157                         {
158                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
159                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
160                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
161                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
162                                 Assert.IsTrue (tz.SupportsDaylightSavingTime);
163                         }
164
165                         [Test]
166                         public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
167                         {
168                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
169                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
170                         }
171
172                         [Test]
173                         public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
174                         {
175                                 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
176                                 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
177                                 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
178                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
179                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
180                         }
181
182                         [Test]
183                         public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
184                         {
185                                 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
186                                 Assert.IsFalse (tz.SupportsDaylightSavingTime);
187                         }
188                 }
189                 
190                 [TestFixture]
191                 public class IsDaylightSavingTimeTests
192                 {
193                         TimeZoneInfo london;
194                 
195                         [SetUp]
196                         public void CreateTimeZones ()
197                         {
198                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
199                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
200                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
201                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
202                         }
203                 
204                         [Test]
205                         public void NoDSTInUTC ()
206                         {
207                                 DateTime june01 = new DateTime (2007, 06, 01);
208                                 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
209                         }
210                 
211                         [Test]
212                         public void DSTInLondon ()
213                         {
214                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
215                                         return;
216                                 DateTime june01 = new DateTime (2007, 06, 01);
217                                 DateTime xmas = new DateTime (2007, 12, 25);
218                                 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
219                                 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
220                         }
221                 
222                         [Test]
223                         public void DSTTransisions ()
224                         {
225                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
226                                         return;
227                                 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
228                                 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
229                                 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
230                                 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
231                                 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
232                                 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
233                                 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
234                                 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
235                         }
236                 
237                         [Test]
238                         public void DSTTransisionsUTC ()
239                         {
240                                 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
241                                 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
242                                 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
243                                 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
244                                 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
245                                 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
246                                 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
247                                 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
248                         }
249                 
250                 #if SLOW_TESTS
251                         [Test]
252                         public void MatchTimeZoneBehavior ()
253                         {
254                                 TimeZone tzone = TimeZone.CurrentTimeZone;
255                                 TimeZoneInfo local = TimeZoneInfo.Local;
256                                 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)) {
257                                         date = DateTime.SpecifyKind (date, DateTimeKind.Local);
258                                         if (local.IsInvalidTime (date))
259                                                 continue;
260                                         Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
261                                 }
262                         }
263                 #endif
264                 }
265                 
266                 [TestFixture]
267                 public class ConvertTimeTests
268                 {
269                         TimeZoneInfo london;
270                 
271                         [SetUp]
272                         public void CreateTimeZones ()
273                         {
274                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
275                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
276                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
277                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
278                         }
279                 
280                         [Test]
281                         [ExpectedException (typeof (ArgumentException))]
282                         public void ConvertFromUtc_KindIsLocalException ()
283                         {
284                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
285                                         throw new ArgumentException ();
286                                 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);  
287                         }
288                 
289                         [Test]
290                         [ExpectedException (typeof (ArgumentNullException))]
291                         public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
292                         {
293                                 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);            
294                         }
295                 
296                         [Test]
297                         public void ConvertFromUtc_DestinationIsUTC ()
298                         {
299                                 DateTime now = DateTime.UtcNow;
300                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
301                                 Assert.AreEqual (now, converted);
302                         }
303                         
304                         [Test]
305                         public void ConvertFromUTC_ConvertInWinter ()
306                         {
307                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
308                                         return;
309                                 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
310                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
311                                 Assert.AreEqual (utc, converted);
312                         }
313                 
314                         [Test]
315                         public void ConvertFromUtc_ConvertInSummer ()
316                         {
317                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
318                                         return;
319                                 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
320                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
321                                 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
322                         }
323                 
324                         [Test]
325                         public void ConvertToUTC_KindIsUtc ()
326                         {
327                                 DateTime now = DateTime.UtcNow;
328                                 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
329                                 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
330                                 Assert.AreEqual (now, converted);
331                         }
332                 
333                         [Test]
334                         [ExpectedException (typeof (ArgumentException))]
335                         public void ConvertToUTC_KindIsUTCButSourceIsNot ()
336                         {
337                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
338                         }
339                 
340                         [Test]
341                         [ExpectedException (typeof (ArgumentException))]
342                         public void ConvertToUTC_KindIsLocalButSourceIsNot ()
343                         {
344                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
345                                         throw new ArgumentException ();
346                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);        
347                         }
348                 
349                         [Test]
350                         [ExpectedException (typeof (ArgumentException))]
351                         public void ConvertToUTC_InvalidDate ()
352                         {
353                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
354                         }
355                 
356                         [Test]
357                         [ExpectedException (typeof (ArgumentNullException))]
358                         public void ConvertToUTC_SourceIsNull ()
359                         {
360                                 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
361                         }
362                 
363                 #if SLOW_TESTS
364                         [Test]
365                         public void ConvertToUtc_MatchDateTimeBehavior ()
366                         {
367                                 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)) {
368                                         Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
369                                 }
370                         }
371                 #endif
372                 
373                         [Test]
374                         public void ConvertFromToUtc ()
375                         {
376                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
377                                         return;
378                                 DateTime utc = DateTime.UtcNow;
379                                 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
380                                 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
381                                 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
382                                 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
383                                 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
384                                 Assert.AreEqual (utc, back);
385                 
386                         }
387                 }
388                 
389                 [TestFixture]
390                 public class IsInvalidTimeTests
391                 {
392                         TimeZoneInfo london;
393                 
394                         [SetUp]
395                         public void CreateTimeZones ()
396                         {
397                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
398                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
399                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
400                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
401                         }
402                 
403                 #if SLOW_TESTS
404                         [Test]
405                         public void UTCDate ()
406                         {
407                                 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)) {
408                                         date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
409                                         Assert.IsFalse (london.IsInvalidTime (date));
410                                 }
411                         }
412                 #endif
413                         [Test]
414                         public void InvalidDates ()
415                         {
416                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
417                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
418                                 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
419                                 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
420                         }
421                 }
422                 
423                 [TestFixture]
424                 public class IsAmbiguousTimeTests
425                 {
426                         TimeZoneInfo london;
427                 
428                         [SetUp]
429                         public void CreateTimeZones ()
430                         {
431                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
432                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
433                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
434                                 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
435                         }
436                 
437                         [Test]
438                         public void AmbiguousDates ()
439                         {
440                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
441                                         return;
442                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
443                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
444                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
445                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
446                         }
447                 
448                         [Test]
449                         public void AmbiguousUTCDates ()
450                         {
451                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
452                                         return;
453                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
454                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
455                                 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
456                                 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
457                         }
458                 
459                 #if SLOW_TESTS
460                         [Test]
461                         public void AmbiguousInUTC ()
462                         {
463                                 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)) {
464                                         Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
465                                 }
466                         }
467                 #endif
468                 }
469                 
470                 [TestFixture]
471                 public class GetSystemTimeZonesTests
472                 {
473                         [Test]
474                         public void NotEmpty ()
475                         {
476                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
477                                         return;
478                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
479                                 Assert.IsNotNull(systemTZ, "SystemTZ is null");
480                                 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
481                         }
482                 
483                         [Test]
484                         public void ContainsBrussels ()
485                         {
486                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
487                                         return;
488                                 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
489                                 foreach (TimeZoneInfo tz in systemTZ) {
490                                         if (tz.Id == "Europe/Brussels")
491                                                 return;
492                                 }
493                                 Assert.Fail ("Europe/Brussels not found in SystemTZ");
494                         }
495                 }
496                 
497                 [TestFixture]
498                 public class FindSystemTimeZoneByIdTests
499                 {
500                         [Test]
501                         [ExpectedException (typeof (ArgumentNullException))]
502                         public void NullId ()
503                         {
504                                 TimeZoneInfo.FindSystemTimeZoneById (null);
505                         }
506                 
507                         [Test]
508                         [ExpectedException (typeof (TimeZoneNotFoundException))]
509                         public void NonSystemTimezone ()
510                         {
511                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
512                                         throw new TimeZoneNotFoundException ();
513                                 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
514                         }
515                 
516                         [Test]
517                         public void FindBrusselsTZ ()
518                         {
519                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
520                                         return;
521                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
522                                 Assert.IsNotNull (brussels);
523                         }
524                 
525                         [Test]
526                         public void OffsetIsCorrectInKinshasa ()
527                         {
528                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
529                                         return;
530                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
531                                 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
532                         }
533                 
534                         [Test]
535                         public void OffsetIsCorrectInBrussels ()
536                         {
537                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
538                                         return;
539                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
540                                 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
541                         }
542                 
543                         [Test]
544                         public void NoDSTInKinshasa ()
545                         {
546                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
547                                         return;
548                                 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
549                                 Assert.IsFalse (kin.SupportsDaylightSavingTime);
550                         }
551                 
552                         [Test]
553                         public void BrusselsSupportsDST ()
554                         {
555                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
556                                         return;
557                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
558                                 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
559                         }
560                 
561                         [Test]
562                         public void MelbourneSupportsDST ()
563                         {
564                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
565                                         return;
566                                 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
567                                 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
568                         }
569                 
570                         [Test]
571                         public void RomeAndVaticanSharesTime ()
572                         {
573                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
574                                         return;
575                                 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
576                                 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
577                                 Assert.IsTrue (rome.HasSameRules (vatican));
578                         }
579
580                         [Test]
581                         public void FindSystemTimeZoneById_Local_Roundtrip ()
582                         {
583                                 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
584                         }
585
586                         [Test]
587                         public void Test326 ()
588                         {
589                                 DateTime utc = DateTime.UtcNow;
590                                 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
591                                 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
592                         }
593                 
594                 #if SLOW_TESTS
595                         [Test]
596                         public void BrusselsAdjustments ()
597                         {
598                                 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
599                                 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
600                                 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
601                                 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
602                 
603                                 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
604                 
605                                 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)) {
606                                         Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
607                                         Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
608                                 }               
609                         }
610                 #endif
611                 }
612                 
613                 [TestFixture]
614                 public class GetAmbiguousTimeOffsetsTests
615                 {
616                         [Test]
617                         [ExpectedException (typeof(ArgumentException))]
618                         public void DateIsNotAmbiguous ()
619                         {
620                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
621                                         throw new ArgumentException ();
622                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
623                                 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
624                                 brussels.GetAmbiguousTimeOffsets (date);
625                         }
626                 
627                         [Test]
628                         public void AmbiguousOffsets ()
629                         {
630                                 if (Environment.OSVersion.Platform != PlatformID.Unix)
631                                         return;
632                                 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
633                                 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
634                                 Assert.IsTrue (brussels.IsAmbiguousTime (date));
635                                 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
636                                 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
637                         }
638                 }
639
640                 [TestFixture]
641                 public class HasSameRulesTests
642                 {
643                         [Test]
644                         public void NullAdjustments () //bnc #391011
645                         {
646                                 TimeZoneInfo utc = TimeZoneInfo.Utc;
647                                 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
648                                 Assert.IsTrue (utc.HasSameRules (custom));
649                         }
650                 }
651         }
652 }
653 #endif