5 * Stephane Delcroix <stephane@delcroix.org>
7 * Copyright 2011 Xamarin Inc.
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
32 using NUnit.Framework;
34 namespace MonoTests.System
36 public class TimeZoneInfoTest
39 public class PropertiesTests
42 public void GetLocal ()
44 if (Environment.OSVersion.Platform != PlatformID.Unix)
46 TimeZoneInfo local = TimeZoneInfo.Local;
47 Assert.IsNotNull (local);
53 public class CreateCustomTimezoneTests
56 [ExpectedException (typeof (ArgumentNullException))]
57 public void IdIsNullException ()
59 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
63 [ExpectedException (typeof (ArgumentException))]
64 public void IdIsEmptyString ()
66 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
70 [ExpectedException (typeof (ArgumentException))]
71 public void OffsetIsNotMinutes ()
73 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
77 [ExpectedException (typeof (ArgumentOutOfRangeException))]
78 public void OffsetTooBig ()
80 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
84 [ExpectedException (typeof (ArgumentOutOfRangeException))]
85 public void OffsetTooSmall ()
87 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
92 [ExpectedException (typeof (ArgumentException))]
93 public void IdLongerThan32 ()
95 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
100 [ExpectedException (typeof (InvalidTimeZoneException))]
101 public void AdjustmentRulesOverlap ()
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});
113 [ExpectedException (typeof (InvalidTimeZoneException))]
114 public void RulesNotOrdered ()
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});
126 [ExpectedException (typeof (InvalidTimeZoneException))]
127 public void OffsetOutOfRange ()
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});
136 [ExpectedException (typeof (InvalidTimeZoneException))]
137 public void NullRule ()
139 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
143 [ExpectedException (typeof (InvalidTimeZoneException))]
144 public void MultiplesRulesForDate ()
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});
157 public class IsDaylightSavingTimeTests
162 public void CreateTimeZones ()
164 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
165 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
166 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
167 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
171 public void NoDSTInUTC ()
173 DateTime june01 = new DateTime (2007, 06, 01);
174 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
178 public void DSTInLondon ()
180 if (Environment.OSVersion.Platform != PlatformID.Unix)
182 DateTime june01 = new DateTime (2007, 06, 01);
183 DateTime xmas = new DateTime (2007, 12, 25);
184 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
185 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
189 public void DSTTransisions ()
191 if (Environment.OSVersion.Platform != PlatformID.Unix)
193 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
194 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
195 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
196 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
197 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
198 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
199 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
200 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
204 public void DSTTransisionsUTC ()
206 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
207 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
208 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
209 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
210 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
211 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
212 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
213 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
218 public void MatchTimeZoneBehavior ()
220 TimeZone tzone = TimeZone.CurrentTimeZone;
221 TimeZoneInfo local = TimeZoneInfo.Local;
222 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)) {
223 date = DateTime.SpecifyKind (date, DateTimeKind.Local);
224 if (local.IsInvalidTime (date))
226 Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
233 public class ConvertTimeTests
238 public void CreateTimeZones ()
240 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
241 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
242 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
243 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
247 [ExpectedException (typeof (ArgumentException))]
248 public void ConvertFromUtc_KindIsLocalException ()
250 if (Environment.OSVersion.Platform != PlatformID.Unix)
251 throw new ArgumentException ();
252 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
256 [ExpectedException (typeof (ArgumentNullException))]
257 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
259 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
263 public void ConvertFromUtc_DestinationIsUTC ()
265 DateTime now = DateTime.UtcNow;
266 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
267 Assert.AreEqual (now, converted);
271 public void ConvertFromUTC_ConvertInWinter ()
273 if (Environment.OSVersion.Platform != PlatformID.Unix)
275 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
276 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
277 Assert.AreEqual (utc, converted);
281 public void ConvertFromUtc_ConvertInSummer ()
283 if (Environment.OSVersion.Platform != PlatformID.Unix)
285 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
286 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
287 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
291 public void ConvertToUTC_KindIsUtc ()
293 DateTime now = DateTime.UtcNow;
294 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
295 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
296 Assert.AreEqual (now, converted);
300 [ExpectedException (typeof (ArgumentException))]
301 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
303 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
307 [ExpectedException (typeof (ArgumentException))]
308 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
310 if (Environment.OSVersion.Platform != PlatformID.Unix)
311 throw new ArgumentException ();
312 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
316 [ExpectedException (typeof (ArgumentException))]
317 public void ConvertToUTC_InvalidDate ()
319 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
323 [ExpectedException (typeof (ArgumentNullException))]
324 public void ConvertToUTC_SourceIsNull ()
326 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
331 public void ConvertToUtc_MatchDateTimeBehavior ()
333 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)) {
334 Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
340 public void ConvertFromToUtc ()
342 if (Environment.OSVersion.Platform != PlatformID.Unix)
344 DateTime utc = DateTime.UtcNow;
345 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
346 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
347 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
348 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
349 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
350 Assert.AreEqual (utc, back);
356 public class IsInvalidTimeTests
361 public void CreateTimeZones ()
363 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
364 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
365 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
366 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
371 public void UTCDate ()
373 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)) {
374 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
375 Assert.IsFalse (london.IsInvalidTime (date));
380 public void InvalidDates ()
382 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
383 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
384 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
385 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
390 public class IsAmbiguousTimeTests
395 public void CreateTimeZones ()
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});
404 public void AmbiguousDates ()
406 if (Environment.OSVersion.Platform != PlatformID.Unix)
408 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
409 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
410 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
411 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
415 public void AmbiguousUTCDates ()
417 if (Environment.OSVersion.Platform != PlatformID.Unix)
419 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
420 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
421 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
422 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
427 public void AmbiguousInUTC ()
429 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)) {
430 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
437 public class GetSystemTimeZonesTests
440 public void NotEmpty ()
442 if (Environment.OSVersion.Platform != PlatformID.Unix)
444 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
445 Assert.IsNotNull(systemTZ, "SystemTZ is null");
446 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
450 public void ContainsBrussels ()
452 if (Environment.OSVersion.Platform != PlatformID.Unix)
454 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
455 foreach (TimeZoneInfo tz in systemTZ) {
456 if (tz.Id == "Europe/Brussels")
459 Assert.Fail ("Europe/Brussels not found in SystemTZ");
464 public class FindSystemTimeZoneByIdTests
467 [ExpectedException (typeof (ArgumentNullException))]
468 public void NullId ()
470 TimeZoneInfo.FindSystemTimeZoneById (null);
474 [ExpectedException (typeof (TimeZoneNotFoundException))]
475 public void NonSystemTimezone ()
477 if (Environment.OSVersion.Platform != PlatformID.Unix)
478 throw new TimeZoneNotFoundException ();
479 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
483 public void FindBrusselsTZ ()
485 if (Environment.OSVersion.Platform != PlatformID.Unix)
487 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
488 Assert.IsNotNull (brussels);
492 public void OffsetIsCorrectInKinshasa ()
494 if (Environment.OSVersion.Platform != PlatformID.Unix)
496 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
497 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
501 public void OffsetIsCorrectInBrussels ()
503 if (Environment.OSVersion.Platform != PlatformID.Unix)
505 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
506 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
510 public void NoDSTInKinshasa ()
512 if (Environment.OSVersion.Platform != PlatformID.Unix)
514 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
515 Assert.IsFalse (kin.SupportsDaylightSavingTime);
519 public void BrusselsSupportsDST ()
521 if (Environment.OSVersion.Platform != PlatformID.Unix)
523 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
524 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
528 public void MelbourneSupportsDST ()
530 if (Environment.OSVersion.Platform != PlatformID.Unix)
532 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
533 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
537 public void RomeAndVaticanSharesTime ()
539 if (Environment.OSVersion.Platform != PlatformID.Unix)
541 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
542 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
543 Assert.IsTrue (rome.HasSameRules (vatican));
547 public void FindSystemTimeZoneById_Local_Roundtrip ()
549 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
553 public void Test326 ()
555 DateTime utc = DateTime.UtcNow;
556 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
557 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
562 public void BrusselsAdjustments ()
564 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
565 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
566 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
567 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
569 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
571 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)) {
572 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
573 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
580 public class GetAmbiguousTimeOffsetsTests
583 [ExpectedException (typeof(ArgumentException))]
584 public void DateIsNotAmbiguous ()
586 if (Environment.OSVersion.Platform != PlatformID.Unix)
587 throw new ArgumentException ();
588 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
589 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
590 brussels.GetAmbiguousTimeOffsets (date);
594 public void AmbiguousOffsets ()
596 if (Environment.OSVersion.Platform != PlatformID.Unix)
598 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
599 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
600 Assert.IsTrue (brussels.IsAmbiguousTime (date));
601 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
602 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
607 public class HasSameRulesTests
610 public void NullAdjustments () //bnc #391011
612 TimeZoneInfo utc = TimeZoneInfo.Utc;
613 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
614 Assert.IsTrue (utc.HasSameRules (custom));