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});
156 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
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);
166 public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
168 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
169 Assert.IsFalse (tz.SupportsDaylightSavingTime);
173 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
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);
183 public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
185 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
186 Assert.IsFalse (tz.SupportsDaylightSavingTime);
191 public class IsDaylightSavingTimeTests
196 public void CreateTimeZones ()
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});
205 public void NoDSTInUTC ()
207 DateTime june01 = new DateTime (2007, 06, 01);
208 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
212 public void DSTInLondon ()
214 if (Environment.OSVersion.Platform != PlatformID.Unix)
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");
223 public void DSTTransisions ()
225 if (Environment.OSVersion.Platform != PlatformID.Unix)
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");
238 public void DSTTransisionsUTC ()
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");
252 public void MatchTimeZoneBehavior ()
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))
260 Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
267 public class ConvertTimeTests
272 public void CreateTimeZones ()
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});
281 [ExpectedException (typeof (ArgumentException))]
282 public void ConvertFromUtc_KindIsLocalException ()
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);
290 [ExpectedException (typeof (ArgumentNullException))]
291 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
293 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
297 public void ConvertFromUtc_DestinationIsUTC ()
299 DateTime now = DateTime.UtcNow;
300 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
301 Assert.AreEqual (now, converted);
305 public void ConvertFromUTC_ConvertInWinter ()
307 if (Environment.OSVersion.Platform != PlatformID.Unix)
309 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
310 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
311 Assert.AreEqual (utc, converted);
315 public void ConvertFromUtc_ConvertInSummer ()
317 if (Environment.OSVersion.Platform != PlatformID.Unix)
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);
325 public void ConvertToUTC_KindIsUtc ()
327 DateTime now = DateTime.UtcNow;
328 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
329 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
330 Assert.AreEqual (now, converted);
334 [ExpectedException (typeof (ArgumentException))]
335 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
337 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
341 [ExpectedException (typeof (ArgumentException))]
342 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
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);
350 [ExpectedException (typeof (ArgumentException))]
351 public void ConvertToUTC_InvalidDate ()
353 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
357 [ExpectedException (typeof (ArgumentNullException))]
358 public void ConvertToUTC_SourceIsNull ()
360 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
365 public void ConvertToUtc_MatchDateTimeBehavior ()
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 ());
374 public void ConvertFromToUtc ()
376 if (Environment.OSVersion.Platform != PlatformID.Unix)
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);
389 public void ConvertToTimeZone ()
391 if (Environment.OSVersion.Platform != PlatformID.Unix)
394 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
399 public class IsInvalidTimeTests
404 public void CreateTimeZones ()
406 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
407 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
408 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
409 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
414 public void UTCDate ()
416 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)) {
417 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
418 Assert.IsFalse (london.IsInvalidTime (date));
423 public void InvalidDates ()
425 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
426 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
427 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
428 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
433 public class IsAmbiguousTimeTests
438 public void CreateTimeZones ()
440 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
441 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
442 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
443 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
447 public void AmbiguousDates ()
449 if (Environment.OSVersion.Platform != PlatformID.Unix)
451 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
452 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
453 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
454 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
458 public void AmbiguousUTCDates ()
460 if (Environment.OSVersion.Platform != PlatformID.Unix)
462 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
463 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
464 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
465 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
470 public void AmbiguousInUTC ()
472 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)) {
473 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
480 public class GetSystemTimeZonesTests
483 public void NotEmpty ()
485 if (Environment.OSVersion.Platform != PlatformID.Unix)
487 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
488 Assert.IsNotNull(systemTZ, "SystemTZ is null");
489 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
493 public void ContainsBrussels ()
495 if (Environment.OSVersion.Platform != PlatformID.Unix)
497 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
498 foreach (TimeZoneInfo tz in systemTZ) {
499 if (tz.Id == "Europe/Brussels")
502 Assert.Fail ("Europe/Brussels not found in SystemTZ");
507 public class FindSystemTimeZoneByIdTests
510 [ExpectedException (typeof (ArgumentNullException))]
511 public void NullId ()
513 TimeZoneInfo.FindSystemTimeZoneById (null);
517 [ExpectedException (typeof (TimeZoneNotFoundException))]
518 public void NonSystemTimezone ()
520 if (Environment.OSVersion.Platform != PlatformID.Unix)
521 throw new TimeZoneNotFoundException ();
522 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
526 public void FindBrusselsTZ ()
528 if (Environment.OSVersion.Platform != PlatformID.Unix)
530 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
531 Assert.IsNotNull (brussels);
535 public void OffsetIsCorrectInKinshasa ()
537 if (Environment.OSVersion.Platform != PlatformID.Unix)
539 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
540 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
544 public void OffsetIsCorrectInBrussels ()
546 if (Environment.OSVersion.Platform != PlatformID.Unix)
548 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
549 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
553 public void NoDSTInKinshasa ()
555 if (Environment.OSVersion.Platform != PlatformID.Unix)
557 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
558 Assert.IsFalse (kin.SupportsDaylightSavingTime);
562 public void BrusselsSupportsDST ()
564 if (Environment.OSVersion.Platform != PlatformID.Unix)
566 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
567 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
571 public void MelbourneSupportsDST ()
573 if (Environment.OSVersion.Platform != PlatformID.Unix)
575 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
576 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
580 public void RomeAndVaticanSharesTime ()
582 if (Environment.OSVersion.Platform != PlatformID.Unix)
584 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
585 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
586 Assert.IsTrue (rome.HasSameRules (vatican));
590 public void FindSystemTimeZoneById_Local_Roundtrip ()
592 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
596 public void Test326 ()
598 DateTime utc = DateTime.UtcNow;
599 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
600 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
605 public void BrusselsAdjustments ()
607 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
608 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
609 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
610 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
612 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
614 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)) {
615 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
616 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
623 public class GetAmbiguousTimeOffsetsTests
626 [ExpectedException (typeof(ArgumentException))]
627 public void DateIsNotAmbiguous ()
629 if (Environment.OSVersion.Platform != PlatformID.Unix)
630 throw new ArgumentException ();
631 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
632 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
633 brussels.GetAmbiguousTimeOffsets (date);
637 public void AmbiguousOffsets ()
639 if (Environment.OSVersion.Platform != PlatformID.Unix)
641 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
642 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
643 Assert.IsTrue (brussels.IsAmbiguousTime (date));
644 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
645 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
650 public class HasSameRulesTests
653 public void NullAdjustments () //bnc #391011
655 TimeZoneInfo utc = TimeZoneInfo.Utc;
656 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
657 Assert.IsTrue (utc.HasSameRules (custom));